First release RozESP
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
#include <Ticker.h>
|
||||
#include <AsyncMqttClient.h>
|
||||
#include <EEPROM.h>
|
||||
#include <Bounce2.h>
|
||||
|
||||
#define BUTT1 (0)
|
||||
#define BUTT2 (2)
|
||||
@@ -15,9 +16,13 @@
|
||||
const char* ssid = "wf-home";
|
||||
const char* password = "0ndthnrf";
|
||||
const char* mqtt_server = "192.168.1.250";
|
||||
|
||||
Bounce but1 = Bounce();
|
||||
Bounce but2 = Bounce();
|
||||
|
||||
unsigned long cRun = millis();
|
||||
int nSec, nSampl;
|
||||
bool stat1, stat2;
|
||||
int nSec;
|
||||
bool stat1, stat2, oldstat1, oldstat2;
|
||||
|
||||
AsyncMqttClient mqttClient;
|
||||
Ticker mqttReconnectTimer;
|
||||
@@ -33,11 +38,12 @@ 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 setup(){
|
||||
Serial.begin(9600);
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.hostname("Electro");
|
||||
WiFi.hostname("Rozetka1");
|
||||
Serial.begin(9600);
|
||||
|
||||
ArduinoOTA.onStart([]() {
|
||||
@@ -68,43 +74,66 @@ void setup(){
|
||||
|
||||
mqttClient.onConnect(onMqttConnect);
|
||||
mqttClient.onDisconnect(onMqttDisconnect);
|
||||
mqttClient.onMessage(onMqttMessage);
|
||||
mqttClient.setServer(mqtt_server, 1883);
|
||||
mqttClient.setClientId("Electro");
|
||||
mqttClient.setClientId("Rozetka1");
|
||||
|
||||
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);
|
||||
but1.attach(BUTT1, INPUT_PULLUP);
|
||||
but2.attach(BUTT2, INPUT_PULLUP);
|
||||
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;
|
||||
connectToWifi();
|
||||
nSec = 0;
|
||||
nSampl = 0;
|
||||
}
|
||||
|
||||
void loop(){
|
||||
static bool led_wrk = false;
|
||||
static unsigned long lwms = millis();
|
||||
|
||||
ArduinoOTA.handle();
|
||||
yield();
|
||||
if(but1.fell()){
|
||||
stat1 = !stat1;
|
||||
}
|
||||
if(but2.fell()){
|
||||
stat2 = !stat2;
|
||||
}
|
||||
if(stat1 != oldstat1){
|
||||
oldstat1 = stat1;
|
||||
digitalWrite(SOCK1, stat1);
|
||||
EEPROM.put(0, stat1);
|
||||
EEPROM.commit();
|
||||
pubBool("/home/roz1/on1", stat1);
|
||||
}
|
||||
if(stat2 != oldstat2){
|
||||
oldstat2 = stat2;
|
||||
digitalWrite(SOCK2, stat2);
|
||||
EEPROM.put(1, stat2);
|
||||
EEPROM.commit();
|
||||
pubBool("/home/roz1/on2", stat2);
|
||||
}
|
||||
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;
|
||||
char v[15];
|
||||
ultoa(millis(), v, 10);
|
||||
mqttClient.publish("/home/roz1/millis", 1, false, v);
|
||||
digitalWrite(LED_WRK, LOW);
|
||||
lwms = millis();
|
||||
}
|
||||
}
|
||||
if(lwms + 300 < millis()) digitalWrite(LED_WRK, HIGH);
|
||||
}
|
||||
|
||||
void connectToWifi() {
|
||||
@@ -122,21 +151,27 @@ void onWifiConnect(const WiFiEventStationModeGotIP& event) {
|
||||
Serial.print("IP: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
connectToMqtt();
|
||||
digitalWrite(LED_WF, HIGH);
|
||||
digitalWrite(LED_WF, LOW);
|
||||
}
|
||||
|
||||
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);
|
||||
digitalWrite(LED_WF, HIGH);
|
||||
}
|
||||
|
||||
void onMqttConnect(bool sessionPresent) {
|
||||
Serial.println("Connected to MQTT.");
|
||||
Serial.print("Session present: ");
|
||||
Serial.println(sessionPresent);
|
||||
digitalWrite(LED_MQ, HIGH);
|
||||
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.subscribe("/home/roz1/on1_set", 1);
|
||||
mqttClient.subscribe("/home/roz1/on2_set", 1);
|
||||
digitalWrite(LED_MQ, LOW);
|
||||
}
|
||||
|
||||
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
|
||||
@@ -145,6 +180,36 @@ void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
|
||||
if (WiFi.isConnected()) {
|
||||
mqttReconnectTimer.once(2, connectToMqtt);
|
||||
}
|
||||
digitalWrite(LED_MQ, LOW);
|
||||
digitalWrite(LED_MQ, HIGH);
|
||||
}
|
||||
|
||||
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total){
|
||||
char pl[20];
|
||||
strncpy(pl, payload, len);
|
||||
pl[len] = 0;
|
||||
Serial.println(topic);
|
||||
Serial.println(pl);
|
||||
if(strcmp(topic, "/home/roz1/on1_set") == 0){
|
||||
if(strcmp(pl, "true") == 0){
|
||||
stat1 = false;
|
||||
}
|
||||
else{
|
||||
stat1 = true;
|
||||
}
|
||||
return;
|
||||
}
|
||||
if(strcmp(topic, "/home/roz1/on2_set") == 0){
|
||||
if(strcmp(pl, "true") == 0){
|
||||
stat2 = false;
|
||||
}
|
||||
else{
|
||||
stat2 = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void pubBool(const char* topic, bool pl){
|
||||
if(mqttClient.connected()){
|
||||
mqttClient.publish(topic, 1, false, pl != true ? "true" : "false");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user