#include <WiFi.h>
#include <WiFiManager.h>
#include <HTTPClient.h>
#include <HTTPUpdate.h>
#include "DHTesp.h"
#include <DallasTemperature.h>
#include <OneWire.h>
#define PIN_HEATER 16
#define PIN_ONEWIRE 4
unsigned long software_version = 1;
const char* ap_ssid = "Brewknot_ota";
bool is_wokwi;
String firmware_url = "https://www.zoam.at/ota/";
const int DHT_PIN = 15;
DHTesp dhtSensor;
OneWire oneWire (PIN_ONEWIRE);
DallasTemperature sensors (&oneWire);
DeviceAddress tempSensor1, tempSensor2;
bool checkDHT(){
TempAndHumidity data = dhtSensor.getTempAndHumidity();
if (isnan(data.temperature)) {
Serial.println("no sensor! you are at home");
return false;
} else {
Serial.println("dht sensor found ! you are at wokwi");
return true;
}
}
void configModeCallback(WiFiManager *myWiFiManager) {
Serial.println("Konfigurationsmodus gestartet. Bitte konfiguriere dein WLAN.");
}
void setup() {
Serial.begin(115200);
Serial.println("------- init esp --------");
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
is_wokwi = checkDHT();
if (is_wokwi){
WiFi.begin("Wokwi-GUEST", "");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
} else {
WiFiManager wifiManager;
// Setze den Callback für den Konfigurationsmodus
wifiManager.setAPCallback(configModeCallback);
// Starte den WiFiManager
if (!wifiManager.autoConnect(ap_ssid)) {
Serial.println("Fehlgeschlagen, die Verbindung zu konfigurieren und verbunden zu bleiben. Starte neu...");
delay(3000);
ESP.restart();
delay(5000);
}
}
// ... z.B. Überprüfung auf verfügbare Updates
checkFirmwareUpdate();
initDS18B20();
}
void loop() {
delay(20);
}
/* *** BEGIN These are used by the updater below *** */
void update_started() {
Serial.println("CALLBACK: HTTP update process started");
}
void update_finished() {
Serial.println("CALLBACK: HTTP update process finished");
}
void update_progress(int cur, int total) {
Serial.printf("CALLBACK: HTTP update process at %d of %d bytes...\n", cur, total);
}
void update_error(int err) {
Serial.printf("CALLBACK: HTTP update fatal error code %d\n", err);
}
void initDS18B20(){
if (is_wokwi){
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
} else {
sensors.begin ();
if (!sensors.getAddress(tempSensor1, 0)) Serial.println("Unable to find address for Device 0");
if (!sensors.getAddress(tempSensor2, 1)) Serial.println("Unable to find address for Device 1");
}
}
void checkFirmwareUpdate(){
if(WiFi.status()== WL_CONNECTED){
HTTPClient http;
String version_url = firmware_url + "fw.txt";
const char* firmwareFileName = "fw.bin";
unsigned long update_version = 0;
http.begin(version_url.c_str());
int httpResponseCode = http.GET();
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
String http_string = http.getString();
unsigned long update_version = http_string.toInt();
Serial.print("Current FW version: "); Serial.println(software_version);
Serial.print("Server FW version: "); Serial.println(update_version);
// ***** BEGIN UPDATER CODE *****
if (software_version != update_version){
String newbin_url = firmware_url + "fw." + update_version + ".bin";
Serial.print("New firmware version available. Updating to "); Serial.print(newbin_url); Serial.print("\n");
httpUpdate.onStart(update_started);
httpUpdate.onEnd(update_finished);
httpUpdate.onProgress(update_progress);
httpUpdate.onError(update_error);
//t_httpUpdate_return ret = httpUpdate.update(client, "newbin_url");
t_httpUpdate_return ret = httpUpdate.update(http, newbin_url);
// Or:
//t_httpUpdate_return ret = httpUpdate.update(client, "server", 80, "/file.bin");
switch (ret) {
case HTTP_UPDATE_FAILED:
Serial.printf("HTTP_UPDATE_FAILED Error (%d): %s\n", httpUpdate.getLastError(), httpUpdate.getLastErrorString().c_str());
break;
case HTTP_UPDATE_NO_UPDATES:
Serial.println("HTTP_UPDATE_NO_UPDATES");
break;
case HTTP_UPDATE_OK:
Serial.println("HTTP_UPDATE_OK");
break;
}
}
// ***** END UPDATER CODE *****
}else{
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
}else{
Serial.println("WiFi Disconnected");
}
}