This commit is contained in:
avaksru
2023-03-13 16:13:16 +03:00
parent d6b3d2dc68
commit b7430f87e8
8 changed files with 260 additions and 0 deletions

View File

@@ -0,0 +1,100 @@
#include "Global.h"
#include "classes/IoTItem.h"
class HttpGet : public IoTItem
{
public:
HttpGet(String parameters) : IoTItem(parameters)
{
}
void sendHttpPOST(String url, String msg)
{
if (WiFi.status() == WL_CONNECTED)
{
WiFiClient client;
HTTPClient http;
http.begin(client, url);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String httpRequestData = msg;
int httpResponseCode = http.POST(httpRequestData);
String payload = http.getString();
SerialPrint("<-", F("HttpPOST"), "URL: " + url + ", msg: " + msg);
SerialPrint("->", F("HttpPOST"), "URL: " + url + ", server: " + httpResponseCode);
if (httpResponseCode > 0)
{
value.valS = payload;
SerialPrint("->", F("HttpPOST"), "msg from server: " + (String)payload.c_str());
value.valS = payload;
regEvent(value.valS, "HttpGet");
}
http.end();
}
}
void sendHttpGET(String url)
{
WiFiClient client;
HTTPClient http;
#if defined ESP8266
if (!http.begin(client, url))
{
#elif defined ESP32
if (!http.begin(url))
{
#endif
SerialPrint("I", F("HttpGet"), "connection failed ");
}
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpResponseCode = http.GET();
String payload = http.getString();
SerialPrint("<-", F("HttpGET"), "URL: " + url);
SerialPrint("->", F("HttpGET"), "URL: " + url + ", server: " + httpResponseCode);
if (httpResponseCode > 0)
{
value.valS = payload;
SerialPrint("->", F("HttpGET"), "msg from server: " + (String)payload.c_str());
value.valS = payload;
regEvent(value.valS, "HttpGet");
}
http.end();
}
IoTValue execute(String command, std::vector<IoTValue> &param)
{
if (param.size() > 0)
{
if (command == "get")
{
if (param.size())
{
sendHttpGET(param[0].valS);
}
}
else if (command == "post")
{
if (param.size())
{
sendHttpPOST(param[0].valS, param[1].valS);
}
}
}
return {};
}
~HttpGet(){};
};
void *getAPI_HttpGet(String subtype, String param)
{
if (subtype == F("HttpGet"))
{
return new HttpGet(param);
}
else
{
return nullptr;
}
}

View File

@@ -0,0 +1,58 @@
{
"menuSection": "Исполнительные устройства",
"configItem": [
{
"global": 0,
"name": "HttpGet",
"type": "Writing",
"subtype": "HttpGet",
"id": "http",
"widget": "",
"page": "",
"descr": "",
"token": "",
"chatID": ""
}
],
"about": {
"authorName": "AVAKS",
"authorContact": "https://t.me/@avaks_dev",
"authorGit": "https://github.com/avaksru",
"specialThanks": "",
"moduleName": "HttpGet",
"moduleVersion": "1",
"usedRam": {
"esp32_4mb": 15,
"esp8266_4mb": 15
},
"title": "Отправка запросов по http",
"moduleDesc": "Запросы по протоколу https НЕ РАБОТАЮТ!",
"propInfo": {},
"retInfo": "",
"funcInfo": [
{
"name": "get",
"descr": "Отправить http запрос методом GET.",
"params": [
"http.get('URL')"
]
},
{
"name": "post",
"descr": "Отправить http запрос методом POST.",
"params": [
"http.post('URL','message')"
]
}
]
},
"defActive": false,
"usedLibs": {
"esp32_4mb": [],
"esp8266_4mb": [],
"esp8266_1mb": [],
"esp8266_1mb_ota": [],
"esp8285_1mb": [],
"esp8285_1mb_ota": []
}
}