problem Signature

Post Reply
Juszczaczek1
Posts: 12
Joined: Wed Jan 08, 2025 7:24 pm

witam co w tym kodzie jest zle ze zwraca mi taki blad Kod HTTP: 200
Odpowiedź serwera:
{"errno":40256,"msg":"Parameter could not be parsed correctly illegal signature"}

Code: Select all

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecureBearSSL.h>
#include <MD5Builder.h>
#include <NTPClient.h>
#include <WiFiUdp.h>

// Dane sieci WiFi
const char* ssid = "xxxxx";
const char* password = "xxxxxx";

// Dane API FoxESS
const String token = "xxxxxxxxxxxxxxxxxxxx";  // Twój token
const String endpoint = "/op/v0/device/list"; // Ścieżka zgodnie z dokumentacją
const String apiURL = "https://www.foxesscloud.com";  // URL API
const String lang = "en"; // Język odpowiedzi

// Zmienna do synchronizacji czasu
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", 0, 60000); // Serwer NTP, czas UTC

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);

  Serial.println("Łączenie z WiFi...");
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }

  Serial.println("\nPołączono z WiFi!");
  Serial.print("Adres IP: ");
  Serial.println(WiFi.localIP());

  // Synchronizacja czasu z NTP
  Serial.println("Synchronizowanie czasu z serwerem NTP...");
  timeClient.begin();
  while (!timeClient.update()) {
    timeClient.forceUpdate();
  }

  unsigned long long timestamp = getCurrentUTCTimestamp();
  Serial.print("Czas UTC (ms): ");
  Serial.println(timestamp);

  // Generowanie sygnatury
  String signature = generateSignature(endpoint, token, timestamp);

  // Wysłanie żądania do API
  connectToAPI(endpoint, timestamp, signature);
}

void loop() {
  // Nic w pętli głównej
}

// Funkcja do generowania sygnatury
String generateSignature(const String& endpoint, const String& token, unsigned long long timestamp) {
  // Zgodnie z dokumentacją: endpoint + "\r\n" + token + "\r\n" + timestamp
  String data = endpoint + "\r\n" + token + "\r\n" + String(timestamp);

  Serial.print("Dane do sygnatury: '");
  Serial.print(data);
  Serial.println("'");

  // Generowanie sygnatury MD5
  MD5Builder md5;
  md5.begin();
  md5.add(data); // Dodaj dane do MD5
  md5.calculate();
  String signature = md5.toString(); // Wynik MD5 w formacie hex

  Serial.print("Wygenerowana sygnatura: ");
  Serial.println(signature);
  return signature;
}

// Funkcja do uzyskania aktualnego czasu UTC w milisekundach
unsigned long long getCurrentUTCTimestamp() {
  return static_cast<unsigned long long>(timeClient.getEpochTime()) * 1000;
}

// Funkcja do wysyłania żądania do API FoxESS
void connectToAPI(const String& endpoint, unsigned long long timestamp, const String& signature) {
  std::unique_ptr<BearSSL::WiFiClientSecure> client(new BearSSL::WiFiClientSecure);
  client->setInsecure(); // Pomijanie weryfikacji certyfikatu dla testów

  HTTPClient http;

  String url = apiURL + endpoint; // Pełny URL
  if (http.begin(*client, url)) {
    // Dodawanie nagłówków
    http.addHeader("Content-Type", "application/json");
    http.addHeader("signature", signature);  // Dodanie sygnatury do nagłówków
    http.addHeader("token", token);  // Dodanie tokenu do nagłówków
    http.addHeader("lang", lang);  // Dodanie języka do nagłówków
    http.addHeader("timestamp", String(timestamp)); // Dodanie timestampu do nagłówków

    // Przygotowanie danych POST w formacie JSON
    String postData = "{}"; // Puste ciało zapytania

    Serial.println("Nawiązywanie połączenia z API...");
    int httpResponseCode = http.POST(postData);

    // Obsługa odpowiedzi
    if (httpResponseCode > 0) {
      Serial.print("Kod HTTP: ");
      Serial.println(httpResponseCode);
      Serial.println("Odpowiedź serwera:");
      Serial.println(http.getString());
    } else {
      Serial.print("Błąd HTTP: ");
      Serial.println(httpResponseCode);
    }

    http.end();
  } else {
    Serial.println("Błąd inicjalizacji połączenia HTTPS.");
  }
}
tony.matthews1
Posts: 20
Joined: Sun Mar 19, 2023 4:37 pm

Looks like the same problem as previously?
You have used CR LF in the encoding string instead of character literals \\r\\n.
H1-6.0-E hybrid inverter
6 x HV2600 v2 batteries
16 x JA Solar 405w panels
7 x Tigo TS4-A-O optimisers
Juszczaczek1
Posts: 12
Joined: Wed Jan 08, 2025 7:24 pm

Dzięki działa :⁠-⁠)
Post Reply