Created
April 18, 2018 05:28
-
-
Save elktros/77db16ce9f1e933a25fffae07f7fc34d to your computer and use it in GitHub Desktop.
Code for Interfacing DS18B20 Temperature Sensor with ESP8266 and posting the information on ThingSpeak API. Code is uploaded directly to ESP8266 Module.
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 <OneWire.h> | |
#include <DallasTemperature.h> | |
#include <ESP8266WiFi.h> | |
#define DS18B20 2 //DS18B20 is connected to GPIO Pin 2 | |
String apiKey = "XXXXXXXXXXXXXXXX"; // Enter your Write API key from ThingSpeak | |
const char* ssid = "SSID"; // Enter your WiFi Network's SSID | |
const char* pass = "PASSWORD"; // Enter your WiFi Network's Password | |
const char* server = "api.thingspeak.com"; | |
float temp; | |
OneWire ourWire(DS18B20); | |
DallasTemperature sensor(&ourWire); | |
WiFiClient client; | |
void setup() | |
{ | |
Serial.begin(115200); | |
delay(1000); | |
sensor.begin(); | |
Serial.print("Connecting to: "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, pass); | |
while (WiFi.status() != WL_CONNECTED) | |
{ | |
delay(100); | |
Serial.print("*"); | |
} | |
Serial.println(""); | |
Serial.println("***WiFi connected***"); | |
} | |
void loop() | |
{ | |
sensor.requestTemperatures(); | |
temp = sensor.getTempCByIndex(0); | |
if (client.connect(server,80)) // "184.106.153.149" or api.thingspeak.com | |
{ | |
String sendData = apiKey+"&field1="+String(temp)+"\r\n\r\n"; | |
//Serial.println(sendData); | |
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(sendData.length()); | |
client.print("\n\n"); | |
client.print(sendData); | |
Serial.print("Temperature: "); | |
Serial.print(temp); | |
Serial.println("deg C. Connecting to Thingspeak.."); | |
} | |
client.stop(); | |
Serial.println("Sending...."); | |
delay(10000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment