MidroomNew Light Sensor
This commit is contained in:
@@ -9,8 +9,9 @@ private:
|
||||
bool inv, state;
|
||||
int onMS;
|
||||
unsigned long curMS;
|
||||
byte bright;
|
||||
public:
|
||||
leds(int ledPin, int onms = 300, bool inverse = false);
|
||||
leds(int ledPin, int onms = 300, byte bright = 255, bool inverse = false);
|
||||
//~leds();
|
||||
void start();
|
||||
void tick();
|
||||
|
||||
@@ -5,15 +5,24 @@
|
||||
|
||||
#include <Adafruit_BME280.h>
|
||||
#include <MHZ19.h>
|
||||
#include <RunningMedian.h>
|
||||
//#include "RemoteDebug.h" //https://github.com/JoaoLopesF/RemoteDebug
|
||||
|
||||
//#define POWER_SENS (7)
|
||||
#define R_LED (13) //D7
|
||||
#define G_LED (12) //D6
|
||||
#define B_LED (0) //D3
|
||||
#define MOV_SENS (16) //D0
|
||||
#define P_SENS (14) //D5
|
||||
#define WF_LED 2
|
||||
#define HOST_NAME "MidRoom"
|
||||
#define TOPIC "/home/midroom"
|
||||
|
||||
Adafruit_BME280 bme;
|
||||
MHZ19 mhz19;
|
||||
RunningMedian samples = RunningMedian(10 * sizeof(int));
|
||||
|
||||
unsigned long crun;
|
||||
unsigned long crun, cRunADC;
|
||||
uint32_t co2;
|
||||
bool firstRun;
|
||||
float p, t, h;
|
||||
//uint16_t cntSec;
|
||||
int light, lightPrev;
|
||||
|
||||
@@ -10,12 +10,16 @@
|
||||
|
||||
[env:nodemcuv2]
|
||||
platform = espressif8266
|
||||
board = esp12e
|
||||
;board = esp12e
|
||||
board = d1
|
||||
framework = arduino
|
||||
board_build.f_cpu = 26000000L
|
||||
board_build.ldscript = eagle.flash.1m.ld
|
||||
;board_build.f_cpu = 26000000L
|
||||
;board_build.ldscript = eagle.flash.1m.ld
|
||||
;board_build.flash_mode = dout
|
||||
upload_protocol = espota
|
||||
upload_port = 192.168.1.132
|
||||
upload_port = 192.168.1.114
|
||||
lib_deps =
|
||||
jwrw/ESP_EEPROM @ ^2.0.0
|
||||
jwrw/ESP_EEPROM @ ^2.0.0
|
||||
robtillaart/RunningMedian @ ^0.3.3
|
||||
;joaolopesf/RemoteDebug @ 3.0.5
|
||||
|
||||
@@ -1,21 +1,27 @@
|
||||
#include <leds.h>
|
||||
|
||||
leds::leds(int pinLED, int onms, bool inverse)
|
||||
leds::leds(int pinLED, int onms, byte bright, bool inverse)
|
||||
{
|
||||
ledPin = pinLED;
|
||||
inv = inverse;
|
||||
onMS = onms;
|
||||
state = false;
|
||||
this->bright = bright;
|
||||
// if(inv)
|
||||
// analogWrite(ledPin, bright);
|
||||
// else
|
||||
// analogWrite(ledPin, 0);
|
||||
}
|
||||
|
||||
void leds::start()
|
||||
{
|
||||
curMS = millis();
|
||||
state = true;
|
||||
if(!inv)
|
||||
analogWrite(ledPin, 100);
|
||||
if(!inv) digitalWrite(ledPin, 1);
|
||||
//analogWrite(ledPin, bright);
|
||||
else
|
||||
analogWrite(ledPin, 0);
|
||||
//analogWrite(ledPin, 0);
|
||||
digitalWrite(ledPin, 0);
|
||||
//digitalWrite(ledPin, !inv);
|
||||
}
|
||||
|
||||
@@ -23,10 +29,15 @@ void leds::tick()
|
||||
{
|
||||
if(state && ((curMS + onMS) < millis())){
|
||||
state = false;
|
||||
if(inv)
|
||||
analogWrite(ledPin, 1);
|
||||
// if(inv)
|
||||
// analogWrite(ledPin, bright);
|
||||
// else
|
||||
// analogWrite(ledPin, 0);
|
||||
// //digitalWrite(ledPin, inv);
|
||||
if(inv) digitalWrite(ledPin, 1);
|
||||
//analogWrite(ledPin, bright);
|
||||
else
|
||||
analogWrite(ledPin, 0);
|
||||
//digitalWrite(ledPin, inv);
|
||||
//analogWrite(ledPin, 0);
|
||||
digitalWrite(ledPin, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,12 +5,6 @@
|
||||
#include <leds.h>
|
||||
#include <ESP_EEPROM.h>
|
||||
|
||||
#define R_LED (13)
|
||||
#define G_LED (12)
|
||||
#define B_LED (0)
|
||||
#define MOV_SENS (16)
|
||||
#define P_SENS (14)
|
||||
|
||||
bool meas = false;
|
||||
short minCnt;
|
||||
bool send_move = false;
|
||||
@@ -43,7 +37,8 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties
|
||||
void onMqttUnsubscribe(uint16_t packetId);
|
||||
void onMqttPublish(uint16_t packetId); */
|
||||
|
||||
leds g_led(G_LED, 100);
|
||||
leds g_led(G_LED, 300, 100, true);
|
||||
leds b_led(B_LED, 300, 100, true);
|
||||
|
||||
//leds r_led(R_LED, 100);
|
||||
|
||||
@@ -88,7 +83,7 @@ void setup() {
|
||||
mqttClient.onPublish(onMqttPublish); */
|
||||
mqttClient.onMessage(onMqttMessage);
|
||||
mqttClient.setServer(mqtt_server, 1883);
|
||||
mqttClient.setClientId("MidRoom");
|
||||
mqttClient.setClientId("MidRoomT");
|
||||
|
||||
pinMode(MOV_SENS, INPUT_PULLUP);
|
||||
pinMode(P_SENS, OUTPUT);
|
||||
@@ -96,15 +91,17 @@ void setup() {
|
||||
pinMode(G_LED, OUTPUT);
|
||||
pinMode(B_LED, OUTPUT);
|
||||
//analogWriteRange(100);
|
||||
digitalWrite(P_SENS, HIGH);
|
||||
digitalWrite(P_SENS, LOW);
|
||||
//digitalWrite(WF_LED, HIGH);
|
||||
digitalWrite(R_LED, LOW);
|
||||
digitalWrite(G_LED, HIGH);
|
||||
digitalWrite(B_LED, HIGH);
|
||||
digitalWrite(R_LED, HIGH);
|
||||
//Serial1.println(F("Begin MH-Z19"));
|
||||
for(int i = 0; i < 5; i++){
|
||||
Serial1.print('.');
|
||||
//Serial1.print('.');
|
||||
delay(1000);
|
||||
}
|
||||
Serial1.println();
|
||||
//Serial1.println();
|
||||
mhz19.begin(Serial);
|
||||
Wire.begin();
|
||||
bool status = bme.begin(BME280_ADDRESS_ALTERNATE);
|
||||
@@ -132,6 +129,7 @@ void setup() {
|
||||
connectToWifi();
|
||||
|
||||
cRun = millis();
|
||||
cRunADC = millis();
|
||||
//Serial1.println(F("Start Loop"));
|
||||
//(R_LED, HIGH);
|
||||
}
|
||||
@@ -140,38 +138,76 @@ void loop() {
|
||||
float pt, tt, ht;
|
||||
char v[11];
|
||||
static int stDelay = 0;
|
||||
|
||||
ArduinoOTA.handle();
|
||||
g_led.tick();
|
||||
b_led.tick();
|
||||
//r_led.tick();
|
||||
if(digitalRead(MOV_SENS) > 0){
|
||||
if(mvDelay == -1) mqttClient.publish("/home/midroom/move", 1, false, "1");
|
||||
if(mvDelay == -1){
|
||||
g_led.start();
|
||||
mqttClient.publish(TOPIC"/move", 1, false, "1");
|
||||
}
|
||||
mvDelay = mvDelaySet;
|
||||
}
|
||||
|
||||
if((cRunADC + 99) < millis()){
|
||||
cRunADC = millis();
|
||||
adc = analogRead(A0);
|
||||
samples.add(adc);
|
||||
adc = samples.getMedian();
|
||||
if(abs(adc - lastADC) > 10){
|
||||
lastADC = adc;
|
||||
itoa(adc, v, 10);
|
||||
g_led.start();
|
||||
mqttClient.publish(TOPIC"/light", 1, false, v);
|
||||
}
|
||||
}
|
||||
/*if (digitalRead(MOV_SENS) != old_mov){
|
||||
old_mov = digitalRead(MOV_SENS);
|
||||
//Serial1.println(F("Change mov detected"));
|
||||
mqttClient.publish("/home/midroom/move", 1, false, old_mov ? "1" : "0");
|
||||
mqttClient.publish(TOPIC"/move", 1, false, old_mov ? "1" : "0");
|
||||
}*/
|
||||
|
||||
if(cRun + 999 < millis()){
|
||||
if((cRun + 999) < millis()){
|
||||
cRun = millis();
|
||||
// Serial.println("ADC:"+String(adc));
|
||||
// itoa(mvDelay, v, 10);
|
||||
// mqttClient.publish(TOPIC"/movesec", 0, false, v);
|
||||
// itoa(mvDelaySet, v, 10);
|
||||
// mqttClient.publish(TOPIC"/movesecset", 0, false, v);
|
||||
if(mvDelay == 0) {
|
||||
mqttClient.publish("/home/midroom/move", 1, false, "0");
|
||||
g_led.start();
|
||||
mqttClient.publish(TOPIC"/move", 1, false, "0");
|
||||
mvDelay = -1;
|
||||
}
|
||||
if(mvDelay > 0) mvDelay--;
|
||||
|
||||
adc = analogRead(A0);
|
||||
if(abs(adc - lastADC) > 50){
|
||||
lastADC = adc;
|
||||
itoa(adc, v, 10);
|
||||
mqttClient.publish("/home/midroom/light", 1, false, v);
|
||||
if(minCnt % 10 == 0){
|
||||
co2 = mhz19.getCO2();
|
||||
// /* Serial1.print("CO2: ");
|
||||
// Serial1.println(co2); */
|
||||
// // Serial.println("Measure BME");
|
||||
// digitalWrite(P_SENS, HIGH);
|
||||
// bme.begin(BME280_ADDRESS_ALTERNATE);
|
||||
// delay(10);
|
||||
// pt = bme.readPressure();
|
||||
// tt = bme.readTemperature();
|
||||
// ht = bme.readHumidity();
|
||||
// if(!isnan(pt))
|
||||
// p = pt;//+= (pt - p) / 60.0f ;
|
||||
// if(!isnan(tt))
|
||||
// t = tt;//+= (tt - t) / 30.0f ;
|
||||
// if(!isnan(ht))
|
||||
// h = ht;//+= (ht - h) / 30.0f ;
|
||||
// // Serial.println(p);
|
||||
// // Serial.println(t);
|
||||
// // Serial.println(h);
|
||||
// digitalWrite(P_SENS, LOW);
|
||||
}
|
||||
if(minCnt % 20 == 0){
|
||||
co2 = mhz19.getCO2();
|
||||
/* Serial1.print("CO2: ");
|
||||
Serial1.println(co2); */
|
||||
if(++minCnt == 60){
|
||||
minCnt = 0;
|
||||
|
||||
digitalWrite(P_SENS, HIGH);
|
||||
bme.begin(BME280_ADDRESS_ALTERNATE);
|
||||
delay(10);
|
||||
@@ -184,19 +220,22 @@ void loop() {
|
||||
t = tt;//+= (tt - t) / 30.0f ;
|
||||
if(!isnan(ht))
|
||||
h = ht;//+= (ht - h) / 30.0f ;
|
||||
// Serial.println(p);
|
||||
// Serial.println(t);
|
||||
// Serial.println(h);
|
||||
digitalWrite(P_SENS, LOW);
|
||||
}
|
||||
if(++minCnt == 60){
|
||||
minCnt = 0;
|
||||
|
||||
if(mqttClient.connected()){
|
||||
g_led.start();
|
||||
//r_led.start();
|
||||
//Serial1.println("Begin Publish");
|
||||
itoa(adc, v, 10);
|
||||
mqttClient.publish(TOPIC"/light", 1, false, v);
|
||||
dtostrf(t, 5, 1,v);
|
||||
mqttClient.publish("/home/midroom/temp", 1, false, v);
|
||||
mqttClient.publish(TOPIC"/temp", 1, false, v);
|
||||
//Serial1.println("Publish1");
|
||||
dtostrf(h, 5, 1,v);
|
||||
mqttClient.publish("/home/midroom/humid", 1, false, v);
|
||||
mqttClient.publish(TOPIC"/humid", 1, false, v);
|
||||
//Serial1.println("Publish2");
|
||||
if(firstRun){
|
||||
stDelay++;
|
||||
@@ -204,48 +243,45 @@ void loop() {
|
||||
}
|
||||
if(!firstRun && (mhz19.errorCode == RESULT_OK)){
|
||||
itoa(co2, v, 10);
|
||||
mqttClient.publish("/home/midroom/co2", 1, false, v);
|
||||
mqttClient.publish(TOPIC"/co2", 1, false, v);
|
||||
//Serial1.println("Publish3");
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(minCnt % 10 == 0){
|
||||
ultoa(millis(), v, 10);
|
||||
else if(minCnt % 20 == 0){
|
||||
dtostrf(millis() / 60000.0, 7, 2, v);
|
||||
//ultoa(millis(), v, 10);
|
||||
if(mqttClient.connected()){
|
||||
mqttClient.publish("/home/midroom/millis", 0, false, v);
|
||||
itoa(mvDelay, v, 10);
|
||||
mqttClient.publish("/home/midroom/movesec", 0, false, v);
|
||||
itoa(mvDelaySet, v, 10);
|
||||
mqttClient.publish("/home/midroom/movesecset", 0, false, v);
|
||||
g_led.start();
|
||||
mqttClient.publish(TOPIC"/millis", 0, false, v);
|
||||
// dtostrf(t, 5, 1,v);
|
||||
// mqttClient.publish("/home/midroom/temp", 0, false, v);
|
||||
// mqttClient.publish(TOPIC"/temp", 0, false, v);
|
||||
// //Serial1.println("Publish1");
|
||||
// dtostrf(h, 5, 1,v);
|
||||
// mqttClient.publish("/home/midroom/humid", 0, false, v);
|
||||
g_led.start();
|
||||
// mqttClient.publish(TOPIC"/humid", 0, false, v);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void connectToWifi() {
|
||||
// Serial1.println(F("Connecting to Wi-Fi..."));
|
||||
//Serial.println(F("Connecting to Wi-Fi..."));
|
||||
//Serial1.flush();
|
||||
WiFi.begin(ssid, password);
|
||||
}
|
||||
|
||||
void connectToMqtt() {
|
||||
// Serial1.println(F("Connecting to MQTT..."));
|
||||
//Serial.println(F("Connecting to MQTT..."));
|
||||
//Serial1.flush();
|
||||
mqttClient.connect();
|
||||
}
|
||||
|
||||
void onWifiConnect(const WiFiEventStationModeGotIP& event) {
|
||||
/* Serial1.println(F("Connected to Wi-Fi."));
|
||||
Serial1.print(F("IP: "));
|
||||
//Serial.println(F("Connected to Wi-Fi."));
|
||||
//Serial.print(F("IP: "));
|
||||
//Serial1.flush();
|
||||
Serial1.println(WiFi.localIP()); */
|
||||
digitalWrite(R_LED, LOW);
|
||||
//Serial.println(WiFi.localIP());
|
||||
//digitalWrite(WF_LED, LOW);
|
||||
connectToMqtt();
|
||||
}
|
||||
|
||||
@@ -254,20 +290,21 @@ void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
|
||||
//Serial1.flush();
|
||||
mqttReconnectTimer.detach(); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
|
||||
wifiReconnectTimer.once(2, connectToWifi);
|
||||
digitalWrite(R_LED, HIGH);
|
||||
//digitalWrite(WF_LED, HIGH);
|
||||
}
|
||||
|
||||
void onMqttConnect(bool sessionPresent) {
|
||||
char v[15];
|
||||
sprintf(v, "%u", mvDelaySet);
|
||||
//itoa(mvDelaySet, v, 10);
|
||||
mqttClient.publish("/home/midroom/mvdelay", 1, false, v);
|
||||
mqttClient.subscribe("/home/midroom/mvdelay", 1);
|
||||
mqttClient.publish(TOPIC"/mvdelay", 1, false, v);
|
||||
mqttClient.subscribe(TOPIC"/mvdelay", 1);
|
||||
// ultoa(ESP.getFlashChipSize(), v, 10);
|
||||
// mqttClient.publish("/home/midroom/chipsize", 1, false, v);
|
||||
// mqttClient.publish(TOPIC"/chipsize", 1, false, v);
|
||||
// ultoa(ESP.getFlashChipRealSize(), v, 10);
|
||||
// mqttClient.publish("/home/midroom/realchipsize", 1, false, v);
|
||||
digitalWrite(B_LED, LOW);
|
||||
// mqttClient.publish(TOPIC"/realchipsize", 1, false, v);
|
||||
//Serial.println("Connected to MQTT");
|
||||
digitalWrite(R_LED, HIGH);
|
||||
}
|
||||
|
||||
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
|
||||
@@ -277,7 +314,7 @@ void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
|
||||
if (WiFi.isConnected()) {
|
||||
mqttReconnectTimer.once(2, connectToMqtt);
|
||||
}
|
||||
digitalWrite(B_LED, HIGH);
|
||||
digitalWrite(R_LED, LOW);
|
||||
}
|
||||
|
||||
/*void onMqttSubscribe(uint16_t packetId, uint8_t qos) {
|
||||
@@ -295,11 +332,12 @@ void onMqttUnsubscribe(uint16_t packetId) {
|
||||
}*/
|
||||
|
||||
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
|
||||
if(strcmp(topic, "/home/midroom/mvdelay") == 0){
|
||||
if(strcmp(topic, TOPIC"/mvdelay") == 0){
|
||||
mvDelaySet = atoi(payload);
|
||||
EEPROM.put(4, mvDelaySet);
|
||||
EEPROM.commit();
|
||||
}
|
||||
b_led.start();
|
||||
}
|
||||
/*
|
||||
void onMqttPublish(uint16_t packetId) {
|
||||
|
||||
@@ -18,12 +18,13 @@
|
||||
|
||||
#include <MySensors.h>
|
||||
|
||||
#define TOP_LOCK A0
|
||||
#define INT_LOCK A1
|
||||
#define DOWN_LOCK A3
|
||||
#define SMALL_LOCK A2
|
||||
#define DOOR A4
|
||||
|
||||
#define TOP_LOCK A0 // 7 - Green/White
|
||||
#define INT_LOCK A1 // 2 - Green
|
||||
#define DOWN_LOCK A3 // 6 - Blue/White
|
||||
#define SMALL_LOCK A2 // 1 - Blue
|
||||
#define DOOR A4 // 3 - Brown
|
||||
// 4 - 5V - Orange
|
||||
// 8 - GND - Orange/White Brown/White
|
||||
#define TOP_LOCK_ID 0
|
||||
#define INT_LOCK_ID 1
|
||||
#define DOWN_LOCK_ID 2
|
||||
|
||||
Reference in New Issue
Block a user