Last active
August 5, 2020 03:43
-
-
Save zk-1/4bbce712ca4bc53f402a2469d1117f35 to your computer and use it in GitHub Desktop.
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 <ESP8266WiFi.h> | |
#include <ESP8266mDNS.h> | |
#include <ArduinoOTA.h> | |
#include <WiFiUdp.h> | |
#include <PubSubClient.h> | |
#include <NTPClient.h> | |
#include <ArduinoJson.h> | |
#include <string> | |
#include <DHT.h> | |
/* | |
* Code: Weather Station (DHT22) | |
* Author: Z Kelly | |
* Date: 20200806 | |
* Updated: 20200806 | |
* Notes: Conversion from Tasmota to Arduino code | |
* For long loops (30 sec), MQTT KeepAlive needs to be higher (i.e. 60 sec) | |
* Keepalive is sent to broker to inform, it's not necessarily the ping interval | |
* ### DHT.cpp v.13.10 line 260 patched as per https://github.com/adafruit/DHT-sensor-library/issues/116#issuecomment-616213303 | |
*/ | |
#define DHTPIN 2 | |
#define DHTTYPE DHT22 | |
const char* device_name = "ESP-SENS-1"; | |
const char* ota_password = "pw"; | |
const char* ssid = "Sonic Soul Surfer"; | |
const char* password = "pw"; | |
const char* mqttServer = "10.10.10.10"; | |
const int mqttPort = 1883; | |
const float adc_correction = -0.211; | |
// --------------------------------------- | |
ADC_MODE(ADC_VCC); | |
DHT dht(DHTPIN, DHTTYPE); | |
WiFiUDP ntpUDP; | |
NTPClient timeClient(ntpUDP, "pool.ntp.org"); | |
WiFiClient espClient; | |
PubSubClient MQTTClient(espClient); | |
float calcDewpoint(float humi, float temp) { | |
float k; | |
k = log(humi/100) + (17.62 * temp) / (243.12 + temp); | |
return roundf((243.12 * k / (17.62 - k)) * 10) / 10; | |
} | |
String timePad (int input) { | |
char buffer [4]; | |
sprintf (buffer, "%02u", input); | |
return String(buffer); | |
} | |
void setup() { | |
pinMode(1, OUTPUT); | |
digitalWrite(LED_BUILTIN, HIGH); | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
} | |
ArduinoOTA.setHostname(device_name); | |
ArduinoOTA.setPassword(ota_password); | |
ArduinoOTA.begin(); | |
dht.begin(); | |
timeClient.begin(); | |
MQTTClient.setServer(mqttServer, mqttPort); | |
MQTTClient.setKeepAlive(60); | |
while (!MQTTClient.connected()) { | |
if (!MQTTClient.connect(device_name)) { | |
delay(500); | |
} | |
} | |
MQTTClient.publish("tele/ESP-SENS-1/STATE", "ESP-SENS-1 Online"); | |
delay(2000); | |
} | |
void loop() { | |
ArduinoOTA.handle(); | |
timeClient.update(); | |
MQTTClient.loop(); | |
while (!MQTTClient.connected()) { | |
if (!MQTTClient.connect(device_name)) { | |
delay(500); | |
} | |
} | |
int NTPHour = timeClient.getHours(); | |
int NTPMin = timeClient.getMinutes(); | |
String theTime = timePad(NTPHour) + ":" + timePad(NTPMin) + " UTC"; | |
float dhtTemp = dht.readTemperature(); | |
float dhtHum = dht.readHumidity(); | |
float dhtDew = calcDewpoint(dhtHum, dhtTemp); | |
float vcc = ESP.getVcc()/1000.0+adc_correction; | |
StaticJsonDocument<200> doc; | |
doc["Time"] = theTime; | |
doc["Voltage"] = vcc; | |
JsonObject sensor1 = doc.createNestedObject("AM2301"); | |
sensor1["Temperature"] = dhtTemp; | |
sensor1["Humidity"] = dhtHum; | |
sensor1["DewPoint"] = dhtDew; | |
doc["TempUnit"] = "C"; | |
char buffer[140]; | |
serializeJson(doc, buffer); | |
MQTTClient.publish("tele/ESP-SENS-1/SENSOR", buffer); | |
digitalWrite(LED_BUILTIN, LOW); | |
delay(40); | |
digitalWrite(LED_BUILTIN, HIGH); | |
for (int i = 0; i <= 12; i++) { | |
delay(2500); | |
ArduinoOTA.handle(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment