285 lines
8.8 KiB
C++
285 lines
8.8 KiB
C++
#include "main.h"
|
|
|
|
void presentation();
|
|
void sendDataI(MyMessage msg, uint32_t status);
|
|
void sendDataF(MyMessage msg, float status);
|
|
void sendDataB(MyMessage msg, bool status);
|
|
|
|
MyMessage msgTemp(0, V_TEMP);
|
|
MyMessage msgHum(1, V_HUM);
|
|
MyMessage msgMove(2, V_TRIPPED);
|
|
MyMessage msgCO2(3, V_VAR1);
|
|
MyMessage msgLight(3, V_VAR2);
|
|
MyMessage msgTimeWork(3, V_VAR3);
|
|
MyMessage msgTimeMotion(3, V_VAR4);
|
|
//MyMessage msgPeriodCO2(3, V_VAR5);
|
|
MyMessage msgWLamp(4, V_DIMMER);
|
|
MyMessage msgSPLight(5, V_VAR1);
|
|
MyMessage msgDBLight(5, V_VAR2);
|
|
MyMessage msgWLight(5, V_VAR3);
|
|
MyMessage msgTimeLight(5, V_VAR4);
|
|
|
|
void before(){
|
|
pinMode(PS, OUTPUT);
|
|
digitalWrite(PS, LOW);
|
|
pinMode(LED_BLUE, OUTPUT);
|
|
pinMode(LED_WHITE, OUTPUT);
|
|
digitalWrite(LED_BLUE, HIGH);
|
|
digitalWrite(LED_WHITE, HIGH);
|
|
}
|
|
|
|
void setup() {
|
|
pinMode(MOTION, INPUT_PULLUP);
|
|
mySerial.begin(BAUDRATE); // Uno example: Begin Stream with MHZ19 baudrate
|
|
myMHZ19.begin(mySerial); // *Important, Pass your Stream reference here
|
|
myMHZ19.autoCalibration(false);
|
|
|
|
/*char myVersion[4];
|
|
myMHZ19.getVersion(myVersion);
|
|
Serial.print("\nFirmware Version: ");
|
|
for(byte i = 0; i < 4; i++)
|
|
{
|
|
Serial.print(myVersion[i]);
|
|
if(i == 1)
|
|
Serial.print(".");
|
|
}
|
|
|
|
Serial.println("");
|
|
Serial.print("Range: ");
|
|
Serial.println(myMHZ19.getRange());
|
|
Serial.print("Background CO2: ");
|
|
Serial.println(myMHZ19.getBackgroundCO2());
|
|
Serial.print("Temperature Cal: ");
|
|
Serial.println(myMHZ19.getTempAdjustment());
|
|
Serial.print("ABC Status: "); myMHZ19.getABC() ? Serial.println("ON") : Serial.println("OFF");*/
|
|
// periodMinCO2 = loadState(0);
|
|
// sendDataI(msgPeriodCO2, periodMinCO2);
|
|
// if(periodMinCO2 == 0) periodMinCO2 = 1;
|
|
periodMotion = loadState(11);
|
|
sendDataI(msgTimeMotion, periodMotion);
|
|
// if(periodMotion == 0) periodMotion = 60;
|
|
spLight = (loadState(12) << 8) + loadState(13);
|
|
sendDataI(msgSPLight, spLight);
|
|
dbLight = (loadState(14) << 8) + loadState(15);
|
|
sendDataI(msgDBLight, dbLight);
|
|
levelBlue = loadState(6);
|
|
levelWhite = loadState(7);
|
|
// timeLight = loadState(8);
|
|
// sendDataI(msgTimeWork, timeLight);
|
|
// Serial.print(F("PeriodCO2:"));Serial.println(periodMinCO2);
|
|
Serial.print(F("PeriodMove:"));Serial.println(periodMotion);
|
|
Serial.print(F("SPLight:"));Serial.println(spLight);
|
|
Serial.print(F("DBLight:"));Serial.println(dbLight);
|
|
Serial.print(F("BLevel:"));Serial.println(levelBlue);
|
|
Serial.print(F("WLevel:"));Serial.println(levelWhite);
|
|
//Serial.print(F("TimeLight:"));Serial.println(timeLight);
|
|
myHTU21D.begin();
|
|
curDelay = -1;
|
|
wLamp = bLamp = false;
|
|
statLamp = 0;
|
|
}
|
|
|
|
void loop() {
|
|
static uint32_t cRun = millis();
|
|
static uint8_t sec = 0;
|
|
//static uint8_t minuts = 0;
|
|
|
|
if(digitalRead(MOTION) > 0){
|
|
if(curDelay == -1) sendDataB(msgMove, true);
|
|
move = true;
|
|
curDelay = periodMotion;
|
|
}
|
|
|
|
uint16_t LightLev = analogRead(A0);
|
|
samples.add(LightLev);
|
|
LightLev = samples.getMedian();
|
|
if ((LightLev < spLight) && move && !bLamp){
|
|
analogWrite(LED_BLUE, (levelBlue) * 2.55);
|
|
statLamp |= 0x01;
|
|
bLamp = true;
|
|
Serial.println(F("BLamp On"));
|
|
}
|
|
else if((LightLev > (spLight + dbLight)) || (move == 0)){
|
|
//analogWrite(LED_BLUE, 0);
|
|
digitalWrite(LED_BLUE, LOW);
|
|
statLamp &= 0xFE;
|
|
bLamp = false;
|
|
}
|
|
|
|
if(lightWhite && !wLamp){
|
|
analogWrite(LED_WHITE, (levelWhite) * 2.55);
|
|
statLamp |= 0x02;
|
|
wLamp = true;
|
|
Serial.println(F("WLamp On"));
|
|
}
|
|
else if (!lightWhite){
|
|
//analogWrite(LED_WHITE, 0);
|
|
digitalWrite(LED_WHITE, LOW);
|
|
statLamp &= 0xFD;
|
|
wLamp = false;
|
|
}
|
|
if(statLamp > 0) digitalWrite(PS, HIGH);
|
|
else digitalWrite(PS, LOW);
|
|
|
|
|
|
if((cRun + 1000) <= millis()){ //Once per second
|
|
cRun = millis();
|
|
if(curDelay > -1){
|
|
Serial.println("ADC: " + String(LightLev) + " MV:" + String(move) + " TMv:" + curDelay + " SL:" + String(statLamp) + " PS:" + digitalRead(PS));
|
|
Serial.println("SPL: " + String(spLight) + " DBL:" + String(dbLight)+ " BL:" + String(bLamp));
|
|
}
|
|
sec++;
|
|
if(curDelay > 0) curDelay--;
|
|
if(curDelay == 0){
|
|
sendDataB(msgMove, false);
|
|
move = false;
|
|
curDelay = -1;
|
|
}
|
|
if((sec + 2) % 30 == 0){
|
|
temp = myHTU21D.readTemperature();
|
|
hum = myHTU21D.readCompensatedHumidity();
|
|
// Serial.println("Temp: " + String(temp));
|
|
// Serial.println("Hum: " + String(hum));
|
|
}
|
|
if(sec == 59){
|
|
//minuts++;
|
|
sec = 0;
|
|
// }
|
|
// if(minuts == periodMinCO2){
|
|
int CO2; // Buffer for CO2
|
|
CO2 = myMHZ19.getCO2(); // Request CO2 (as ppm)
|
|
|
|
if(myMHZ19.errorCode == RESULT_OK) // RESULT_OK is an alis for 1. Either can be used to confirm the response was OK.
|
|
{
|
|
Serial.println(CO2);
|
|
sendDataI(msgCO2, CO2);
|
|
wait(50);
|
|
}
|
|
// else{
|
|
// Serial.println("CO2err:" + String(myMHZ19.errorCode));
|
|
// }
|
|
if(temp != HTU21D_ERROR){
|
|
// Serial.println("Send");
|
|
|
|
// Serial.println("Temp: " + String(temp));
|
|
// Serial.println("Hum: " + String(hum));
|
|
sendDataF(msgTemp, temp);
|
|
wait(50);
|
|
sendDataF(msgHum, hum);
|
|
wait(50);
|
|
}
|
|
sendDataF(msgTimeWork, cRun / 60000.0);
|
|
wait(50);
|
|
sendDataI(msgLight, LightLev);
|
|
//minuts = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
void presentation()
|
|
{
|
|
sendSketchInfo(F("MidRoom"), F("1.0"));
|
|
present(0, S_TEMP, F("Temp"));
|
|
present(1, S_HUM, F("Humid"));
|
|
present(2, S_MOTION, F("Motion"));
|
|
present(3, S_CUSTOM, F("CO2_Light_Time"));
|
|
present(4, S_DIMMER, F("White Light"));
|
|
present(5, S_CUSTOM, F("Blue Light SP_DB"));
|
|
}
|
|
|
|
void receive(const MyMessage &message)
|
|
{
|
|
// if((message.sensor == 3) && (message.type == V_VAR5)){
|
|
// periodMinCO2 = message.getByte();
|
|
// saveState(0, periodMinCO2);
|
|
// }
|
|
if((message.sensor == 3) && (message.type == V_VAR4)){
|
|
periodMotion = message.getULong();
|
|
printDeb("PerMot:" + String(periodMotion));
|
|
saveState(11, periodMotion);
|
|
}
|
|
if((message.sensor == 4) && (message.type == V_DIMMER)){
|
|
levelWhite = message.getULong();
|
|
printDeb("LevelW:" + String(dbLight));
|
|
saveState(7, levelWhite);
|
|
if(wLamp) analogWrite(LED_WHITE, (levelWhite) * 2.55);
|
|
}
|
|
if((message.sensor == 5) && (message.type == V_VAR3)){
|
|
levelBlue = message.getULong();
|
|
printDeb("LevelB:" + String(levelBlue));
|
|
saveState(6, levelBlue);
|
|
if(bLamp) analogWrite(LED_BLUE, (levelBlue) * 2.55);
|
|
}
|
|
if((message.sensor == 4) && (message.type == V_STATUS)){
|
|
lightWhite = message.getBool();
|
|
//saveState(6, lightWhite);
|
|
}
|
|
if((message.sensor == 5) && (message.type == V_VAR1)){
|
|
spLight = message.getULong();
|
|
printDeb("SPL:" + String(spLight));
|
|
saveState(12, (spLight >> 8) & 0xFF);
|
|
saveState(13, spLight & 0xFF);
|
|
|
|
}
|
|
if((message.sensor == 5) && (message.type == V_VAR2)){
|
|
dbLight = message.getULong();
|
|
printDeb("DBL:" + String(dbLight));
|
|
saveState(14, (dbLight >> 8) & 0xFF);
|
|
saveState(15, dbLight & 0xFF);
|
|
}
|
|
}
|
|
|
|
void sendDataI(MyMessage msg, uint32_t val)
|
|
{
|
|
bool send_data = false;
|
|
uint8_t count = 0;
|
|
while(send_data == false){
|
|
count++;
|
|
send_data = send(msg.set(val), true);
|
|
if(!send_data)
|
|
wait(500, C_SET, msg.type);
|
|
if ((count == 3) && (send_data == 0)){ // Если сделано 3 попытки и нет подтверждения отправки
|
|
count = 0; // Обнуляем счётчик
|
|
send_data = 1; // Выходим из цикла
|
|
}
|
|
}
|
|
}
|
|
|
|
void sendDataF(MyMessage msg, float val)
|
|
{
|
|
bool send_data = false;
|
|
uint8_t count = 0;
|
|
while(send_data == false){
|
|
count++;
|
|
send_data = send(msg.set(val, 2), true);
|
|
if(!send_data)
|
|
wait(500, C_SET, msg.type);
|
|
if ((count == 3) && (send_data == 0)){ // Если сделано 3 попытки и нет подтверждения отправки
|
|
count = 0; // Обнуляем счётчик
|
|
send_data = 1; // Выходим из цикла
|
|
}
|
|
}
|
|
}
|
|
|
|
void sendDataB(MyMessage msg, bool val)
|
|
{
|
|
bool send_data = false;
|
|
uint8_t count = 0;
|
|
while(send_data == false){
|
|
count++;
|
|
send_data = send(msg.set(val), true);
|
|
if(!send_data)
|
|
wait(500, C_SET, msg.type);
|
|
if ((count == 3) && (send_data == 0)){ // Если сделано 3 попытки и нет подтверждения отправки
|
|
count = 0; // Обнуляем счётчик
|
|
send_data = 1; // Выходим из цикла
|
|
}
|
|
}
|
|
}
|
|
|
|
void printDeb(String msg)
|
|
{
|
|
#ifdef DEBUG
|
|
Serial.println(msg);
|
|
#endif
|
|
} |