This commit is contained in:
2022-08-23 10:30:25 +02:00
parent fec46b617a
commit d54ff019ae
3 changed files with 125 additions and 46 deletions

View File

@@ -6,6 +6,14 @@ void initWifi();
void initHA(); void initHA();
void initArduinoOTA();
void initWebSerial();
void recvMsg(uint8_t *data, size_t len);
void serialPrint(String message);
void onCoverLeftCommand(HACover::CoverCommand cmd); void onCoverLeftCommand(HACover::CoverCommand cmd);
void onCoverRightCommand(HACover::CoverCommand cmd); void onCoverRightCommand(HACover::CoverCommand cmd);

View File

@@ -12,6 +12,16 @@
platform = espressif8266 platform = espressif8266
board = nodemcuv2 board = nodemcuv2
framework = arduino framework = arduino
upload_protocol = espota
upload_port = 192.168.1.92
lib_deps =
ayushsharma82/WebSerial@^1.3.0
ottowinter/ESPAsyncWebServer-esphome@^2.1.0
ottowinter/ESPAsyncTCP-esphome@^1.2.3
[env:native] [env:native]
platform = native platform = native
lib_deps =
ayushsharma82/WebSerial@^1.3.0
ottowinter/ESPAsyncWebServer-esphome@^2.1.0
ottowinter/ESPAsyncTCP-esphome@^1.2.3

View File

@@ -1,18 +1,23 @@
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <ArduinoHA.h> #include <ArduinoHA.h>
#include <main.h> #include <main.h>
#include <ArduinoOTA.h>
#include <WebSerial.h>
#define BROKER_ADDR IPAddress(192,168,1,46) #define MQTT_BROKER_ADDR IPAddress(192,168,1,74)
#define MQTT_BROKER_PORT 1883
#define MQTT_BROKER_USERNAME "Julian"
#define MQTT_BROKER_PASSWORD "Original_91"
#define WIFI_SSID "Heimathafen" #define WIFI_SSID "Heimathafen"
#define WIFI_PASSWORD "43762579905371198122" #define WIFI_PASSWORD "43762579905371198122"
#define COVER_CLOSING_TIME_TOTAL_MILLIS 27500 #define COVER_CLOSING_TIME_TOTAL_MILLIS 27500
#define REMOTE_BUTTON_PRESS_LENGTH_MILLIS 1000 #define REMOTE_BUTTON_PRESS_LENGTH_MILLIS 1000
#define REMOTE_BUTTON_PRESS_COOLDOWN_MILLIS 500 #define REMOTE_BUTTON_PRESS_COOLDOWN_MILLIS 500
#define MAX_REMOTE_COMMAND_QUEUE_LENGTH 20 #define MAX_REMOTE_COMMAND_QUEUE_LENGTH 20
#define PIN_LEFT_OPEN D5 #define PIN_LEFT_OPEN D7
#define PIN_LEFT_CLOSE D6 #define PIN_LEFT_CLOSE D6
#define PIN_RIGHT_OPEN D7 #define PIN_RIGHT_OPEN D8
#define PIN_RIGHT_CLOSE D8 #define PIN_RIGHT_CLOSE D5
byte mac[] = {0x00, 0x10, 0xFA, 0x6E, 0x38, 0x4A}; byte mac[] = {0x00, 0x10, 0xFA, 0x6E, 0x38, 0x4A};
@@ -46,8 +51,8 @@ HACover coverLeft("rolladen-wohnzimmer-links", false, false);
HACover coverRight("rolladen-wohnzimmer-rechts", false, false); HACover coverRight("rolladen-wohnzimmer-rechts", false, false);
String lastCommandLeft = ""; String lastCommandLeft = "";
String lastCommandRight = ""; String lastCommandRight = "";
int currentPositionLeft = 0; int currentPositionLeft = 100;
int currentPositionRight = 0; int currentPositionRight = 100;
int targetPositionLeft = 0; int targetPositionLeft = 0;
int targetPositionRight = 0; int targetPositionRight = 0;
unsigned long startMovingLeftMillis = 0; unsigned long startMovingLeftMillis = 0;
@@ -56,22 +61,40 @@ CoverState currentStateLeft = CoverState::Stopped;
CoverState currentStateRight = CoverState::Stopped; CoverState currentStateRight = CoverState::Stopped;
unsigned long startSendRemoteCommandMillis = 0; unsigned long startSendRemoteCommandMillis = 0;
int remoteState = RemoteState::Idle; int remoteState = RemoteState::Idle;
AsyncWebServer server(80);
void setup() { void setup() {
Serial.begin(9600); Serial.begin(9600);
Serial.println("Starting..."); Serial.println("Starting...");
initPins(); initPins();
initWifi(); initWifi();
initWebSerial();
initArduinoOTA();
initHA(); initHA();
} }
void loop() { void loop() {
ArduinoOTA.handle();
mqtt.loop(); mqtt.loop();
processStateLeft(); processStateLeft();
processStateRight(); processStateRight();
sendRemoteCommand(); sendRemoteCommand();
} }
void initWebSerial() {
WebSerial.begin(&server);
WebSerial.msgCallback(recvMsg);
server.begin();
}
void recvMsg(uint8_t *data, size_t len){
String d = "";
for(int i=0; i < len; i++){
d += char(data[i]);
}
WebSerial.println(d);
}
void initPins() { void initPins() {
pinMode(PIN_LEFT_OPEN, OUTPUT); pinMode(PIN_LEFT_OPEN, OUTPUT);
pinMode(PIN_LEFT_CLOSE, OUTPUT); pinMode(PIN_LEFT_CLOSE, OUTPUT);
@@ -104,55 +127,84 @@ void initHA() {
device.setName("NodeMCU_Rollladen_Wohnzimmer"); device.setName("NodeMCU_Rollladen_Wohnzimmer");
device.setSoftwareVersion("1.0.0"); device.setSoftwareVersion("1.0.0");
mqtt.begin(BROKER_ADDR); mqtt.begin(MQTT_BROKER_ADDR, MQTT_BROKER_PORT, MQTT_BROKER_USERNAME, MQTT_BROKER_PASSWORD);
}
void initArduinoOTA() {
ArduinoOTA.onStart([]() {
serialPrint("Start OTA");
});
ArduinoOTA.onEnd([]() {
serialPrint("\nEnd OTA");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) serialPrint("Auth Failed");
else if (error == OTA_BEGIN_ERROR) serialPrint("Begin Failed");
else if (error == OTA_CONNECT_ERROR) serialPrint("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) serialPrint("Receive Failed");
else if (error == OTA_END_ERROR) serialPrint("End Failed");
});
ArduinoOTA.begin();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void serialPrint(String message) {
WebSerial.println(message);
Serial.println(message);
} }
void onCoverLeftCommand(HACover::CoverCommand cmd) { void onCoverLeftCommand(HACover::CoverCommand cmd) {
if (cmd == HACover::CommandOpen) { if (cmd == HACover::CommandOpen) {
Serial.println("Received Command: Open"); serialPrint("Received Command Left: Open");
if(targetPositionLeft != 100) { if(targetPositionLeft != 100) {
sendCoverLeftStop(); sendCoverLeftStop();
targetPositionLeft = 100; targetPositionLeft = 100;
} }
} else if (cmd == HACover::CommandClose) { } else if (cmd == HACover::CommandClose) {
Serial.println("Received Command: Close"); serialPrint("Received Command Left: Close");
if(targetPositionLeft != 0) { if(targetPositionLeft != 0) {
sendCoverLeftStop(); sendCoverLeftStop();
targetPositionLeft = 0; targetPositionLeft = 0;
} }
} else if (cmd == HACover::CommandStop) { } else if (cmd == HACover::CommandStop) {
Serial.println("Received Command: Stop"); serialPrint("Received Command Left: Stop");
sendCoverLeftStop(); sendCoverLeftStop();
} }
} }
void onCoverRightCommand(HACover::CoverCommand cmd) { void onCoverRightCommand(HACover::CoverCommand cmd) {
if (cmd == HACover::CommandOpen) { if (cmd == HACover::CommandOpen) {
Serial.println("Received Command: Open"); serialPrint("Received Command Right: Open");
if(targetPositionRight != 100) { if(targetPositionRight != 100) {
sendCoverRightStop(); sendCoverRightStop();
targetPositionRight = 100; targetPositionRight = 100;
} }
} else if (cmd == HACover::CommandClose) { } else if (cmd == HACover::CommandClose) {
Serial.println("Received Command: Close"); serialPrint("Received Command Right: Close");
if(targetPositionRight != 0) { if(targetPositionRight != 0) {
sendCoverRightStop(); sendCoverRightStop();
targetPositionRight = 0; targetPositionRight = 0;
} }
} else if (cmd == HACover::CommandStop) { } else if (cmd == HACover::CommandStop) {
Serial.println("Received Command: Stop"); serialPrint("Received Command Right: Stop");
sendCoverRightStop(); sendCoverRightStop();
} }
} }
void onCoverLeftSetPositionCommand(uint8_t pos) { void onCoverLeftSetPositionCommand(uint8_t pos) {
Serial.println("Received Set Position Command: " + String(pos)); serialPrint("Received Set Position Command Left: " + String(pos));
sendCoverLeftStop(); sendCoverLeftStop();
targetPositionLeft = pos; targetPositionLeft = pos;
} }
void onCoverRightSetPositionCommand(uint8_t pos) { void onCoverRightSetPositionCommand(uint8_t pos) {
Serial.println("Received Set Position Command: " + String(pos)); serialPrint("Received Set Position Command Right: " + String(pos));
sendCoverRightStop(); sendCoverRightStop();
targetPositionRight = pos; targetPositionRight = pos;
} }
@@ -195,7 +247,7 @@ void processStateRight() {
void sendCoverLeftOpen() { void sendCoverLeftOpen() {
coverLeft.setState(HACover::StateOpening); coverLeft.setState(HACover::StateOpening);
Serial.println("Send Cover Left: OPEN."); serialPrint("Send Cover Left: OPEN.");
remoteCommandQueue[remoteCommandQueueLength] = RemoteCommands::LeftOpen; remoteCommandQueue[remoteCommandQueueLength] = RemoteCommands::LeftOpen;
remoteCommandQueueLength++; remoteCommandQueueLength++;
currentStateLeft = CoverState::Opening; currentStateLeft = CoverState::Opening;
@@ -204,17 +256,22 @@ void sendCoverLeftOpen() {
void sendCoverLeftStop() { void sendCoverLeftStop() {
int movementDiff = (millis() - startMovingLeftMillis) * 100 / COVER_CLOSING_TIME_TOTAL_MILLIS; int movementDiff = (millis() - startMovingLeftMillis) * 100 / COVER_CLOSING_TIME_TOTAL_MILLIS;
if(currentStateLeft == CoverState::Opening) {
Serial.println("Send Cover Left: CLOSE TO STOP."); if(currentStateLeft == CoverState::Opening) {
remoteCommandQueue[remoteCommandQueueLength] = RemoteCommands::LeftClose;
remoteCommandQueueLength++;
currentPositionLeft = currentPositionLeft + movementDiff; currentPositionLeft = currentPositionLeft + movementDiff;
if(currentPositionLeft < 98) {
serialPrint("Send Cover Left: CLOSE TO STOP.");
remoteCommandQueue[remoteCommandQueueLength] = RemoteCommands::LeftClose;
remoteCommandQueueLength++;
}
} }
if(currentStateLeft == CoverState::Closing) { if(currentStateLeft == CoverState::Closing) {
Serial.println("Send Cover Left: OPEN TO STOP.");
remoteCommandQueue[remoteCommandQueueLength] = RemoteCommands::LeftOpen;
remoteCommandQueueLength++;
currentPositionLeft = currentPositionLeft - movementDiff; currentPositionLeft = currentPositionLeft - movementDiff;
if(targetPositionLeft > 2) {
serialPrint("Send Cover Left: OPEN TO STOP.");
remoteCommandQueue[remoteCommandQueueLength] = RemoteCommands::LeftOpen;
remoteCommandQueueLength++;
}
} }
currentStateLeft = CoverState::Stopped; currentStateLeft = CoverState::Stopped;
@@ -222,12 +279,12 @@ void sendCoverLeftStop() {
coverLeft.setState(HACover::StateStopped); coverLeft.setState(HACover::StateStopped);
coverLeft.setPosition(currentPositionLeft); coverLeft.setPosition(currentPositionLeft);
// Serial.println("Current Position: " + String(currentPositionLeft)); // serialPrint("Current Position: " + String(currentPositionLeft));
} }
void sendCoverLeftClose() { void sendCoverLeftClose() {
coverLeft.setState(HACover::StateClosing); coverLeft.setState(HACover::StateClosing);
Serial.println("Send Cover Left: CLOSE."); serialPrint("Send Cover Left: CLOSE.");
remoteCommandQueue[remoteCommandQueueLength] = RemoteCommands::LeftClose; remoteCommandQueue[remoteCommandQueueLength] = RemoteCommands::LeftClose;
remoteCommandQueueLength++; remoteCommandQueueLength++;
currentStateLeft = CoverState::Closing; currentStateLeft = CoverState::Closing;
@@ -236,7 +293,7 @@ void sendCoverLeftClose() {
void sendCoverRightOpen() { void sendCoverRightOpen() {
coverRight.setState(HACover::StateOpening); coverRight.setState(HACover::StateOpening);
Serial.println("Send Cover Right: OPEN."); serialPrint("Send Cover Right: OPEN.");
remoteCommandQueue[remoteCommandQueueLength] = RemoteCommands::RightOpen; remoteCommandQueue[remoteCommandQueueLength] = RemoteCommands::RightOpen;
remoteCommandQueueLength++; remoteCommandQueueLength++;
currentStateRight = CoverState::Opening; currentStateRight = CoverState::Opening;
@@ -245,17 +302,21 @@ void sendCoverRightOpen() {
void sendCoverRightStop() { void sendCoverRightStop() {
int movementDiff = (millis() - startMovingRightMillis) * 100 / COVER_CLOSING_TIME_TOTAL_MILLIS; int movementDiff = (millis() - startMovingRightMillis) * 100 / COVER_CLOSING_TIME_TOTAL_MILLIS;
if(currentStateRight == CoverState::Opening) { if(currentStateRight == CoverState::Opening) {
Serial.println("Send Cover Right: CLOSE TO STOP.");
remoteCommandQueue[remoteCommandQueueLength] = RemoteCommands::RightClose;
remoteCommandQueueLength++;
currentPositionRight = currentPositionRight + movementDiff; currentPositionRight = currentPositionRight + movementDiff;
if(currentPositionRight < 98) {
serialPrint("Send Cover Right: CLOSE TO STOP.");
remoteCommandQueue[remoteCommandQueueLength] = RemoteCommands::RightClose;
remoteCommandQueueLength++;
}
} }
if(currentStateRight == CoverState::Closing) { if(currentStateRight == CoverState::Closing) {
Serial.println("Send Cover Right: OPEN TO STOP.");
remoteCommandQueue[remoteCommandQueueLength] = RemoteCommands::RightOpen;
remoteCommandQueueLength++;
currentPositionRight = currentPositionRight - movementDiff; currentPositionRight = currentPositionRight - movementDiff;
if(currentPositionRight > 2) {
serialPrint("Send Cover Right: OPEN TO STOP.");
remoteCommandQueue[remoteCommandQueueLength] = RemoteCommands::RightOpen;
remoteCommandQueueLength++;
}
} }
currentStateRight = CoverState::Stopped; currentStateRight = CoverState::Stopped;
@@ -263,12 +324,12 @@ void sendCoverRightStop() {
coverRight.setState(HACover::StateStopped); coverRight.setState(HACover::StateStopped);
coverRight.setPosition(currentPositionRight); coverRight.setPosition(currentPositionRight);
// Serial.println("Current Position: " + String(currentPositionRight)); // serialPrint("Current Position: " + String(currentPositionRight));
} }
void sendCoverRightClose() { void sendCoverRightClose() {
coverRight.setState(HACover::StateClosing); coverRight.setState(HACover::StateClosing);
Serial.println("Send Cover Right: CLOSE."); serialPrint("Send Cover Right: CLOSE.");
remoteCommandQueue[remoteCommandQueueLength] = RemoteCommands::RightClose; remoteCommandQueue[remoteCommandQueueLength] = RemoteCommands::RightClose;
remoteCommandQueueLength++; remoteCommandQueueLength++;
currentStateRight = CoverState::Closing; currentStateRight = CoverState::Closing;
@@ -278,7 +339,7 @@ void sendCoverRightClose() {
void sendRemoteCommand() { void sendRemoteCommand() {
// Stop sending remote command // Stop sending remote command
if(remoteState == RemoteState::Busy && millis() - REMOTE_BUTTON_PRESS_LENGTH_MILLIS > startSendRemoteCommandMillis) { if(remoteState == RemoteState::Busy && millis() - REMOTE_BUTTON_PRESS_LENGTH_MILLIS > startSendRemoteCommandMillis) {
Serial.println("RemoteState::Cooldown"); serialPrint("RemoteState::Cooldown");
remoteState = RemoteState::Cooldown; remoteState = RemoteState::Cooldown;
digitalWrite(PIN_LEFT_OPEN, LOW); digitalWrite(PIN_LEFT_OPEN, LOW);
digitalWrite(PIN_LEFT_CLOSE, LOW); digitalWrite(PIN_LEFT_CLOSE, LOW);
@@ -288,13 +349,13 @@ void sendRemoteCommand() {
// Wait Cooldown before sending next button press // Wait Cooldown before sending next button press
if(remoteState == RemoteState::Cooldown && millis() - REMOTE_BUTTON_PRESS_LENGTH_MILLIS - REMOTE_BUTTON_PRESS_COOLDOWN_MILLIS > startSendRemoteCommandMillis) { if(remoteState == RemoteState::Cooldown && millis() - REMOTE_BUTTON_PRESS_LENGTH_MILLIS - REMOTE_BUTTON_PRESS_COOLDOWN_MILLIS > startSendRemoteCommandMillis) {
Serial.println("RemoteState::Idle"); serialPrint("RemoteState::Idle");
remoteState = RemoteState::Idle; remoteState = RemoteState::Idle;
} }
// Send next command // Send next command
if (remoteState == RemoteState::Idle && remoteCommandQueueLength > 0) { if (remoteState == RemoteState::Idle && remoteCommandQueueLength > 0) {
// Serial.println("remoteCommandQueueLength: " + String(remoteCommandQueueLength)); // serialPrint("remoteCommandQueueLength: " + String(remoteCommandQueueLength));
int nextCommand = remoteCommandQueue[0]; int nextCommand = remoteCommandQueue[0];
remoteCommandQueueLength--; remoteCommandQueueLength--;
@@ -306,19 +367,19 @@ void sendRemoteCommand() {
switch (nextCommand) switch (nextCommand)
{ {
case RemoteCommands::LeftOpen: case RemoteCommands::LeftOpen:
Serial.println("Remote: Press Button Left Open"); serialPrint("Remote: Press Button Left Open");
digitalWrite(PIN_LEFT_OPEN, HIGH); digitalWrite(PIN_LEFT_OPEN, HIGH);
break; break;
case RemoteCommands::LeftClose: case RemoteCommands::LeftClose:
Serial.println("Remote: Press Button Left Close"); serialPrint("Remote: Press Button Left Close");
digitalWrite(PIN_LEFT_CLOSE, HIGH); digitalWrite(PIN_LEFT_CLOSE, HIGH);
break; break;
case RemoteCommands::RightOpen: case RemoteCommands::RightOpen:
Serial.println("Remote: Press Button Right Open"); serialPrint("Remote: Press Button Right Open");
digitalWrite(PIN_RIGHT_OPEN, HIGH); digitalWrite(PIN_RIGHT_OPEN, HIGH);
break; break;
case RemoteCommands::RightClose: case RemoteCommands::RightClose:
Serial.println("Remote: Press Button Right Close"); serialPrint("Remote: Press Button Right Close");
digitalWrite(PIN_RIGHT_CLOSE, HIGH); digitalWrite(PIN_RIGHT_CLOSE, HIGH);
break; break;
@@ -326,7 +387,7 @@ void sendRemoteCommand() {
break; break;
} }
Serial.println("RemoteState::Busy"); serialPrint("RemoteState::Busy");
remoteState = RemoteState::Busy; remoteState = RemoteState::Busy;
startSendRemoteCommandMillis = millis(); startSendRemoteCommandMillis = millis();
} }