0

The below reading data from API:

#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>

const char* ssid = "****";
const char* pass = "****";

void setup()
{
  Serial.begin(115200);
  WiFi.begin(ssid, pass);
  Serial.print("COnnecting");

  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }

  Serial.print("\nIP Address");
  Serial.println(WiFi.localIP());
}

void loop() {
  const char* root_0_id ;
  const char* root_0_Title ;
  if (WiFi.status() == WL_CONNECTED)
  {
    long rnd = random(1, 3);
    HTTPClient client;

    client.begin("https://6336d6c365d1e8ef267481aa.mockapi.io/api/bitmap/images?id=1");
    int httpCode = client.GET();

    if (httpCode > 0) {
      String payload = client.getString();
      Serial.println("\nstatuscode: " + String(httpCode));
      if (payload == "[]")
        Serial.println("\nNOT FOUND");
      else
      {

        DynamicJsonDocument doc(49152);
        DeserializationError error = deserializeJson(doc, payload);
        if (error) {
          Serial.print("deserializeJson() failed: ");
          Serial.println(error.c_str());
          return;
        }

        root_0_id = doc[0]["id"]; // "41"
        root_0_Title = doc[0]["Bitmap"];

        Serial.print("\n\nData : ");
        Serial.println(root_0_Title);

        Serial.println(sizeof(root_0_Title));
        client.end();
        dataReceived = 1;
        delay(10000);
        
      }

    }
    else {
      Serial.println("Error on HTTP request");
    }
  }
}

I need help with parsing the const char* root_0_Title="0x21,0x10,0xfa,0x7a,0xff", to uint8_t[5]={0x21, 0x10, 0xfa, 0x7a, 0xff}?

2
  • almost the same question on SO with answer using standard library stackoverflow.com/questions/73969744/…
    – Juraj
    Commented Oct 10, 2022 at 7:41
  • @Juraj I have tried it but I thought it was for probably C not Arduino C that's why its not compiling for ESP32. Also, it's a bit higher level for me to make changes. I wanted to know the for ESP32 perspective. But if you saying it should work with ESP32 too? I'll delete this question Commented Oct 10, 2022 at 7:48

1 Answer 1

1

you can use strtok (string tokenizer) and strtol (string to long) C functions.

  int count = 0;
  uint8_t bytes[MAX_SIZE]; 
  char* tok = strtok(root_0_Title, ",");
  while (tok != NULL) {
    bytes[count++] = strtol(tok, NULL, 16);
    tok = strtok(NULL, ","); 
  }
2
  • const char* root_0_Title causing error as char * strtok ( char * str, const char * delimiters ); Commented Oct 10, 2022 at 9:13
  • 1
    remove const from const char* root_0_Title ;
    – Juraj
    Commented Oct 10, 2022 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.