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
// Uncomment to override default HW configurations
#define MY_DEFAULT_ERR_LED_PIN 4 // Error led pin
#define MY_DEFAULT_RX_LED_PIN 6 // Receive led pin
#define MY_DEFAULT_TX_LED_PIN 5 // the PCB, on board LED
#define MY_DEFAULT_ERR_LED_PIN A3 // Error led pin
#define MY_DEFAULT_TX_LED_PIN A4 // the PCB, on board LED
#define MY_DEFAULT_RX_LED_PIN A5 // Receive led pin
#include <MySensors.h>
//#include <SdsDustSensor.h>
@@ -91,16 +91,24 @@
#include <SoftwareSerial.h>
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 TX_PIN 3
#define LAMP_OUT 5
SoftwareSerial sSerial(RX_PIN, TX_PIN);
SDS011 sds;
//SdsDustSensor sds(sSerial);
MyMessage msgHum25(0, V_LEVEL);
MyMessage msgHum25_UN(0, V_UNIT_PREFIX);
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 setup()
@@ -116,23 +124,74 @@ void setup()
//Serial.println(sds.setCustomWorkingPeriod(3).toString()); // sensor sends data every 3 minutes
//sds.setActiveReportingMode();
//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();
cSec = 0;
}
void presentation()
{
sendSketchInfo("Koridor", "1.0");
present(0, S_DUST, "pm2.5");
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()
{
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()){
cRun = millis();
int error = sds.read(&p25, &p10);
//Serial.print("ADC: ");Serial.print(adc);Serial.print(", Move: ");Serial.println(move);
if (!error) {
Serial.println(millis()/1000);
Serial.println("P2.5: " + String(p25));
@@ -143,11 +202,48 @@ void loop()
}
if (++cSec > 19){
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 //////////////////////
void SDS011workmode(byte mode, SoftwareSerial *ser)
@@ -158,4 +254,4 @@ void SDS011workmode(byte mode, SoftwareSerial *ser)
for (uint8_t i = 0; i < 19; i++) {
ser->write(sleep_command[i]);
}
}
}