Add RozetkaESP
This commit is contained in:
@@ -77,6 +77,10 @@
|
|||||||
{
|
{
|
||||||
"name": "Rozetka",
|
"name": "Rozetka",
|
||||||
"path": "Rozetka"
|
"path": "Rozetka"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "RozetkaESP",
|
||||||
|
"path": "RozetkaESP"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"settings": {
|
"settings": {
|
||||||
|
|||||||
5
RozetkaESP/.gitignore
vendored
Normal file
5
RozetkaESP/.gitignore
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
.pio
|
||||||
|
.vscode/.browse.c_cpp.db*
|
||||||
|
.vscode/c_cpp_properties.json
|
||||||
|
.vscode/launch.json
|
||||||
|
.vscode/ipch
|
||||||
7
RozetkaESP/.vscode/extensions.json
vendored
Normal file
7
RozetkaESP/.vscode/extensions.json
vendored
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
// See http://go.microsoft.com/fwlink/?LinkId=827846
|
||||||
|
// for the documentation about the extensions.json format
|
||||||
|
"recommendations": [
|
||||||
|
"platformio.platformio-ide"
|
||||||
|
]
|
||||||
|
}
|
||||||
39
RozetkaESP/include/README
Normal file
39
RozetkaESP/include/README
Normal file
@@ -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
|
||||||
46
RozetkaESP/lib/README
Normal file
46
RozetkaESP/lib/README
Normal file
@@ -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 <Foo.h>
|
||||||
|
#include <Bar.h>
|
||||||
|
|
||||||
|
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
|
||||||
17
RozetkaESP/platformio.ini
Normal file
17
RozetkaESP/platformio.ini
Normal file
@@ -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
|
||||||
150
RozetkaESP/src/main.cpp
Normal file
150
RozetkaESP/src/main.cpp
Normal file
@@ -0,0 +1,150 @@
|
|||||||
|
#include <Arduino.h>
|
||||||
|
#include <ArduinoOTA.h>
|
||||||
|
#include <Ticker.h>
|
||||||
|
#include <AsyncMqttClient.h>
|
||||||
|
#include <EEPROM.h>
|
||||||
|
|
||||||
|
#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);
|
||||||
|
}
|
||||||
|
|
||||||
11
RozetkaESP/test/README
Normal file
11
RozetkaESP/test/README
Normal file
@@ -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
|
||||||
Reference in New Issue
Block a user