Add RozetkaESP
This commit is contained in:
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user