Adrian Fernandez
Published

IoT real-time dashboard with PubNub & TI LaunchPad

Create a real-time sensor data dashboards using PubNub & TI LaunchPad development kits.

BeginnerFull instructions provided6,944
IoT real-time dashboard with PubNub & TI LaunchPad

Things used in this project

Story

Read more

Code

Encode JSON & Publish to PubNub

Arduino
This is the main task in Energia. It will encode the latest sensor readings into a JSON payload & publish it up to PubNub. Be sure to modify this code example with your WiFi credentials & appropriate PubNub pub/sub keys & channel.
/*
  PubNub sample client

  This sample client will publish raw (JSON pre-encoded) PubNub messages.

  This code is in the public domain.
  */

#include <SPI.h>
#include <WiFi.h>
#include <PubNub.h>
#include <aJSON.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

WiFiClient *client;
PubSubClient *subClient;
  
char pubkey[] = "ADD_YOUR_PUBKEY_HERE";
char subkey[] = "ADD_YOUR_SUBKEY_HERE";
char* channel = "ADD_YOUR_CHANNEL_HERE";
//char subChannel[] = "hello_world_sub";

// your network name also called SSID
char ssid[] = "ADD_YOUR_WIFI_SSID_HERE";
// your network password
char password[] = "ADD_YOUR_WIFI_PASSWORD_HERE";

//GLOBAL VARIABLES
int MOISTURE = 0;
int LIGHT = 0;

void setup()
{
  Serial.begin(115200);
  Serial.println("Serial set up");

  // attempt to connect to Wifi network:
  Serial.print("Attempting to connect to Network named: ");
  Serial.println(ssid); 
  
  // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
  WiFi.begin(ssid, password);
  
  while ( WiFi.status() != WL_CONNECTED) {
    // print dots while we wait to connect
    Serial.print(".");
    delay(300);
  }
  Serial.println("\nYou're connected to the network");
  
  Serial.println("Waiting for an ip address");
  while (WiFi.localIP() == INADDR_NONE) {
    // print dots while we wait for an ip addresss
    Serial.print(".");
    delay(300);
  }
  Serial.println("\nIP Address obtained");

  // Print the WiFi status.
  printWifiStatus();

  PubNub.begin(pubkey, subkey);
  Serial.println("PubNub set up");
}

void loop()
{
  
    // CREATE JSON ENCODED PAYLOAD TO PUBLISH
    aJsonObject *msg = createMessage();
    char* myPayload = (char*)aJson.print(msg);
  
    client = PubNub.publish(channel, myPayload);  // https://jsfiddle.net/qc3bzzjx/embedded/result/
    Serial.print("published message: ");
    Serial.println(myPayload);
    aJson.deleteItem(msg);
    free(myPayload);
    
    delay(250);

}


aJsonObject *createMessage()
{
  aJsonObject *msg = aJson.createObject();
  aJsonObject *eon = aJson.createObject();
  aJson.addStringToObject(eon, "myName", "TI_LaunchPad");
  aJson.addNumberToObject(eon, "LIGHT", (int)LIGHT);
  aJson.addNumberToObject(eon, "MOISTURE", (int)MOISTURE);
  aJson.addItemToObject(msg, "eon", eon);
  return msg;
}

void printWifiStatus() {
	// print the SSID of the network you're attached to:
	Serial.print("SSID: ");
	Serial.println(WiFi.SSID());

	// print your WiFi IP address:
	IPAddress ip = WiFi.localIP();
	Serial.print("IP Address: ");
	Serial.println(ip);

	// print the received signal strength:
	long rssi = WiFi.RSSI();
	Serial.print("signal strength (RSSI):");
	Serial.print(rssi);
	Serial.println(" dBm");
}

Read light sensor data

Arduino
void setupLight(){

}

void loopLight(){
  LIGHT = analogRead(23);
  delay(250);
}

Read Moisture sensor

Arduino
void setupMoisture(){

}

void loopMoisture(){
  MOISTURE = analogRead(24);
  delay(250);
}

Credits

Adrian Fernandez

Adrian Fernandez

10 projects • 47 followers
peek a boo my name is adrian

Comments