implemented credential config via WifiManager

This commit is contained in:
strawmanbobi
2022-02-11 15:06:16 +08:00
parent 579a0d37b6
commit ea5e04d62a
28 changed files with 238 additions and 411 deletions

13
.vscode/settings.json vendored Normal file
View File

@@ -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"
}
}

View File

@@ -11,14 +11,15 @@
[common] [common]
board_1m = esp01 board_1m = esp01
ldscript_1m = eagle.flash.1m192.ld ldscript_1m = eagle.flash.1m192.ld
workspace = D:/Project/irext/iris-kit
[env] [env]
platform = espressif8266 platform = espressif8266
framework = arduino framework = arduino
monitor_speed = 115200 monitor_speed = 115200
upload_speed = 115200 upload_speed = 115200
monitor_port = COM7 upload_port = COM5
upload_port = COM7 monitor_port = COM5
board_build.flash_mode = dout board_build.flash_mode = dout
build_flags = build_flags =
-Wno-unused-function -Wno-unused-function
@@ -26,6 +27,7 @@ build_flags =
-DLOG_DEBUG=1 -DLOG_DEBUG=1
-DLOG_INFO=1 -DLOG_INFO=1
-DLOG_ERROR=1 -DLOG_ERROR=1
-I${common.workspace}/lib/IRext/include
[env:esp8266-1m-base] [env:esp8266-1m-base]
board = ${common.board_1m} board = ${common.board_1m}

View File

@@ -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 * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
@@ -31,6 +31,8 @@
#include "defines.h" #include "defines.h"
#include "IRBabyIRIS.h" #include "IRBabyIRIS.h"
char iris_credential_token[CREDENTIAL_MAX] = { 0 };
int getIRISKitVersion(char *buffer, int buffer_size) { int getIRISKitVersion(char *buffer, int buffer_size) {
if (NULL == buffer) { if (NULL == buffer) {
return -1; return -1;

View File

@@ -1,6 +1,6 @@
/** /**
* *
* Copyright (c) 2020-2021 IRbaby-IRext * Copyright (c) 2020-2022 IRbaby-IRext
* *
* Author: Strawmanbobi and Caffreyfans * Author: Strawmanbobi and Caffreyfans
* *
@@ -29,7 +29,6 @@
#include "defines.h" #include "defines.h"
#include "IRbabyIR.h" #include "IRbabyIR.h"
#include "IRbabyUDP.h"
#include "IRbabyOTA.h" #include "IRbabyOTA.h"
#if defined USE_IRBABY_MQTT #if defined USE_IRBABY_MQTT
@@ -41,8 +40,14 @@
#include "IRbabyMsgHandler.h" #include "IRbabyMsgHandler.h"
#include "IRbabyGlobal.h" #include "IRbabyGlobal.h"
#include "IRbabyUserSettings.h" #include "IRbabyUserSettings.h"
#include "IRbabyHttp.h"
#include "IRbabyIRIS.h"
#include "IRbabyRF.h" #include "IRbabyRF.h"
extern char iris_server_address[];
extern char iris_credential_token[];
void uploadIP(); // device info upload to devicehive void uploadIP(); // device info upload to devicehive
void IRAM_ATTR resetHandle(); // interrupt handle void IRAM_ATTR resetHandle(); // interrupt handle
@@ -82,33 +87,49 @@ void setup() {
INFOLN("== IRIS Kit [1.2.7] Powered by IRBaby =="); INFOLN("== IRIS Kit [1.2.7] Powered by IRBaby ==");
// custom parameter for iris credentials // custom parameter for iris credentials
char iris_credential[40] = { 0 };
WiFiManagerParameter irisCredential("server", "Credential", iris_credential, 40); WiFiManagerParameter server_address("server", "Server", "http://192.168.2.31:8081", URL_SHORT_MAX);
wifi_manager.addParameter(&irisCredential); WiFiManagerParameter credential_token("credential", "Credential", "", CREDENTIAL_MAX);
wifi_manager.addParameter(&server_address);
wifi_manager.addParameter(&credential_token);
wifi_manager.autoConnect(); wifi_manager.autoConnect();
strcpy(iris_credential, irisCredential.getValue()); memset(iris_server_address, 0, URL_SHORT_MAX);
INFOF("get iris credential : %s\n", iris_credential); 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 settingsLoad(); // load user settings form fs
delay(5); delay(5);
udpInit();
connectToAliyunIoT(); connectToAliyunIoT();
#ifdef USE_RF #ifdef USE_RF
initRF(); // RF init initRF(); // RF init
#endif #endif
loadIRPin(ConfigData["pin"]["ir_send"], ConfigData["pin"]["ir_receive"]); loadIRPin(ConfigData["pin"]["ir_send"], ConfigData["pin"]["ir_receive"]);
#ifdef USE_INFO_UPLOAD
uploadIP();
#endif
#if defined USE_IRBABY_MQTT #if defined USE_IRBABY_MQTT
mqttCheckTask.attach_scheduled(MQTT_CHECK_INTERVALS, mqttCheck); mqttCheckTask.attach_scheduled(MQTT_CHECK_INTERVALS, mqttCheck);
#else #else
alinkCheckTask.attach_scheduled(MQTT_CHECK_INTERVALS, checkAlinkMQTT); alinkCheckTask.attach_scheduled(MQTT_CHECK_INTERVALS, checkAlinkMQTT);
#endif #endif
disableIRTask.attach_scheduled(DISABLE_SIGNAL_INTERVALS, disableIR); disableIRTask.attach_scheduled(DISABLE_SIGNAL_INTERVALS, disableIR);
disableRFTask.attach_scheduled(DISABLE_SIGNAL_INTERVALS, disableRF); disableRFTask.attach_scheduled(DISABLE_SIGNAL_INTERVALS, disableRF);
saveDataTask.attach_scheduled(SAVE_DATA_INTERVALS, settingsSave); saveDataTask.attach_scheduled(SAVE_DATA_INTERVALS, settingsSave);
@@ -121,6 +142,8 @@ void loop() {
/* RF receive */ /* RF receive */
recvRF(); recvRF();
#endif #endif
#if 0
/* UDP receive and handle */ /* UDP receive and handle */
char *msg = udpRecive(); char *msg = udpRecive();
if (msg) { if (msg) {
@@ -131,6 +154,7 @@ void loop() {
} }
msgHandle(&udp_msg_doc, MsgType::udp); msgHandle(&udp_msg_doc, MsgType::udp);
} }
#endif
#if defined USE_IRBABY_MQTT #if defined USE_IRBABY_MQTT
/* MQTT loop */ /* MQTT loop */
@@ -153,30 +177,3 @@ void resetHandle() {
settingsClear(); 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<String>();
INFOF("update %s to devicehive\n", body.c_str());
http.PUT(body);
http.end();
}

View File

@@ -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 * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal

View File

@@ -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 * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal

View File

@@ -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 * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
@@ -26,8 +26,7 @@
StaticJsonDocument<1024> recv_msg_doc; StaticJsonDocument<1024> recv_msg_doc;
StaticJsonDocument<1024> send_msg_doc; StaticJsonDocument<1024> send_msg_doc;
StaticJsonDocument<1024> udp_msg_doc; StaticJsonDocument<1024> http_json_doc;
StaticJsonDocument<1024> mqtt_msg_doc;
WiFiManager wifi_manager; WiFiManager wifi_manager;
WiFiClient wifi_client; WiFiClient wifi_client;

View File

@@ -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 * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
@@ -31,8 +31,6 @@
/* goable json variable */ /* goable json variable */
extern StaticJsonDocument<1024> recv_msg_doc; extern StaticJsonDocument<1024> recv_msg_doc;
extern StaticJsonDocument<1024> send_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 WiFiManager wifi_manager;
extern WiFiClient wifi_client; extern WiFiClient wifi_client;

106
src/IRbabyHttp.cpp Normal file
View File

@@ -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 <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <LittleFS.h>
#include <ESP8266HTTPClient.h>
#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();
}

View File

@@ -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 * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
@@ -21,20 +21,17 @@
* SOFTWARE. * SOFTWARE.
*/ */
#ifndef IRBABY_UDP_H #ifndef IRBABY_HTTP_H
#define IRBABY_UDP_H #define IRBABY_HTTP_H
#include "ArduinoJson.h" #include <WString.h>
#include "ESP8266WiFi.h"
#define UDP_PORT 4210 #define URL_SHORT_MAX (128)
#define UDP_PACKET_SIZE 255
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 void downLoadFile(String file, String path);
#endif // IRBABY_HTTP_H

View File

@@ -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 * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
@@ -23,17 +23,16 @@
#include <LittleFS.h> #include <LittleFS.h>
#include "IRbabyIR.h"
#include "ESP8266HTTPClient.h"
#include "IRbabySerial.h"
#include "IRbabyUDP.h"
#include "IRbabyUserSettings.h"
#include "IRbabyGlobal.h"
#include "defines.h" #include "defines.h"
#define DOWNLOAD_PREFIX "http://irext-debug.oss-cn-hangzhou.aliyuncs.com/irda_" #include "IRbabyGlobal.h"
#define DOWNLOAD_SUFFIX ".bin" #include "IRbabySerial.h"
#define SAVE_PATH "/bin/" #include "IRbabyUserSettings.h"
#include "IRbabyHttp.h"
#include "IRbabyIR.h"
#define SAVE_PATH "/bin/"
decode_results results; // Somewhere to store the results decode_results results; // Somewhere to store the results
const uint8_t kTimeout = 50; const uint8_t kTimeout = 50;
@@ -44,33 +43,6 @@ static IRsend * ir_send = nullptr;
static IRrecv * ir_recv = nullptr; static IRrecv * ir_recv = nullptr;
bool saveSignal(); 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) { bool sendIR(String file_name) {
String save_path = SAVE_PATH + 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) { void sendStatus(String file, t_remote_ac_status status) {
String save_path = SAVE_PATH + file; String save_path = SAVE_PATH + file;
if (!LittleFS.exists(save_path)) { if (!LittleFS.exists(save_path)) {
downLoadFile(file); downLoadFile(file, SAVE_PATH);
} }
if (LittleFS.exists(save_path)) { if (LittleFS.exists(save_path)) {
@@ -152,7 +124,7 @@ void recvIR() {
send_msg_doc["params"]["length"] = results.rawlen; send_msg_doc["params"]["length"] = results.rawlen;
send_msg_doc["params"]["value"] = raw_data.c_str(); send_msg_doc["params"]["value"] = raw_data.c_str();
DEBUGLN(raw_data.c_str()); DEBUGLN(raw_data.c_str());
sendUDP(&send_msg_doc, remote_ip); // sendUDP(&send_msg_doc, remote_ip);
saveSignal(); saveSignal();
} }
} }

View File

@@ -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 * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
@@ -21,13 +21,13 @@
* SOFTWARE. * SOFTWARE.
*/ */
#ifndef IRBABYIR_H #ifndef IRBABY_IR_H
#define IRBABYIR_H #define IRBABY_IR_H
#include <Arduino.h> #include <Arduino.h>
#include <IRsend.h> #include <IRsend.h>
#include <IRrecv.h> #include <IRrecv.h>
#include "../lib/Irext/include/ir_decode.h" #include "ir_decode.h"
void loadIRPin(uint8_t send_pin, uint8_t recv_pin); void loadIRPin(uint8_t send_pin, uint8_t recv_pin);
void enableIR(); void enableIR();
@@ -38,4 +38,5 @@ bool sendIR(String file_name);
void recvIR(); void recvIR();
bool saveIR(String file_name); bool saveIR(String file_name);
void initAC(String); void initAC(String);
#endif // IRBABAYIR_H
#endif // IRBABY_IR_H

View File

@@ -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 * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
@@ -24,6 +24,8 @@
#ifndef IRBABY_IRIS_H #ifndef IRBABY_IRIS_H
#define IRBABY_IRIS_H #define IRBABY_IRIS_H
#define CREDENTIAL_MAX (40)
int getIRISKitVersion(char *buffer, int buffer_size); int getIRISKitVersion(char *buffer, int buffer_size);
int getDeviceID(char* buffer, int buffer_size); int getDeviceID(char* buffer, int buffer_size);

View File

@@ -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();
}
}

View File

@@ -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 <WString.h>
/* 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

View File

@@ -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 * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
@@ -21,21 +21,22 @@
* SOFTWARE. * SOFTWARE.
*/ */
#include <LittleFS.h>
#include "defines.h"
#include "IRbabyMsgHandler.h" #include "IRbabyMsgHandler.h"
#include "IRbabySerial.h" #include "IRbabySerial.h"
#include "IRbabyUserSettings.h" #include "IRbabyUserSettings.h"
#include "IRbabyMQTT.h"
#include "IRbabyUDP.h"
#include "ESP8266WiFi.h" #include "ESP8266WiFi.h"
#include "IRbabyIR.h" #include "IRbabyIR.h"
#include "../lib/Irext/include/ir_ac_control.h"
#include "IRbabyOTA.h" #include "IRbabyOTA.h"
#include "defines.h"
#include <LittleFS.h>
#include "IRbabyRF.h" #include "IRbabyRF.h"
#include "IRbabyGlobal.h" #include "IRbabyGlobal.h"
#include "IRbabyha.h" #include "IRbabyha.h"
#include "ir_ac_control.h"
bool msgHandle(StaticJsonDocument<1024> *p_recv_msg_doc, MsgType msg_type) { bool msgHandle(StaticJsonDocument<1024> *p_recv_msg_doc, MsgType msg_type) {
if (LOG_DEBUG) { if (LOG_DEBUG) {
serializeJsonPretty(*p_recv_msg_doc, Serial); 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"]; String ip = obj["params"]["ip"];
remote_ip.fromString(ip); // remote_ip.fromString(ip);
sendUDP(&send_msg_doc, remote_ip); // sendUDP(&send_msg_doc, remote_ip);
} else if (type.equals("info")) { } else if (type.equals("info")) {
String free_mem = String(ESP.getFreeHeap() / 1024) + "KB"; String free_mem = String(ESP.getFreeHeap() / 1024) + "KB";
String chip_id = String(ESP.getChipId(), HEX); 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"]["fs_used_bytes"] = fs_used_bytes;
send_msg_doc["params"]["version_name"] = FIRMWARE_VERSION; send_msg_doc["params"]["version_name"] = FIRMWARE_VERSION;
send_msg_doc["params"]["version_code"] = VERSION_CODE; 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")) { else if (type.equals("record")) {
String ip = params["ip"]; String ip = params["ip"];
remote_ip.fromString(ip); // remote_ip.fromString(ip);
DEBUGLN("start record"); DEBUGLN("start record");
enableIR(); enableIR();
enableRF(); enableRF();
@@ -247,10 +248,8 @@ bool msgHandle(StaticJsonDocument<1024> *p_recv_msg_doc, MsgType msg_type) {
} }
send_msg_doc["cmd"] = "return"; send_msg_doc["cmd"] = "return";
send_msg_doc["params"]["message"] = "set success"; send_msg_doc["params"]["message"] = "set success";
returnUDP(&send_msg_doc); // returnUDP(&send_msg_doc);
settingsSave(); settingsSave();
mqttDisconnect();
mqttReconnect();
loadIRPin(ConfigData["pin"]["ir_send"], ConfigData["pin"]["ir_receive"]); loadIRPin(ConfigData["pin"]["ir_send"], ConfigData["pin"]["ir_receive"]);
} }

View File

@@ -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 * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal

View File

@@ -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 * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal

View File

@@ -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 * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal

View File

@@ -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 * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
@@ -25,7 +25,6 @@
#include <LittleFS.h> #include <LittleFS.h>
#include "IRbabySerial.h" #include "IRbabySerial.h"
#include "IRbabyGlobal.h" #include "IRbabyGlobal.h"
#include "IRbabyUDP.h"
#include "defines.h" #include "defines.h"
#include <Ticker.h> #include <Ticker.h>
RCSwitch rf315; RCSwitch rf315;
@@ -113,7 +112,7 @@ void recvRF(void) {
send_msg_doc["cmd"] = "record_rt"; send_msg_doc["cmd"] = "record_rt";
send_msg_doc["params"]["signal"] = "RF433"; send_msg_doc["params"]["signal"] = "RF433";
send_msg_doc["params"]["value"] = String(code_tmp); 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); serializeJsonPretty(send_msg_doc, Serial);
} }
@@ -126,7 +125,7 @@ void recvRF(void) {
send_msg_doc["cmd"] = "record_rt"; send_msg_doc["cmd"] = "record_rt";
send_msg_doc["params"]["signal"] = "RF315"; send_msg_doc["params"]["signal"] = "RF315";
send_msg_doc["params"]["value"] = String(code_tmp); 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); serializeJsonPretty(send_msg_doc, Serial);
} }
} }

View File

@@ -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 * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal

View File

@@ -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 * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal

View File

@@ -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 <WiFiUdp.h>
#include <ESP8266WiFi.h>
#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();
}

View File

@@ -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 * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
@@ -25,7 +25,6 @@
#include "IRbabyUserSettings.h" #include "IRbabyUserSettings.h"
#include "IRbabySerial.h" #include "IRbabySerial.h"
#include "IRbabyMQTT.h"
#include "WiFiManager.h" #include "WiFiManager.h"
#include "IRbabyGlobal.h" #include "IRbabyGlobal.h"
#include "IRbabyIR.h" #include "IRbabyIR.h"

View File

@@ -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 * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
@@ -25,7 +25,7 @@
#define IRBABY_USER_SETTINGS_H #define IRBABY_USER_SETTINGS_H
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include "../lib/Irext/include/ir_ac_control.h" #include "ir_ac_control.h"
/* save settings */ /* save settings */
bool settingsSave(); bool settingsSave();

View File

@@ -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 * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
@@ -23,7 +23,6 @@
#include "IRbabyha.h" #include "IRbabyha.h"
#include "IRbabyGlobal.h" #include "IRbabyGlobal.h"
#include "IRbabyMQTT.h"
#include "IRbabySerial.h" #include "IRbabySerial.h"
void returnACStatus(String filename, t_remote_ac_status ac_status) { 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* mode[] = {"cool", "heat", "auto", "fan_only", "dry"};
const char* fan[] = {"auto", "low", "medium", "high", "max"}; const char* fan[] = {"auto", "low", "medium", "high", "max"};
const char* swing[] = {"on", "off"}; const char* swing[] = {"on", "off"};
if (ac_status.ac_power == AC_POWER_OFF) if (ac_status.ac_power == AC_POWER_OFF) {
mqttPublish(topic_head + "mode", "off"); // mqttPublish(topic_head + "mode", "off");
else } else {
mqttPublish(topic_head + "mode", mode[(int)ac_status.ac_mode]); // mqttPublish(topic_head + "mode", mode[(int)ac_status.ac_mode]);
mqttPublish(topic_head + "temperature", String((int)ac_status.ac_temp + 16)); // 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 + "fan", fan[(int)ac_status.ac_wind_speed]);
mqttPublish(topic_head + "swing", swing[(int)ac_status.ac_swing]); // mqttPublish(topic_head + "swing", swing[(int)ac_status.ac_swing]);
}
} }
// save current AC status // save current AC status
@@ -99,5 +99,5 @@ void registAC(String filename, bool flag) {
serializeJson(send_msg_doc, reg_content); serializeJson(send_msg_doc, reg_content);
} }
DEBUGLN(reg_topic_head); DEBUGLN(reg_topic_head);
mqttPublishRetained(reg_topic_head, reg_content); // mqttPublishRetained(reg_topic_head, reg_content);
} }

View File

@@ -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 * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
@@ -25,7 +25,7 @@
#define IRBABYHA_H #define IRBABYHA_H
#include <WString.h> #include <WString.h>
#include "../lib/Irext/include/ir_ac_control.h" #include "ir_ac_control.h"
void returnACStatus(String filename, t_remote_ac_status ac_status); void returnACStatus(String filename, t_remote_ac_status ac_status);

View File

@@ -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 * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal