Change mqtt settings
This commit is contained in:
@@ -18,6 +18,7 @@ Ticker wifiReconnectTimer;
|
||||
|
||||
bool lStat1, oldLStat1;
|
||||
bool lStat2, oldLStat2;
|
||||
bool rcv;
|
||||
unsigned long cRun;
|
||||
#define B_LED (16)
|
||||
#define R_LED1 (4)
|
||||
@@ -28,57 +29,15 @@ unsigned long cRun;
|
||||
Bounce l1 = Bounce();
|
||||
Bounce l2 = Bounce();
|
||||
|
||||
void connectToWifi() {
|
||||
WiFi.begin(ssid, password);
|
||||
}
|
||||
bool switchLight(uint8_t nLamp, int state, bool pub);
|
||||
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 connectToMqtt() {
|
||||
mqttClient.connect();
|
||||
}
|
||||
|
||||
void onWifiConnect(const WiFiEventStationModeGotIP& event) {
|
||||
connectToMqtt();
|
||||
}
|
||||
|
||||
void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
|
||||
mqttReconnectTimer.detach(); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
|
||||
wifiReconnectTimer.once(2, connectToWifi);
|
||||
}
|
||||
|
||||
void onMqttConnect(bool sessionPresent) {
|
||||
mqttClient.subscribe("/home/bigroom/lamp1_set", 0);
|
||||
mqttClient.subscribe("/home/bigroom/lamp2_set", 0);
|
||||
mqttClient.publish("/home/bigroom/lamp1", 0, false, lStat1 ? "1" : "0");
|
||||
mqttClient.publish("/home/bigroom/lamp2", 0, false, lStat2 ? "1" : "0");
|
||||
digitalWrite(B_LED, LOW);
|
||||
}
|
||||
|
||||
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
|
||||
if (WiFi.isConnected()) {
|
||||
mqttReconnectTimer.once(2, connectToMqtt);
|
||||
}
|
||||
digitalWrite(B_LED, HIGH);
|
||||
}
|
||||
|
||||
void onMqttSubscribe(uint16_t packetId, uint8_t qos) {
|
||||
}
|
||||
|
||||
void onMqttUnsubscribe(uint16_t packetId) {
|
||||
}
|
||||
|
||||
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
|
||||
if(strcmp(topic, "/home/bigroom/lamp1_set") == 0){
|
||||
if (atoi(payload) == 1) lStat1 = true;
|
||||
else lStat1 = false;
|
||||
}
|
||||
if(strcmp(topic, "/home/bigroom/lamp2_set") == 0){
|
||||
if (atoi(payload) == 1) lStat2 = true;
|
||||
else lStat2 = false;
|
||||
}
|
||||
}
|
||||
|
||||
void onMqttPublish(uint16_t packetId) {
|
||||
}
|
||||
|
||||
void setup() {
|
||||
WiFi.mode(WIFI_STA);
|
||||
@@ -122,6 +81,7 @@ void setup() {
|
||||
EEPROM.get(1, lStat2);
|
||||
oldLStat1 = lStat1;
|
||||
oldLStat2 = lStat2;
|
||||
rcv = false;
|
||||
// Serial.print("Lamp1: ");
|
||||
// Serial.println(lStat1);
|
||||
digitalWrite(R_LED1, lStat1);
|
||||
@@ -133,10 +93,10 @@ void setup() {
|
||||
|
||||
mqttClient.onConnect(onMqttConnect);
|
||||
mqttClient.onDisconnect(onMqttDisconnect);
|
||||
mqttClient.onSubscribe(onMqttSubscribe);
|
||||
mqttClient.onUnsubscribe(onMqttUnsubscribe);
|
||||
// mqttClient.onSubscribe(onMqttSubscribe);
|
||||
// mqttClient.onUnsubscribe(onMqttUnsubscribe);
|
||||
mqttClient.onMessage(onMqttMessage);
|
||||
mqttClient.onPublish(onMqttPublish);
|
||||
// mqttClient.onPublish(onMqttPublish);
|
||||
mqttClient.setServer(mqtt_server, 1883);
|
||||
mqttClient.setClientId("SW_Bigroom");
|
||||
|
||||
@@ -150,25 +110,33 @@ void loop() {
|
||||
l1.update();
|
||||
l2.update();
|
||||
if(l1.fell()){
|
||||
lStat1 = !lStat1;
|
||||
switchLight(R_LED1, !digitalRead(R_LED1), true);
|
||||
//lStat1 = !lStat1;
|
||||
}
|
||||
if(l2.fell()){
|
||||
lStat2 = !lStat2;
|
||||
}
|
||||
if(lStat1 != oldLStat1){
|
||||
digitalWrite(R_LED1, lStat1);
|
||||
oldLStat1 = lStat1;
|
||||
mqttClient.publish("/home/bigroom/lamp1", 0, false, lStat1 ? "1" : "0");
|
||||
EEPROM.put(0, lStat1);
|
||||
EEPROM.commit();
|
||||
}
|
||||
if(lStat2 != oldLStat2){
|
||||
digitalWrite(R_LED2, lStat2);
|
||||
oldLStat2 = lStat2;
|
||||
mqttClient.publish("/home/bigroom/lamp2", 0, false, lStat2 ? "1" : "0");
|
||||
EEPROM.put(1, lStat2);
|
||||
EEPROM.commit();
|
||||
switchLight(R_LED2, !digitalRead(R_LED2), true);
|
||||
//lStat2 = !lStat2;
|
||||
}
|
||||
// if(lStat1 != oldLStat1){
|
||||
// digitalWrite(R_LED1, lStat1);
|
||||
// oldLStat1 = lStat1;
|
||||
// if(!rcv)
|
||||
// mqttClient.publish("/home/bigroom/lamp1", 0, false, lStat1 ? "1" : "0");
|
||||
// else
|
||||
// rcv = false;
|
||||
// EEPROM.put(0, lStat1);
|
||||
// EEPROM.commit();
|
||||
// }
|
||||
// if(lStat2 != oldLStat2){
|
||||
// digitalWrite(R_LED2, lStat2);
|
||||
// oldLStat2 = lStat2;
|
||||
// if(!rcv)
|
||||
// mqttClient.publish("/home/bigroom/lamp2", 0, false, lStat2 ? "1" : "0");
|
||||
// else
|
||||
// rcv = false;
|
||||
// EEPROM.put(1, lStat2);
|
||||
// EEPROM.commit();
|
||||
// }
|
||||
if (cRun + 9999 < millis()){
|
||||
cRun = millis();
|
||||
char v[11];
|
||||
@@ -177,4 +145,61 @@ void loop() {
|
||||
}
|
||||
}
|
||||
|
||||
bool switchLight(uint8_t nLamp, int state, bool pub)
|
||||
{
|
||||
digitalWrite(nLamp, state);
|
||||
EEPROM.put(nLamp == R_LED1 ? 0 : 1, state);
|
||||
EEPROM.commit();
|
||||
String topic = "/home/bigroom/lamp";
|
||||
char n = nLamp == R_LED1 ? '1' : '2';
|
||||
if (pub) mqttClient.publish(String(topic + n).c_str(), 0, false, state ? "1" : "0");
|
||||
return state;
|
||||
}
|
||||
|
||||
void connectToWifi() {
|
||||
WiFi.begin(ssid, password);
|
||||
}
|
||||
|
||||
void connectToMqtt() {
|
||||
mqttClient.connect();
|
||||
}
|
||||
|
||||
void onWifiConnect(const WiFiEventStationModeGotIP& event) {
|
||||
connectToMqtt();
|
||||
}
|
||||
|
||||
void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
|
||||
mqttReconnectTimer.detach(); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
|
||||
wifiReconnectTimer.once(2, connectToWifi);
|
||||
}
|
||||
|
||||
void onMqttConnect(bool sessionPresent) {
|
||||
mqttClient.publish("/home/bigroom/lamp1", 0, false, lStat1 ? "1" : "0");
|
||||
mqttClient.publish("/home/bigroom/lamp2", 0, false, lStat2 ? "1" : "0");
|
||||
mqttClient.subscribe("/home/bigroom/lamp1", 0);
|
||||
mqttClient.subscribe("/home/bigroom/lamp2", 0);
|
||||
digitalWrite(B_LED, LOW);
|
||||
}
|
||||
|
||||
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
|
||||
if (WiFi.isConnected()) {
|
||||
mqttReconnectTimer.once(2, connectToMqtt);
|
||||
}
|
||||
digitalWrite(B_LED, HIGH);
|
||||
}
|
||||
|
||||
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
|
||||
if(String(topic) == "/home/bigroom/lamp1"){
|
||||
|
||||
if (atoi(payload) == 1) switchLight(R_LED1, 1, false);
|
||||
//lStat1 = true;
|
||||
else switchLight(R_LED1, 0, false);
|
||||
//lStat1 = false;
|
||||
//rcv = true;
|
||||
}
|
||||
if(String(topic) == "/home/bigroom/lamp2"){
|
||||
if (atoi(payload) == 1) switchLight(R_LED2, 1, false);
|
||||
//lStat1 = true;
|
||||
else switchLight(R_LED2, 0, false);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user