Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
7 years, 3 months ago.
WiFiClient ?
Are there any WiFi repositories or examples that would allow for simple HTTP calls? I did see the wifi client and server examples, but haven't been able to get them to do what I'm after. Please forgive me as mbed is a new venture for me. What I did see I could plug in my RemoteIP, but couldn't append anything to it.
Something like:
char server[] = "192.168.0.4"
if (client.connect(server, 80)) { Serial.println("connected to server"); Make a HTTP request: client.println("GET /X HTTP/1.1"); client.println("Host: 192.168.0.4"); client.println("Connection: close");
All I'm looking to do is make the board GET from 192.168.0.4/X.
Thank you
Question relating to:
2 Answers
7 years, 3 months ago.
I wish somebody can adapt the WiFi drivers provided by ST to any of the Client libraries available on Mbed. Unfortunately I don't have the expertise and time to work on it, so I'm just working with the low level functions for now, in the same way they have been used in the provided 2 examples.
Here's how I do a POST request for example:
char *InfluxServerUrl = DB_SERVER_URL; uint16_t InfluxServerPort = DB_SERVER_PORT; uint8_t socket_id = 0; static uint8_t http_request[1024]; char request_body[512]; static uint8_t http_resp[512]; uint16_t reqLen; uint16_t respLen; char buffer[128];
sprintf((char *)http_request, "POST %s HTTP/1.1\r\nHost: %s \r\n", "/writedb=dbName&precision=s&rp=rpMame",InfluxServerUrl); strcat((char *)http_request, "Accept: */*\r\n"); strcat((char *)http_request, "User-agent: ES-WIFI TcpClient\r\n"); strcat((char *)http_request, "Connection: Close\r\n"; sprintf(buffer, "Content-Length: %d \r\n\r\n", strlen(request_body)); strcat((char *)http_request, buffer); append body to the request strcat((char *)http_request, request_body);
reqLen = strlen((char *)http_request); printf("> Sending a POST request with length=%d including a body length=%d\n", reqLen, strlen(request_body)); printf((char *)http_request);
uint16_t dataLen;
if (WIFI_SendData(socket_id, http_request, reqLen, &dataLen, WIFI_WRITE_TIMEOUT) != WIFI_STATUS_OK) { socket_id = 0; reset the socket id so a new connection can be attempted }
7 years, 3 months ago.
http-example works on every WiFi driver that implements the NetworkStack
API, that includes X-NUCLEO-IDW01M1 shield, ODIN-W2 dev board and ESP8266. Unfortunately the DISCO board you mention does not implement this API... It shouldn't be too hard to wrap it though.
In the mean time you can use mbed-http (the HTTP library underneath http-example) to construct HTTP requests and parse HTTP response while using https://developer.mbed.org/teams/ST/code/DISCO_IOT-wifi_client/ for network comms.