// connect the necessary libraries
#include <DallasTemperature.h>
#include <OneWire.h>
#include <SoftwareSerial.h>
#include <LiquidCrystal.h>
// pause between sending data, ms
unsigned long pauseTime = 60000;
// variables for pause organization
unsigned long currentTime;
unsigned long previousTime = 0;
// assign the output D10 to connect the temperature sensor D18B20
// and create instances of the ONE WIRE bus and sensor objects
#define ONE_WIRE_BUS 10
OneWire oneWire (ONE_WIRE_BUS);
DallasTemperature sensors (& oneWire);
// set the parameters for connecting to WiFi
#define SSID "Your Access Point Name"
#define PASS "Your Access Point"
// set the IP address of the site thingspeak.com
#define IP "184.106.153.149"
// set the GET command line according to thingspeak.com API
String GET = "GET / update? Key = your API key & field1 =";
// set the pins for connecting the module ESP8266 for which we will
// send AT commands to the module;
// (the system uses AT commands v0.21 SDK 0.9.5
//
esp8266.ru/esp8266-sdk-0-9-5-v0-21 )
SoftwareSerial esp (11, 12); // RX, TX
// set the pins for connecting the LCD indicator
LiquidCrystal lcd (A1, A2, A3, 2, 4, 7);
void setup ()
{
// initialize:
esp.begin (9600); // transmit-receive in ESP8266 at 9600 baud
Serial.begin (9600); // send-receive to the virtual COM port of the computer (for control)
sensors.begin (); // temperature sensor
lcd.begin (16, 2); // LCD indicator
lcd.clear ();
// send AT command to ESP8266
Send ("AT");
delay (1000);
// if the module responded OK, connect to the wireless network
if (esp.find ("OK")) {
Serial.println ("ESP8266 Ready: OK");
connectWiFi ();
previousTime = millis () + 2 * pauseTime;
}
}
void loop () {
currentTime = millis ();
if (currentTime - previousTime> pauseTime) {
// request temperature from the sensor
sensors.requestTemperatures ();
float tempC = sensors.getTempCByIndex (0);
// convert temperature to character string
char buffer [10];
String temp = dtostrf (tempC, 4, 1, buffer);
// send this line to the channel of the site thingspeak.com
updateData (temp);
lcd.setCursor (0,0);
lcd.print (temp);
lcd.print ("\ xDF" "C"); // \ xDF - degree icon
previousTime = currentTime;
}
}
// function to send data from ESP8266 to thingspeak.com
void updateData (String data) {
// form and send a connection command to thingspeak.com
String cmd = "AT + CIPSTART = \" TCP \ ", \" ";
cmd + = IP;
cmd + = "\", 80 ";
Send (cmd);
delay (3000);
if (esp.find ("OK"))
Serial.println ("CONNECT TO IP: OK");
else
Serial.println ("CONNECT TO IP: Error");
// form and send the command to send data
cmd = GET;
cmd + = data;
cmd + = "\ r \ n";
esp.print ("AT + CIPSEND =");
esp.println (cmd.length ());
// if the ESP8266 module is ready to receive and send data
if (esp.find (">")) {
Serial.print (">");
// send data
esp.print (cmd);
Serial.print (cmd);
if (esp.find ("OK")) Serial.println ("DATA SEND: OK");
else Serial.println ("DATA SEND: Error");
}
else {
Send ("AT + CIPCLOSE");
if (esp.find ("OK")) Serial.println ("CONNECTION FOR CLOSE: OK");
else Serial.println ("CONNECTION FOR IP CLOSE: Error");
}
Serial.println ();
}
// function to send data to the ESP8266 module
void Send (String cmd) {
Serial.print (“SEND TO ESP8266:„);
esp.println (cmd);
Serial.println (cmd);
}
// function to connect to a WiFi network
boolean connectWiFi () {
esp.println (“AT + CWMODE = 1”);
delay (2000);
String cmd = "AT + CWJAP = \" ";
cmd + = SSID;
cmd + = "\", \ "";
cmd + = PASS;
cmd + = "\" ";
Send (cmd);
delay (5000);
if (esp.find ("OK")) {
Serial.println ("CONNECT TO WIFI: OK");
return true;
}
else {
Serial.println ("CONNECT TO WIFI: Error");
return false;
}
}