Hardware components | ||||||
| × | 1 | ||||
Software apps and online services | ||||||
![]() |
|
A routine that I used a long time ago for my aquarium, it's for a Node-MCU 0.9.
You need some extra libraries.
// 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);
}
Comments
Please log in or sign up to comment.