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 initArduinoOTA();
void initWebSerial();
void recvMsg(uint8_t *data, size_t len);
void serialPrint(String message);
void onCoverLeftCommand(HACover::CoverCommand cmd);
void onCoverRightCommand(HACover::CoverCommand cmd);

View File

@@ -12,6 +12,16 @@
platform = espressif8266
board = nodemcuv2
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]
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 <ArduinoHA.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_PASSWORD "43762579905371198122"
#define COVER_CLOSING_TIME_TOTAL_MILLIS 27500
#define REMOTE_BUTTON_PRESS_LENGTH_MILLIS 1000
#define REMOTE_BUTTON_PRESS_COOLDOWN_MILLIS 500
#define MAX_REMOTE_COMMAND_QUEUE_LENGTH 20
#define PIN_LEFT_OPEN D5
#define PIN_LEFT_OPEN D7
#define PIN_LEFT_CLOSE D6
#define PIN_RIGHT_OPEN D7
#define PIN_RIGHT_CLOSE D8
#define PIN_RIGHT_OPEN D8
#define PIN_RIGHT_CLOSE D5
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);
String lastCommandLeft = "";
String lastCommandRight = "";
int currentPositionLeft = 0;
int currentPositionRight = 0;
int currentPositionLeft = 100;
int currentPositionRight = 100;
int targetPositionLeft = 0;
int targetPositionRight = 0;
unsigned long startMovingLeftMillis = 0;
@@ -56,22 +61,40 @@ CoverState currentStateLeft = CoverState::Stopped;
CoverState currentStateRight = CoverState::Stopped;
unsigned long startSendRemoteCommandMillis = 0;
int remoteState = RemoteState::Idle;
AsyncWebServer server(80);
void setup() {
Serial.begin(9600);
Serial.println("Starting...");
initPins();
initWifi();
initWebSerial();
initArduinoOTA();
initHA();
}
void loop() {
ArduinoOTA.handle();
mqtt.loop();
processStateLeft();
processStateRight();
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() {
pinMode(PIN_LEFT_OPEN, OUTPUT);
pinMode(PIN_LEFT_CLOSE, OUTPUT);
@@ -104,55 +127,84 @@ void initHA() {
device.setName("NodeMCU_Rollladen_Wohnzimmer");
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) {
if (cmd == HACover::CommandOpen) {
Serial.println("Received Command: Open");
serialPrint("Received Command Left: Open");
if(targetPositionLeft != 100) {
sendCoverLeftStop();
targetPositionLeft = 100;
}
} else if (cmd == HACover::CommandClose) {
Serial.println("Received Command: Close");
serialPrint("Received Command Left: Close");
if(targetPositionLeft != 0) {
sendCoverLeftStop();
targetPositionLeft = 0;
}
} else if (cmd == HACover::CommandStop) {
Serial.println("Received Command: Stop");
serialPrint("Received Command Left: Stop");
sendCoverLeftStop();
}
}
void onCoverRightCommand(HACover::CoverCommand cmd) {
if (cmd == HACover::CommandOpen) {
Serial.println("Received Command: Open");
serialPrint("Received Command Right: Open");
if(targetPositionRight != 100) {
sendCoverRightStop();
targetPositionRight = 100;
}
} else if (cmd == HACover::CommandClose) {
Serial.println("Received Command: Close");
serialPrint("Received Command Right: Close");
if(targetPositionRight != 0) {
sendCoverRightStop();
targetPositionRight = 0;
}
} else if (cmd == HACover::CommandStop) {
Serial.println("Received Command: Stop");
serialPrint("Received Command Right: Stop");
sendCoverRightStop();
}
}
void onCoverLeftSetPositionCommand(uint8_t pos) {
Serial.println("Received Set Position Command: " + String(pos));
serialPrint("Received Set Position Command Left: " + String(pos));
sendCoverLeftStop();
targetPositionLeft = pos;
}
void onCoverRightSetPositionCommand(uint8_t pos) {
Serial.println("Received Set Position Command: " + String(pos));
serialPrint("Received Set Position Command Right: " + String(pos));
sendCoverRightStop();
targetPositionRight = pos;
}
@@ -195,7 +247,7 @@ void processStateRight() {
void sendCoverLeftOpen() {
coverLeft.setState(HACover::StateOpening);
Serial.println("Send Cover Left: OPEN.");
serialPrint("Send Cover Left: OPEN.");
remoteCommandQueue[remoteCommandQueueLength] = RemoteCommands::LeftOpen;
remoteCommandQueueLength++;
currentStateLeft = CoverState::Opening;
@@ -204,17 +256,22 @@ void sendCoverLeftOpen() {
void sendCoverLeftStop() {
int movementDiff = (millis() - startMovingLeftMillis) * 100 / COVER_CLOSING_TIME_TOTAL_MILLIS;
if(currentStateLeft == CoverState::Opening) {
Serial.println("Send Cover Left: CLOSE TO STOP.");
remoteCommandQueue[remoteCommandQueueLength] = RemoteCommands::LeftClose;
remoteCommandQueueLength++;
if(currentStateLeft == CoverState::Opening) {
currentPositionLeft = currentPositionLeft + movementDiff;
if(currentPositionLeft < 98) {
serialPrint("Send Cover Left: CLOSE TO STOP.");
remoteCommandQueue[remoteCommandQueueLength] = RemoteCommands::LeftClose;
remoteCommandQueueLength++;
}
}
if(currentStateLeft == CoverState::Closing) {
Serial.println("Send Cover Left: OPEN TO STOP.");
remoteCommandQueue[remoteCommandQueueLength] = RemoteCommands::LeftOpen;
remoteCommandQueueLength++;
if(currentStateLeft == CoverState::Closing) {
currentPositionLeft = currentPositionLeft - movementDiff;
if(targetPositionLeft > 2) {
serialPrint("Send Cover Left: OPEN TO STOP.");
remoteCommandQueue[remoteCommandQueueLength] = RemoteCommands::LeftOpen;
remoteCommandQueueLength++;
}
}
currentStateLeft = CoverState::Stopped;
@@ -222,12 +279,12 @@ void sendCoverLeftStop() {
coverLeft.setState(HACover::StateStopped);
coverLeft.setPosition(currentPositionLeft);
// Serial.println("Current Position: " + String(currentPositionLeft));
// serialPrint("Current Position: " + String(currentPositionLeft));
}
void sendCoverLeftClose() {
coverLeft.setState(HACover::StateClosing);
Serial.println("Send Cover Left: CLOSE.");
serialPrint("Send Cover Left: CLOSE.");
remoteCommandQueue[remoteCommandQueueLength] = RemoteCommands::LeftClose;
remoteCommandQueueLength++;
currentStateLeft = CoverState::Closing;
@@ -236,7 +293,7 @@ void sendCoverLeftClose() {
void sendCoverRightOpen() {
coverRight.setState(HACover::StateOpening);
Serial.println("Send Cover Right: OPEN.");
serialPrint("Send Cover Right: OPEN.");
remoteCommandQueue[remoteCommandQueueLength] = RemoteCommands::RightOpen;
remoteCommandQueueLength++;
currentStateRight = CoverState::Opening;
@@ -245,17 +302,21 @@ void sendCoverRightOpen() {
void sendCoverRightStop() {
int movementDiff = (millis() - startMovingRightMillis) * 100 / COVER_CLOSING_TIME_TOTAL_MILLIS;
if(currentStateRight == CoverState::Opening) {
Serial.println("Send Cover Right: CLOSE TO STOP.");
remoteCommandQueue[remoteCommandQueueLength] = RemoteCommands::RightClose;
remoteCommandQueueLength++;
if(currentStateRight == CoverState::Opening) {
currentPositionRight = currentPositionRight + movementDiff;
if(currentPositionRight < 98) {
serialPrint("Send Cover Right: CLOSE TO STOP.");
remoteCommandQueue[remoteCommandQueueLength] = RemoteCommands::RightClose;
remoteCommandQueueLength++;
}
}
if(currentStateRight == CoverState::Closing) {
Serial.println("Send Cover Right: OPEN TO STOP.");
remoteCommandQueue[remoteCommandQueueLength] = RemoteCommands::RightOpen;
remoteCommandQueueLength++;
if(currentStateRight == CoverState::Closing) {
currentPositionRight = currentPositionRight - movementDiff;
if(currentPositionRight > 2) {
serialPrint("Send Cover Right: OPEN TO STOP.");
remoteCommandQueue[remoteCommandQueueLength] = RemoteCommands::RightOpen;
remoteCommandQueueLength++;
}
}
currentStateRight = CoverState::Stopped;
@@ -263,12 +324,12 @@ void sendCoverRightStop() {
coverRight.setState(HACover::StateStopped);
coverRight.setPosition(currentPositionRight);
// Serial.println("Current Position: " + String(currentPositionRight));
// serialPrint("Current Position: " + String(currentPositionRight));
}
void sendCoverRightClose() {
coverRight.setState(HACover::StateClosing);
Serial.println("Send Cover Right: CLOSE.");
serialPrint("Send Cover Right: CLOSE.");
remoteCommandQueue[remoteCommandQueueLength] = RemoteCommands::RightClose;
remoteCommandQueueLength++;
currentStateRight = CoverState::Closing;
@@ -278,7 +339,7 @@ void sendCoverRightClose() {
void sendRemoteCommand() {
// Stop sending remote command
if(remoteState == RemoteState::Busy && millis() - REMOTE_BUTTON_PRESS_LENGTH_MILLIS > startSendRemoteCommandMillis) {
Serial.println("RemoteState::Cooldown");
serialPrint("RemoteState::Cooldown");
remoteState = RemoteState::Cooldown;
digitalWrite(PIN_LEFT_OPEN, LOW);
digitalWrite(PIN_LEFT_CLOSE, LOW);
@@ -288,13 +349,13 @@ void sendRemoteCommand() {
// Wait Cooldown before sending next button press
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;
}
// Send next command
if (remoteState == RemoteState::Idle && remoteCommandQueueLength > 0) {
// Serial.println("remoteCommandQueueLength: " + String(remoteCommandQueueLength));
// serialPrint("remoteCommandQueueLength: " + String(remoteCommandQueueLength));
int nextCommand = remoteCommandQueue[0];
remoteCommandQueueLength--;
@@ -306,19 +367,19 @@ void sendRemoteCommand() {
switch (nextCommand)
{
case RemoteCommands::LeftOpen:
Serial.println("Remote: Press Button Left Open");
serialPrint("Remote: Press Button Left Open");
digitalWrite(PIN_LEFT_OPEN, HIGH);
break;
case RemoteCommands::LeftClose:
Serial.println("Remote: Press Button Left Close");
serialPrint("Remote: Press Button Left Close");
digitalWrite(PIN_LEFT_CLOSE, HIGH);
break;
case RemoteCommands::RightOpen:
Serial.println("Remote: Press Button Right Open");
serialPrint("Remote: Press Button Right Open");
digitalWrite(PIN_RIGHT_OPEN, HIGH);
break;
case RemoteCommands::RightClose:
Serial.println("Remote: Press Button Right Close");
serialPrint("Remote: Press Button Right Close");
digitalWrite(PIN_RIGHT_CLOSE, HIGH);
break;
@@ -326,7 +387,7 @@ void sendRemoteCommand() {
break;
}
Serial.println("RemoteState::Busy");
serialPrint("RemoteState::Busy");
remoteState = RemoteState::Busy;
startSendRemoteCommandMillis = millis();
}