Sending array by using wifi

nataly
Posts: 12
Joined: Sat Oct 16, 2021 12:16 pm

Sending array by using wifi

Postby nataly » Wed May 11, 2022 10:05 pm

Hello,
In this code I return string from the function readTemp().Now I'm asking what is I should use rather than c_str if I need to return unsigned char?

Code: Select all

string readTemp() {
    return string(50);
}
void setup(){
    request->send_P(200, "text/plain", readTemp().c_str());  
}
Now I need to deal with

Code: Select all

unsigned char readTemp() {
    unsigned char a[5]={1,2,3,4,5};
    return (a);
}
void setup(){
    request->send_P(200, "text/plain", readTemp().c_str()); // here what should I use
                                                            // instead of c_str to sent  unsigned char array ?  
}

Craige Hales
Posts: 94
Joined: Tue Sep 07, 2021 12:07 pm

Re: Sending array by using wifi

Postby Craige Hales » Thu May 12, 2022 12:59 am

Does readTemp compile? It looks like it might try to return a pointer to a temporary on the stack as an 8-bit value.
Perhaps you want it to return unsigned char * and add static to the a[5].
And then you'll want to change the 5 to a 6 and add a null terminator so it can be a c string.
And then you might want to change the 1,2,3,4,5 to be ASCII codes; it isn't clear why you'd send binary data as text/plain, so maybe '1','2','3','4','5'.

Untested, but something like this.

Code: Select all

unsigned char * readTemp() {
    static unsigned char a[6]={'1','2','3','4','5',0};
    return (a);
}
void setup(){
    request->send_P(200, "text/plain", readTemp());  
}
Craige

lbernstone
Posts: 636
Joined: Mon Jul 22, 2019 3:20 pm

Re: Sending array by using wifi

Postby lbernstone » Thu May 12, 2022 1:52 am

I will assume you are talking about a ESPAsyncWebServer response. The documentation is at https://github.com/me-no-dev/ESPAsyncWebServer. There is a send() function for sending strings. I can't imagine why you would send a bare string like that, so I'll demonstrate wrapping some json.

Code: Select all

uint8_t a[10] = {22, 23, 27, 18, 19, 20, 21, 25, 23, 22};
void handleTemp(AsyncWebServerRequest *request) {
    String json = "{ \"temp\" : [" + (String)a[0];
    for (int i=1;  i < sizeof(a); i++) {
        json += "," + (String)a[i];
    }
    json += "] }";
    request->send(200, "application/json", json);
}

nataly
Posts: 12
Joined: Sat Oct 16, 2021 12:16 pm

Re: Sending array by using wifi

Postby nataly » Thu May 12, 2022 10:41 am

Craige Hales wrote:
Thu May 12, 2022 12:59 am
Does readTemp compile? It looks like it might try to return a pointer to a temporary on the stack as an 8-bit value.
Perhaps you want it to return unsigned char * and add static to the a[5].
And then you'll want to change the 5 to a 6 and add a null terminator so it can be a c string.
And then you might want to change the 1,2,3,4,5 to be ASCII codes; it isn't clear why you'd send binary data as text/plain, so maybe '1','2','3','4','5'.

Untested, but something like this.

Code: Select all

unsigned char * readTemp() {
    static unsigned char a[6]={'1','2','3','4','5',0};
    return (a);
}
void setup(){
    request->send_P(200, "text/plain", readTemp());  
}
In this case I get on :invalid conversion from 'unsigned char*' to 'const char*' [-fpermissive]

nataly
Posts: 12
Joined: Sat Oct 16, 2021 12:16 pm

Re: Sending array by using wifi

Postby nataly » Thu May 12, 2022 10:50 am

lbernstone wrote:
Thu May 12, 2022 1:52 am
I will assume you are talking about a ESPAsyncWebServer response. The documentation is at https://github.com/me-no-dev/ESPAsyncWebServer. There is a send() function for sending strings. I can't imagine why you would send a bare string like that, so I'll demonstrate wrapping some json.

Code: Select all

uint8_t a[10] = {22, 23, 27, 18, 19, 20, 21, 25, 23, 22};
void handleTemp(AsyncWebServerRequest *request) {
    String json = "{ \"temp\" : [" + (String)a[0];
    for (int i=1;  i < sizeof(a); i++) {
        json += "," + (String)a[i];
    }
    json += "] }";
    request->send(200, "application/json", json);
}
Actually I'm trying to send this array "unsigned char "so this is just an attempt because I don't know how to do that. For this code how can I call handleTemp() in setup()
?

nataly
Posts: 12
Joined: Sat Oct 16, 2021 12:16 pm

Re: Sending array by using wifi

Postby nataly » Thu May 12, 2022 11:12 am

OK, in this way I can send array after define the array and json global :

Code: Select all

unsigned char  a[10] = {22, 23, 27, 18, 19, 20, 21, 25, 23, 22};
String json;
in serup():

Code: Select all

json = "{ \"temp\" : [" + (String)a[0];
    for (int i=1;  i < sizeof(a); i++) {
        json += "," + (String)a[i];
    }
    json += "] }";
   // request->send(200, "application/json", json);
    
       server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(200, "application/json",json);
  });
NOW how can I deal with the value of this array in the receiver side? because now I receive it like this:

Code: Select all

if(WiFi.status()== WL_CONNECTED ){ 
      temperature = httpGETRequest(serverNameTemp);
      Serial.println("Temperature: " + temperature );  

Craige Hales
Posts: 94
Joined: Tue Sep 07, 2021 12:07 pm

Re: Sending array by using wifi

Postby Craige Hales » Thu May 12, 2022 11:40 am

In this case I get on :invalid conversion from 'unsigned char*' to 'const char*' [-fpermissive]
either cast with (const char *)readtemp() or remove the unsigned keyword.
I think the other proposed solution using JSON may be better. What does it print? You should see the JSON string you constructed on the server.
Craige

nataly
Posts: 12
Joined: Sat Oct 16, 2021 12:16 pm

Re: Sending array by using wifi

Postby nataly » Thu May 12, 2022 11:48 am

I think the other proposed solution using JSON may be better. What does it print? You should see the JSON string you constructed on the server.
[/quote]

Yes, I use json as @lbernstone suggested but now my problem how can deal with the values in receiver side? I need to make some processing on the received value array

Craige Hales
Posts: 94
Joined: Tue Sep 07, 2021 12:07 pm

Re: Sending array by using wifi

Postby Craige Hales » Thu May 12, 2022 1:34 pm

All the top google hits seem to use ArduinoJSON library.

https://randomnerdtutorials.com/decodin ... r-esp8266/
https://www.learnrobotics.org/blog/pars ... a-arduino/

The data you have,
1,2,3,4,5
is binary. JSON is a way to package the binary data as a printable string, and later turn the string back into binary data. There are many ways to do that; sprintf(...) is another way. JSON is a good choice, especially if you decide to add another sensor to the server's response. The new sensor might get a name like \"humidity\" instead of \"temp\" and the old receiver code will still work because it will only look for \"temp\" and still find it. New receiver code will also find the new value under humidity.
Craige

nataly
Posts: 12
Joined: Sat Oct 16, 2021 12:16 pm

Re: Sending array by using wifi

Postby nataly » Thu May 12, 2022 1:52 pm

I get output like this for this code

Code: Select all

      temperature = httpGETRequest(serverNameTemp);
:
this is the output,
temperature =
{ "temp" : [22,23,27,18,19,20,21,25,23,22] }
but I need to reach to each value inside this received array, what is the change on

Code: Select all

 temperature = httpGETRequest(serverNameTemp);

Who is online

Users browsing this forum: Baidu [Spider], Google [Bot] and 65 guests