Skip to content

Instantly share code, notes, and snippets.

@bessarabov
Created September 30, 2019 14:19
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save bessarabov/49bad13db327fb76f18bc44263919403 to your computer and use it in GitHub Desktop.
#include <DHT.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#define WIFI_NAME "%wifi%"
#define WIFI_PASSWORD "%password%"
#define SERVER_ENDPOINT "http://site/api/dot"
#define DELAY_SECONDS 30
#define DHTPIN 5
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
WiFi.begin(WIFI_NAME, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
Serial.println("Connecting to Wi-Fi...");
delay(500);
}
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Mac address: ");
Serial.println(WiFi.macAddress());
dht.begin();
}
void loop() {
delay(DELAY_SECONDS * 1000);
if (WiFi.status()== WL_CONNECTED) {
float t = dht.readTemperature();
float h = dht.readHumidity();
if (isnan(t) || isnan(h)) {
Serial.println("Error. Can't get temperature/humidity.");
return;
}
char body[200];
char mac[100];
String stringMac = WiFi.macAddress();
stringMac.toCharArray(mac, 100);
sprintf(body, "{\"t\": %.2f, \"h\": %.2f, \"mac\": \"%s\"}", t, h, mac);
Serial.println(body);
HTTPClient http;
http.begin(SERVER_ENDPOINT);
http.addHeader("Content-Type", "text/plain");
int httpCode = http.POST(body);
String payload = http.getString();
Serial.println(httpCode);
Serial.println(payload);
http.end();
} else {
Serial.println("Error in WiFi connection");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment