// This #include statement was automatically added by the Particle IDE.
#include <ThingSpeak.h>
// This #include statement was automatically added by the Particle IDE.
#include <JsonParserGeneratorRK.h>
// This #include statement was automatically added by the Particle IDE.
#include <LiquidCrystal.h>
// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_DHT.h>
// This example assumes the sensor to be plugged into CONN2
#define DHTPIN D2 // what pin we're connected to
// Here we define the type of sensor used
#define DHTTYPE DHT11 // DHT 11
TCPClient client;
unsigned long myChannelNumber = 913676;
const char * myWriteAPIKey = "RU3LJ5FSFWP2XCTS";
unsigned long lastPubMillis = 0;
unsigned long pubInterval =1000;
int GK_tempF;
int GK_humidity;
int Ravs_tempF;
int Ravs_humidity;
int lasttempF = 0;
int tempFdelta;
int lasthumidity = 0;
int humiditydelta;
unsigned long lastSensorRead = 0;
unsigned long sensorInterval = 1000;
//Global variables to store messages.
char *message = "Temp Humidity";
char msg[128];
char *Rmessage = "Temp Humidity";
char Rmsg[128];
void printJson(JsonParser &jp);
// Create a parser to handle 2K of data and 100 tokens
JsonParserStatic<2048, 100> jsonParser;
// this will be called whenever an event arrives we subscribed to
void GKHandler(const char *event, const char *data)
{
Serial.println(data); // print out the data as it comes in for debugging
strncpy(msg, data, sizeof(msg)-1); // copy the incoming data to the global variable msg (with boundary limit)
jsonParser.clear(); // make sure the parser buffer is fresh and empty
jsonParser.addString(data); // copy the received data into the parser buffer for it to work with
if (jsonParser.parse()) // let the parser do its job and split up the data internally
{
// first ask the parser for the value connected with the key 'GK_tempF'
// if this is successful pop that value into the provided variable tempF
// if not, the function will return false which - in turn - triggers the error output
if (jsonParser.getOuterValueByKey("GK_tempF", GK_tempF) == false)
{
Serial.println("failed to get Temperature");
}
// do the same for 'humidity'
// if(!someBoolean) is short hand for if(someBoolean == false)
if (!jsonParser.getOuterValueByKey("GK_humidity", GK_humidity)) {
Serial.println("failed to get Humidity");
}
}
}
void JRHandler(const char *event, const char *data)
{
Serial.println(data); // print out the data as it comes in for debugging
strncpy(Rmsg, data, sizeof(Rmsg)-1); // copy the incoming data to the global variable msg (with boundary limit)
jsonParser.clear(); // make sure the parser buffer is fresh and empty
jsonParser.addString(data); // copy the received data into the parser buffer for it to work with
if (jsonParser.parse()) // let the parser do its job and split up the data internally
{
// first ask the parser for the value connected with the key 'GK_tempF'
// if this is successful pop that value into the provided variable tempF
// if not, the function will return false which - in turn - triggers the error output
if (jsonParser.getOuterValueByKey("Ravs_tempF", Ravs_tempF) == false)
{
Serial.println("failed to get Temperature");
}
// do the same for 'humidity'
// if(!someBoolean) is short hand for if(someBoolean == false)
if (!jsonParser.getOuterValueByKey("Ravs_humidity", Ravs_humidity)) {
Serial.println("failed to get Humidity");
}
}
}
int Nichol_temp;
int Nichol_humidity;
int pinDHT11 = 6;
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal lcd(7, 8, 6, 5, 4, 3);
void setup(){
ThingSpeak.begin(client);
dht.begin();
Serial.begin(9600);
lcd.begin(16,2);
Particle.subscribe("GKs_Argon",GKHandler); //I am collecting data from GKs_Argon
Particle.subscribe("Ravs_Argon",JRHandler);
}
void loop(){
//==================================================================
//DHT11 Takes Temperature and Humidity readings (Start)
//==================================================================
if (millis() - lastSensorRead > sensorInterval)
{
Nichol_humidity = dht.getHumidity();
Nichol_temp = dht.getTempFarenheit();
tempFdelta = Nichol_temp - lasttempF;
tempFdelta = abs(tempFdelta);
humiditydelta = Nichol_humidity - lasthumidity;
humiditydelta = abs(humiditydelta);
if (tempFdelta < 2 && humiditydelta < 2) //if temp difference is more than 1 degree F in 5 seconds it's probably a spike in the data
{
Particle.publish("Nichol_temp", String(Nichol_temp) + " °F");
Particle.publish("Nichol_humidity", String(Nichol_humidity) + "%");
ThingSpeak.setField(1,Nichol_temp);
ThingSpeak.setField(2,Nichol_humidity);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
delay(1000);
lcd.clear();
//Choosing the first line and row
lcd.setCursor(0,0);
//Typing Temp: to the first line starting from the first row
lcd.print("Temp: ");
//Typing the temperature readings after "Temp: "
lcd.print((int)Nichol_temp);
//Choosing the second line and first row
lcd.print("F");
lcd.setCursor(0,1);
//Typing Humidity(%): to the second line starting from the first row
lcd.print("Humidity(%): ");
//Typing the humidity readings after "Humidity(%): "
lcd.print((int)Nichol_humidity);
}
//Update the sensor read millis timer.
lastSensorRead = millis();
lasttempF = Nichol_temp;
lasthumidity = Nichol_humidity;
}
//==================================================================
//DHT11 Takes Temperature and Humidity readings (End)
//==================================================================
// ThingSpeak.setField(1,Nichol_temp);
// ThingSpeak.setField(2,Nichol_humidity);
//==================================================================
//Print data onto LCD Screen (Start)
//==================================================================
if (millis() - lastPubMillis > pubInterval)
{
Serial.printlnf("RoomEnv: '%s'", msg);//for debugging
Particle.publish("GK_Argon", msg);
Particle.publish("Ravs_Argon", Rmsg);
//Telling our lcd to refresh itself every 0.75 seconds
}
//==================================================================
//Print data onto LCD Screen (End)
//==================================================================
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
}
Comments