Streaming data to internet could not have been easier with the release of ESP8266.
Gone are the days of using a concoction of software, permanently tether a microcontroller based temperature sensor to a computer for it's internet connecting capability, and visualizing the data. back in 2012, yours truly have dabbled in such complex setup of hardware and software URL: http://shin-ajaran.blogspot.sg/2012/07/iot-how-to-stream-temperature-sensor.html for the purpose of demonstrating IoT and of course to stream the office's temperature to the internet http://shin-ajaran.blogspot.sg/2012/07/iot-diy-solar-air-conditioning-with.html . the sole purpose is to make an informed choice of coming to office or not on the weekends, based on the reading of the temperature data.
If you wonder what is ESP8266, check out previous post http://shin-ajaran.blogspot.sg/2014/12/noobs-guide-to-esp8266-with-arduino.html ESP8266 is a very low cost WiFi upgrade to "Internet Enable" any microcontroller based systems that are able to speak serial with it. It is retailing for S$9.5 here or ~USD5 on friendly website of middle kingdom. Earlier, A C&C (command & control) type of application is created with ESP8266 http://shin-ajaran.blogspot.sg/2015/01/internet-controlled-ac-appliances-with.html allowing appliances to be controlled from the Internet. This round, let's explore uplink type of application; to stream real and live data to the internet. The IoT data store of choice is thingspeak https://thingspeak.com/docs/tutorials/ for the fact that it is easy to register for an account, clear and concise documentation, and any microcontroller based system can be setup to stream data to in in practically no time.
Your truly is no stranger to sensor for the purpose of temperature data acquisition. The matter of fact, expensive sensor does matter http://shin-ajaran.blogspot.sg/2013/07/expensive-sensor-does-matter-ds18b20.html. Obviously, the weapon of choice in this application will be the trusty DS18B20 temperature probe, or it's variant. Setting up DS18B20 can be slightly tedious as compared to LM35, but it is well worth the effort. As far as my DIY sous vide setup is concerned, DS18B20 temperature sensor served it's purpose.
If you can't wait to make it yourself and want to jump straight into the visualization of the temperature in the office from the internet, follow this URL https://thingspeak.com/channels/22051
iframe of the channel is here:
Parts needed for this uplink IoT setup
1. Arduino Mega
2. ESP8266
3. DS18B20 temperature sensor
Wiring:
please refer to the previous posts mentioned earlier on hooking up the ESP8266 with Arduino Mega, and DS18B20 temperature sensor.
Once every partss are wired accordingly and dandy, the final outcome should look somewhat similar to the following.
API:
Go to the thingspeak website to register for an account. it is FREE!!!!
Once registration is done, navigate to the following to acquire the API key.
This API key will allow ESP8266 and Arduino Mega to send data to thingspeak server.
program arduino mega with the code that is available at the footer.
then observe from the serial monitor whether data has been sent successfully. On the thingspeak channel of yours, a simple graph is available to visualize the data received via IoT.
there are several cool features on thingspeak, such as public view and private view of streaming data; data can be downloaded in CSV for later manipulations.
the following is the said temperature sensor data from the office, streamed to thingspeak using ESP8266 and Arduino Mega since 31dec14.
can you spot the trend of the visualization and make some sense out of it?
code here:
This file contains 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
// | |
//#include <SoftwareSerial.h> | |
//use mega Serial 2 for serial monitor; Serial 1 on pins 19 (RX) and 18 (TX);// Serial2 on pins 17 (RX) and 16 (TX), Serial3 on pins 15 (RX) and 14 (TX). | |
//DS18B20 uses onewire protocol; need 5k pullup | |
#include <OneWire.h> | |
#include <DallasTemperature.h> | |
#define SSID "SSID_HERE" | |
#define PASS "PASSWORD_HERE" | |
#define DST_IP "220.181.111.85" //baidu.com | |
#define thingspeakIP "184.106.153.149" // thingspeak.com | |
//SoftwareSerial dbgSerial(10, 11); // RX, TX | |
#define acPin 7 | |
char myChar; | |
//Data wire is plugged into pin A1 on the Arduino | |
#define ONE_WIRE_BUS A1 | |
// Setup a oneWire instance to communicate with any OneWire devices | |
// (not just Maxim/Dallas temperature ICs) | |
OneWire oneWire(ONE_WIRE_BUS); | |
// Pass our oneWire reference to Dallas Temperature. | |
DallasTemperature ds18B20(&oneWire); | |
//thingspeak API | |
String GET = "GET /update?key=API_KEY_HERE"; | |
//my write key: APIKEY1 | |
//my read key: APIKEY2 | |
//my IoT Channel: 22051 | |
void setup() | |
{ | |
// Start up the library for ds18B20 | |
ds18B20.begin(); | |
// Open serial communications and wait for port to open: | |
//serial 2 is to esp8266 | |
Serial2.begin(9600);//9600 (mine), 57600, 115200 | |
Serial2.setTimeout(1000); | |
//serial 0 is to usb | |
Serial.begin(115200); | |
//mod1 otherwise module cant be ready | |
while(!Serial); | |
while(!Serial2); | |
Serial.println("ESP8266 on Mega2560 to thingspeak IoT "); | |
//mod1 otherwise module cant be ready | |
while(Serial2.available()>0) | |
Serial2.read(); | |
delay(1000); | |
//test if the module is ready by reset | |
//Serial2.println("AT+RST");// high chance err: module not responding | |
Serial2.println("AT");//test esp8266 via "AT" instead | |
//delay(1000); | |
//delay(1000); | |
Serial.println("Resetting module"); | |
//mod1 | |
//Serial2.flush(); | |
//if(Serial2.find("ready")) | |
if(Serial2.find("OK")) | |
//if(Serial2.find("Ready")||Serial2.find("ready")||Serial2.find("OK")) | |
{ | |
//dbgSerial.println("Module is ready"); | |
Serial.println("Module is ready"); | |
} | |
else | |
{ | |
//dbgSerial.println("Module have no response."); | |
Serial.println("Module have no response."); | |
while(1);//hang here | |
} | |
delay(1000); | |
//connect to the wifi | |
boolean connected=false; | |
for(int i=0;i<5;i++) | |
{ | |
if(connectWiFi()) | |
{ | |
Serial.println(" WiFi connected"); | |
connected = true; | |
break; | |
} | |
} | |
if (!connected){ | |
Serial.println("No WiFi connected"); | |
while(1); | |
} | |
delay(5000); | |
//print the ip addr | |
Serial2.println("AT+CIFSR=?"); | |
Serial.println("IP address:"); | |
while (Serial2.available()) | |
Serial.write(Serial2.read()); | |
//set the single connection mode | |
//Serial2.println("AT+CIPMUX=0");//default single conn | |
//LEDserver use | |
//startLEDserver(); | |
} | |
void loop() | |
{ | |
/* | |
//esp8266 as browser to open baidu.com | |
String cmd = "AT+CIPSTART=\"TCP\",\""; | |
cmd += DST_IP; | |
cmd += "\",80"; | |
Serial2.println(cmd); | |
//dbgSerial.println(cmd); | |
Serial.println(cmd); | |
if(Serial2.find("Error")) return; | |
cmd = "GET / HTTP/1.0\r\n\r\n"; | |
Serial2.print("AT+CIPSEND="); | |
Serial2.println(cmd.length()); | |
if(Serial2.find(">")) | |
{ | |
//dbgSerial.print(">"); | |
Serial.print(">"); | |
}else | |
{ | |
Serial2.println("AT+CIPCLOSE"); | |
//dbgSerial.println("connect timeout"); | |
Serial.println("connect timeout"); | |
delay(1000); | |
return; | |
} | |
Serial2.print(cmd); | |
delay(2000); | |
//Serial.find("+IPD"); | |
while (Serial2.available()) | |
{ | |
char c = Serial2.read(); | |
//dbgSerial.write(c); | |
Serial.write(c); | |
//if(c=='\r') dbgSerial.print('\n'); | |
if(c=='\r') Serial.print('\n'); | |
} | |
//dbgSerial.println("===="); | |
Serial.println("===="); | |
delay(1000); | |
*/ | |
/* LED server | |
//Serial.println("waiting for cmd"); | |
if (Serial2.available()>0){ | |
if(Serial2.find("LED")){ | |
setLED(); | |
} | |
} | |
*/ | |
float temp = readThermo();// read DS18B20 | |
char buffer[10]; | |
String tempC = dtostrf(temp, 4, 1, buffer); | |
updateThingSpeakTemp(tempC); | |
delay(60000);//read every 60sec | |
} | |
boolean connectWiFi() | |
{ | |
Serial2.println("AT+CWMODE=1"); | |
String cmd="AT+CWJAP=\""; | |
cmd+=SSID; | |
cmd+="\",\""; | |
cmd+=PASS; | |
cmd+="\""; | |
//dbgSerial.println(cmd); | |
Serial2.println(cmd); | |
Serial.println(cmd); | |
delay(2000); | |
if(Serial2.find("OK")) | |
{ | |
//dbgSerial.println("OK, Connected to WiFi."); | |
Serial.println("OK, Connected to WiFi."); | |
return true; | |
} | |
else | |
{ | |
//dbgSerial.println("Can not connect to the WiFi."); | |
Serial.println("Can not connect to the WiFi."); | |
return false; | |
} | |
} | |
void setLED(){ | |
int red = Serial.parseInt(); | |
int green = Serial.parseInt(); | |
int blue = Serial.parseInt(); | |
Serial.print("LED set. Red: "); | |
Serial.print(red); | |
Serial.print(" Green "); | |
Serial.print(green); | |
Serial.print(" Blue "); | |
Serial.println(blue); | |
//colorWipe(strip.Color(red,green,blue),200); | |
//simple test AC on/off for 2sec | |
Serial.println("Set AC on/off"); | |
digitalWrite(acPin,HIGH); | |
delay(2000); | |
digitalWrite(acPin,LOW); | |
} | |
float readThermo() {//input pin# | |
//using ds18B20 | |
ds18B20.requestTemperatures(); | |
float val = ds18B20.getTempCByIndex(0); | |
return val; // Return the Temperature in degC | |
} | |
void startLEDserver(){ | |
//LED server use | |
Serial2.println("AT+CIPMUX=1");//led server multi conn | |
delay(1000); | |
Serial2.println("AT+CIPSERVER=1,8266"); | |
delay(1000); | |
Serial.println("Starting TCP Server"); | |
} | |
void updateThingSpeakTemp(String tempC){ | |
String cmd = "AT+CIPSTART=\"TCP\",\""; | |
cmd += thingspeakIP; | |
cmd += "\",80"; | |
sendDebug(cmd); | |
delay(2000); | |
if(Serial2.find("Error")){ | |
Serial.print("RECEIVED: Error"); | |
return; | |
} | |
cmd = GET; | |
cmd += tempC; | |
cmd += "\r\n"; | |
Serial2.print("AT+CIPSEND="); | |
Serial2.println(cmd.length()); | |
if(Serial2.find(">")){ | |
Serial.print(">"); | |
Serial.print(cmd); | |
Serial2.print(cmd); | |
}else{ | |
sendDebug("AT+CIPCLOSE"); | |
} | |
if(Serial2.find("OK")){ | |
Serial.println("RECEIVED: OK"); | |
}else{ | |
Serial.println("RECEIVED: Error"); | |
} | |
} | |
void sendDebug(String cmd){ | |
Serial.print("SEND: "); | |
Serial.println(cmd); | |
//send cmd | |
Serial2.println(cmd); | |
} |
2 comments:
The images connected to your dropbox account are no longer viewable.
Thanks for sharing the project!
Thanks for Sharing Shin,
I have used this to make a fish tank temperature sensor. You also introduced me to the thing speak web service.
https://youtu.be/Qexn9HP7zd4
Post a Comment