Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
DS18B20 nodeMCU v1.0 with ESP8266 arduino IDE stream data to thingspeak
//nodeMCU v1.0 (black) with Arduino IDE
//stream temperature data DS18B20 with 1wire on ESP8266 ESP12-E (nodeMCU v1.0)
//shin-ajaran.blogspot.com
//nodemcu pinout https://github.com/esp8266/Arduino/issues/584
#include <ESP8266WiFi.h>
#include <OneWire.h>
#include <DallasTemperature.h>
//Def
#define myPeriodic 15 //in sec | Thingspeak pub is 15sec
#define ONE_WIRE_BUS 2 // DS18B20 on arduino pin2 corresponds to D4 on physical board
//#define mySSR 0 // Solid State Relay on pin 0
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
float prevTemp = 0;
const char* server = "api.thingspeak.com";
String apiKey ="your API key here";
const char* MY_SSID = "your SSID here";
const char* MY_PWD = "your SSID password here";
int sent = 0;
void setup() {
Serial.begin(115200);
connectWifi();
}
void loop() {
float temp;
//char buffer[10];
DS18B20.requestTemperatures();
temp = DS18B20.getTempCByIndex(0);
//String tempC = dtostrf(temp, 4, 1, buffer);//handled in sendTemp()
Serial.print(String(sent)+" Temperature: ");
Serial.println(temp);
//if (temp != prevTemp)
//{
//sendTeperatureTS(temp);
//prevTemp = temp;
//}
sendTeperatureTS(temp);
int count = myPeriodic;
while(count--)
delay(1000);
}
void connectWifi()
{
Serial.print("Connecting to "+*MY_SSID);
WiFi.begin(MY_SSID, MY_PWD);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("Connected");
Serial.println("");
}//end connect
void sendTeperatureTS(float temp)
{
WiFiClient client;
if (client.connect(server, 80)) { // use ip 184.106.153.149 or api.thingspeak.com
Serial.println("WiFi Client connected ");
String postStr = apiKey;
postStr += "&field1=";
postStr += String(temp);
postStr += "\r\n\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
delay(1000);
}//end if
sent++;
client.stop();
}//end send
@pointandyshoot
Copy link

Was just about to write this. Works great, thanks!

@matteo5871
Copy link

Thanks, it works fine for me!

@leardinet
Copy link

Great, it works. How can I insert a second probe and use the Blynk app? www.blynk.cc/

@kishoremani
Copy link

what library i should download for connecting wifi module

@petkrt
Copy link

petkrt commented May 24, 2018

Thanks, it works fine for me!thanks a lot

@danylaca
Copy link

danylaca commented Oct 9, 2018

Cannot compile. Got error
exit status 1
Error compiling for board NodeMCU 1.0 (ESP-12E Module).
Need help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment