Add reciever power

This commit is contained in:
2022-10-15 12:19:44 +03:00
parent 0aab4963c4
commit 5a1b30658d
6 changed files with 76 additions and 7 deletions

View File

@@ -38,6 +38,7 @@ extern "C" {
#define LED_R 13 #define LED_R 13
#define LED_G 15 #define LED_G 15
#define LED_B 2 #define LED_B 2
#define RECIVER 18
#define TOPIC "home/bigroom/" #define TOPIC "home/bigroom/"
@@ -48,9 +49,10 @@ uint8_t timeLight;
byte levelBlue, levelWhite; byte levelBlue, levelWhite;
bool move, lightWhite; bool move, lightWhite;
float temp, hum; float temp, hum;
bool bLamp, wLamp; bool bLamp, wLamp, reciever;
uint8_t statLamp; uint8_t statLamp;
AsyncWebServer server(80); AsyncWebServer server(80);
#endif // __MAIN__ #endif // __MAIN__

View File

@@ -77,6 +77,8 @@ void setup() {
pinMode(LED_G, OUTPUT); pinMode(LED_G, OUTPUT);
pinMode(LED_B, OUTPUT); pinMode(LED_B, OUTPUT);
pinMode(POWER, OUTPUT); pinMode(POWER, OUTPUT);
pinMode(RECIVER, OUTPUT);
ledcSetup(PWM_WHITE, 5000, 8); ledcSetup(PWM_WHITE, 5000, 8);
ledcSetup(PWM_BLUE, 5000, 8); ledcSetup(PWM_BLUE, 5000, 8);
ledcAttachPin(LED_WHITE, PWM_WHITE); ledcAttachPin(LED_WHITE, PWM_WHITE);
@@ -209,8 +211,8 @@ void loop() {
ledcWrite(PWM_BLUE, levBlue); ledcWrite(PWM_BLUE, levBlue);
statLamp |= 0x01; statLamp |= 0x01;
bLamp = true; bLamp = true;
Serial.println(F("BLamp On")); //Serial.println(F("BLamp On"));
WebSerial.println(F("Blue Lamp On")); //WebSerial.println(F("Blue Lamp On"));
} }
else if((LightLev > (spLight + dbLight)) || (move == 0)){ else if((LightLev > (spLight + dbLight)) || (move == 0)){
if((levRunB + 50 <= millis() && levBlue > 0)){ if((levRunB + 50 <= millis() && levBlue > 0)){
@@ -240,7 +242,7 @@ void loop() {
wLamp = false; wLamp = false;
} }
if(statLamp > 0) digitalWrite(POWER, HIGH); if(statLamp > 0) digitalWrite(POWER, HIGH);
else if((levBlue > 0) && (levWhite > 0)) digitalWrite(POWER, LOW); else if((levBlue == 0) && (levWhite == 0)) digitalWrite(POWER, LOW);
if((cRun + 1000) <= millis()){ //Once per second if((cRun + 1000) <= millis()){ //Once per second
cRun = millis(); cRun = millis();
if(abs(LightLev - prevLLevel) > 100){ if(abs(LightLev - prevLLevel) > 100){
@@ -370,6 +372,7 @@ void onMqttConnect(bool sessionPresent) {
mqttClient.publish(TOPIC"spLevel", 1, false, s); mqttClient.publish(TOPIC"spLevel", 1, false, s);
itoa(dbLight, s, 10); itoa(dbLight, s, 10);
mqttClient.publish(TOPIC"dbLevel", 1, false, s); mqttClient.publish(TOPIC"dbLevel", 1, false, s);
mqttClient.publish(TOPIC"reciever", 1, false, "0");
mqttClient.publish(TOPIC"lampw", 1, false, lightWhite ? "1" : "0"); mqttClient.publish(TOPIC"lampw", 1, false, lightWhite ? "1" : "0");
mqttClient.publish(TOPIC"lampw_set", 1, false, lightWhite ? "1" : "0"); mqttClient.publish(TOPIC"lampw_set", 1, false, lightWhite ? "1" : "0");
@@ -380,6 +383,7 @@ void onMqttConnect(bool sessionPresent) {
mqttClient.subscribe(TOPIC"moveperiod", 1); mqttClient.subscribe(TOPIC"moveperiod", 1);
mqttClient.subscribe(TOPIC"spLevel", 1); mqttClient.subscribe(TOPIC"spLevel", 1);
mqttClient.subscribe(TOPIC"dbLevel", 1); mqttClient.subscribe(TOPIC"dbLevel", 1);
mqttClient.subscribe(TOPIC"reciever", 1);
Serial.print(F("PeriodMove:"));Serial.println(periodMotion); Serial.print(F("PeriodMove:"));Serial.println(periodMotion);
Serial.print(F("SPLight:"));Serial.println(spLight); Serial.print(F("SPLight:"));Serial.println(spLight);
@@ -449,6 +453,15 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties
//EEPROM.put(0, levelBlue); //EEPROM.put(0, levelBlue);
//w = true; //w = true;
} }
if(strcmp(topic, TOPIC"reciever") == 0){
reciever = atoi(pl);
digitalWrite(RECIVER, reciever);
Serial.printf("New Reciever: %d\n", lightWhite);
WebSerial.print("New Reciever: ");WebSerial.println(lightWhite);
//mqttClient.publish(TOPIC"reciever", 1, false, reciever ? "1" : "0");
//EEPROM.put(0, levelBlue);
//w = true;
}
if(strcmp(topic, TOPIC"moveperiod") == 0){ if(strcmp(topic, TOPIC"moveperiod") == 0){
periodMotion = atoi(pl); periodMotion = atoi(pl);
Serial.printf("New MovePeriod: %d\n", periodMotion); Serial.printf("New MovePeriod: %d\n", periodMotion);

View File

@@ -0,0 +1,33 @@
#include <Arduino.h>
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
<title>ESP Web Server</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="data:,">
<style>
html {font-family: Arial; display: inline-block; text-align: center;}
h2 {font-size: 3.0rem;}
p {font-size: 3.0rem;}
body {max-width: 600px; margin:0px auto; padding-bottom: 25px;}
.switch {position: relative; display: inline-block; width: 120px; height: 68px}
.switch input {display: none}
.slider {position: absolute; top: 0; left: 0; right: 0; bottom: 0; background-color: #ccc; border-radius: 6px}
.slider:before {position: absolute; content: ""; height: 52px; width: 52px; left: 8px; bottom: 8px; background-color: #fff; -webkit-transition: .4s; transition: .4s; border-radius: 3px}
input:checked+.slider {background-color: #b30000}
input:checked+.slider:before {-webkit-transform: translateX(52px); -ms-transform: translateX(52px); transform: translateX(52px)}
</style>
</head>
<body>
<h2>ESP Web Server</h2>
%BUTTONPLACEHOLDER%
<script>function toggleCheckbox(element) {
var xhr = new XMLHttpRequest();
if(element.checked){ xhr.open("GET", "/update?output="+element.id+"&state=1", true); }
else { xhr.open("GET", "/update?output="+element.id+"&state=0", true); }
xhr.send();
}
</script>
</body>
</html>
)rawliteral";

View File

@@ -2,6 +2,9 @@
#include <ArduinoOTA.h> #include <ArduinoOTA.h>
#include <Ticker.h> #include <Ticker.h>
#include <AsyncMqttClient.h> #include <AsyncMqttClient.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <WebSerial.h>
#include <Adafruit_BME280.h> #include <Adafruit_BME280.h>
#include <MHZ19.h> #include <MHZ19.h>

View File

@@ -23,5 +23,7 @@ lib_deps =
jwrw/ESP_EEPROM @ ^2.0.0 jwrw/ESP_EEPROM @ ^2.0.0
robtillaart/RunningMedian @ ^0.3.3 robtillaart/RunningMedian @ ^0.3.3
crankyoldgit/IRremoteESP8266 @ ^2.8.2 crankyoldgit/IRremoteESP8266 @ ^2.8.2
ayushsharma82/WebSerial @ ^1.3.0
ottowinter/ESPAsyncWebServer-esphome @ ^3.0.0
;joaolopesf/RemoteDebug @ 3.0.5 ;joaolopesf/RemoteDebug @ 3.0.5

View File

@@ -21,6 +21,7 @@ const char* mqtt_server = "192.168.1.111";
AsyncMqttClient mqttClient; AsyncMqttClient mqttClient;
Ticker mqttReconnectTimer; Ticker mqttReconnectTimer;
AsyncWebServer server(80);
WiFiEventHandler wifiConnectHandler; WiFiEventHandler wifiConnectHandler;
WiFiEventHandler wifiDisconnectHandler; WiFiEventHandler wifiDisconnectHandler;
@@ -105,7 +106,7 @@ void setup() {
} }
//Serial1.println(); //Serial1.println();
mhz19.begin(Serial); mhz19.begin(Serial);
mhz19.autoCalibration(); //mhz19.autoCalibration();
Wire.begin(); Wire.begin();
bool status = bme.begin(BME280_ADDRESS_ALTERNATE); bool status = bme.begin(BME280_ADDRESS_ALTERNATE);
if (!status) { if (!status) {
@@ -138,6 +139,9 @@ void setup() {
cRun = millis(); cRun = millis();
cRunADC = millis(); cRunADC = millis();
firstRun = true;
WebSerial.begin(&server);
server.begin();
//Serial1.println(F("Start Loop")); //Serial1.println(F("Start Loop"));
//(R_LED, HIGH); //(R_LED, HIGH);
} }
@@ -155,6 +159,7 @@ void loop() {
if(mvDelay == -1){ if(mvDelay == -1){
g_led.start(); g_led.start();
mqttClient.publish(TOPIC"/move", 1, false, "1"); mqttClient.publish(TOPIC"/move", 1, false, "1");
WebSerial.println("Move On");
} }
mvDelay = mvDelaySet; mvDelay = mvDelaySet;
} }
@@ -187,12 +192,16 @@ void loop() {
if(mvDelay == 0) { if(mvDelay == 0) {
g_led.start(); g_led.start();
mqttClient.publish(TOPIC"/move", 1, false, "0"); mqttClient.publish(TOPIC"/move", 1, false, "0");
WebSerial.println("Move Off");
mvDelay = -1; mvDelay = -1;
} }
if(mvDelay > 0) mvDelay--; if(mvDelay > 0) mvDelay--;
if(minCnt % 10 == 0){ if(minCnt % 5 == 0){
co2 = mhz19.getCO2(); co2 = mhz19.getCO2();
char s[20];
sprintf(s, "Ms=%lu,co2=%d", millis(), co2);
WebSerial.println(s);
// /* Serial1.print("CO2: "); // /* Serial1.print("CO2: ");
// Serial1.println(co2); */ // Serial1.println(co2); */
// // Serial.println("Measure BME"); // // Serial.println("Measure BME");
@@ -249,7 +258,7 @@ void loop() {
//Serial1.println("Publish2"); //Serial1.println("Publish2");
if(firstRun){ if(firstRun){
stDelay++; stDelay++;
if(stDelay > 3) firstRun = false; if(stDelay > 2) firstRun = false;
} }
if(!firstRun && (mhz19.errorCode == RESULT_OK)){ if(!firstRun && (mhz19.errorCode == RESULT_OK)){
itoa(co2, v, 10); itoa(co2, v, 10);
@@ -345,6 +354,7 @@ void onMqttConnect(bool sessionPresent) {
mqttClient.subscribe(TOPIC"/ac/eco", 1); mqttClient.subscribe(TOPIC"/ac/eco", 1);
mqttClient.publish(TOPIC"/ac/state", 1, false, ac.getPower() ? "1" : "0"); mqttClient.publish(TOPIC"/ac/state", 1, false, ac.getPower() ? "1" : "0");
mqttClient.subscribe(TOPIC"/ac/state", 1); mqttClient.subscribe(TOPIC"/ac/state", 1);
mqttClient.subscribe(TOPIC"/co2cal", 1);
// ultoa(ESP.getFlashChipSize(), v, 10); // ultoa(ESP.getFlashChipSize(), v, 10);
// mqttClient.publish(TOPIC"/chipsize", 1, false, v); // mqttClient.publish(TOPIC"/chipsize", 1, false, v);
// ultoa(ESP.getFlashChipRealSize(), v, 10); // ultoa(ESP.getFlashChipRealSize(), v, 10);
@@ -436,6 +446,12 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties
else ac.setTurbo(false); else ac.setTurbo(false);
ac.send(); ac.send();
} }
if(strcmp(topic, TOPIC"/co2cal") == 0){
char m = payload[0];
if(m == '1'){
mhz19.calibrate();
}
}
b_led.start(); b_led.start();
} }
/* /*