Things used in this project

Code

Encode JSON & Publish to PubNubArduino
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 dataArduino
void setupLight(){

}

void loopLight(){
  LIGHT = analogRead(23);
  delay(250);
}
Read Moisture sensorArduino
void setupMoisture(){

}

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

Credits

10384512 10105541378165610 2876926985073736385 n
Adrian Fernandez

peek a boo my name is adrian

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Similar projects you might like

Losant with Seeed Studio Grove on MSP432
Easy
  • 111
  • 4

Full instructions

Demonstrate an IoT application connecting Seeed Studio Grove Starter Kit sensors to the Losant IoT cloud platform.

ESP8266 & Seeed Studio Buzzer - setup in under 2 mins
Easy
  • 37
  • 2

Full instructions

Explains how use Seeed Studio Buzzer with Mongoose OS on ESP8266 NodeMCU. Solderless, in less than 2 minutes and in a simple JavaScript!

Texas Instruments MSP-EXP432 - MultiTasking
Easy
  • 352
  • 6

Protip

MultiTasking ultrasonic and color sensor with Texas Instruments MSP-EXP432 and Energia.

ESP8266 & Seeed Light Sensor - quick start in 2 mins
Easy
  • 281
  • 3

Full instructions

Explains how use Seeed Light sensor with Mongoose OS on ESP8266 NodeMCU. Solderless, in less than 3 minutes and in a simple JavaScript!

ESP8266 & Motion Sensor by Seeed - quick start in 3 mins
Easy
  • 137
  • 0

Full instructions

Explains how use Seeed Motion sensor with Mongoose OS on ESP8266 NodeMCU. Solderless, in less than 3 minutes and in a simple JavaScript!

Grove starter kit for arduino --- Temperature sensor
Easy
  • 160
  • 2

Protip

Teaches you how to use the temperature sensor in the Arduino Grove starter kit.

ProjectsCommunitiesContestsLiveAppsBetaFree StoreBlogAdd projectSign up / Login
Respect project
Feedback