Added Move Sensors & Light Sensor
This commit is contained in:
@@ -16,3 +16,7 @@ board_build.ldscript = eagle.flash.1m.ld
|
|||||||
board_build.f_cpu = 26000000L
|
board_build.f_cpu = 26000000L
|
||||||
upload_protocol = espota
|
upload_protocol = espota
|
||||||
upload_port = 192.168.1.7
|
upload_port = 192.168.1.7
|
||||||
|
lib_deps =
|
||||||
|
Adafruit HTU21DF Library @ ^1.1.0
|
||||||
|
PubSubClient @ ^2.8
|
||||||
|
; ESP_EEPROM @ ^2.0.0
|
||||||
@@ -7,6 +7,7 @@
|
|||||||
#include <Adafruit_HTU21DF.h>
|
#include <Adafruit_HTU21DF.h>
|
||||||
#include <PubSubClient.h>
|
#include <PubSubClient.h>
|
||||||
#include <leds.h>
|
#include <leds.h>
|
||||||
|
#include <EEPROM.h>
|
||||||
|
|
||||||
#define LED_BLUE D5 //GPIO14
|
#define LED_BLUE D5 //GPIO14
|
||||||
#define LED_GREEN D6 //GPIO12
|
#define LED_GREEN D6 //GPIO12
|
||||||
@@ -31,11 +32,12 @@ unsigned long cRun;
|
|||||||
int minCnt = 0;
|
int minCnt = 0;
|
||||||
int measCnt = 1;
|
int measCnt = 1;
|
||||||
uint8_t mv, oldmv;
|
uint8_t mv, oldmv;
|
||||||
uint16_t adc, oldadc, delta;
|
int adc, oldadc, delta;
|
||||||
|
|
||||||
void reconnect();
|
void reconnect();
|
||||||
void publishMin();
|
void publishMin();
|
||||||
void publishSec();
|
void publishSec();
|
||||||
|
void callback(char* topic, byte* payload, unsigned int length);
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
@@ -83,10 +85,14 @@ void setup() {
|
|||||||
rel_hum = htu.readHumidity();
|
rel_hum = htu.readHumidity();
|
||||||
//digitalWrite(PWR_SENS, LOW);
|
//digitalWrite(PWR_SENS, LOW);
|
||||||
client.setServer(mqtt_server, 1883);
|
client.setServer(mqtt_server, 1883);
|
||||||
|
client.setCallback(callback);
|
||||||
|
EEPROM.begin(8);
|
||||||
|
EEPROM.get(0, delta);
|
||||||
cRun = millis();
|
cRun = millis();
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
static bool lsSent = false, lbSent = false;
|
||||||
ArduinoOTA.handle();
|
ArduinoOTA.handle();
|
||||||
lGreen.tick();
|
lGreen.tick();
|
||||||
lRed.tick();
|
lRed.tick();
|
||||||
@@ -103,10 +109,16 @@ void loop() {
|
|||||||
mv = digitalRead(MOVE_SENS);
|
mv = digitalRead(MOVE_SENS);
|
||||||
if(mv != oldmv){
|
if(mv != oldmv){
|
||||||
oldmv = mv;
|
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){
|
//if(abs(adc - oldadc) > delta){
|
||||||
//publish
|
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){
|
if(measCnt == 0){
|
||||||
temp += (htu.readTemperature() - temp) / 6.0f;
|
temp += (htu.readTemperature() - temp) / 6.0f;
|
||||||
@@ -136,7 +148,10 @@ void reconnect() {
|
|||||||
//Serial.println("connected"); // "подключен"
|
//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 {
|
} else {
|
||||||
//Serial.print("failed, rc="); // "подключение не удалось"
|
//Serial.print("failed, rc="); // "подключение не удалось"
|
||||||
//Serial.print(client.state());
|
//Serial.print(client.state());
|
||||||
@@ -160,10 +175,10 @@ void publishMin()
|
|||||||
dtostrf(rel_hum, 6, 1, strFVal);
|
dtostrf(rel_hum, 6, 1, strFVal);
|
||||||
client.publish("/home/smallroom/rel_hum", strFVal);
|
client.publish("/home/smallroom/rel_hum", strFVal);
|
||||||
}
|
}
|
||||||
itoa(adc, strFVal, 10);
|
ultoa(adc, strFVal, 10);
|
||||||
client.publish("/home/smallroom/light", strFVal);
|
client.publish("/home/smallroom/light", strFVal);
|
||||||
// itoa(cRun, strFVal, 10);
|
ultoa(cRun, strFVal, 10);
|
||||||
// client.publish("/home/smallroom/millis", strFVal);
|
client.publish("/home/smallroom/millis", strFVal);
|
||||||
//digitalWrite(LED_BLUE, LOW);
|
//digitalWrite(LED_BLUE, LOW);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -184,10 +199,20 @@ void publishSec()
|
|||||||
}
|
}
|
||||||
itoa(WiFi.RSSI(), strFVal, 10);
|
itoa(WiFi.RSSI(), strFVal, 10);
|
||||||
client.publish("/hometest/smallroom/RSSI", strFVal);
|
client.publish("/hometest/smallroom/RSSI", strFVal);
|
||||||
itoa(cRun, strFVal, 10);
|
ultoa(cRun, strFVal, 10);
|
||||||
client.publish("/hometest/smallroom/millis", strFVal);
|
client.publish("/hometest/smallroom/millis", strFVal);
|
||||||
itoa(adc, strFVal, 10);
|
ltoa(adc, strFVal, 10);
|
||||||
client.publish("/home/smallroom/light", strFVal);
|
client.publish("/hometest/smallroom/light", strFVal);
|
||||||
|
ultoa(delta, strFVal, 10);
|
||||||
|
client.publish("/hometest/smallroom/ldelta", strFVal);
|
||||||
//digitalWrite(LED_GREEN, LOW);
|
//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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user