From ea5e04d62a0b0eedf74a3b38ab188ebec65f9e64 Mon Sep 17 00:00:00 2001 From: strawmanbobi Date: Fri, 11 Feb 2022 15:06:16 +0800 Subject: [PATCH] implemented credential config via WifiManager --- .vscode/settings.json | 13 +++ platformio.ini | 6 +- src/IRBabyIRIS.cpp | 4 +- src/IRbaby.cpp | 79 +++++++++-------- src/IRbabyAlink.cpp | 2 +- src/IRbabyAlink.h | 2 +- src/IRbabyGlobal.cpp | 5 +- src/IRbabyGlobal.h | 4 +- src/IRbabyHttp.cpp | 106 +++++++++++++++++++++++ src/{IRbabyUDP.h => IRbabyHttp.h} | 29 +++---- src/IRbabyIR.cpp | 50 +++-------- src/IRbabyIR.h | 11 +-- src/IRbabyIRIS.h | 4 +- src/IRbabyMQTT.cpp | 136 ------------------------------ src/IRbabyMQTT.h | 52 ------------ src/IRbabyMsgHandler.cpp | 25 +++--- src/IRbabyMsgHandler.h | 2 +- src/IRbabyOTA.cpp | 2 +- src/IRbabyOTA.h | 2 +- src/IRbabyRF.cpp | 7 +- src/IRbabyRF.h | 2 +- src/IRbabySerial.h | 2 +- src/IRbabyUDP.cpp | 71 ---------------- src/IRbabyUserSettings.cpp | 3 +- src/IRbabyUserSettings.h | 4 +- src/IRbabyha.cpp | 20 ++--- src/IRbabyha.h | 4 +- src/defines.h | 2 +- 28 files changed, 238 insertions(+), 411 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 src/IRbabyHttp.cpp rename src/{IRbabyUDP.h => IRbabyHttp.h} (73%) delete mode 100644 src/IRbabyMQTT.cpp delete mode 100644 src/IRbabyMQTT.h delete mode 100644 src/IRbabyUDP.cpp diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..89f6be4 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,13 @@ +{ + "files.associations": { + "array": "cpp", + "deque": "cpp", + "list": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "string_view": "cpp", + "initializer_list": "cpp", + "ranges": "cpp" + } +} \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index 5be51ca..36618b9 100644 --- a/platformio.ini +++ b/platformio.ini @@ -11,14 +11,15 @@ [common] board_1m = esp01 ldscript_1m = eagle.flash.1m192.ld +workspace = D:/Project/irext/iris-kit [env] platform = espressif8266 framework = arduino monitor_speed = 115200 upload_speed = 115200 -monitor_port = COM7 -upload_port = COM7 +upload_port = COM5 +monitor_port = COM5 board_build.flash_mode = dout build_flags = -Wno-unused-function @@ -26,6 +27,7 @@ build_flags = -DLOG_DEBUG=1 -DLOG_INFO=1 -DLOG_ERROR=1 + -I${common.workspace}/lib/IRext/include [env:esp8266-1m-base] board = ${common.board_1m} diff --git a/src/IRBabyIRIS.cpp b/src/IRBabyIRIS.cpp index 98fcd63..fda2206 100644 --- a/src/IRBabyIRIS.cpp +++ b/src/IRBabyIRIS.cpp @@ -1,6 +1,6 @@ /** * - * Copyright (c) 2020-2021 IRbaby-IRext + * Copyright (c) 2020-2022 IRbaby-IRext * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -31,6 +31,8 @@ #include "defines.h" #include "IRBabyIRIS.h" +char iris_credential_token[CREDENTIAL_MAX] = { 0 }; + int getIRISKitVersion(char *buffer, int buffer_size) { if (NULL == buffer) { return -1; diff --git a/src/IRbaby.cpp b/src/IRbaby.cpp index c5b51ca..4566a31 100644 --- a/src/IRbaby.cpp +++ b/src/IRbaby.cpp @@ -1,6 +1,6 @@ /** * - * Copyright (c) 2020-2021 IRbaby-IRext + * Copyright (c) 2020-2022 IRbaby-IRext * * Author: Strawmanbobi and Caffreyfans * @@ -29,7 +29,6 @@ #include "defines.h" #include "IRbabyIR.h" -#include "IRbabyUDP.h" #include "IRbabyOTA.h" #if defined USE_IRBABY_MQTT @@ -41,8 +40,14 @@ #include "IRbabyMsgHandler.h" #include "IRbabyGlobal.h" #include "IRbabyUserSettings.h" +#include "IRbabyHttp.h" +#include "IRbabyIRIS.h" #include "IRbabyRF.h" + +extern char iris_server_address[]; +extern char iris_credential_token[]; + void uploadIP(); // device info upload to devicehive void IRAM_ATTR resetHandle(); // interrupt handle @@ -82,33 +87,49 @@ void setup() { INFOLN("== IRIS Kit [1.2.7] Powered by IRBaby =="); // custom parameter for iris credentials - char iris_credential[40] = { 0 }; - WiFiManagerParameter irisCredential("server", "Credential", iris_credential, 40); - wifi_manager.addParameter(&irisCredential); - wifi_manager.autoConnect(); - strcpy(iris_credential, irisCredential.getValue()); - INFOF("get iris credential : %s\n", iris_credential); + WiFiManagerParameter server_address("server", "Server", "http://192.168.2.31:8081", URL_SHORT_MAX); + WiFiManagerParameter credential_token("credential", "Credential", "", CREDENTIAL_MAX); + + wifi_manager.addParameter(&server_address); + wifi_manager.addParameter(&credential_token); + + wifi_manager.autoConnect(); + + memset(iris_server_address, 0, URL_SHORT_MAX); + strcpy(iris_server_address, server_address.getValue()); + + memset(iris_credential_token, 0, CREDENTIAL_MAX); + strcpy(iris_credential_token, credential_token.getValue()); + + INFOF("Wifi Connected, IRIS server = %s, credential token = %s\n", + iris_server_address, iris_credential_token); + + do { + if(WiFi.status()== WL_CONNECTED) { + if (0 == fetchIrisCredential(iris_credential_token)) { + break; + } + } else { + delay(1000); + } + } while (1); + + INFOF("credential matched : %s\n", iris_credential_token); settingsLoad(); // load user settings form fs delay(5); - udpInit(); connectToAliyunIoT(); #ifdef USE_RF initRF(); // RF init #endif loadIRPin(ConfigData["pin"]["ir_send"], ConfigData["pin"]["ir_receive"]); -#ifdef USE_INFO_UPLOAD - uploadIP(); -#endif - #if defined USE_IRBABY_MQTT mqttCheckTask.attach_scheduled(MQTT_CHECK_INTERVALS, mqttCheck); #else alinkCheckTask.attach_scheduled(MQTT_CHECK_INTERVALS, checkAlinkMQTT); #endif - disableIRTask.attach_scheduled(DISABLE_SIGNAL_INTERVALS, disableIR); disableRFTask.attach_scheduled(DISABLE_SIGNAL_INTERVALS, disableRF); saveDataTask.attach_scheduled(SAVE_DATA_INTERVALS, settingsSave); @@ -121,6 +142,8 @@ void loop() { /* RF receive */ recvRF(); #endif + +#if 0 /* UDP receive and handle */ char *msg = udpRecive(); if (msg) { @@ -131,6 +154,7 @@ void loop() { } msgHandle(&udp_msg_doc, MsgType::udp); } +#endif #if defined USE_IRBABY_MQTT /* MQTT loop */ @@ -152,31 +176,4 @@ void resetHandle() { if (end_time - start_time > 3000) { settingsClear(); } -} - -// only upload chip id -void uploadIP() { - HTTPClient http; - StaticJsonDocument<128> body_json; - String chip_id = String(ESP.getChipId(), HEX); - chip_id.toUpperCase(); - String head = "http://playground.devicehive.com/api/rest/device/"; - head += chip_id; - http.begin(wifi_client, head); - http.addHeader("Content-Type", "application/json"); - http.addHeader("Authorization", - "Bearer eyJhbGciOiJIUzI1NiJ9.eyJ" - "wYXlsb2FkIjp7ImEiOlsyLDMsNCw1LD" - "YsNyw4LDksMTAsMTEsMTIsMTUsMTYsM" - "TddLCJlIjoxNzQzNDM2ODAwMDAwLCJ0" - "IjoxLCJ1Ijo2NjM1LCJuIjpbIjY1NDI" - "iXSwiZHQiOlsiKiJdfX0.WyyxNr2OD5" - "pvBSxMq84NZh6TkNnFZe_PXenkrUkRS" - "iw"); - body_json["name"] = chip_id; - body_json["networkId"] = "6542"; - String body = body_json.as(); - INFOF("update %s to devicehive\n", body.c_str()); - http.PUT(body); - http.end(); } \ No newline at end of file diff --git a/src/IRbabyAlink.cpp b/src/IRbabyAlink.cpp index 98165a8..3dca057 100644 --- a/src/IRbabyAlink.cpp +++ b/src/IRbabyAlink.cpp @@ -1,6 +1,6 @@ /** * - * Copyright (c) 2020-2021 IRbaby-IRext + * Copyright (c) 2020-2022 IRbaby-IRext * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal diff --git a/src/IRbabyAlink.h b/src/IRbabyAlink.h index 2d943ab..fd45434 100644 --- a/src/IRbabyAlink.h +++ b/src/IRbabyAlink.h @@ -1,6 +1,6 @@ /** * - * Copyright (c) 2020-2021 IRbaby-IRext + * Copyright (c) 2020-2022 IRbaby-IRext * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal diff --git a/src/IRbabyGlobal.cpp b/src/IRbabyGlobal.cpp index a485b28..f856646 100644 --- a/src/IRbabyGlobal.cpp +++ b/src/IRbabyGlobal.cpp @@ -1,6 +1,6 @@ /** * - * Copyright (c) 2020-2021 IRbaby-IRext + * Copyright (c) 2020-2022 IRbaby-IRext * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -26,8 +26,7 @@ StaticJsonDocument<1024> recv_msg_doc; StaticJsonDocument<1024> send_msg_doc; -StaticJsonDocument<1024> udp_msg_doc; -StaticJsonDocument<1024> mqtt_msg_doc; +StaticJsonDocument<1024> http_json_doc; WiFiManager wifi_manager; WiFiClient wifi_client; diff --git a/src/IRbabyGlobal.h b/src/IRbabyGlobal.h index 1aba11a..17c1d04 100644 --- a/src/IRbabyGlobal.h +++ b/src/IRbabyGlobal.h @@ -1,6 +1,6 @@ /** * - * Copyright (c) 2020-2021 IRbaby-IRext + * Copyright (c) 2020-2022 IRbaby-IRext * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -31,8 +31,6 @@ /* goable json variable */ extern StaticJsonDocument<1024> recv_msg_doc; extern StaticJsonDocument<1024> send_msg_doc; -extern StaticJsonDocument<1024> udp_msg_doc; -extern StaticJsonDocument<1024> mqtt_msg_doc; extern WiFiManager wifi_manager; extern WiFiClient wifi_client; diff --git a/src/IRbabyHttp.cpp b/src/IRbabyHttp.cpp new file mode 100644 index 0000000..fb99d1b --- /dev/null +++ b/src/IRbabyHttp.cpp @@ -0,0 +1,106 @@ +/** + * + * Copyright (c) 2020-2022 IRbaby-IRext + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include +#include +#include + +#include +#include + +#include "defines.h" +#include "IRbabyGlobal.h" +#include "IRbabySerial.h" + +#include "IRbabyHttp.h" + +#define FETCH_CREDENTIAL_SUFFIX "/irext-collect/credentials/fetch_credential" +#define DOWNLOAD_PREFIX "http://irext-debug.oss-cn-hangzhou.aliyuncs.com/irda_" +#define DOWNLOAD_SUFFIX ".bin" + + +extern StaticJsonDocument<1024> http_json_doc; + + +char iris_server_address[URL_SHORT_MAX] = { 0 }; + + +int fetchIrisCredential(String credential_token) { + int ret = -1; + + HTTPClient http_client; + String fetch_credential_url(iris_server_address); + int response_code = 0; + fetch_credential_url.concat(String(FETCH_CREDENTIAL_SUFFIX)); + + INFOF("fetch credential URL = %s\n", fetch_credential_url.c_str()); + + http_client.begin(wifi_client, fetch_credential_url); + http_client.addHeader("Content-Type", "application/json"); + http_json_doc.clear(); + http_json_doc["endpointSN"] = String(ESP.getChipId(), HEX); + http_json_doc["credentialToken"] = credential_token; + String request_data = ""; + serializeJson(http_json_doc, request_data); + + response_code = http_client.POST(request_data); + if (response_code > 0) { + INFOF("HTTP response code: %d\n", response_code); + String payload = http_client.getString(); + INFOF("HTTP response payload = %s\n", payload.c_str()); + ret = 0; + } + + http_client.end(); + + return ret; +} + + +void downLoadFile(String file, String path) { + HTTPClient http_client; + String download_url = DOWNLOAD_PREFIX + file + DOWNLOAD_SUFFIX; + String save_path = path + file; + File cache = LittleFS.open(save_path, "w"); + bool download_flag = false; + if (cache) { + http_client.begin(wifi_client, download_url); + for (int i = 0; i < 5; i++) { + if (http_client.GET() == HTTP_CODE_OK) { + download_flag = true; + break; + } + delay(200); + } + if (download_flag) { + http_client.writeToStream(&cache); + DEBUGF("Download %s success\n", file.c_str()); + } else { + LittleFS.remove(save_path); + ERRORF("Download %s failed\n", file.c_str()); + } + } else + ERRORLN("Don't have enough file zoom"); + cache.close(); + http_client.end(); +} diff --git a/src/IRbabyUDP.h b/src/IRbabyHttp.h similarity index 73% rename from src/IRbabyUDP.h rename to src/IRbabyHttp.h index 3879c4e..0a28902 100644 --- a/src/IRbabyUDP.h +++ b/src/IRbabyHttp.h @@ -1,17 +1,17 @@ /** * - * Copyright (c) 2020-2021 IRbaby-IRext - * + * Copyright (c) 2020-2022 IRbaby-IRext + * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: - * + * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. - * + * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE @@ -21,20 +21,17 @@ * SOFTWARE. */ -#ifndef IRBABY_UDP_H -#define IRBABY_UDP_H +#ifndef IRBABY_HTTP_H +#define IRBABY_HTTP_H -#include "ArduinoJson.h" -#include "ESP8266WiFi.h" +#include -#define UDP_PORT 4210 -#define UDP_PACKET_SIZE 255 +#define URL_SHORT_MAX (128) -void udpInit(); -char* udpRecive(); -uint32_t sendUDP(StaticJsonDocument<1024>* doc, IPAddress ip); -uint32_t returnUDP(StaticJsonDocument<1024>* doc); -extern IPAddress remote_ip; +int fetchIrisCredential(String credential_token); -#endif // IRBABY_UDP_H \ No newline at end of file +void downLoadFile(String file, String path); + + +#endif // IRBABY_HTTP_H \ No newline at end of file diff --git a/src/IRbabyIR.cpp b/src/IRbabyIR.cpp index 9d9936c..883c136 100644 --- a/src/IRbabyIR.cpp +++ b/src/IRbabyIR.cpp @@ -1,6 +1,6 @@ /** * - * Copyright (c) 2020-2021 IRbaby-IRext + * Copyright (c) 2020-2022 IRbaby-IRext * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -23,17 +23,16 @@ #include -#include "IRbabyIR.h" -#include "ESP8266HTTPClient.h" -#include "IRbabySerial.h" -#include "IRbabyUDP.h" -#include "IRbabyUserSettings.h" -#include "IRbabyGlobal.h" #include "defines.h" -#define DOWNLOAD_PREFIX "http://irext-debug.oss-cn-hangzhou.aliyuncs.com/irda_" -#define DOWNLOAD_SUFFIX ".bin" -#define SAVE_PATH "/bin/" +#include "IRbabyGlobal.h" +#include "IRbabySerial.h" +#include "IRbabyUserSettings.h" +#include "IRbabyHttp.h" + +#include "IRbabyIR.h" + +#define SAVE_PATH "/bin/" decode_results results; // Somewhere to store the results const uint8_t kTimeout = 50; @@ -44,33 +43,6 @@ static IRsend * ir_send = nullptr; static IRrecv * ir_recv = nullptr; bool saveSignal(); -void downLoadFile(String file) { - HTTPClient http_client; - String download_url = DOWNLOAD_PREFIX + file + DOWNLOAD_SUFFIX; - String save_path = SAVE_PATH + file; - File cache = LittleFS.open(save_path, "w"); - bool download_flag = false; - if (cache) { - http_client.begin(wifi_client, download_url); - for (int i = 0; i < 5; i++) { - if (http_client.GET() == HTTP_CODE_OK) { - download_flag = true; - break; - } - delay(200); - } - if (download_flag) { - http_client.writeToStream(&cache); - DEBUGF("Download %s success\n", file.c_str()); - } else { - LittleFS.remove(save_path); - ERRORF("Download %s failed\n", file.c_str()); - } - } else - ERRORLN("Don't have enough file zoom"); - cache.close(); - http_client.end(); -} bool sendIR(String file_name) { String save_path = SAVE_PATH + file_name; @@ -100,7 +72,7 @@ bool sendIR(String file_name) { void sendStatus(String file, t_remote_ac_status status) { String save_path = SAVE_PATH + file; if (!LittleFS.exists(save_path)) { - downLoadFile(file); + downLoadFile(file, SAVE_PATH); } if (LittleFS.exists(save_path)) { @@ -152,7 +124,7 @@ void recvIR() { send_msg_doc["params"]["length"] = results.rawlen; send_msg_doc["params"]["value"] = raw_data.c_str(); DEBUGLN(raw_data.c_str()); - sendUDP(&send_msg_doc, remote_ip); + // sendUDP(&send_msg_doc, remote_ip); saveSignal(); } } diff --git a/src/IRbabyIR.h b/src/IRbabyIR.h index 345475a..c4af6fa 100644 --- a/src/IRbabyIR.h +++ b/src/IRbabyIR.h @@ -1,6 +1,6 @@ /** * - * Copyright (c) 2020-2021 IRbaby-IRext + * Copyright (c) 2020-2022 IRbaby-IRext * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -21,13 +21,13 @@ * SOFTWARE. */ -#ifndef IRBABYIR_H -#define IRBABYIR_H +#ifndef IRBABY_IR_H +#define IRBABY_IR_H #include #include #include -#include "../lib/Irext/include/ir_decode.h" +#include "ir_decode.h" void loadIRPin(uint8_t send_pin, uint8_t recv_pin); void enableIR(); @@ -38,4 +38,5 @@ bool sendIR(String file_name); void recvIR(); bool saveIR(String file_name); void initAC(String); -#endif // IRBABAYIR_H \ No newline at end of file + +#endif // IRBABY_IR_H \ No newline at end of file diff --git a/src/IRbabyIRIS.h b/src/IRbabyIRIS.h index 50a92fb..136686b 100644 --- a/src/IRbabyIRIS.h +++ b/src/IRbabyIRIS.h @@ -1,6 +1,6 @@ /** * - * Copyright (c) 2020-2021 IRbaby-IRext + * Copyright (c) 2020-2022 IRbaby-IRext * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -24,6 +24,8 @@ #ifndef IRBABY_IRIS_H #define IRBABY_IRIS_H +#define CREDENTIAL_MAX (40) + int getIRISKitVersion(char *buffer, int buffer_size); int getDeviceID(char* buffer, int buffer_size); diff --git a/src/IRbabyMQTT.cpp b/src/IRbabyMQTT.cpp deleted file mode 100644 index 1857399..0000000 --- a/src/IRbabyMQTT.cpp +++ /dev/null @@ -1,136 +0,0 @@ -/** - * - * Copyright (c) 2020-2021 IRbaby-IRext - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -#include "IRbabyMQTT.h" -#include "PubSubClient.h" -#include "IRbabySerial.h" -#include "ArduinoJson.h" -#include "IRbabyUserSettings.h" -#include "IRbabyMsgHandler.h" -#include "IRbabyGlobal.h" -PubSubClient mqtt_client(wifi_client); - -void callback(char *topic, byte *payload, unsigned int length); - -void mqttInit() { - INFOLN("MQTT Init"); - mqttReconnect(); - mqtt_client.setCallback(callback); -} - -/************************************** - * sub example: /IRbaby/chip_id/set/# - **************************************/ -bool mqttReconnect() { - bool flag = false; - if (ConfigData.containsKey("mqtt")) { - JsonObject mqtt_obj = ConfigData["mqtt"]; - const char *host = mqtt_obj["host"]; - int port = mqtt_obj["port"]; - const char *user = mqtt_obj["user"]; - const char *password = mqtt_obj["password"]; - if (host && port) { - mqtt_client.setServer(host, port); - String chip_id = String(ESP.getChipId(), HEX); - chip_id.toUpperCase(); - DEBUGF("Trying to connect %s:%d\n", host, port); - if (mqtt_client.connect(chip_id.c_str(), user, - password)) { - String sub_topic = String("/IRbaby/") + - chip_id + String("/send/#"); - DEBUGF("MQTT subscribe %s\n", sub_topic.c_str()); - mqtt_client.subscribe(sub_topic.c_str()); - flag = true; - } - } - INFOF("MQTT state rc = %d\n", mqtt_client.state()); - } - delay(1000); - return flag; -} - -void mqttDisconnect() { - mqtt_client.disconnect(); -} - -void callback(char *topic, byte *payload, unsigned int length) { - mqtt_msg_doc.clear(); - - String payload_str = ""; - for (uint32_t i = 0; i < length; i++) - payload_str += (char)payload[i]; - String topic_str(topic); - uint8_t index = 0; - String option; - String func; - do { - int divsion = topic_str.lastIndexOf("/"); - String tmp = topic_str.substring(divsion + 1, -1); - topic_str = topic_str.substring(0, divsion); - switch (index++) { - case 0: - func = tmp; - break; - case 1: - mqtt_msg_doc["params"]["file"] = tmp; - break; - case 2: - mqtt_msg_doc["params"]["type"] = tmp; - option = tmp; - break; - case 3: - mqtt_msg_doc["params"]["signal"] = tmp; - break; - case 4: - mqtt_msg_doc["cmd"] = tmp; - default: - break; - } - } while (topic_str.lastIndexOf("/") > 0); - mqtt_msg_doc["params"][option][func] = payload_str; - msgHandle(&mqtt_msg_doc, MsgType::mqtt); -} - -bool mqttConnected() { - return mqtt_client.connected(); -} - -void mqttLoop() { - mqtt_client.loop(); -} - -void mqttPublish(String topic, String payload) { - mqtt_client.publish(topic.c_str(), payload.c_str()); -} - -void mqttPublishRetained(String topic, String payload) { - mqtt_client.publish(topic.c_str(), payload.c_str(), true); -} - -void mqttCheck() { - if (!mqttConnected()) { - DEBUGLN("MQTT disconnect, try to reconnect"); - mqtt_client.disconnect(); - mqttReconnect(); - } -} \ No newline at end of file diff --git a/src/IRbabyMQTT.h b/src/IRbabyMQTT.h deleted file mode 100644 index 9531e9e..0000000 --- a/src/IRbabyMQTT.h +++ /dev/null @@ -1,52 +0,0 @@ -/** - * - * Copyright (c) 2020-2021 IRbaby-IRext - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -#ifndef IRBABY_MQTT_H -#define IRBABY_MQTT_H - -#include -/* MQTT init */ -void mqttInit(); - -/* MQTT reconnect */ -bool mqttReconnect(); - -/* MQTT disconnect */ -void mqttDisconnect(); - -/* MQTT connected */ -bool mqttConnected(); - -/* MQTT loop */ -void mqttLoop(); - -/* MQTT publish */ -void mqttPublish(String topic, String payload); - -/* MQTT receive */ -void mqttPublishRetained(String topic, String payload); - -/* MQTT heartbeat */ -void mqttCheck(); - -#endif // IRBABY_MQTT_H \ No newline at end of file diff --git a/src/IRbabyMsgHandler.cpp b/src/IRbabyMsgHandler.cpp index 5b37e55..6302721 100644 --- a/src/IRbabyMsgHandler.cpp +++ b/src/IRbabyMsgHandler.cpp @@ -1,6 +1,6 @@ /** * - * Copyright (c) 2020-2021 IRbaby-IRext + * Copyright (c) 2020-2022 IRbaby-IRext * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -21,21 +21,22 @@ * SOFTWARE. */ +#include + +#include "defines.h" + #include "IRbabyMsgHandler.h" #include "IRbabySerial.h" #include "IRbabyUserSettings.h" -#include "IRbabyMQTT.h" -#include "IRbabyUDP.h" #include "ESP8266WiFi.h" #include "IRbabyIR.h" -#include "../lib/Irext/include/ir_ac_control.h" #include "IRbabyOTA.h" -#include "defines.h" -#include #include "IRbabyRF.h" #include "IRbabyGlobal.h" #include "IRbabyha.h" +#include "ir_ac_control.h" + bool msgHandle(StaticJsonDocument<1024> *p_recv_msg_doc, MsgType msg_type) { if (LOG_DEBUG) { serializeJsonPretty(*p_recv_msg_doc, Serial); @@ -68,8 +69,8 @@ bool msgHandle(StaticJsonDocument<1024> *p_recv_msg_doc, MsgType msg_type) { } String ip = obj["params"]["ip"]; - remote_ip.fromString(ip); - sendUDP(&send_msg_doc, remote_ip); + // remote_ip.fromString(ip); + // sendUDP(&send_msg_doc, remote_ip); } else if (type.equals("info")) { String free_mem = String(ESP.getFreeHeap() / 1024) + "KB"; String chip_id = String(ESP.getChipId(), HEX); @@ -96,7 +97,7 @@ bool msgHandle(StaticJsonDocument<1024> *p_recv_msg_doc, MsgType msg_type) { send_msg_doc["params"]["fs_used_bytes"] = fs_used_bytes; send_msg_doc["params"]["version_name"] = FIRMWARE_VERSION; send_msg_doc["params"]["version_code"] = VERSION_CODE; - returnUDP(&send_msg_doc); + // returnUDP(&send_msg_doc); } } @@ -210,7 +211,7 @@ bool msgHandle(StaticJsonDocument<1024> *p_recv_msg_doc, MsgType msg_type) { else if (type.equals("record")) { String ip = params["ip"]; - remote_ip.fromString(ip); + // remote_ip.fromString(ip); DEBUGLN("start record"); enableIR(); enableRF(); @@ -247,10 +248,8 @@ bool msgHandle(StaticJsonDocument<1024> *p_recv_msg_doc, MsgType msg_type) { } send_msg_doc["cmd"] = "return"; send_msg_doc["params"]["message"] = "set success"; - returnUDP(&send_msg_doc); + // returnUDP(&send_msg_doc); settingsSave(); - mqttDisconnect(); - mqttReconnect(); loadIRPin(ConfigData["pin"]["ir_send"], ConfigData["pin"]["ir_receive"]); } diff --git a/src/IRbabyMsgHandler.h b/src/IRbabyMsgHandler.h index c631c76..387f383 100644 --- a/src/IRbabyMsgHandler.h +++ b/src/IRbabyMsgHandler.h @@ -1,6 +1,6 @@ /** * - * Copyright (c) 2020-2021 IRbaby-IRext + * Copyright (c) 2020-2022 IRbaby-IRext * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal diff --git a/src/IRbabyOTA.cpp b/src/IRbabyOTA.cpp index 4cb0502..a66b917 100644 --- a/src/IRbabyOTA.cpp +++ b/src/IRbabyOTA.cpp @@ -1,6 +1,6 @@ /** * - * Copyright (c) 2020-2021 IRbaby-IRext + * Copyright (c) 2020-2022 IRbaby-IRext * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal diff --git a/src/IRbabyOTA.h b/src/IRbabyOTA.h index bec8aa6..c5c6504 100644 --- a/src/IRbabyOTA.h +++ b/src/IRbabyOTA.h @@ -1,6 +1,6 @@ /** * - * Copyright (c) 2020-2021 IRbaby-IRext + * Copyright (c) 2020-2022 IRbaby-IRext * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal diff --git a/src/IRbabyRF.cpp b/src/IRbabyRF.cpp index f96ec53..200374c 100644 --- a/src/IRbabyRF.cpp +++ b/src/IRbabyRF.cpp @@ -1,6 +1,6 @@ /** * - * Copyright (c) 2020-2021 IRbaby-IRext + * Copyright (c) 2020-2022 IRbaby-IRext * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -25,7 +25,6 @@ #include #include "IRbabySerial.h" #include "IRbabyGlobal.h" -#include "IRbabyUDP.h" #include "defines.h" #include RCSwitch rf315; @@ -113,7 +112,7 @@ void recvRF(void) { send_msg_doc["cmd"] = "record_rt"; send_msg_doc["params"]["signal"] = "RF433"; send_msg_doc["params"]["value"] = String(code_tmp); - sendUDP(&send_msg_doc, remote_ip); + // sendUDP(&send_msg_doc, remote_ip); serializeJsonPretty(send_msg_doc, Serial); } @@ -126,7 +125,7 @@ void recvRF(void) { send_msg_doc["cmd"] = "record_rt"; send_msg_doc["params"]["signal"] = "RF315"; send_msg_doc["params"]["value"] = String(code_tmp); - sendUDP(&send_msg_doc, remote_ip); + // sendUDP(&send_msg_doc, remote_ip); serializeJsonPretty(send_msg_doc, Serial); } } diff --git a/src/IRbabyRF.h b/src/IRbabyRF.h index 8640d77..09997d5 100644 --- a/src/IRbabyRF.h +++ b/src/IRbabyRF.h @@ -1,6 +1,6 @@ /** * - * Copyright (c) 2020-2021 IRbaby-IRext + * Copyright (c) 2020-2022 IRbaby-IRext * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal diff --git a/src/IRbabySerial.h b/src/IRbabySerial.h index 417a19a..5661355 100644 --- a/src/IRbabySerial.h +++ b/src/IRbabySerial.h @@ -1,6 +1,6 @@ /** * - * Copyright (c) 2020-2021 IRbaby-IRext + * Copyright (c) 2020-2022 IRbaby-IRext * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal diff --git a/src/IRbabyUDP.cpp b/src/IRbabyUDP.cpp deleted file mode 100644 index 28b1b85..0000000 --- a/src/IRbabyUDP.cpp +++ /dev/null @@ -1,71 +0,0 @@ -/** - * - * Copyright (c) 2020-2021 IRbaby-IRext - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -#include -#include -#include "IRbabyUDP.h" -#include "IRbabySerial.h" - -WiFiUDP UDP; -IPAddress remote_ip; - -char incomingPacket[UDP_PACKET_SIZE]; // buffer for incoming packets - -void udpInit() -{ - UDP.begin(UDP_PORT); - DEBUGF("Now listening at IP %s, UDP port %d\n", WiFi.localIP().toString().c_str(), UDP_PORT); -} - -char *udpRecive() -{ - int packetSize = UDP.parsePacket(); - if (packetSize) - { - /* receive incoming UDP packets */ - DEBUGF("Received %d bytes from %s, port %d\n", packetSize, UDP.remoteIP().toString().c_str(), UDP.remotePort()); - int len = UDP.read(incomingPacket, 255); - if (len > 0) - { - incomingPacket[len] = 0; - } - return incomingPacket; - } - return nullptr; -} - -uint32_t sendUDP(StaticJsonDocument<1024>* doc, IPAddress ip) -{ - DEBUGF("return message %s\n", ip.toString().c_str()); - UDP.beginPacket(ip, UDP_PORT); - serializeJson(*doc, UDP); - return UDP.endPacket(); -} - -uint32_t returnUDP(StaticJsonDocument<1024>* doc) -{ - DEBUGF("return message to %s\n", UDP.remoteIP().toString().c_str()); - UDP.beginPacket(UDP.remoteIP(), UDP_PORT); - serializeJson(*doc, UDP); - return UDP.endPacket(); -} \ No newline at end of file diff --git a/src/IRbabyUserSettings.cpp b/src/IRbabyUserSettings.cpp index 7e93f01..f03af0c 100644 --- a/src/IRbabyUserSettings.cpp +++ b/src/IRbabyUserSettings.cpp @@ -1,6 +1,6 @@ /** * - * Copyright (c) 2020-2021 IRbaby-IRext + * Copyright (c) 2020-2022 IRbaby-IRext * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -25,7 +25,6 @@ #include "IRbabyUserSettings.h" #include "IRbabySerial.h" -#include "IRbabyMQTT.h" #include "WiFiManager.h" #include "IRbabyGlobal.h" #include "IRbabyIR.h" diff --git a/src/IRbabyUserSettings.h b/src/IRbabyUserSettings.h index 3c51005..58a09aa 100644 --- a/src/IRbabyUserSettings.h +++ b/src/IRbabyUserSettings.h @@ -1,6 +1,6 @@ /** * - * Copyright (c) 2020-2021 IRbaby-IRext + * Copyright (c) 2020-2022 IRbaby-IRext * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -25,7 +25,7 @@ #define IRBABY_USER_SETTINGS_H #include -#include "../lib/Irext/include/ir_ac_control.h" +#include "ir_ac_control.h" /* save settings */ bool settingsSave(); diff --git a/src/IRbabyha.cpp b/src/IRbabyha.cpp index 7e11aad..75a196b 100644 --- a/src/IRbabyha.cpp +++ b/src/IRbabyha.cpp @@ -1,6 +1,6 @@ /** * - * Copyright (c) 2020-2021 IRbaby-IRext + * Copyright (c) 2020-2022 IRbaby-IRext * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -23,7 +23,6 @@ #include "IRbabyha.h" #include "IRbabyGlobal.h" -#include "IRbabyMQTT.h" #include "IRbabySerial.h" void returnACStatus(String filename, t_remote_ac_status ac_status) { @@ -34,13 +33,14 @@ void returnACStatus(String filename, t_remote_ac_status ac_status) { const char* mode[] = {"cool", "heat", "auto", "fan_only", "dry"}; const char* fan[] = {"auto", "low", "medium", "high", "max"}; const char* swing[] = {"on", "off"}; - if (ac_status.ac_power == AC_POWER_OFF) - mqttPublish(topic_head + "mode", "off"); - else - mqttPublish(topic_head + "mode", mode[(int)ac_status.ac_mode]); - mqttPublish(topic_head + "temperature", String((int)ac_status.ac_temp + 16)); - mqttPublish(topic_head + "fan", fan[(int)ac_status.ac_wind_speed]); - mqttPublish(topic_head + "swing", swing[(int)ac_status.ac_swing]); + if (ac_status.ac_power == AC_POWER_OFF) { + // mqttPublish(topic_head + "mode", "off"); + } else { + // mqttPublish(topic_head + "mode", mode[(int)ac_status.ac_mode]); + // mqttPublish(topic_head + "temperature", String((int)ac_status.ac_temp + 16)); + // mqttPublish(topic_head + "fan", fan[(int)ac_status.ac_wind_speed]); + // mqttPublish(topic_head + "swing", swing[(int)ac_status.ac_swing]); + } } // save current AC status @@ -99,5 +99,5 @@ void registAC(String filename, bool flag) { serializeJson(send_msg_doc, reg_content); } DEBUGLN(reg_topic_head); - mqttPublishRetained(reg_topic_head, reg_content); + // mqttPublishRetained(reg_topic_head, reg_content); } diff --git a/src/IRbabyha.h b/src/IRbabyha.h index b3f7f5d..0e7cd19 100644 --- a/src/IRbabyha.h +++ b/src/IRbabyha.h @@ -1,6 +1,6 @@ /** * - * Copyright (c) 2020-2021 IRbaby-IRext + * Copyright (c) 2020-2022 IRbaby-IRext * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -25,7 +25,7 @@ #define IRBABYHA_H #include -#include "../lib/Irext/include/ir_ac_control.h" +#include "ir_ac_control.h" void returnACStatus(String filename, t_remote_ac_status ac_status); diff --git a/src/defines.h b/src/defines.h index b361f49..5be0c6f 100644 --- a/src/defines.h +++ b/src/defines.h @@ -1,6 +1,6 @@ /** * - * Copyright (c) 2020-2021 IRbaby-IRext + * Copyright (c) 2020-2022 IRbaby-IRext * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal