Added Move Sensors & Light Sensor

This commit is contained in:
2020-11-08 20:53:33 +03:00
parent 65d54b609e
commit 3314626daa
2 changed files with 40 additions and 11 deletions

View File

@@ -7,6 +7,7 @@
#include <Adafruit_HTU21DF.h>
#include <PubSubClient.h>
#include <leds.h>
#include <EEPROM.h>
#define LED_BLUE D5 //GPIO14
#define LED_GREEN D6 //GPIO12
@@ -31,11 +32,12 @@ unsigned long cRun;
int minCnt = 0;
int measCnt = 1;
uint8_t mv, oldmv;
uint16_t adc, oldadc, delta;
int adc, oldadc, delta;
void reconnect();
void publishMin();
void publishSec();
void callback(char* topic, byte* payload, unsigned int length);
void setup() {
Serial.begin(9600);
@@ -83,10 +85,14 @@ void setup() {
rel_hum = htu.readHumidity();
//digitalWrite(PWR_SENS, LOW);
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
EEPROM.begin(8);
EEPROM.get(0, delta);
cRun = millis();
}
void loop() {
static bool lsSent = false, lbSent = false;
ArduinoOTA.handle();
lGreen.tick();
lRed.tick();
@@ -103,10 +109,16 @@ void loop() {
mv = digitalRead(MOVE_SENS);
if(mv != oldmv){
oldmv = mv;
client.publish("/home/smallroom/light", mv == 0 ? "0" : "1");
client.publish("/home/smallroom/move", mv == 0 ? "0" : "1");
};
if(abs(adc - oldadc) > delta){
//publish
//if(abs(adc - oldadc) > delta){
if(((adc < delta) && !lsSent) || ((adc >= (delta + 5)) && !lbSent)){
char strFVal[6];
itoa(adc, strFVal, 10);
client.publish("/home/smallroom/light", strFVal);
oldadc = adc;
if(adc < delta) {lsSent = true; lbSent = false;}
else {lbSent = true; lsSent = false;}
}
if(measCnt == 0){
temp += (htu.readTemperature() - temp) / 6.0f;
@@ -136,7 +148,10 @@ void reconnect() {
//Serial.println("connected"); // "подключен"
// подписываемся или переподписываемся на топик;
// можно подписаться не только на один, а на несколько топиков
client.subscribe("home/smallroom/ldelta");
char v[6];
ltoa(delta, v, 10);
client.publish("/home/smallroom/ldelta", v);
client.subscribe("/home/smallroom/ldelta");
} else {
//Serial.print("failed, rc="); // "подключение не удалось"
//Serial.print(client.state());
@@ -160,10 +175,10 @@ void publishMin()
dtostrf(rel_hum, 6, 1, strFVal);
client.publish("/home/smallroom/rel_hum", strFVal);
}
itoa(adc, strFVal, 10);
ultoa(adc, strFVal, 10);
client.publish("/home/smallroom/light", strFVal);
// itoa(cRun, strFVal, 10);
// client.publish("/home/smallroom/millis", strFVal);
ultoa(cRun, strFVal, 10);
client.publish("/home/smallroom/millis", strFVal);
//digitalWrite(LED_BLUE, LOW);
}
}
@@ -184,10 +199,20 @@ void publishSec()
}
itoa(WiFi.RSSI(), strFVal, 10);
client.publish("/hometest/smallroom/RSSI", strFVal);
itoa(cRun, strFVal, 10);
ultoa(cRun, strFVal, 10);
client.publish("/hometest/smallroom/millis", strFVal);
itoa(adc, strFVal, 10);
client.publish("/home/smallroom/light", strFVal);
ltoa(adc, strFVal, 10);
client.publish("/hometest/smallroom/light", strFVal);
ultoa(delta, strFVal, 10);
client.publish("/hometest/smallroom/ldelta", strFVal);
//digitalWrite(LED_GREEN, LOW);
}
}
void callback(char* topic, byte* payload, unsigned int length) {
if(strcmp(topic,"/home/smallroom/ldelta") == 0){
delta = atoi((char*)payload);
EEPROM.put(0, delta);
EEPROM.commit();
}
}