ExtSens Sleep

This commit is contained in:
2020-11-08 15:41:08 +03:00
parent a75143cdd8
commit 23ccecb0b7
2 changed files with 34 additions and 18 deletions

View File

@@ -40,7 +40,7 @@ void setup() {
void loop() { void loop() {
float temp, hum; float temp, hum;
if((cRun + 29999) < millis()){ //if((cRun + 29999) < millis()){
cRun = millis(); cRun = millis();
temp = myAHT10.readTemperature(AHT10_FORCE_READ_DATA); temp = myAHT10.readTemperature(AHT10_FORCE_READ_DATA);
hum = myAHT10.readHumidity(AHT10_USE_READ_DATA); hum = myAHT10.readHumidity(AHT10_USE_READ_DATA);
@@ -56,7 +56,8 @@ void loop() {
batteryPcnt = (v * 100) / 4.2; batteryPcnt = (v * 100) / 4.2;
sendBatteryLevel(batteryPcnt); sendBatteryLevel(batteryPcnt);
send(msgVolts.set(v, 2)); send(msgVolts.set(v, 2));
} //}
sleep(60000 - (millis() - cRun));
} }
void presentation() void presentation()

View File

@@ -120,6 +120,8 @@ void setup() {
void loop() { void loop() {
float tempT(NAN), humT(NAN), pressT(NAN); float tempT(NAN), humT(NAN), pressT(NAN);
static int gint = 1;
static bool gdir = true;
ArduinoOTA.handle(); ArduinoOTA.handle();
@@ -131,16 +133,23 @@ void loop() {
} }
if((crun + 199) < millis()){ if((crun + 199) < millis()){
crun = millis(); crun = millis();
minCount++;
if(gdir) gint += 25;
else gint -= 25;
if((gint > 499) || (gint < 2)) gdir = !gdir;
analogWrite(GREEN, gint);
adc = analogRead(A0); adc = analogRead(A0);
mv = digitalRead(MOVE_S); mv = digitalRead(MOVE_S);
if(mv != prMV){ if(mv != prMV){
if(mv){ if(mv){
//mv = true; mv = true;
lcd.setBacklight(255); lcd.setBacklight(255);
mqttClient.publish("/home/kuh/move", 1, false, "1"); mqttClient.publish("/home/kuh/move", 1, false, "1");
} }
else{ else{
//mv = false; mv = false;
lcd.setBacklight(0); lcd.setBacklight(0);
mqttClient.publish("/home/kuh/move", 1, false, "0"); mqttClient.publish("/home/kuh/move", 1, false, "0");
} }
@@ -155,22 +164,21 @@ void loop() {
lightOn = true; lightOn = true;
} }
//if(((minCount+5) % 10) == 0){ //Once in Second if(((minCount+5) % 10) == 0){ //Once in 2 Second
bme.read(pressT, tempT, humT, tempUnit, presUnit); bme.read(pressT, tempT, humT, tempUnit, presUnit);
if(!isnan(pressT)){ if(!isnan(pressT)){
if(!isnan(press)) press += (pressT / 133.322f - press) / 600.0f; if(!isnan(press)) press += (pressT / 133.322f - press) / 30.0f;
else press = pressT / 133.322f; else press = pressT / 133.322f;
} }
if(!isnan(tempT)){ if(!isnan(tempT)){
if(!isnan(tempIn)) tempIn += (tempT - tempIn) / 300.0f; if(!isnan(tempIn)) tempIn += (tempT - tempIn) / 30.0f;
else tempIn = tempT; else tempIn = tempT;
} }
if(!isnan(humT)){ if(!isnan(humT)){
if(!isnan(hum)) hum += (humT - hum) / 300.0f; if(!isnan(hum)) hum += (humT - hum) / 30.0f;
else hum = humT; else hum = humT;
} }
//} }
minCount++;
if (minCount == 300){ //Once in Minute if (minCount == 300){ //Once in Minute
lcd.begin(16, 2); lcd.begin(16, 2);
publishMin(); publishMin();
@@ -178,7 +186,8 @@ void loop() {
} }
if((minCount % 5) == 0){ //Once in Second if((minCount % 5) == 0){ //Once in Second
getTemp(); getTemp();
publishSec(); //digitalWrite(GREEN, !digitalRead(GREEN));
//publishSec();
showLCD(LCDpage, mv); showLCD(LCDpage, mv);
} }
} }
@@ -221,7 +230,7 @@ void showLCD(int page, bool l_on)
void publishSec() void publishSec()
{ {
digitalWrite(GREEN, HIGH); /* digitalWrite(GREEN, HIGH);
dtostrf(tempOut, 6, 1, strFVal); dtostrf(tempOut, 6, 1, strFVal);
mqttClient.publish("/hometest/kuh1s/temp_out", 1, false, strFVal); mqttClient.publish("/hometest/kuh1s/temp_out", 1, false, strFVal);
dtostrf(tempIn, 6, 1, strFVal); dtostrf(tempIn, 6, 1, strFVal);
@@ -240,8 +249,7 @@ void publishSec()
mqttClient.publish("/hometest/kuh1s/moroz", 1, false, strFVal); mqttClient.publish("/hometest/kuh1s/moroz", 1, false, strFVal);
itoa(crun, strFVal, 10); itoa(crun, strFVal, 10);
mqttClient.publish("/hometest/kuh1s/millis", 1, false, strFVal); mqttClient.publish("/hometest/kuh1s/millis", 1, false, strFVal);
digitalWrite(GREEN, LOW); digitalWrite(GREEN, LOW);*/
//}
} }
void publishMin() void publishMin()
@@ -263,10 +271,10 @@ void publishMin()
dtostrf(press, 6, 1, strFVal); dtostrf(press, 6, 1, strFVal);
mqttClient.publish("/home/kuh/pressure", 1, false, strFVal); mqttClient.publish("/home/kuh/pressure", 1, false, strFVal);
} }
itoa(lightSP, strFVal, 10); //itoa(lightSP, strFVal, 10);
mqttClient.publish("/home/kuh/light_sp", 1, false, strFVal); //mqttClient.publish("/home/kuh/light_sp", 1, false, strFVal);
itoa(lightDB, strFVal, 10); //itoa(lightDB, strFVal, 10);
mqttClient.publish("/home/kuh/light_db", 1, false, strFVal); //mqttClient.publish("/home/kuh/light_db", 1, false, strFVal);
itoa(adc, strFVal, 10); itoa(adc, strFVal, 10);
mqttClient.publish("/home/kuh/light_cur", 1, false, strFVal); mqttClient.publish("/home/kuh/light_cur", 1, false, strFVal);
if(!isnan(tempHol)){ if(!isnan(tempHol)){
@@ -346,8 +354,10 @@ void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
void onMqttConnect(bool sessionPresent) { void onMqttConnect(bool sessionPresent) {
char v[6]; char v[6];
itoa(lightData.SP, v, 10); itoa(lightData.SP, v, 10);
mqttClient.publish("/home/kuh/light_sp", 1, false, v);
mqttClient.publish("/home/kuh/light_sp_set", 1, false, v); mqttClient.publish("/home/kuh/light_sp_set", 1, false, v);
itoa(lightData.DB, v, 10); itoa(lightData.DB, v, 10);
mqttClient.publish("/home/kuh/light_db", 1, false, v);
mqttClient.publish("/home/kuh/light_db_set", 1, false, v); mqttClient.publish("/home/kuh/light_db_set", 1, false, v);
mqttClient.publish("/home/kuh/move", 1, false, mv == 1 ? "1" : "0"); mqttClient.publish("/home/kuh/move", 1, false, mv == 1 ? "1" : "0");
mqttClient.subscribe("/home/kuh/light_sp_set", 1); mqttClient.subscribe("/home/kuh/light_sp_set", 1);
@@ -364,13 +374,18 @@ void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) { void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
bool w = false; bool w = false;
char v[6];
if(strcmp(topic, "/home/kuh/light_sp_set") == 0){ if(strcmp(topic, "/home/kuh/light_sp_set") == 0){
lightSP = atoi(payload); lightSP = atoi(payload);
w = true; w = true;
itoa(lightData.SP, v, 10);
mqttClient.publish("/home/kuh/light_sp", 1, false, v);
} }
if(strcmp(topic, "/home/kuh/light_db_set") == 0){ if(strcmp(topic, "/home/kuh/light_db_set") == 0){
lightDB = atoi(payload); lightDB = atoi(payload);
w = true; w = true;
itoa(lightData.DB, v, 10);
mqttClient.publish("/home/kuh/light_db", 1, false, v);
} }
if(w){ if(w){
lightData.SP = lightSP; lightData.SP = lightSP;