2023-05-18 11:39:34 +02:00
|
|
|
#include <Arduino.h>
|
2023-05-18 12:10:48 +02:00
|
|
|
#include <FS.h> //this needs to be first, or it all crashes and burns...
|
|
|
|
#include <string>
|
|
|
|
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
|
|
|
|
#include <PubSubClient.h>
|
|
|
|
#include <Ticker.h>
|
|
|
|
|
2024-07-03 10:48:23 +02:00
|
|
|
// #define BUILTIN_LED D2
|
2023-05-18 12:10:48 +02:00
|
|
|
#define TRIGGER_PIN D7
|
|
|
|
#define START_STOP_PIN D6
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void reconnect();
|
|
|
|
void setup_wifi();
|
2023-05-18 23:38:20 +02:00
|
|
|
|
2023-05-18 12:10:48 +02:00
|
|
|
void verifyFingerprint();
|
|
|
|
void callback(char* topic1, byte* payload, unsigned int length);
|
|
|
|
void pulse_pin(uint8_t pin);
|
|
|
|
|
2023-09-23 20:40:29 +02:00
|
|
|
WiFiClient espClient;
|
2023-05-18 12:10:48 +02:00
|
|
|
PubSubClient client(espClient);
|
|
|
|
|
2023-05-19 14:15:31 +02:00
|
|
|
#include <error.h>
|
|
|
|
|
2023-05-18 12:10:48 +02:00
|
|
|
#include<mess_htu21.h>
|
|
|
|
|
2024-07-03 10:48:23 +02:00
|
|
|
//#include <mess_BMP280.h>
|
|
|
|
#include <mess_BME280.h>
|
2023-05-18 12:10:48 +02:00
|
|
|
|
2024-07-03 15:09:00 +02:00
|
|
|
#include <mess_Ub_old.h>
|
2023-05-18 12:10:48 +02:00
|
|
|
|
|
|
|
#include <messADS1115.h>
|
|
|
|
|
|
|
|
#include <mcp9808.h>
|
|
|
|
|
2023-05-18 23:38:20 +02:00
|
|
|
|
2023-05-18 12:10:48 +02:00
|
|
|
|
|
|
|
const char* ssid = STASSID;
|
|
|
|
const char* password = STAPSK;
|
|
|
|
String hostname = NAME;
|
|
|
|
|
|
|
|
const char *MyIP = KMYIP;
|
|
|
|
IPAddress ip;
|
|
|
|
IPAddress gateway;
|
|
|
|
IPAddress subnet(255, 255, 255, 0);
|
|
|
|
IPAddress dns;
|
|
|
|
IPAddress secondaryDNS(8, 8, 8, 8);
|
|
|
|
|
|
|
|
|
|
|
|
const char* mqtt_fprint = "a3:44:1d:aa:6e:e5:c7:55:02:20:98:ea:9b:df:1a:42:a2:f3:e3:0d";
|
|
|
|
const char* mqtt_user = "mqtt";
|
|
|
|
const char* mqtt_pass = "fische";
|
|
|
|
|
|
|
|
const unsigned long interval = TINTERVAL * 60000000LU; // Minuten * Mikrosekunden für Sleep Mode
|
|
|
|
const unsigned long intervalLowBatt = TLOWBATT * 60000000LU; // Minuten * Mikrosekunden für Sleep Mode, Akku entladen
|
|
|
|
const unsigned long stoerung = TERROR * 60000000LU; // Minuten * Mikrosekunden für Sleep Mode
|
|
|
|
|
|
|
|
unsigned long previousMillis = 0;
|
|
|
|
|
|
|
|
long deviceId;
|
|
|
|
char sID[16];
|
|
|
|
char clientName[30];
|
|
|
|
unsigned long startTime;
|
|
|
|
unsigned long endTime;
|
|
|
|
char topic[100];
|
|
|
|
char topic_1[50];
|
|
|
|
char topicWert[20];
|
|
|
|
char msg[20];
|
2023-05-22 14:29:45 +02:00
|
|
|
long int Feldstaerke;
|
2023-05-18 12:10:48 +02:00
|
|
|
|
|
|
|
|
2023-05-18 11:39:34 +02:00
|
|
|
|
|
|
|
void setup() {
|
2023-05-18 23:38:20 +02:00
|
|
|
startTime = millis();
|
2024-07-03 10:48:23 +02:00
|
|
|
/* pinMode(BUILTIN_LED, OUTPUT);
|
|
|
|
digitalWrite(BUILTIN_LED, LOW);
|
|
|
|
delay(5000);
|
|
|
|
digitalWrite(BUILTIN_LED, HIGH); */
|
2023-05-18 12:10:48 +02:00
|
|
|
Serial.begin(74880);
|
|
|
|
while ( !Serial ) delay(100); // wait for native usb
|
|
|
|
WiFi.mode( WIFI_OFF );
|
|
|
|
WiFi.forceSleepBegin();
|
|
|
|
Serial.println();
|
2023-05-18 23:38:20 +02:00
|
|
|
|
2023-05-18 12:10:48 +02:00
|
|
|
Serial.println();
|
|
|
|
Serial.println();
|
|
|
|
Serial.println();
|
2023-05-19 14:15:31 +02:00
|
|
|
readSystemStatus();
|
|
|
|
Serial.printf("Systemstatus : 0x%02x \n", SystemStatus);
|
|
|
|
SystemStatus = 0x00;
|
2024-07-03 10:48:23 +02:00
|
|
|
writeSystemStatus();
|
2023-05-19 14:15:31 +02:00
|
|
|
delay(500);
|
2023-05-18 12:10:48 +02:00
|
|
|
#if (MQTT == 0)
|
|
|
|
init_HTU21();
|
2024-07-03 10:48:23 +02:00
|
|
|
//Init_BMP280();
|
|
|
|
Sensor_BME280();
|
|
|
|
|
2023-05-18 23:38:20 +02:00
|
|
|
init_MCP9808();
|
2023-05-19 14:15:31 +02:00
|
|
|
initADS();
|
2023-05-18 12:10:48 +02:00
|
|
|
#endif
|
2023-05-22 14:29:45 +02:00
|
|
|
if (!F_ADS1115 == true){
|
2024-07-03 15:09:00 +02:00
|
|
|
AKKU = getBattery(korectur); // ca. 170 ms
|
2024-09-28 22:01:53 +02:00
|
|
|
/* Serial.print("ALTE UB MESSUNG:\t\t");
|
|
|
|
Serial.print(AKKU); Serial.println(" V"); */
|
2023-05-22 14:29:45 +02:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
MessungADS();
|
|
|
|
AKKU = readChannel(ADS1115_COMP_0_GND);
|
|
|
|
}
|
2023-05-18 12:10:48 +02:00
|
|
|
// ca. 280 ms
|
|
|
|
if (F_HTU_21D == true){
|
|
|
|
read_HTU21D();
|
|
|
|
}
|
2023-05-19 14:15:31 +02:00
|
|
|
if (F_ADS1115 == true){
|
|
|
|
MessungADS();
|
|
|
|
}
|
2024-07-03 10:48:23 +02:00
|
|
|
/* if (F_BMP280 == true){
|
2023-05-19 14:15:31 +02:00
|
|
|
read_BMP_280();
|
2024-07-03 10:48:23 +02:00
|
|
|
}*/
|
2023-05-19 14:15:31 +02:00
|
|
|
if (F_MCP9808 == true){
|
|
|
|
valTemp = getTemperature_MCP9808();
|
|
|
|
}
|
2023-05-18 12:10:48 +02:00
|
|
|
setup_wifi(); // ca. 4,5 s
|
|
|
|
deviceId = ESP.getChipId();
|
|
|
|
sprintf(sID, "%010ld", deviceId);
|
|
|
|
Serial.print("ID: \t\t"); Serial.println(deviceId);
|
|
|
|
|
|
|
|
// ca. 5 s
|
2023-09-23 20:40:29 +02:00
|
|
|
//espClient.setFingerprint(mqtt_fprint);
|
2023-05-18 12:10:48 +02:00
|
|
|
client.setServer(mqtt_server, mqtt_port);
|
|
|
|
client.setCallback(callback);
|
2023-05-19 14:15:31 +02:00
|
|
|
Serial.printf("Systemstatus : 0x%02x \n", SystemStatus);
|
2023-05-18 12:10:48 +02:00
|
|
|
//----------
|
|
|
|
|
2023-05-18 11:39:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
2023-05-18 12:10:48 +02:00
|
|
|
unsigned long Pause = 0;
|
|
|
|
if (!client.connected()) {
|
|
|
|
reconnect();
|
|
|
|
}
|
|
|
|
int currentMillis = millis();
|
|
|
|
if (currentMillis - previousMillis >= 10000) {
|
|
|
|
previousMillis = currentMillis;
|
|
|
|
dtostrf(AKKU,8,2,ADSData.Akku);
|
|
|
|
sprintf(topic, "%s%s%s", "hjk/devices/", hostname.c_str(), "/telemetry/battery" );
|
|
|
|
client.publish(topic, ADSData.Akku, true);
|
2023-05-22 14:29:45 +02:00
|
|
|
if (F_ADS1115 == true){
|
|
|
|
sprintf(topic, "%s%s%s", "hjk/devices/", hostname.c_str(), "/telemetry/Solar" );
|
|
|
|
client.publish(topic, ADSData.Solar, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (F_HTU_21D) M2M_HTU21D(hostname.c_str());
|
2024-07-03 10:48:23 +02:00
|
|
|
M2M_BME280(hostname.c_str());
|
2023-05-22 14:29:45 +02:00
|
|
|
if (F_MCP9808) M2M_Temperatur_MCP9808(hostname.c_str());
|
2023-05-18 12:10:48 +02:00
|
|
|
sprintf(msg,"%ld", Feldstaerke);
|
|
|
|
sprintf(topic, "%s%s%s", "hjk/devices/", hostname.c_str(), "/telemetry/RSSI" );
|
|
|
|
client.publish(topic, msg, true);
|
2023-05-22 17:24:00 +02:00
|
|
|
sprintf(msg,"0x%02x", SystemStatus);
|
2023-05-22 14:29:45 +02:00
|
|
|
sprintf(topic, "%s%s%s", "hjk/devices/", hostname.c_str(), "/telemetry/SystemStatus" );
|
|
|
|
client.publish(topic, msg, true);
|
2023-05-18 12:10:48 +02:00
|
|
|
client.loop();
|
|
|
|
delay(500);
|
2024-07-03 10:48:23 +02:00
|
|
|
//digitalWrite(BUILTIN_LED, HIGH);
|
2023-05-18 12:10:48 +02:00
|
|
|
/* ESP.deepSleep(5e6);
|
|
|
|
delay(100); */
|
|
|
|
endTime = millis();
|
|
|
|
if (AKKU < MinimalSpannung){
|
2023-05-18 23:38:20 +02:00
|
|
|
|
2023-05-18 12:10:48 +02:00
|
|
|
// ---------------------------------
|
|
|
|
Pause = intervalLowBatt -((endTime - startTime) * 1000); // Pause ca. 60 Minuten
|
|
|
|
Serial.println("AKKU entladen!");
|
2023-05-19 14:15:31 +02:00
|
|
|
SystemStatus = (SystemStatus | AkkuLeer);
|
2023-05-22 17:24:00 +02:00
|
|
|
Serial.printf("Systemstatus : 0x%04x \n", SystemStatus);
|
2023-05-19 14:15:31 +02:00
|
|
|
writeSystemStatus();
|
2023-05-18 12:10:48 +02:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
Pause = interval -((endTime - startTime) * 1000); // Pause ca. 15 Minuten
|
|
|
|
}
|
|
|
|
if(Pause <=0){
|
|
|
|
Pause = 1;
|
|
|
|
}
|
|
|
|
//ESP.restart();
|
|
|
|
Serial.print("Ich gehe für ca. "); Serial.print((Pause/1000/1000/60)+1); Serial.println( " Minuten schlafen.");
|
|
|
|
//ESP.deepSleep(Pause, WAKE_RF_DISABLED); // Pause
|
|
|
|
digitalWrite(START_STOP_PIN, !LOW);
|
|
|
|
#if (DEBUG == 1)
|
|
|
|
ESP.deepSleep(10e6);
|
|
|
|
#else
|
2023-05-19 14:15:31 +02:00
|
|
|
writeSystemStatus();
|
2023-05-18 12:10:48 +02:00
|
|
|
ESP.deepSleep(Pause);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
delay(100);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void setup_wifi() {
|
|
|
|
|
|
|
|
long ErrCount = 0;
|
|
|
|
|
|
|
|
delay(10);
|
|
|
|
// We start by connecting to a WiFi network
|
|
|
|
Serial.println();
|
|
|
|
Serial.print("Connecting to ");
|
|
|
|
Serial.print(ssid);
|
|
|
|
Serial.print(" ");
|
|
|
|
|
|
|
|
WiFi.forceSleepWake();
|
|
|
|
delay( 1 );
|
|
|
|
WiFi.persistent( false );
|
|
|
|
WiFi.mode( WIFI_STA );
|
2023-05-18 23:38:20 +02:00
|
|
|
WiFi.setHostname(hostname.c_str()); //define hostname
|
2023-05-18 12:10:48 +02:00
|
|
|
WiFi.hostname(hostname.c_str());
|
|
|
|
WiFi.mode( WIFI_STA );
|
|
|
|
if (!ip.fromString(MyIP)) { // try to parse into the IPAddress
|
|
|
|
Serial.println("UnParsable IP");
|
|
|
|
}
|
|
|
|
if (!dns.fromString(DNS)) { // try to parse into the IPAddress
|
|
|
|
Serial.println("UnParsable DNS");
|
|
|
|
}
|
|
|
|
if (!gateway.fromString(GATEWAY)) { // try to parse into the IPAddress
|
|
|
|
Serial.println("UnParsable GATEWAY");
|
|
|
|
}
|
|
|
|
//WiFi.config( ip, dns, gateway, subnet );
|
|
|
|
if (!WiFi.config(ip, gateway, subnet, dns, secondaryDNS))
|
|
|
|
{
|
|
|
|
Serial.println("STA Failed to configure");
|
|
|
|
}
|
|
|
|
|
|
|
|
WiFi.begin(ssid, password);
|
|
|
|
|
|
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
|
|
delay(500);
|
|
|
|
Serial.print(".");
|
|
|
|
ErrCount ++;
|
|
|
|
if (ErrCount >= MaxErrCount){
|
|
|
|
endTime = millis();
|
|
|
|
unsigned long Pause = stoerung -((endTime - startTime) * 1000); // Pause
|
|
|
|
Serial.println();
|
2024-09-28 22:01:53 +02:00
|
|
|
Serial.print("STÖRUNG WiFi!!!\t");
|
2023-05-18 12:10:48 +02:00
|
|
|
Serial.print("Ich gehe für ca. "); Serial.print(Pause/1000/1000/60); Serial.println( " Minuten schlafen.");
|
|
|
|
ESP.deepSleep(Pause, WAKE_NO_RFCAL); // Pause
|
|
|
|
delay(100);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Serial.println(" WiFi connected");
|
|
|
|
Serial.print("IP address: \t");
|
2023-05-22 14:29:45 +02:00
|
|
|
Feldstaerke = WiFi.RSSI();
|
|
|
|
Serial.print(WiFi.localIP()); Serial.print("\tRESSI: "); Serial.println(Feldstaerke);
|
2023-05-18 12:10:48 +02:00
|
|
|
/* delay(2000);
|
|
|
|
ESP.deepSleep(2e6, WAKE_RF_DEFAULT);
|
|
|
|
delay(100); */
|
|
|
|
}
|
|
|
|
|
|
|
|
void callback(char* topic1, byte* payload, unsigned int length)
|
|
|
|
{
|
|
|
|
Serial.print("Message arrived [");
|
|
|
|
Serial.print(topic1);
|
|
|
|
Serial.print("] ");
|
|
|
|
for (unsigned int i = 0; i < length; i++) {
|
|
|
|
msg[i] = (char)payload[i];
|
|
|
|
}
|
|
|
|
msg[length] = '\0';
|
|
|
|
Serial.println(msg);
|
|
|
|
if(strcmp(topic1, topic_1)== 0){
|
|
|
|
Serial.print(msg);
|
|
|
|
Serial.println();
|
|
|
|
korectur = atof(msg);
|
|
|
|
Serial.print("Korektur:\t");Serial.println(korectur, 8);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void reconnect() {
|
|
|
|
// Loop until we're reconnected
|
2024-07-03 10:48:23 +02:00
|
|
|
sprintf(clientName, "%s%s", "ESP8266Wetter", sID);
|
2023-05-18 12:10:48 +02:00
|
|
|
while (!client.connected()) {
|
|
|
|
Serial.print("Attempting MQTT connection...");
|
2023-09-23 20:40:29 +02:00
|
|
|
//verifyFingerprint();
|
2023-05-18 12:10:48 +02:00
|
|
|
// Attempt to connect
|
|
|
|
if (client.connect(clientName)) {
|
|
|
|
Serial.println("connected");
|
|
|
|
// Once connected, publish an announcement...
|
2024-07-03 10:48:23 +02:00
|
|
|
// client.publish("outTopic", "hello world");
|
2023-05-18 12:10:48 +02:00
|
|
|
// ... and resubscribe
|
2024-07-03 10:48:23 +02:00
|
|
|
// client.subscribe("inTopic");
|
2023-05-18 12:10:48 +02:00
|
|
|
} else {
|
|
|
|
Serial.print("failed, rc=");
|
|
|
|
Serial.print(client.state());
|
|
|
|
Serial.println(" try again in 5 seconds");
|
|
|
|
// Wait 5 seconds before retrying
|
|
|
|
delay(5000);
|
2024-09-28 22:01:53 +02:00
|
|
|
//
|
2023-05-18 12:10:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void verifyFingerprint() {
|
|
|
|
unsigned long Pause = 0;
|
|
|
|
if(client.connected() || espClient.connected()) return; //Already connected
|
|
|
|
|
|
|
|
Serial.print("\n\tChecking TLS @ ");
|
|
|
|
Serial.print(mqtt_server);
|
|
|
|
Serial.print("...");
|
|
|
|
|
|
|
|
if (!espClient.connect(mqtt_server, mqtt_port)) {
|
|
|
|
//Serial.println("\n\tConnection failed. Rebooting.");
|
|
|
|
Serial.println("\n\tConnection failed.");
|
|
|
|
Serial.flush();
|
|
|
|
//blink.detach();
|
|
|
|
//blink.attach(0.05, flip);
|
|
|
|
//delay(5000);
|
|
|
|
endTime = millis();
|
|
|
|
Pause = stoerung -((endTime - startTime) * 1000); // Pause ca. 15 Minuten
|
|
|
|
Serial.print("\tIch gehe für "); Serial.print((Pause/1000/1000/60)+1); Serial.println( " Minuten schlafen.");
|
|
|
|
ESP.deepSleep(Pause, WAKE_RF_DISABLED); // Pause
|
|
|
|
delay(500);
|
|
|
|
}
|
|
|
|
espClient.stop();
|
|
|
|
delay(100);
|
|
|
|
}
|
|
|
|
void pulse_pin(uint8_t pin)
|
|
|
|
{
|
|
|
|
digitalWrite(pin, LOW);
|
|
|
|
delay(1);
|
|
|
|
digitalWrite(pin, HIGH);
|
2023-05-18 11:39:34 +02:00
|
|
|
}
|