Skip to content

Instantly share code, notes, and snippets.

@shadoh6
Created September 18, 2016 17:01
Show Gist options
  • Save shadoh6/9624dda2b7afd944f6009f6794d63fa0 to your computer and use it in GitHub Desktop.
Save shadoh6/9624dda2b7afd944f6009f6794d63fa0 to your computer and use it in GitHub Desktop.
#include <SoftwareSerial.h>
#include <Wire.h>
#include <SparkFunHTU21D.h>
#include<stdlib.h>
#include "DHT.h"
#define SSID "XinCheJian"//your network name
#define PASS "imakestuff"//your network password
#define IP "184.106.153.149" // thingspeak.com
//#define DHTPIN 7 // what pin the DHT sensor is connected to
//#define DHTPIN1 8
//#define DHTTYPE DHT11 // Change to DHT22 if that's what you have
//#define DHTTYPE1 DHT11
#define Baud_Rate 115200 //Another common value is 9600
#define GREEN_LED 3 //optional LED's for debugging
#define RED_LED 4 //optional LED's for debugging
#define DELAY_TIME 60000 //time in ms between posting data to ThingSpeak
//Can use a post also
String GET = "GET /update?key=3A69VVRXPYI6YCSP&field1=";
String FIELD2 = "&field2=";
String FIELD3 = "&field3=";
String FIELD4 = "&field4=";
//if you want to add more fields this is how
//String FIELD3 = "&field3=";
bool updated;
//DHT dht(DHTPIN, DHTTYPE);
//DHT dht1(DHTPIN1, DHTTYPE);
HTU21D myHumidity;
SoftwareSerial mySerial(10, 11); // RX, TX
//this runs once
void setup()
{
mySerial.begin(Baud_Rate);
mySerial.println("AT");
delay(5000);
if(mySerial.find("OK")){
//connect to your wifi netowork
bool connected = connectWiFi();
if(!connected){
//failure, need to check your values and try again
Error();
}
}else{
Error();
}
//initalize DHT sensor
// dht.begin();
// dht1.begin();
myHumidity.begin();
}
//this runs over and over
void loop(){
// float h = dht.readHumidity();
// Read temperature as Fahrenheit (isFahrenheit = true)
float h = myHumidity.readHumidity();
//float t = dht.readTemperature();
float t = myHumidity.readTemperature();
//float h1 = dht1.readHumidity();
// Read temperature as Fahrenheit (isFahrenheit = true)
// float t1 = dht1.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
LightRed();
return;
}
//update ThingSpeak channel with new values
updated = updateTemp(String(t), String(h));
// updated = updateTemp(String(t1), String(h1));
//if update succeeded light up green LED, else light up red LED
if(updated){
LightGreen();
}else{
LightRed();
}
//wait for delay time before attempting to post again
delay(DELAY_TIME);
}
bool updateTemp(String tempC, String humid){
//initialize your AT command string
String cmd = "AT+CIPSTART=\"TCP\",\"";
//add IP address and port
cmd += IP;
cmd += "\",80";
//connect
mySerial.println(cmd);
delay(2000);
if(mySerial.find("Error")){
return false;
}
//build GET command, ThingSpeak takes Post or Get commands for updates, I use a Get
cmd = GET;
cmd += tempC;
cmd += FIELD2;
cmd += humid;
//continue to add data here if you have more fields such as a light sensor
//cmd += FIELD3;
//cmd += <field 3 value>
cmd += "\r\n";
//Use AT commands to send data
mySerial.print("AT+CIPSEND=");
mySerial.println(cmd.length());
if(mySerial.find(">")){
//send through command to update values
mySerial.print(cmd);
}else{
mySerial.println("AT+CIPCLOSE");
}
if(mySerial.find("OK")){
//success! Your most recent values should be online.
return true;
}else{
return false;
}
}
boolean connectWiFi(){
//set ESP8266 mode with AT commands
mySerial.println("AT+CWMODE=1");
delay(2000);
//build connection command
String cmd="AT+CWJAP=\"";
cmd+=SSID;
cmd+="\",\"";
cmd+=PASS;
cmd+="\"";
//connect to WiFi network and wait 5 seconds
mySerial.println(cmd);
delay(5000);
//if connected return true, else false
if(mySerial.find("OK")){
return true;
}else{
return false;
}
}
void LightGreen(){
digitalWrite(RED_LED, LOW);
digitalWrite(GREEN_LED, HIGH);
}
void LightRed(){
digitalWrite(GREEN_LED, LOW);
digitalWrite(RED_LED, HIGH);
}
//if an error has occurred alternate green and red leds
void Error(){
while(true){
LightRed();
delay(2000);
LightGreen();
delay(2000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment