NodeMCU + LED 7-segment 4-digit odczyt temperatury z cloud

roman106
Posts: 36
Joined: Sun Nov 05, 2023 5:21 pm
Location: Ireland

Post

Witam!

Chciałbym zrobić sobie wyświetlacz temperatury pobierający dane z linku bezpośredniego z Supla Cloud i wyświetlającego na module 4-cyfrowym LED. Dokładnie jest to temperatura wody w bojlerze.

Podzespoły:
- NodeMCU v3,
- 4-digit LCD na TM1637.

Nie wiem jak poprawnie pobierać dane z serwera i jak je wyświetlić chociażby na monitorze portu szeregowego. Kod próbowałem stworzyć za pomocą ChatGPT, ale podczas kompilacji zawsze są jakieś błędy. Myślę, że ktoś już ma taki bezprzewodowy wyswietlacz temperatury i pomoże. Poniżej kod, który jest błędny:
- const size_t capacity = JSON_OBJECT_SIZE(4);
- StaticJsonDocument<200> doc;

Code: Select all

#define ARDUINOJSON_ENABLE_DEPRECATED 1
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
#include <TM1637Display.h>
#include <ESP8266HTTPClient.h>

const int CLK = D2;
const int DIO = D3;
const char* ssid = "nazwa_sieci";
const char* password = "password";
const char* host = "svr15.supla.org";
const String url = "/direct/xxxx/xxxxxxxx/read";

TM1637Display display(CLK, DIO);

void setup() {
  display.setBrightness(0x0a);
  Serial.begin(115200);
  delay(10);
  WiFi.begin(ssid, password);
  Serial.print("Connecting to ");
  Serial.println(ssid);
  int i = 0;
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(++i);
    Serial.print(' ');
  }
  Serial.println('\n');
  Serial.println("Connection established!");
  Serial.print("IP address:\t");
  Serial.println(WiFi.localIP());
}

void loop() {
  if (WiFi.status() == WL_CONNECTED) {
    WiFiClient client;
    HTTPClient http;
    http.begin(client, host, 443, url);
    int httpResponseCode = http.GET();
    if (httpResponseCode > 0) {
      Serial.print("HTTP Response code: ");
      Serial.println(httpResponseCode);
      String payload = http.getString();
      Serial.println("Received payload: " + payload);
      
      // Parse JSON payload
      const size_t capacity = JSON_OBJECT_SIZE(1) + 30; // Adjust capacity according to your JSON structure
      StaticJsonDocument<200> doc;
      deserializeJson(doc, payload);
      
      // Extract temperature value from JSON
      float temperature = doc["temperature"];
      
      // Display temperature on the TM1637 display
      display.showNumberDec(temperature * 10, false); // Assuming temperature is in degrees Celsius
      
    } else {
      Serial.print("Error code: ");
      Serial.println(httpResponseCode);
    }
    http.end();
  }
  delay(10000);
}
roman106
Posts: 36
Joined: Sun Nov 05, 2023 5:21 pm
Location: Ireland

Post

Upraszczam pytanie: Jak ESP8266 ma odpytać serwer o temperaturę za pomocą linku bezpośredniego?

const char* ssid = "nazwa_sieci";
const char* password = "haslo";
const char* host = "svr15.supla.org";
const int port = 443; // Port dla HTTPS
...potem...
String url = "/direct/1111/xxxxxxxxxx/read";

Takie coś zwraca błąd z serwera HTTP -1.

Przez wygenerowanie na GG kodu, serial monitor pokazuje temperaturę poprawnie. Jednak w GG nie ma obsługi wyświetlacza TM1637. Z tym sobie poradzę, tylko chodzi mi jak poprawnie wysłać zapytanie o odczyt temperatury.
User avatar
Duch__
Posts: 2199
Joined: Wed Aug 24, 2016 7:26 pm
Location: Opole
Been thanked: 6 times

Post

Utworzyłeś link bezpośredni na serwerze?
roman106
Posts: 36
Joined: Sun Nov 05, 2023 5:21 pm
Location: Ireland

Post

Oczywiście. W przeglądarce normalnie się wyświetla.
User avatar
klew
Posts: 13908
Joined: Thu Jun 27, 2019 12:16 pm
Location: Wrocław
Has thanked: 134 times
Been thanked: 137 times

Post

Linki bezpośrednie działają po szyfrowanym połączeniu https.
Nie wiem jak klasa HTTPClient, ale WifiClient na pewno występuje w wersji WiFiClientSecure. Ten zwykły WiFiClient raczej nie ruszy na szyfrowanym połączeniu.
Najlepsze suple dla Twojego domu :mrgreen:
Beholder
Posts: 98
Joined: Wed Jul 19, 2023 2:47 pm
Has thanked: 6 times
Been thanked: 2 times

Post

Tak na szybko: czy klient na pewno łączy się https, metoda GET i żądanie do serwera powinno mieć formę /read?format=json żeby zwróciło json.
roman106
Posts: 36
Joined: Sun Nov 05, 2023 5:21 pm
Location: Ireland

Post

klew wrote: Mon Feb 19, 2024 10:02 am Nie wiem jak klasa HTTPClient, ale WifiClient na pewno występuje w wersji WiFiClientSecure. Ten zwykły WiFiClient raczej nie ruszy na szyfrowanym połączeniu.
Skąd wziąć bibliotekę WiFiClientSecure? Nie znalazłem do ściągnięcia archiwum zip.
Beholder wrote: Mon Feb 19, 2024 10:06 am żądanie do serwera powinno mieć formę /read?format=json żeby zwróciło json.
Próbowałem wcześniej dopisywać "?format=json", ale nie było efektu.
User avatar
klew
Posts: 13908
Joined: Thu Jun 27, 2019 12:16 pm
Location: Wrocław
Has thanked: 134 times
Been thanked: 137 times

Post

WiFiClientSecure jest częścią boardów dla ESP.
Najlepsze suple dla Twojego domu :mrgreen:
roman106
Posts: 36
Joined: Sun Nov 05, 2023 5:21 pm
Location: Ireland

Post

Ogarnąłem już. Jednak dalej się nie łączy:
Connecting to server: svr15.supla.org
Connection to server failed
User avatar
klew
Posts: 13908
Joined: Thu Jun 27, 2019 12:16 pm
Location: Wrocław
Has thanked: 134 times
Been thanked: 137 times

Post

roman106 wrote: Mon Feb 19, 2024 1:36 pm Ogarnąłem już. Jednak dalej się nie łączy:
Connecting to server: svr15.supla.org
Connection to server failed
Spróbuj włączyć logowanie z biblitek i boardów. Tam powinno być widać w czym problem.

Ewnetualnie, znajdź na forum przykład jak odczytywać linki bezpośrednie, lub zobacz jak to jest w GuiGeneric zrobione.
Najlepsze suple dla Twojego domu :mrgreen:

Return to “Pomoc”