Created
August 24, 2018 21:43
-
-
Save thomasbrueggemann/b407e5bf41fcc1c3fe0cb5a3d8fa0451 to your computer and use it in GitHub Desktop.
Wemos D1 mini Pro with SHT3x Temperature sensor to ThingSpeak
This file contains hidden or 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 <WEMOS_SHT3X.h> | |
SHT3X sht30(0x45); | |
// Wifi and ThingSpeak settings | |
#include <ESP8266WiFi.h> | |
const char* ssid = ""; | |
const char* password = ""; | |
const char* server = "api.thingspeak.com"; | |
const char* api_key = ""; | |
WiFiClient client; | |
void postData(float temperature, float humidity) | |
{ | |
// Send data to ThingSpeak | |
if (client.connect(server, 80)) | |
{ | |
Serial.println("Connect to ThingSpeak - OK"); | |
String dataToThingSpeak = ""; | |
dataToThingSpeak+="GET /update?api_key="; | |
dataToThingSpeak+=api_key; | |
dataToThingSpeak+="&field1="; | |
dataToThingSpeak+=String(temperature); | |
dataToThingSpeak+="&field2="; | |
dataToThingSpeak+=String(humidity); | |
dataToThingSpeak+=" HTTP/1.1\r\nHost: a.c.d\r\nConnection: close\r\n\r\n"; | |
dataToThingSpeak+=""; | |
client.print(dataToThingSpeak); | |
int timeout = millis() + 5000; | |
while (client.available() == 0) | |
{ | |
if (timeout - millis() < 0) | |
{ | |
Serial.println("Error: Client Timeout!"); | |
client.stop(); | |
return; | |
} | |
} | |
} | |
while(client.available()) | |
{ | |
String line = client.readStringUntil('\r'); | |
Serial.print(line); | |
} | |
} | |
void setup() { | |
Serial.begin(115200); | |
WiFi.begin(ssid, password); | |
} | |
void loop() { | |
sht30.get(); | |
Serial.print("Temperature in Celsius : "); | |
Serial.println(sht30.cTemp); | |
Serial.print("Relative Humidity : "); | |
Serial.println(sht30.humidity); | |
Serial.println(); | |
postData(sht30.cTemp, sht30.humidity); | |
delay(60000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment