Power to MAX

This commit is contained in:
2021-01-04 17:12:20 +03:00
parent 8bd7f648fa
commit dea3e1e391
4 changed files with 98 additions and 54 deletions

View File

@@ -18,4 +18,5 @@ upload_port = 192.168.1.137
lib_deps =
ottowinter/AsyncMqttClient-esphome @ ^0.8.4
jwrw/ESP_EEPROM @ ^2.1.0
; sstaub/Ticker @ ^3.2.0

View File

@@ -2,16 +2,16 @@
#include <ArduinoOTA.h>
#include <Ticker.h>
#include <AsyncMqttClient.h>
#include <EEPROM.h>
#include <ESP_EEPROM.h>
#include <Bounce2.h>
#define BUTT1 (0)
#define BUTT2 (2)
#define SOCK1 (4)
#define SOCK2 (5)
#define LED_WF (14)
#define LED_WF (13)
#define LED_MQ (12)
#define LED_WRK (13)
#define LED_WRK (14)
const char* ssid = "wf-home";
const char* password = "0ndthnrf";
@@ -22,7 +22,8 @@ Bounce but2 = Bounce();
unsigned long cRun = millis();
int nSec;
bool stat1, stat2, oldstat1, oldstat2;
uint8_t stat1, stat2, oldstat1, oldstat2;
unsigned long lwms;
AsyncMqttClient mqttClient;
Ticker mqttReconnectTimer;
@@ -38,7 +39,8 @@ 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 pubBool(const char* topic, bool pl);
void pubBool(const char* topic, uint8_t pl);
void onMqttPublish(uint16_t packetId);
void setup(){
Serial.begin(9600);
@@ -75,62 +77,70 @@ void setup(){
mqttClient.onConnect(onMqttConnect);
mqttClient.onDisconnect(onMqttDisconnect);
mqttClient.onMessage(onMqttMessage);
mqttClient.onPublish(onMqttPublish);
mqttClient.setServer(mqtt_server, 1883);
mqttClient.setClientId("Rozetka1");
pinMode(LED_WF, OUTPUT);
pinMode(LED_MQ, OUTPUT);
pinMode(LED_WRK, OUTPUT);
but1.attach(BUTT1, INPUT_PULLUP);
but2.attach(BUTT2, INPUT_PULLUP);
pinMode(BUTT1, INPUT_PULLUP);
pinMode(BUTT2, INPUT_PULLUP);
but1.attach(BUTT1);
but2.attach(BUTT2);
but1.interval(50);
but2.interval(50);
pinMode(SOCK1, OUTPUT);
pinMode(SOCK2, OUTPUT);
digitalWrite(LED_WF, HIGH);
digitalWrite(LED_MQ, HIGH);
digitalWrite(LED_WRK, HIGH);
EEPROM.begin(4);
EEPROM.get(0, stat1);
oldstat1 = !stat1;
EEPROM.get(1, stat1);
oldstat2 = !stat2;
EEPROM.begin(8);
stat1 = EEPROM.read(0);
stat2 = EEPROM.read(1);
oldstat1 = stat1;
digitalWrite(SOCK1, stat1);
//EEPROM.get(4, stat1);
oldstat2 = stat2;
digitalWrite(SOCK2, stat2);
connectToWifi();
nSec = 0;
lwms = millis();
}
void loop(){
static unsigned long lwms = millis();
ArduinoOTA.handle();
yield();
but1.update();
but2.update();
if(but1.fell()){
stat1 = !stat1;
stat1 == 0 ? stat1=1 : stat1=0;
}
if(but2.fell()){
stat2 = !stat2;
stat2 == 0 ? stat2=1 : stat2=0;
}
if(stat1 != oldstat1){
oldstat1 = stat1;
digitalWrite(SOCK1, stat1);
EEPROM.put(0, stat1);
EEPROM.write(0, stat1);
EEPROM.commit();
pubBool("/home/roz1/on1", stat1);
}
if(stat2 != oldstat2){
oldstat2 = stat2;
digitalWrite(SOCK2, stat2);
EEPROM.put(1, stat2);
EEPROM.write(1, stat2);
EEPROM.commit();
pubBool("/home/roz1/on2", stat2);
}
if(cRun + 999 < millis()){
cRun = millis();
if(++nSec > 59){
nSec = 0;
char v[15];
ultoa(millis(), v, 10);
mqttClient.publish("/home/roz1/millis", 1, false, v);
digitalWrite(LED_WRK, LOW);
lwms = millis();
if(mqttClient.connected()){
if(++nSec > 59){
nSec = 0;
char v[15];
ultoa(millis()/1000, v, 10);
mqttClient.publish("/home/roz1/millis", 1, false, v);
}
}
}
if(lwms + 300 < millis()) digitalWrite(LED_WRK, HIGH);
@@ -165,12 +175,13 @@ void onMqttConnect(bool sessionPresent) {
Serial.println("Connected to MQTT.");
Serial.print("Session present: ");
Serial.println(sessionPresent);
mqttClient.publish("/home/roz1/on1", 1, false, stat1 == true ? "false" : "true");
mqttClient.publish("/home/roz1/on1_set", 1, false, stat1 == true ? "false" : "true");
mqttClient.publish("/home/roz1/on2", 1, false, stat2 == true ? "false" : "true");
mqttClient.publish("/home/roz1/on2_set", 1, false, stat2 == true ? "false" : "true");
mqttClient.publish("/home/roz1/on1", 1, false, stat1 == 0 ? "false" : "true");
mqttClient.publish("/home/roz1/on1_set", 1, false, stat1 == 0 ? "false" : "true");
mqttClient.publish("/home/roz1/on2", 1, false, stat2 == 0 ? "false" : "true");
mqttClient.publish("/home/roz1/on2_set", 1, false, stat2 == 0 ? "false" : "true");
mqttClient.subscribe("/home/roz1/on1_set", 1);
mqttClient.subscribe("/home/roz1/on2_set", 1);
nSec = 59;
digitalWrite(LED_MQ, LOW);
}
@@ -191,25 +202,29 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties
Serial.println(pl);
if(strcmp(topic, "/home/roz1/on1_set") == 0){
if(strcmp(pl, "true") == 0){
stat1 = false;
stat1 = 1;
}
else{
stat1 = true;
stat1 = 0;
}
return;
}
if(strcmp(topic, "/home/roz1/on2_set") == 0){
if(strcmp(pl, "true") == 0){
stat2 = false;
stat2 = 1;
}
else{
stat2 = true;
stat2 = 0;
}
}
}
void onMqttPublish(uint16_t packetId) {
digitalWrite(LED_WRK, LOW);
lwms = millis();
}
void pubBool(const char* topic, bool pl){
void pubBool(const char* topic, uint8_t pl){
if(mqttClient.connected()){
mqttClient.publish(topic, 1, false, pl != true ? "true" : "false");
mqttClient.publish(topic, 1, false, pl == 1 ? "true" : "false");
}
}