Created
June 30, 2018 23:13
-
-
Save nishabe/5a3cab96e85e035478611f80550f463f to your computer and use it in GitHub Desktop.
ThingSpeak for Arduino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Arduino IOT - Temperature (oC) and Humidity (%) on the web | |
*Use the DHT-22 sensor to read temperature and humidity values | |
*Send these values to www.thingSpeak.com with the ESP8266 serial Wifi module | |
Dev: Michalis Vasilakis // Date:23/2/2016 // Update: 25/2/2015 // Ver. 1.3 | |
More info: http://www.ardumotive.com/iot-wifi-temp-and-humidity.html | |
Tip: open the serial monitor for debugging */ | |
//Libraires | |
#include <stdlib.h> | |
#include <DHT.h> | |
/*------------------------DHT SENSOR------------------------*/ | |
#define DHTPIN 2 // DHT data pin connected to Arduino pin 2 | |
#define DHTTYPE DHT22 // DHT 22 (or AM2302) | |
DHT dht(DHTPIN, DHTTYPE); // Initialize the DHT sensor | |
/*----------------------------------------------------------*/ | |
/*-----------------ESP8266 Serial WiFi Module---------------*/ | |
#define SSID "your_SSID-WiFiname" // "SSID-WiFiname" | |
#define PASS "your_wifi_password" // "password" | |
#define IP "184.106.153.149"// thingspeak.com ip | |
String msg = "GET /update?key=your_key_here"; //change it with your key... | |
/*-----------------------------------------------------------*/ | |
//Variables | |
float temp; | |
int hum; | |
String tempC; | |
int error; | |
char ok [] = "OK"; | |
char someError [] = "Error"; | |
char greaterThan [] = ">"; | |
void setup() | |
{ | |
Serial.begin(115200); //or use default 115200. | |
Serial.println("AT"); | |
delay(5000); | |
if(Serial.find(ok)){ | |
connectWiFi(); | |
} | |
} | |
void loop(){ | |
//Read temperature and humidity values from DHT sensor: | |
start: //label | |
error=0; | |
temp = dht.readTemperature(); | |
hum = dht.readHumidity(); | |
char buffer[10]; | |
// there is a useful c function called dtostrf() which will convert a float to a char array | |
//so it can then be printed easily. The format is: dtostrf(floatvar, StringLengthIncDecimalPoint, numVarsAfterDecimal, charbuf); | |
tempC = dtostrf(temp, 4, 1, buffer); | |
updateTemp(); | |
//Resend if transmission is not completed | |
if (error==1){ | |
goto start; //go to label "start" | |
} | |
delay(30000); //Update every 30 seconds. In free plan recommended maximum daily usage based on your annual capacity: 8,219 messages. | |
// 342 messages per hour. 5 messages per minute. ~ 1 message in 15 minutes. | |
} | |
void updateTemp(){ | |
String cmd = "AT+CIPSTART=\"TCP\",\""; | |
cmd += IP; | |
cmd += "\",80"; | |
Serial.println(cmd); | |
delay(2000); | |
if(Serial.find(someError)){ | |
return; | |
} | |
cmd = msg ; | |
cmd += "&field1="; //field 1 for temperature | |
cmd += tempC; | |
cmd += "&field2="; //field 2 for humidity | |
cmd += String(hum); | |
cmd += "\r\n"; | |
Serial.print("AT+CIPSEND="); | |
Serial.println(cmd.length()); | |
if(Serial.find(">")){ | |
Serial.print(cmd); | |
} | |
else{ | |
Serial.println("AT+CIPCLOSE"); | |
//Resend... | |
error=1; | |
} | |
} | |
boolean connectWiFi(){ | |
Serial.println("AT+CWMODE=1"); | |
delay(2000); | |
String cmd="AT+CWJAP=\""; | |
cmd+=SSID; | |
cmd+="\",\""; | |
cmd+=PASS; | |
cmd+="\""; | |
Serial.println(cmd); | |
delay(5000); | |
if(Serial.find(ok)){ | |
return true; | |
}else{ | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment