commit dffec99e0c5d38d77d3a54d1fb9ef7ed48df3c70 Author: hans-jurgen Date: Thu Jun 8 00:03:57 2023 +0200 2023-06-08 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..080e70d --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,10 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/include/README b/include/README new file mode 100644 index 0000000..45496b1 --- /dev/null +++ b/include/README @@ -0,0 +1,39 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/include/error.h b/include/error.h new file mode 100644 index 0000000..db22191 --- /dev/null +++ b/include/error.h @@ -0,0 +1,31 @@ +#include + +#include + +#define AkkuLeer 0x80 +#define ADS1115noReady 0x01 +#define HTU21noReady 0x02 +#define BMP280noReady 0x04 +#define MCP9808noReady 0x08 + +byte SystemStatus; + + + + +void writeSystemStatus() +{ + EEPROM.begin(512); + EEPROM.put(0,SystemStatus); + EEPROM.commit(); + EEPROM.end(); +} + +void readSystemStatus() +{ + EEPROM.begin(512); + EEPROM.get(0,SystemStatus); + EEPROM.commit(); + EEPROM.end(); + +} \ No newline at end of file diff --git a/include/mess_htu21.h b/include/mess_htu21.h new file mode 100644 index 0000000..082c6a4 --- /dev/null +++ b/include/mess_htu21.h @@ -0,0 +1,53 @@ +#include +#include +#include +#include +#include + +Adafruit_HTU21DF htu = Adafruit_HTU21DF(); + +bool F_HTU_21D; +float K_HTU= -0.00; + +struct { + char temperature[15] = {0}; + char humity[15] = {0}; +} htuData; + +void init_HTU21(){ + F_HTU_21D = false; + if (!htu.begin()) { + Serial.println("Couldn't find sensor HUT21D!"); + SystemStatus = SystemStatus | HTU21noReady; + + }else{ + F_HTU_21D = true; + Serial.println("HUT21D gefunden"); + } + +} + +void read_HTU21D() { + float t = htu.readTemperature(); + t = t + K_HTU; + dtostrf(t,10,4,htuData.temperature); + float h = htu.readHumidity(); + dtostrf(h,7,1,htuData.humity); + Serial.print("Temperature (HTU21D):\t"); + Serial.print(htuData.temperature); + Serial.println(" °C"); + Serial.print("Luftfeuchtigkeit:\t"); + Serial.print(htuData.humity); + Serial.println(" %"); + +} + +void M2M_HTU21D(String deviceId = "4711") { + char topic[100]; + sprintf(topic, "%s%s%s", "hjk/devices/", deviceId.c_str(), "/telemetry/temperature_Htu_21" ); + client.publish(topic, htuData.temperature, true); + sprintf(topic, "%s%s%s", "hjk/devices/", deviceId.c_str(), "/telemetry/humity" ); + client.publish(topic, htuData.humity, true); + /* Serial.printf("HTU21:\t\t %s °C\n", htuData.temperature); + Serial.printf("HTU21:\t\t %s %%\n", htuData.humity); */ +} \ No newline at end of file diff --git a/lib/README b/lib/README new file mode 100644 index 0000000..8c9c29c --- /dev/null +++ b/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into executable file. + +The source code of each library should be placed in a an own separate directory +("lib/your_library_name/[here are source files]"). + +For example, see a structure of the following two libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..f6139d3 --- /dev/null +++ b/platformio.ini @@ -0,0 +1,80 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env] +platform = espressif8266 +board = d1 +framework = arduino +board_build.filesystem = littlefs +monitor_port = COM11 +monitor_speed = 74800 +monitor_filters = time +upload_port = COM11 +lib_deps = + knolleary/PubSubClient @ 2.8 + adafruit/Adafruit BusIO @ 1.14.1 + adafruit/Adafruit HTU21DF Library @ 1.0.5 + adafruit/Adafruit BMP280 Library @ 2.6.6 + ; wollewald/ADS1115_WE @ 1.4.3 + ; adafruit/Adafruit MCP9808 Library @ 2.0.0 + + + +build_flags = + -DMaxErrCount=20 + + + +[env:debug] ; Entwicklungssystem +build_flags = ${env.build_flags} + -DDEBUG=0 + -DNAME=\"MESSUNITTEMP\" + -DSTASSID=\"St.-Peters-Gasse\" + -DSTAPSK=\"1952994784599317\" + -DGATEWAY=\"192.168.127.1\" + -DDNS=\"192.168.127.1\" + -DKMYIP=\"192.168.127.52\" + -Dmqtt_server=\"hjkmqtt.dedyn.io\" + -Dmqtt_port=61883 + -DTERROR=5 + -DTLOWBATT=60 + -DTINTERVAL=1 + +[env:marcel] ; Produktivsystem: +build_flags = ${env.build_flags} + -DDEBUG=0 + -DEMAIL=\"koerner.c.m@gmail.com\" + -DNAME=\"MESSUNITTEMP\" + -DSTASSID=\"EasyBox-838169\" + -DSTAPSK=\"RcZmua6Xv4R4V5Kf\" + -DGATEWAY=\"192.168.2.1\" + -DDNS=\"192.168.2.1\" + -DKMYIP=\"192.168.2.81\" + -Dmqtt_server=\"hjkmqtt.dedyn.io\" + -Dmqtt_port=8883 + -DTERROR=20 + -DTLOWBATT=60 + -DTINTERVAL=10 + +[env:boris] ; Produktivsystem: +build_flags = ${env.build_flags} + -DDEBUG=0 + -DEMAIL=\"koerner.c.m@gmail.com\" + -DNAME=\"MESSUNITTEMP\" + -DSTASSID=\"EasyBox-838169\" + -DSTAPSK=\"RcZmua6Xv4R4V5Kf\" + -DGATEWAY=\"192.168.2.1\" + -DDNS=\"192.168.2.1\" + -DKMYIP=\"192.168.2.81\" + -Dmqtt_server=\"hjkmqtt.dedyn.io\" + -Dmqtt_port=8883 + -DTERROR=20 + -DTLOWBATT=60 + -DTINTERVAL=1 diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..4741e8d --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,227 @@ +#include +#include //this needs to be first, or it all crashes and burns... +#include +#include //https://github.com/esp8266/Arduino +#include +#include +#include +#include +#include + + + + +#define BUILTIN_LED D2 + +void reconnect(); +void setup_wifi(); + +void callback(char* topic1, byte* payload, unsigned int length); +void pulse_pin(uint8_t pin); + +WiFiClient espClient; +PubSubClient client(espClient); + +#include + +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]; +long int Feldstaerke; + + + +void setup() { + startTime = millis(); + Serial.begin(74880); + while ( !Serial ) delay(100); // wait for native usb + WiFi.mode( WIFI_OFF ); + WiFi.forceSleepBegin(); + Serial.println(); + + Serial.println(); + Serial.println(); + Serial.println(); + readSystemStatus(); + Serial.printf("Systemstatus : 0x%02x \n", SystemStatus); + SystemStatus = 0x00; + delay(500); + init_HTU21(); + if (F_HTU_21D == true){ + read_HTU21D(); + } + setup_wifi(); // ca. 4,5 s + deviceId = ESP.getChipId(); + sprintf(sID, "%010ld", deviceId); + Serial.print("ID: \t\t"); Serial.println(deviceId); + + // ca. 5 s + client.setServer(mqtt_server, mqtt_port); + client.setCallback(callback); + Serial.printf("Systemstatus : 0x%02x \n", SystemStatus); + //---------- + +} + +void loop() { + unsigned long Pause = 0; + if (!client.connected()) { + reconnect(); + } + int currentMillis = millis(); + if (currentMillis - previousMillis >= 10000) { + previousMillis = currentMillis; + if (F_HTU_21D) M2M_HTU21D(hostname.c_str()); + sprintf(msg,"%ld", Feldstaerke); + sprintf(topic, "%s%s%s", "hjk/devices/", hostname.c_str(), "/telemetry/RSSI" ); + client.publish(topic, msg, true); + sprintf(msg,"0x%02x", SystemStatus); + sprintf(topic, "%s%s%s", "hjk/devices/", hostname.c_str(), "/telemetry/SystemStatus" ); + client.publish(topic, msg, true); + client.loop(); + delay(500); + digitalWrite(BUILTIN_LED, HIGH); + endTime = millis(); + Pause = interval -((endTime - startTime) * 1000); // Pause ca. 15 Minuten + if(Pause <=0){ + Pause = 1; + } + Serial.print("Ich gehe für ca. "); Serial.print((Pause/1000/1000/60)+1); Serial.println( " Minuten schlafen."); + #if (DEBUG == 1) + ESP.deepSleep(10e6); + #else + writeSystemStatus(); + 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 ); + WiFi.setHostname(hostname.c_str()); //define hostname + 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"); + Feldstaerke = WiFi.RSSI(); + Serial.print(WiFi.localIP()); Serial.print("\tRESSI: "); Serial.println(Feldstaerke); + /* 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..."); + // 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(); + } + } +} + + + diff --git a/test/README b/test/README new file mode 100644 index 0000000..b0416ad --- /dev/null +++ b/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Test Runner and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html