implemented credential config via WifiManager
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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<String>();
|
||||
INFOF("update %s to devicehive\n", body.c_str());
|
||||
http.PUT(body);
|
||||
http.end();
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
106
src/IRbabyHttp.cpp
Normal file
106
src/IRbabyHttp.cpp
Normal 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();
|
||||
}
|
||||
@@ -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 <WString.h>
|
||||
|
||||
#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
|
||||
void downLoadFile(String file, String path);
|
||||
|
||||
|
||||
#endif // IRBABY_HTTP_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
|
||||
@@ -23,17 +23,16 @@
|
||||
|
||||
#include <LittleFS.h>
|
||||
|
||||
#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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 <Arduino.h>
|
||||
#include <IRsend.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 enableIR();
|
||||
@@ -38,4 +38,5 @@ bool sendIR(String file_name);
|
||||
void recvIR();
|
||||
bool saveIR(String file_name);
|
||||
void initAC(String);
|
||||
#endif // IRBABAYIR_H
|
||||
|
||||
#endif // IRBABY_IR_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);
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
@@ -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 <LittleFS.h>
|
||||
|
||||
#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 <LittleFS.h>
|
||||
#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"]);
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 <LittleFS.h>
|
||||
#include "IRbabySerial.h"
|
||||
#include "IRbabyGlobal.h"
|
||||
#include "IRbabyUDP.h"
|
||||
#include "defines.h"
|
||||
#include <Ticker.h>
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
@@ -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"
|
||||
|
||||
@@ -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 <ArduinoJson.h>
|
||||
#include "../lib/Irext/include/ir_ac_control.h"
|
||||
#include "ir_ac_control.h"
|
||||
|
||||
/* save settings */
|
||||
bool settingsSave();
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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 <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);
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user