Added stop
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
#include <ArduinoOTA.h>
|
#include <ArduinoOTA.h>
|
||||||
#include <Ticker.h>
|
#include <Ticker.h>
|
||||||
#include <AsyncMqttClient.h>
|
#include <AsyncMqttClient.h>
|
||||||
|
#include <SoftwareSerial.h>
|
||||||
|
|
||||||
#define LED_WF (14)
|
#define LED_WF (14)
|
||||||
#define LED_MQ (12)
|
#define LED_MQ (12)
|
||||||
@@ -40,6 +41,8 @@ WiFiEventHandler wifiConnectHandler;
|
|||||||
WiFiEventHandler wifiDisconnectHandler;
|
WiFiEventHandler wifiDisconnectHandler;
|
||||||
Ticker wifiReconnectTimer;
|
Ticker wifiReconnectTimer;
|
||||||
|
|
||||||
|
SoftwareSerial softSer;
|
||||||
|
|
||||||
void connectToWifi();
|
void connectToWifi();
|
||||||
void connectToMqtt();
|
void connectToMqtt();
|
||||||
void onWifiConnect(const WiFiEventStationModeGotIP& event);
|
void onWifiConnect(const WiFiEventStationModeGotIP& event);
|
||||||
@@ -54,6 +57,7 @@ void writeHex(std::string data);
|
|||||||
|
|
||||||
void setup(){
|
void setup(){
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
|
softSer.begin(9600, SWSERIAL_8N1, D5, D6, false);
|
||||||
WiFi.mode(WIFI_STA);
|
WiFi.mode(WIFI_STA);
|
||||||
WiFi.hostname("Cur-MidRoom");
|
WiFi.hostname("Cur-MidRoom");
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
@@ -110,8 +114,8 @@ void loop(){
|
|||||||
ArduinoOTA.handle();
|
ArduinoOTA.handle();
|
||||||
crc = 0;
|
crc = 0;
|
||||||
offset = 0;
|
offset = 0;
|
||||||
while(Serial.available()){
|
while(softSer.available()){
|
||||||
readByte(Serial.read());
|
readByte(softSer.read());
|
||||||
}
|
}
|
||||||
if(cRun + 999 < millis()){
|
if(cRun + 999 < millis()){
|
||||||
cRun = millis();
|
cRun = millis();
|
||||||
@@ -175,10 +179,39 @@ void onMqttSubscribe(uint16_t packetId, uint8_t qos) {
|
|||||||
|
|
||||||
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) {
|
||||||
if(strcmp("/home/midroom/curtcmd", topic) == 0){
|
if(strcmp("/home/midroom/curtcmd", topic) == 0){
|
||||||
|
crc = 0;
|
||||||
|
switch(atoi(payload)){
|
||||||
|
case 1:
|
||||||
|
writeHex(TUYA_CLOSE);
|
||||||
|
writeByte(crc);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
writeHex(TUYA_OPEN);
|
||||||
|
writeByte(crc);
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
writeHex(TUYA_STOP);
|
||||||
|
writeByte(crc);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
mqttClient.publish("/home/midroom/curtcmd", 0, false, "0");
|
||||||
}
|
}
|
||||||
else if(strcmp("/home/midroom/curtpos_set", topic) == 0){
|
else if(strcmp("/home/midroom/curtpos_set", topic) == 0){
|
||||||
|
int pos = atoi(payload);
|
||||||
|
crc = 0;
|
||||||
|
if(pos == 100){
|
||||||
|
writeHex(TUYA_CLOSE);
|
||||||
|
writeByte(crc);
|
||||||
|
}
|
||||||
|
else if(pos == 0){
|
||||||
|
writeHex(TUYA_OPEN);
|
||||||
|
writeByte(crc);
|
||||||
|
}
|
||||||
|
else if (pos > 0 && pos < 100){
|
||||||
|
writeHex(TUYA_SET_POSITION);
|
||||||
|
writeByte(100 - pos);
|
||||||
|
writeByte(crc);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -194,7 +227,7 @@ void readByte(uint8_t data) {
|
|||||||
if (sum == crc) {
|
if (sum == crc) {
|
||||||
uint8_t cmd_type = buffer[3];
|
uint8_t cmd_type = buffer[3];
|
||||||
uint8_t data_id = buffer[6];
|
uint8_t data_id = buffer[6];
|
||||||
uint8_t data_size = buffer[9]; // only 1byte or 4bytes values
|
//uint8_t data_size = buffer[9]; // only 1byte or 4bytes values
|
||||||
switch (cmd_type) {
|
switch (cmd_type) {
|
||||||
case 0x07: {
|
case 0x07: {
|
||||||
switch (data_id) {
|
switch (data_id) {
|
||||||
@@ -248,12 +281,12 @@ void readByte(uint8_t data) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void writeByte(uint8_t data) {
|
void writeByte(uint8_t data) {
|
||||||
Serial.write(data);
|
softSer.write(data);
|
||||||
crc += data;
|
crc += data;
|
||||||
}
|
}
|
||||||
|
|
||||||
void writeHex(std::string data) {
|
void writeHex(std::string data) {
|
||||||
int i = 0;
|
uint16_t i = 0;
|
||||||
while (i < data.length()) {
|
while (i < data.length()) {
|
||||||
const char hex[2] = {data[i++], data[i++]};
|
const char hex[2] = {data[i++], data[i++]};
|
||||||
uint8_t d = strtoul(hex, NULL, 16);
|
uint8_t d = strtoul(hex, NULL, 16);
|
||||||
|
|||||||
Reference in New Issue
Block a user