There was one thing that kept me occupied lattely. It was temperature datalogger using Arduino, ESP8266 and DS18B20 and these all together save data to thingspeak.com. So now that I have everything working, let me make a little tutorial.
There are quite a few tutorials online about this topic, with different combinations of these components, but I have found none, that worked just the way I wanted them. I wanted to have Arduino as heart of the device, so that I can do more than just read and send temperature.
First we need to connect everything together as on the following Fritzing diagram:
That 1uF capacitor is important! I had a lot of problems before I put it there.
Once we have all of this connected, we need Arduino IDE so that we can program our board. Just go to arduino web page and download latest version. At the time of my vriting it was 1.8.5.
When you run IDE for the first time it will probbably ask to update boards, just accept it.
We need a few extra libraries, if we want to use all of the components. Go to menu Sketch -> Include Library -> Manage Libraries…
First in the window that opens search for Dallas and Install library that says DallasTemperature that supports among others DS18B20.
Second library is OneWire for one wire ICs.
The third is ThingSpeak for communication between thingspeak.com and ESP8266.
The last one is ESP8266 from Adafruit.
After that you need following sketch, where you need to change your router ID and password , you also need your thingspeak Write API key:
>>The code is not mine, I found bits and pieces from internet.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
#include <stdlib.h> #include <SoftwareSerial.h> #include <OneWire.h> #include <DallasTemperature.h> #include <ThingSpeak.h> #define ONE_WIRE_BUS 8 OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); #define SSID "SSID" #define PASS "password" #define IP "184.106.153.149" // thingspeak.com String GET = "GET /update?key=XXXXXXXXXXXXXXXX"; SoftwareSerial wifi(10, 11); // RX, TX void setup() { // put your setup code here, to run once: wifi.begin(9600); Serial.begin(9600); sensors.begin(); delay(1000); Serial.println("Debug works!"); sendDebug("AT"); delay(1000); if(wifi.find("OK")) { Serial.println("RECEIVED: OK\nData ready to sent!"); connectWiFi(); } else { Serial.println("Error -> stop!"); while(1); } } void loop() { char temperatura[7]; sensors.requestTemperatures(); float tempC = sensors.getTempCByIndex(0); Serial.println(tempC); dtostrf(tempC, 4, 1, temperatura); updateTS(temperatura); delay(60000); } //----- update the Thingspeak string with 1 value void updateTS( String temperatura) { // ESP8266 Client String cmd = "AT+CIPSTART=\"TCP\",\"";// Setup TCP connection cmd += IP; cmd += "\",80"; sendDebug(cmd); delay(2000); if(wifi.find( "Error" ) ) { Serial.print( "RECEIVED: Error\nExit1" ); return; } cmd = GET + "&field1=" + temperatura +"\r\n"; wifi.print( "AT+CIPSEND=" ); Serial.print( "AT+CIPSEND=" );//test wifi.println( cmd.length() ); Serial.println( cmd.length() );//test if(wifi.find( ">" ) ) { Serial.print(">"); wifi.println(cmd); Serial.println(cmd); } else { sendDebug( "AT+CIPCLOSE" );//close TCP connection } if( wifi.find("OK") ) { Serial.println( "RECEIVED: OK" ); } else { Serial.println( "RECEIVED: Error\nExit2" ); } } void sendDebug(String cmd) { Serial.print("SEND: "); Serial.println(cmd); wifi.println(cmd); } boolean connectWiFi() { Serial.println("AT+CWMODE=3");//WiFi STA mode - if '3' it is both client and AP delay(2000); //Connect to Router with AT+CWJAP="SSID","Password"; // Check if connected with AT+CWJAP? String cmd="AT+CWJAP=\""; // Join accespoint cmd+=SSID; cmd+="\",\""; cmd+=PASS; cmd+="\""; sendDebug(cmd); delay(10000); if(wifi.find("OK")) { Serial.println("RECEIVED: OK"); return true; } else { Serial.println("RECEIVED: Error -> stop!"); while(1); return false; } cmd = "AT+CIPMUX=0";// Set Single connection sendDebug( cmd ); if( wifi.find( "Error") ) { Serial.print( "RECEIVED: Error" ); return false; } } |
After you do all of this just compile and upload sketch.
Important! I had problems with power and had to add another 3.3V power supply because of ESP8266.
I am sure I forgot something, please let me know if you have problems below in comments.
Happy coding, till next time!
Gregor
hi, i have a problem running the code. i am getting error after uploading. please can i get some help
What kind of error?