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>
|
|
|
|
|
|
|
|
#define BUILTIN_LED D15
|
|
|
|
#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);
|
|
|
|
|
|
|
|
WiFiClientSecure espClient;
|
|
|
|
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>
|
|
|
|
|
|
|
|
#include <mess_BMP280.h>
|
|
|
|
|
2023-05-22 14:29:45 +02:00
|
|
|
#include <mess_Ub.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();
|
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;
|
|
|
|
delay(500);
|
2023-05-18 12:10:48 +02:00
|
|
|
#if (MQTT == 0)
|
|
|
|
init_HTU21();
|
|
|
|
Init_BMP280();
|
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){
|
|
|
|
AKKU = getBattery(); // ca. 170 ms
|
|
|
|
}
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
if (F_BMP280 == true){
|
|
|
|
read_BMP_280();
|
|
|
|
}
|
|
|
|
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
|
|
|
|
espClient.setFingerprint(mqtt_fprint);
|
|
|
|
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());
|
|
|
|
if (F_BMP280) M2M_BMP280(hostname.c_str());
|
|
|
|
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 14:29:45 +02:00
|
|
|
sprintf(msg,"0x%0x", SystemStatus);
|
|
|
|
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);
|
|
|
|
digitalWrite(BUILTIN_LED, HIGH);
|
|
|
|
/* 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);
|
|
|
|
Serial.printf("Systemstatus : 0x%02x \n", SystemStatus);
|
|
|
|
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();
|
|
|
|
Serial.println("STÖRUNG WiFi.");
|
|
|
|
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
|
|
|
|
sprintf(clientName, "%s%s", "ESP8266Client", sID);
|
|
|
|
while (!client.connected()) {
|
|
|
|
Serial.print("Attempting MQTT connection...");
|
|
|
|
verifyFingerprint();
|
|
|
|
// Attempt to connect
|
|
|
|
if (client.connect(clientName)) {
|
|
|
|
Serial.println("connected");
|
|
|
|
// Once connected, publish an announcement...
|
|
|
|
client.publish("outTopic", "hello world");
|
|
|
|
// ... and resubscribe
|
|
|
|
client.subscribe("inTopic");
|
|
|
|
} else {
|
|
|
|
Serial.print("failed, rc=");
|
|
|
|
Serial.print(client.state());
|
|
|
|
Serial.println(" try again in 5 seconds");
|
|
|
|
// Wait 5 seconds before retrying
|
|
|
|
delay(5000);
|
|
|
|
ESP.restart();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|