From 1622d4653797f694f6ee16d165797042fa376cfb Mon Sep 17 00:00:00 2001 From: lexa Date: Sun, 20 Dec 2020 15:34:40 +0300 Subject: [PATCH] Add RozetkaESP --- MYS_Home.code-workspace | 4 + RozetkaESP/.gitignore | 5 + RozetkaESP/.vscode/extensions.json | 7 ++ RozetkaESP/include/README | 39 ++++++++ RozetkaESP/lib/README | 46 +++++++++ RozetkaESP/platformio.ini | 17 ++++ RozetkaESP/src/main.cpp | 150 +++++++++++++++++++++++++++++ RozetkaESP/test/README | 11 +++ 8 files changed, 279 insertions(+) create mode 100644 RozetkaESP/.gitignore create mode 100644 RozetkaESP/.vscode/extensions.json create mode 100644 RozetkaESP/include/README create mode 100644 RozetkaESP/lib/README create mode 100644 RozetkaESP/platformio.ini create mode 100644 RozetkaESP/src/main.cpp create mode 100644 RozetkaESP/test/README diff --git a/MYS_Home.code-workspace b/MYS_Home.code-workspace index 88a659c..69c1f5f 100644 --- a/MYS_Home.code-workspace +++ b/MYS_Home.code-workspace @@ -77,6 +77,10 @@ { "name": "Rozetka", "path": "Rozetka" + }, + { + "name": "RozetkaESP", + "path": "RozetkaESP" } ], "settings": { diff --git a/RozetkaESP/.gitignore b/RozetkaESP/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/RozetkaESP/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/RozetkaESP/.vscode/extensions.json b/RozetkaESP/.vscode/extensions.json new file mode 100644 index 0000000..0f0d740 --- /dev/null +++ b/RozetkaESP/.vscode/extensions.json @@ -0,0 +1,7 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ] +} diff --git a/RozetkaESP/include/README b/RozetkaESP/include/README new file mode 100644 index 0000000..45496b1 --- /dev/null +++ b/RozetkaESP/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/RozetkaESP/lib/README b/RozetkaESP/lib/README new file mode 100644 index 0000000..8c9c29c --- /dev/null +++ b/RozetkaESP/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/RozetkaESP/platformio.ini b/RozetkaESP/platformio.ini new file mode 100644 index 0000000..398d963 --- /dev/null +++ b/RozetkaESP/platformio.ini @@ -0,0 +1,17 @@ +; 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:esp12e] +platform = espressif8266 +board = esp12e +framework = arduino +lib_deps = + ottowinter/AsyncMqttClient-esphome @ ^0.8.4 +; sstaub/Ticker @ ^3.2.0 \ No newline at end of file diff --git a/RozetkaESP/src/main.cpp b/RozetkaESP/src/main.cpp new file mode 100644 index 0000000..90e2327 --- /dev/null +++ b/RozetkaESP/src/main.cpp @@ -0,0 +1,150 @@ +#include +#include +#include +#include +#include + +#define BUTT1 (0) +#define BUTT2 (2) +#define SOCK1 (4) +#define SOCK2 (5) +#define LED_WF (14) +#define LED_MQ (12) +#define LED_WRK (13) + +const char* ssid = "wf-home"; +const char* password = "0ndthnrf"; +const char* mqtt_server = "192.168.1.250"; +unsigned long cRun = millis(); +int nSec, nSampl; +bool stat1, stat2; + +AsyncMqttClient mqttClient; +Ticker mqttReconnectTimer; + +WiFiEventHandler wifiConnectHandler; +WiFiEventHandler wifiDisconnectHandler; +Ticker wifiReconnectTimer; + +void connectToWifi(); +void connectToMqtt(); +void onWifiConnect(const WiFiEventStationModeGotIP& event); +void onWifiDisconnect(const WiFiEventStationModeDisconnected& event); +void onMqttConnect(bool sessionPresent); +void onMqttDisconnect(AsyncMqttClientDisconnectReason reason); +void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total); + +void setup(){ + Serial.begin(9600); + WiFi.mode(WIFI_STA); + WiFi.hostname("Electro"); + Serial.begin(9600); + + ArduinoOTA.onStart([]() { + Serial1.println("Start Update"); // "Начало OTA-апдейта" + }); + ArduinoOTA.onEnd([]() { + Serial1.println("\nEnd Update"); // "Завершение OTA-апдейта" + }); + ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { + //Serial1.printf("Progress: %u%%\r", (progress / (total / 100))); + }); + ArduinoOTA.onError([](ota_error_t error) { + Serial1.printf("Error[%u]: ", error); + if (error == OTA_AUTH_ERROR) Serial1.println("Auth Failed"); + // "Ошибка при аутентификации" + else if (error == OTA_BEGIN_ERROR) Serial1.println("Begin Failed"); + // "Ошибка при начале OTA-апдейта" + else if (error == OTA_CONNECT_ERROR) Serial1.println("Connect Failed"); + // "Ошибка при подключении" + else if (error == OTA_RECEIVE_ERROR) Serial1.println("Receive Failed"); + // "Ошибка при получении данных" + else if (error == OTA_END_ERROR) Serial1.println("End Failed"); + // "Ошибка при завершении OTA-апдейта" + }); + ArduinoOTA.begin(); + wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnect); + wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWifiDisconnect); + + mqttClient.onConnect(onMqttConnect); + mqttClient.onDisconnect(onMqttDisconnect); + mqttClient.setServer(mqtt_server, 1883); + mqttClient.setClientId("Electro"); + + pinMode(LED_WF, OUTPUT); + pinMode(LED_MQ, OUTPUT); + pinMode(LED_WRK, OUTPUT); + //pinMode(5, INPUT_PULLUP); + digitalWrite(LED_WF, LOW); + digitalWrite(LED_MQ, LOW); + digitalWrite(LED_WRK, LOW); + connectToWifi(); + nSec = 0; + nSampl = 0; +} + +void loop(){ + static bool led_wrk = false; + + ArduinoOTA.handle(); + yield(); + if(cRun + 999 < millis()){ + cRun = millis(); + if(WiFi.isConnected()){ + led_wrk = !led_wrk; + digitalWrite(LED_WRK, led_wrk); + nSampl++; + } + if(++nSec > 59){ + nSec = 0; + if(nSampl > 0){ + //Serial.println("Publish"); + char v[7]; + mqttClient.publish("/home/kor/curr", 1, false, v); + } + nSampl = 0; + } + } +} + +void connectToWifi() { + Serial.println("Connecting to Wi-Fi..."); + WiFi.begin(ssid, password); +} + +void connectToMqtt() { + Serial.println("Connecting to MQTT..."); + mqttClient.connect(); +} + +void onWifiConnect(const WiFiEventStationModeGotIP& event) { + Serial.println("Connected to Wi-Fi."); + Serial.print("IP: "); + Serial.println(WiFi.localIP()); + connectToMqtt(); + digitalWrite(LED_WF, HIGH); +} + +void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) { + Serial.println("Disconnected from Wi-Fi."); + mqttReconnectTimer.detach(); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi + wifiReconnectTimer.once(2, connectToWifi); + digitalWrite(LED_WF, LOW); +} + +void onMqttConnect(bool sessionPresent) { + Serial.println("Connected to MQTT."); + Serial.print("Session present: "); + Serial.println(sessionPresent); + digitalWrite(LED_MQ, HIGH); +} + +void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) { + //Serial.println("Disconnected from MQTT."); + + if (WiFi.isConnected()) { + mqttReconnectTimer.once(2, connectToMqtt); + } + digitalWrite(LED_MQ, LOW); +} + diff --git a/RozetkaESP/test/README b/RozetkaESP/test/README new file mode 100644 index 0000000..e7d1588 --- /dev/null +++ b/RozetkaESP/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Unit Testing 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/page/plus/unit-testing.html