Changed QOS for switches

This commit is contained in:
2020-10-22 16:08:38 +03:00
parent 22ae1d5643
commit 8888a0a854
6 changed files with 131 additions and 33 deletions

View File

@@ -81,9 +81,9 @@
// Flash leds on rx/tx/err // Flash leds on rx/tx/err
// Uncomment to override default HW configurations // Uncomment to override default HW configurations
#define MY_DEFAULT_ERR_LED_PIN 4 // Error led pin #define MY_DEFAULT_ERR_LED_PIN A3 // Error led pin
#define MY_DEFAULT_RX_LED_PIN 6 // Receive led pin #define MY_DEFAULT_TX_LED_PIN A4 // the PCB, on board LED
#define MY_DEFAULT_TX_LED_PIN 5 // the PCB, on board LED #define MY_DEFAULT_RX_LED_PIN A5 // Receive led pin
#include <MySensors.h> #include <MySensors.h>
//#include <SdsDustSensor.h> //#include <SdsDustSensor.h>
@@ -91,16 +91,24 @@
#include <SoftwareSerial.h> #include <SoftwareSerial.h>
unsigned long cRun; unsigned long cRun;
int cSec; int cSec, adc, move, oldmov, minLight, minLightDB, LightInt;
uint8_t sdsPeriod;
bool lamp;
//float avgL;
#define RX_PIN 2 #define RX_PIN 2
#define TX_PIN 3 #define TX_PIN 3
#define LAMP_OUT 5
SoftwareSerial sSerial(RX_PIN, TX_PIN); SoftwareSerial sSerial(RX_PIN, TX_PIN);
SDS011 sds; SDS011 sds;
//SdsDustSensor sds(sSerial);
MyMessage msgHum25(0, V_LEVEL); MyMessage msgHum25(0, V_LEVEL);
MyMessage msgHum25_UN(0, V_UNIT_PREFIX);
MyMessage msgHum10(1, V_LEVEL); MyMessage msgHum10(1, V_LEVEL);
MyMessage msgHum10_UN(1, V_UNIT_PREFIX); MyMessage msgLightLev(2, V_LEVEL);
MyMessage msgLightLevSet(3, V_VAR1);
MyMessage msgMillis(3, V_VAR4);
MyMessage msgMove(4, V_TRIPPED);
MyMessage msgLamp(5, V_VAR1);
void SDS011workmode(byte mode, SoftwareSerial *ser); void SDS011workmode(byte mode, SoftwareSerial *ser);
void setup() void setup()
@@ -116,23 +124,74 @@ void setup()
//Serial.println(sds.setCustomWorkingPeriod(3).toString()); // sensor sends data every 3 minutes //Serial.println(sds.setCustomWorkingPeriod(3).toString()); // sensor sends data every 3 minutes
//sds.setActiveReportingMode(); //sds.setActiveReportingMode();
//sds.setCustomWorkingPeriod(1); //sds.setCustomWorkingPeriod(1);
pinMode(A1, INPUT_PULLUP);
pinMode(LAMP_OUT, OUTPUT);
//digitalWrite(7, HIGH);
//saveState(6, 2);
minLight = (loadState(0) << 8) + loadState(1);
minLightDB = (loadState(2) << 8) + loadState(3);
LightInt = (loadState(4) << 8) + loadState(5);
sdsPeriod = loadState(6);
SDS011workmode(sdsPeriod, &sSerial);
// if(minLight == 0) minLight = 5;
//adc = analogRead(A0);
oldmov = 0;
lamp = false;
cRun = millis(); cRun = millis();
cSec = 0; cSec = 0;
} }
void presentation() void presentation()
{ {
sendSketchInfo("Koridor", "1.0");
present(0, S_DUST, "pm2.5"); present(0, S_DUST, "pm2.5");
present(1, S_DUST, "pm10"); present(1, S_DUST, "pm10");
present(2, S_LIGHT_LEVEL, "minLight");
present(3, S_CUSTOM, "minLightSet");
present(4, S_MOTION, "Move");
present(5, S_CUSTOM, "LampOut");
} }
void loop() void loop()
{ {
float p25, p10; float p25, p10;
move = digitalRead(A1);
if(move != oldmov){
oldmov = move;
send(msgMove.set(move));
}
adc = analogRead(A0);
//avgL += ((float)(adc) - avgL) * 0.1f;
if ((adc <= minLight) && (move == 1)){
analogWrite(LAMP_OUT, LightInt);
//digitalWrite(6, HIGH);
if(lamp == false){
Serial.println("Lamp ON");
Serial.print("ADC: ");Serial.print(adc);
Serial.print(", Move: ");Serial.println(move);
send(msgLightLev.set(adc, 2));
wait(50);
send(msgLamp.set(true));
lamp = true;
}
}
else if((adc > (minLight + minLightDB)) || (move == 0)){
analogWrite(LAMP_OUT, 0);
//digitalWrite(6, LOW);
if(lamp == true){
Serial.println("Lamp OFF");
Serial.print("ADC: ");Serial.print(adc);
Serial.print(", Move: ");Serial.println(move);
send(msgLightLev.set(adc, 2));
wait(50);
send(msgLamp.set(false));
lamp = false;
}
}
if(cRun + 999 < millis()){ if(cRun + 999 < millis()){
cRun = millis(); cRun = millis();
int error = sds.read(&p25, &p10); int error = sds.read(&p25, &p10);
//Serial.print("ADC: ");Serial.print(adc);Serial.print(", Move: ");Serial.println(move);
if (!error) { if (!error) {
Serial.println(millis()/1000); Serial.println(millis()/1000);
Serial.println("P2.5: " + String(p25)); Serial.println("P2.5: " + String(p25));
@@ -143,11 +202,48 @@ void loop()
} }
if (++cSec > 19){ if (++cSec > 19){
cSec = 0; cSec = 0;
sendHeartbeat(); //sendHeartbeat();
wait(100);
send(msgLightLev.set(adc));
wait(100);
uint32_t ms = millis();
send(msgMillis.set(ms));
} }
} }
} }
void receive(const MyMessage &message)
{
if((message.sensor == 3) && (message.type == V_VAR1)){
minLight = message.getInt();
saveState(0, (minLight>>8) & 0xFF);
saveState(1, minLight & 0xFF);
Serial.print("minLight:");
Serial.println(minLight);
}
if((message.sensor == 3) && (message.type == V_VAR2)){
minLightDB = message.getInt();
saveState(2, (minLightDB>>8) & 0xFF);
saveState(3, minLightDB & 0xFF);
Serial.print("LightDB:");
Serial.println(minLightDB);
}
if((message.sensor == 3) && (message.type == V_VAR3)){
LightInt = message.getInt();
saveState(4, (LightInt>>8) & 0xFF);
saveState(5, LightInt & 0xFF);
Serial.print("Light Int:");
if (lamp == true) analogWrite(6, LightInt);
Serial.println(LightInt);
}
if((message.sensor == 3) && (message.type == V_VAR5)){
sdsPeriod = message.getInt();
saveState(6, sdsPeriod);
Serial.print("Sds period:");
}
}
////////////////// sends work mode command to SDS011 ////////////////////// ////////////////// sends work mode command to SDS011 //////////////////////
void SDS011workmode(byte mode, SoftwareSerial *ser) void SDS011workmode(byte mode, SoftwareSerial *ser)
@@ -158,4 +254,4 @@ void SDS011workmode(byte mode, SoftwareSerial *ser)
for (uint8_t i = 0; i < 19; i++) { for (uint8_t i = 0; i < 19; i++) {
ser->write(sleep_command[i]); ser->write(sleep_command[i]);
} }
} }

View File

@@ -72,6 +72,7 @@ void setup() {
mqttClient.onDisconnect(onMqttDisconnect); mqttClient.onDisconnect(onMqttDisconnect);
mqttClient.onMessage(onMqttMessage); mqttClient.onMessage(onMqttMessage);
mqttClient.setServer(mqtt_server, 1883); mqttClient.setServer(mqtt_server, 1883);
mqttClient.setClientId("KuhLight");
pinMode(LAMP, OUTPUT); pinMode(LAMP, OUTPUT);
@@ -108,7 +109,7 @@ void loop() {
if(nSec > 59){ if(nSec > 59){
if(mqttClient.connected()){ if(mqttClient.connected()){
char v[11]; char v[11];
itoa(millis(), v, 10); ultoa(millis(), v, 10);
mqttClient.publish("/home/kuh/ltblmillis", 0, false, v); mqttClient.publish("/home/kuh/ltblmillis", 0, false, v);
} }
nSec = 0; nSec = 0;
@@ -119,7 +120,7 @@ void loop() {
bool switchLight(uint8_t nLamp, int state, bool pub) bool switchLight(uint8_t nLamp, int state, bool pub)
{ {
digitalWrite(nLamp, state); digitalWrite(nLamp, state);
if (pub) mqttClient.publish("/home/kuh/lighttbl", 0, false, state ? "1" : "0"); if (pub) mqttClient.publish("/home/kuh/lighttbl", 1, false, state ? "1" : "0");
return state; return state;
} }
@@ -143,8 +144,8 @@ void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
} }
void onMqttConnect(bool sessionPresent) { void onMqttConnect(bool sessionPresent) {
mqttClient.publish("/home/kuh/lighttbl", 0, false, digitalRead(LAMP) == 1 ? "1" : "0"); mqttClient.subscribe("/home/kuh/lighttbl", 1);
mqttClient.subscribe("/home/kuh/lighttbl", 0); mqttClient.publish("/home/kuh/lighttbl", 1, false, digitalRead(LAMP) == 1 ? "1" : "0");
digitalWrite(LED_MQ, HIGH); digitalWrite(LED_MQ, HIGH);
} }

View File

@@ -140,7 +140,7 @@ void loop() {
if (cRun + 9999 < millis()){ if (cRun + 9999 < millis()){
cRun = millis(); cRun = millis();
char v[11]; char v[11];
itoa(cRun, v, 10); ultoa(cRun, v, 10);
mqttClient.publish("/home/bigroom/millislamp", 0, false, v); mqttClient.publish("/home/bigroom/millislamp", 0, false, v);
} }
} }
@@ -152,7 +152,7 @@ bool switchLight(uint8_t nLamp, int state, bool pub)
EEPROM.commit(); EEPROM.commit();
String topic = "/home/bigroom/lamp"; String topic = "/home/bigroom/lamp";
char n = nLamp == R_LED1 ? '1' : '2'; char n = nLamp == R_LED1 ? '1' : '2';
if (pub) mqttClient.publish(String(topic + n).c_str(), 0, false, state ? "1" : "0"); if (pub) mqttClient.publish(String(topic + n).c_str(), 1, false, state ? "1" : "0");
return state; return state;
} }
@@ -174,10 +174,10 @@ void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
} }
void onMqttConnect(bool sessionPresent) { void onMqttConnect(bool sessionPresent) {
mqttClient.publish("/home/bigroom/lamp1", 0, false, lStat1 ? "1" : "0"); mqttClient.subscribe("/home/bigroom/lamp1", 1);
mqttClient.publish("/home/bigroom/lamp2", 0, false, lStat2 ? "1" : "0"); mqttClient.subscribe("/home/bigroom/lamp2", 1);
mqttClient.subscribe("/home/bigroom/lamp1", 0); mqttClient.publish("/home/bigroom/lamp1", 1, false, lStat1 ? "1" : "0");
mqttClient.subscribe("/home/bigroom/lamp2", 0); mqttClient.publish("/home/bigroom/lamp2", 1, false, lStat2 ? "1" : "0");
digitalWrite(B_LED, LOW); digitalWrite(B_LED, LOW);
} }

View File

@@ -148,7 +148,7 @@ void loop() {
if (cRun + 9999 < millis()){ if (cRun + 9999 < millis()){
cRun = millis(); cRun = millis();
char v[11]; char v[11];
itoa(cRun, v, 10); ultoa(cRun, v, 10);
mqttClient.publish("/home/kor/millislamp", 0, false, v); mqttClient.publish("/home/kor/millislamp", 0, false, v);
} }
} }
@@ -158,7 +158,7 @@ bool switchLight(uint8_t nLamp, int state, bool pub)
digitalWrite(nLamp, state); digitalWrite(nLamp, state);
EEPROM.put(0, state); EEPROM.put(0, state);
EEPROM.commit(); EEPROM.commit();
if (pub) mqttClient.publish("/home/kor/lamp1", 0, false, state ? "1" : "0"); if (pub) mqttClient.publish("/home/kor/lamp1", 1, false, state ? "1" : "0");
return state; return state;
} }
@@ -186,8 +186,8 @@ void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
} }
void onMqttConnect(bool sessionPresent) { void onMqttConnect(bool sessionPresent) {
mqttClient.subscribe("/home/kor/lamp1", 1);
mqttClient.publish("/home/kor/lamp1", 0, false, digitalRead(R_LED) == 1 ? "1" : "0"); mqttClient.publish("/home/kor/lamp1", 0, false, digitalRead(R_LED) == 1 ? "1" : "0");
mqttClient.subscribe("/home/kor/lamp1", 0);
digitalWrite(B_LED, LOW); digitalWrite(B_LED, LOW);
} }

View File

@@ -91,6 +91,7 @@ void setup() {
mqttClient.onDisconnect(onMqttDisconnect); mqttClient.onDisconnect(onMqttDisconnect);
mqttClient.onMessage(onMqttMessage); mqttClient.onMessage(onMqttMessage);
mqttClient.setServer(mqtt_server, 1883); mqttClient.setServer(mqtt_server, 1883);
mqttClient.setClientId("Sw_MidRoom");
connectToWifi(); connectToWifi();
@@ -116,7 +117,7 @@ bool switchLight(uint8_t nLamp, int state, bool pub)
EEPROM.commit(); EEPROM.commit();
String topic = "/home/midroom/lamp"; String topic = "/home/midroom/lamp";
char n = nLamp == R_LED1 ? '1' : '2'; char n = nLamp == R_LED1 ? '1' : '2';
if (pub) mqttClient.publish(String(topic + n).c_str(), 0, false, state ? "1" : "0"); if (pub) mqttClient.publish(String(topic + n).c_str(), 1, false, state ? "1" : "0");
return state; return state;
} }
@@ -138,10 +139,10 @@ void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
} }
void onMqttConnect(bool sessionPresent) { void onMqttConnect(bool sessionPresent) {
mqttClient.publish("/home/midroom/lamp1", 0, false, digitalRead(R_LED1) == 1 ? "1" : "0"); mqttClient.subscribe("/home/midroom/lamp1", 1);
mqttClient.publish("/home/midroom/lamp2", 0, false, digitalRead(R_LED2) == 1 ? "1" : "0"); mqttClient.subscribe("/home/midroom/lamp2", 1);
mqttClient.subscribe("/home/midroom/lamp1", 0); mqttClient.publish("/home/midroom/lamp1", 1, false, digitalRead(R_LED1) == 1 ? "1" : "0");
mqttClient.subscribe("/home/midroom/lamp2", 0); mqttClient.publish("/home/midroom/lamp2", 1, false, digitalRead(R_LED2) == 1 ? "1" : "0");
digitalWrite(B_LED, LOW); digitalWrite(B_LED, LOW);
} }

View File

@@ -125,7 +125,7 @@ bool switchLight(uint8_t nLamp, int state, bool pub)
EEPROM.commit(); EEPROM.commit();
String topic = "/home/smallroom/lamp"; String topic = "/home/smallroom/lamp";
char n = nLamp == R_LED1 ? '1' : '2'; char n = nLamp == R_LED1 ? '1' : '2';
if (pub) mqttClient.publish(String(topic + n).c_str(), 0, false, state ? "1" : "0"); if (pub) mqttClient.publish(String(topic + n).c_str(), 1, false, state ? "1" : "0");
return state; return state;
} }
@@ -153,10 +153,10 @@ void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
} }
void onMqttConnect(bool sessionPresent) { void onMqttConnect(bool sessionPresent) {
mqttClient.publish("/home/smallroom/lamp1", 0, false, digitalRead(R_LED1) == 1 ? "1" : "0"); mqttClient.subscribe("/home/smallroom/lamp1", 1);
mqttClient.publish("/home/smallroom/lamp2", 0, false, digitalRead(R_LED2) == 1 ? "1" : "0"); mqttClient.subscribe("/home/smallroom/lamp2", 1);
mqttClient.subscribe("/home/smallroom/lamp1", 0); mqttClient.publish("/home/smallroom/lamp1", 1, false, digitalRead(R_LED1) == 1 ? "1" : "0");
mqttClient.subscribe("/home/smallroom/lamp2", 0); mqttClient.publish("/home/smallroom/lamp2", 1, false, digitalRead(R_LED2) == 1 ? "1" : "0");
digitalWrite(B_LED, LOW); digitalWrite(B_LED, LOW);
} }