209 lines
5.7 KiB
C++
209 lines
5.7 KiB
C++
#include <Arduino.h>
|
|
#include <ArduinoOTA.h>
|
|
#include <Ticker.h>
|
|
#include <AsyncMqttClient.h>
|
|
#include <EmonLib.h>
|
|
|
|
#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();
|
|
double curr;
|
|
int nSec, nSampl;
|
|
|
|
EnergyMonitor emon1;
|
|
|
|
AsyncMqttClient mqttClient;
|
|
Ticker mqttReconnectTimer;
|
|
|
|
WiFiEventHandler wifiConnectHandler;
|
|
WiFiEventHandler wifiDisconnectHandler;
|
|
Ticker wifiReconnectTimer;
|
|
|
|
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);
|
|
}
|
|
|
|
void onMqttSubscribe(uint16_t packetId, uint8_t qos) {
|
|
Serial.println("Subscribe acknowledged.");
|
|
Serial.print(" packetId: ");
|
|
Serial.println(packetId);
|
|
Serial.print(" qos: ");
|
|
Serial.println(qos);
|
|
}
|
|
|
|
void onMqttUnsubscribe(uint16_t packetId) {
|
|
Serial.println("Unsubscribe acknowledged.");
|
|
Serial.print(" packetId: ");
|
|
Serial.println(packetId);
|
|
}
|
|
|
|
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
|
|
}
|
|
|
|
void onMqttPublish(uint16_t packetId) {
|
|
Serial.println("Publish acknowledged.");
|
|
Serial.print(" packetId: ");
|
|
Serial.println(packetId);
|
|
}
|
|
|
|
void setup(){
|
|
Serial.begin(9600);
|
|
WiFi.mode(WIFI_STA);
|
|
WiFi.hostname("Test");
|
|
Serial.begin(9600);
|
|
|
|
emon1.current(0, 30.0);
|
|
|
|
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.onSubscribe(onMqttSubscribe);
|
|
mqttClient.onUnsubscribe(onMqttUnsubscribe);
|
|
mqttClient.onMessage(onMqttMessage);
|
|
mqttClient.onPublish(onMqttPublish);
|
|
mqttClient.setServer(mqtt_server, 1883);
|
|
|
|
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;
|
|
curr = -1;
|
|
nSampl = 0;
|
|
#ifdef ESP8266
|
|
Serial.println("ESP8266");
|
|
delay(1000);
|
|
#endif
|
|
}
|
|
|
|
void loop(){
|
|
static bool led_wrk = false;
|
|
|
|
ArduinoOTA.handle();
|
|
yield();
|
|
if(cRun + 999 < millis()){
|
|
cRun = millis();
|
|
double Irms = 0;
|
|
if(WiFi.isConnected()){
|
|
Irms = emon1.calcIrms(1480) - 0.5;
|
|
if(curr < 0){
|
|
curr = Irms;
|
|
}
|
|
else{
|
|
//curr += (Irms - curr) / 60;
|
|
curr += (Irms);
|
|
}
|
|
// Serial.print("Sec: ");
|
|
// yield();
|
|
// Serial.print(nSec);
|
|
// yield();
|
|
// Serial.print("\t");
|
|
// yield();
|
|
// Serial.print(Irms);
|
|
// yield();
|
|
// Serial.print("\t");
|
|
// yield();
|
|
// Serial.println(Irms * 233);
|
|
led_wrk = !led_wrk;
|
|
digitalWrite(LED_WRK, led_wrk);
|
|
nSampl++;
|
|
}
|
|
if(++nSec > 59){
|
|
nSec = 0;
|
|
curr = curr / (double)nSampl;
|
|
// Serial.print("Millis: ");
|
|
// yield();
|
|
// Serial.print(millis());
|
|
// yield();
|
|
// Serial.print("\t");
|
|
// yield();
|
|
// Serial.print(curr);
|
|
// yield();
|
|
// Serial.print("\t");
|
|
// yield();
|
|
// Serial.println(curr * 233);
|
|
if(nSampl > 0){
|
|
//Serial.println("Publish");
|
|
char v[7];
|
|
sprintf(v, "%.2f", curr);
|
|
mqttClient.publish("/home/kor/curr", 1, false, v);
|
|
}
|
|
nSampl = 0;
|
|
curr = -1;
|
|
}
|
|
}
|
|
}
|
|
|
|
|