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

Contact

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

Automatic Brightness Sensor
Easy
  • 443
  • 4

This small tool checks brightness, and if there is fog or night out, will turn on without human action or worry.

Grove - Introduction in a Buzzer
Easy
  • 80
  • 2

Protip

Beginner-Example

A Mothers Day LCD Special
Easy
  • 205
  • 2

Full instructions

A nice project to show your appreciation to your mother.

Grove - Introduction in a Button & LED String Light
Easy
  • 350
  • 2

Protip

Beginner-Example - I bet Beginners will smile after project - sent me an selfie!

Grove - Introduction in Chainable LED
Easy
  • 175
  • 2

Protip

Beginners-Example

ProjectsCommunitiesTopicsContestsLiveAppsBetaFree StoreBlogAdd projectSign up / Login
Feedback