Tom van Loon
Published © CC BY-NC-SA

Thingspeak Temperature Routine for DS Sensor

A routine that I used a long time ago for my aquarium.

BeginnerShowcase (no instructions)2 hours668
Thingspeak Temperature Routine for DS Sensor

Things used in this project

Hardware components

NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1

Software apps and online services

ThingSpeak API
ThingSpeak API

Story

Read more

Code

Jeroen V3-r

C/C++
Temperature reading with DS sensor
// www.arduinesp.com 
//
// Plot DTH11 data on thingspeak.com using an ESP8266 
// April 11 2015
// Author: Jeroen Beemster
// Website: www.arduinesp.com
 
#include <DHT.h>
#include <ESP8266WiFi.h>
#include <OneWire.h>
#include <DallasTemperature.h>
 
// replace with your channel's thingspeak API key, 
String apiKey = " XXXX ";
const char* ssid = "YYY ";
const char* password = "ZZZ ";
 
const char* server = "api.thingspeak.com";
// #define DHTPIN 2 // what pin we're connected to

// DS18B20 Setup
#define ONE_WIRE_BUS 2 // Connected to D4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
 
//DHT dht(DHTPIN, DHT11,15);

WiFiClient client;

// DS18B20 Setup

void setup() {                
  Serial.begin(115200);
  delay(10);
  //dht.begin();
  sensors.begin();
      
  WiFi.begin(ssid, password);
  
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    
  }
  
}
 
void loop() {
   
  // float h = dht.readHumidity();
  // float t = dht.readTemperature();
  
  // Read out DS18B20
  sensors.requestTemperatures();
  float t1 = (sensors.getTempCByIndex(0));
  
  if (client.connect(server,80)) {  //   "184.106.153.149" or api.thingspeak.com
    String postStr = apiKey;
           postStr +="&field1=";
           postStr += String(t1);
           // postStr +="&field2=";
           // postStr += String(h);
           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);
     
     Serial.print(" Send to thingspeak ");
     Serial.println(t1);
     
  }
  client.stop();
   
  Serial.println("Waiting...");    
  // thingspeak needs minimum 15 sec delay between updates
  
  delay(57500);  
}

Credits

Tom van Loon
4 projects • 2 followers
Sr Test Technician - OPS
Contact

Comments

Please log in or sign up to comment.