pico-examples icon indicating copy to clipboard operation
pico-examples copied to clipboard

Feature request: http and https client examples in C

Open a-h opened this issue 1 year ago • 4 comments

I want to post data from sensors over HTTPS in JSON format, and ideally, also using MQTT.

This seems very difficult with the C API, but is trivial with the urequests API in MicroPython.

On the ESP32, the included WiFiClientSecure type makes the same task simple too.

So far, I've spent hours trying to get something up and running on the Pico W without success.

Reading through https://www.i-programmer.info/programming/hardware/15838-the-picow-in-c-simple-web-client.html got me up and running with a HTTP request, but that's not very useful, since a lot of cloud infrastructure doesn't support unencrypted transports. Regardless, I think it would be a good addition to the examples.

I've looked at https://github.com/cniles/picow-iot but couldn't get it to build. It seems outdated, since the Pico SDK seems to have some support for mbedTLS now. The author didn't get any help on his questions about performance at https://forums.raspberrypi.com/viewtopic.php?t=339620 so I'm not sure if it works anyway.

This unmerged PR seems like it's starting to get to the right place, but it's still very much at the TCP side, rather than higher level. https://github.com/raspberrypi/pico-examples/pull/305 - it doesn't make use of the httpc_get_file_dns function, for example.

Reading through this blog post makes it look like it's possible to do HTTPS, but I don't know how to connect the pieces - https://www.unshiu.com/posts/pico-http-client-part-iii-mbedtls/ - Finally, it seems that the author gave up on the Pico SDK, and switched to try out FreeRTOS (https://www.unshiu.com/posts/pico-with-freertos/)

I think there's two things that would help the developer experience here:

  1. Examples of HTTP and HTTPS connectivity with the existing tools.
  2. Addition of a higher level API for HTTPS requests built into the Raspberry Pi SDK to simplify it.

a-h avatar Jan 22 '23 16:01 a-h

arduino-pico has a good HTTP API that's borrowed from the ESP8266, but all the Arduino libraries are LGPL licensed.

  • https://arduino-pico.readthedocs.io/en/latest/httpclient.html
// Error checking is left as an exercise for the reader...
HTTPClient https;
https.setInsecure();  // Use certs, but do not check their authenticity
if (https.begin("https://my.secure.server/url")) {
    if (https.GET() > 0) {
        String data = https.getString();
    }
    https.end();
}

a-h avatar Jan 23 '23 09:01 a-h

I too would like some examples specificall for the Pico in C with HTTP and HTTPS GET/POST requestes for this as I have attempted to use the httpc_get_file function from #include "lwip/apps/http_client.h", but this seems to fall over after a number of repeated requests with memory issues.

NworbLegin avatar Mar 19 '23 20:03 NworbLegin

See my comment here Based on this example, which also has the memory leak.

You will need to free the memory taken by the struct pbuf *hdr in the headers function and struct pbuf *p in the body function with respectively pbuf_free(hdr); and pbuf_free(p);

pspeybro avatar Apr 06 '23 21:04 pspeybro

I wrote a minimum working HTTPS client example in C which might be useful as a starting point for an example.

It's currently in it's own repo, but would be happy to migrate it here if desired. Might require some refactoring though.

Looks like @peterharperuk is also working on adding some HTTP and HTTPS examples though which might be ready to merge soon.

marceloalcocer avatar May 29 '23 20:05 marceloalcocer