#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <NTPClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <ArduinoOTA.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
const char *ssid = "*******";
const char *password = "*******";
WiFiUDP ntpUDP;
IPAddress ipMulti(10, 0, 1, 255);
unsigned int portMulti = 12345;
NTPClient timeClient(ntpUDP, -18000);
unsigned long previousMillis = 0;
const long interval = 60000;
void setup() {
Serial.begin(115200);
Serial.println("Initializing");
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
sensors.begin();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
timeClient.begin();
ArduinoOTA.onStart([]() {
Serial.println("Start");
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
getTimeandTemp();
}
ArduinoOTA.handle();
}
char sendudp(String sendit) {
ntpUDP.beginPacket(ipMulti, portMulti);
ntpUDP.println(sendit);
ntpUDP.endPacket();
}
void getTimeandTemp() {
String str;
timeClient.update();
sensors.requestTemperatures();
str = "(Downstairs) at: " + String(timeClient.getFormattedTime()) + "/" + String(timeClient.getEpochTime()) + " -> " + String(sensors.getTempFByIndex(0)) + "F";
sendudp(str);
}
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.