Fórumok
Sziasztok!
Csináltam egy ESP8266 alapon egy 5 csatornás hőmérsékletmérő eszközt, ami WiFi-n HTTP POST-al tolná
föl az adatokat egy szerverre. (Arduino framework, Platform.IO)
Nem értem, a válasz a POST kérésnek:
"HTTP Response code: 400" - tehát valami szintaktikai hiba lehet?! Már a szemem kifolyik, nem találom
a hibám.
Ez a programrész:
bool puthttp() { bool result = false; long rssi = WiFi.RSSI(); String localip = WiFi.localIP().toString(); WiFiClient client; HTTPClient http; char jsonstr[400]; char jsonstr2[100]; char jsonstr3[30]; Serial.println(serverstr); snprintf(jsonstr, sizeof(jsonstr), "{\"device_id\":%d, \"ip_address\":\"%s\", \"ssid\":\"%s\", \"rssi\":%d, ",didx,localip.c_str(),ssidstr,rssi); snprintf(jsonstr2, sizeof(jsonstr2), "\"value_1\":%.1f, \"value_2\":%.1f, \"value_3\":%.1f, \"value_4\":%.1f, \"value_5\":%.1f, ",hom1,hom2,hom3,hom4,hom5); snprintf(jsonstr3, sizeof(jsonstr3), "\"timestamp\":%ld}", helyiUTC); strncat(jsonstr, jsonstr2, sizeof(jsonstr2)); strncat(jsonstr, jsonstr3, sizeof(jsonstr3)); Serial.println(jsonstr); // Your Domain name with URL path or IP address with path Serial.print("http.begin:"); bool mi = http.begin(client, serverstr); Serial.println(mi); http.addHeader("Content-Type", "application/json"); int httpResponseCode = http.POST(jsonstr); //int httpResponseCode = http.POST("{\"value\": 20 }"); Serial.print("HTTP Response code: "); Serial.println(httpResponseCode); http.end(); if (httpResponseCode == 200) result = true; return result; }
És ez a készített "jsonstr":
{"device_id":10170, "ip_address":"192.168.3.162", "ssid":"gentoom", "rssi":-62, "value_1":22.8, "value_2":-100.0,
"value_3":-100.0, "value_4":-100.0, "value_5":-100.0, "timestamp":1709045163}
Köszönöm!
Roland
Hozzászólások
Maga amit küldesz, az valid JSON. A kérdés az, hogy a szerveroldal ugyanilyen struktúrájú JSON-t vár-e el, vagy esetleg valamelyik mező neve vagy típusa eltérő, esetleg van hiányzó kötelező mező stb. A szerver logjában mi látszik? Azt is el tudom képzelni, hogy a value az egy tömb, azaz nem value_1, value_2, value_3, value_4, value_5 mező kéne neki külön-külön, hanem egy ilyen json:
De ezt csak a fogadóoldali kód ismeretében tudjuk eldönteni, hiszen ő mondja azt, hogy nem megfelelő a kérés.
A szerverig el sem jut.
Egyébként a szervert nem én használom/csináltam.
Ennyit kaptam az ottani gazditól (így várja az adatokat):
> A végpont:
> https://xxxxx.xx:8080
>
> A kérés:
> POST
> headers: {
> 'Content-Type': 'application/json'
> }
>
> Body data:
>
> {
> "device_id": 1,
> "ip_address": "192.168.1.123",
> "ssid": "Sample_SSID",
> "rssi": -41,
> "value_1": 18.1,
> "value_2": 18.2,
> "value_3": 18.3,
> "value_4": 18.4,
> "value_5": 18.5,
> "timestamp": 1708478505
> }
Hát, oedig 400as responseot az szokott adni szóval gyanusan de.
Ez butaság, ami visszaadja neked a HTTP 400-as választ, az csak a szerver lehet.
Gyanús, hogy te sima httpt küldesz.
Ha `curl` parancssorból meghívod ugyanezt, ugyanígy, akkor az mit ír? Egyszerűbb kitapasztalni a kommunikációs hibákat CLI-ben, mint ESP8266-on.
https://iotguru.cloud
jó ötlet. teszt.
Ez lett:
Érdekes, mert ESPHome-ban így megy:
curl ... -k ... -ezzel megy!
Na, csak ezt kell az ESP8266-ra átírni ...-k...
https://www.esp8266.org/
Mi a
serverstr
?String serverstr = "https://xxxxx.xx:8080"
Azt vedd figyelembe, hogy ESP8266-ról a https nem fog ilyen egyszerűen menni.
https://iotguru.cloud
Faék egyszerű
WiFiClientSecure client;
client.setInsecure();
httpClient.begin(client, URL);
https://www.esp8266.org/
Köszönöm!
Ezzel megy!
egyszerűen működik :) Utólag. Az itteni segítséggel.
Nem, úgy megkerülöd az egész értelmét, ami miatt https-t használnál.
https://iotguru.cloud
Jelen feladatnál szerintem tök felesleges hülyére titkosítani a kommunikációt. Egyébként jogos.
https://www.esp8266.org/
Szerintem is. Sajna a szerver üzemeltetője így kéri.
Lehetne egy
http.getString()
a történetben, hátha van valami hibaüzenet.Érdekes jelenség - most látom.
A rutin első lefutásakor 200 a válasz (jó!), aztán utána x percre rá már -1! és így már nem megy. Nyomozom, hogy mitől lehet, ha ugyanazt hívom meg.
Ez amugy netes kommunikacio vagy helyi?
En ugy tudom, lehet hasznalni a WiFiClientSecure-t self signed-el is.
https://github.com/espressif/arduino-esp32/issues/265
Vagy az uzemeltetonek kene szolni hogy dobjon ra egy Let's Encrypt-et..
Netes.
Amúgy meg pont l.. Nekem így ok. Ha neki ez jó...
A függvény vége:
delete client;
https://www.esp8266.org/
ezt hogy érted? Ez így nem tetszik a fordítónak.
Nekem ez müxik:
https://www.esp8266.org/
én ezt kapom (ESP8266, platform.io - arduino framework)
"include/httppost.h:55:12: error: type 'class BearSSL::WiFiClientSecure' argument given to 'delete', expected pointer
55 | delete client;"
https://www.esp8266.org/
Valamit nagyon elbökök:
lehet amiatt, hogy bent hagytam a http.end(); utasítást? Azaz, most látom 2x volt... Nem elmaradt
bár lefordult, hogy nem volt:
Kivételt dob, ha a http.POST-ra fut...
Az URL https://napiszar.com ?
Ha hiányzik a https:// az URL elejéről, akkor crash. A müxik hsz nálam működik.
https://www.esp8266.org/
A szerver elejében van a "https://
úja ideteszem a kódot, ami kivételt okoz:
Ja, és a "new - delete" nélküli változat simán lefut - mondjuk, csak először, majd a második hívásra -1 a response
Hogy lehetne a http.POST(...)-ból http.print(...)-et csinálni?
Én nem értem. Ez a változat megy 1x meghívva a puthttp függvény. Többször meghívva -1 a "httpResponseCode".
Viszont, ha kívül - nem a függvény törzsében - van a "http" és "client" deklarálva, ismét nem fut, kivételt okoz.
Emlékszem már. Csak a loop-ból meghívva működik.
https://www.esp8266.org/
Így volt.
Basszus most megy, tesztelem.
Két dolgot csináltam, az SPIFFS -> LittleFS átcsere, ill. volt egy változó ami "long" volt, snprintf-ben int-ként kezeltem. Javítottam. Lehet nincs közük hozzá, de most úgy nézem megy...
Ez így gányolás, de nem tudtam rájönni a hiba okára. Van több ilyen könyvtár, pl https://github.com/ThingPulse/esp8266-oled-ssd1306 ami csak loop/setup függvényből hívva működik. Egy profi (pl: Teve) elmagyarázhatná hogy ez mitől van.
https://www.esp8266.org/
Igen, gányolás. Most annak örülök, hogy megy.
(Apró hátrányban vagyok: nem értek a C++-hoz, és az Arduinóhoz.)
Pedig https://github.com/lzsiga/untarArduino szép munka volt. Nem akarod befejezni? A debug üzeneteket F() formába kéne hozni, a végéről a
sort kivenni. Mehetne a hivatalos Arduino könyvtárak közé, hiánypótlás. Köszönöm hogy nekem megcsináltad, de szerintem ezt közkinccsé kéne tenni.
https://www.esp8266.org/