HTTP Client with ESP8266 working example

Dependencies:   ESP8266Interface HTTPClient-SSL WebSocketClient mbed-rtos mbed

Fork of ESP8266_WebSockets_HelloWorld by ESP8266

This repository has been superceded

This project has moved to https://developer.mbed.org/teams/ESP8266/code/mbed-os-example-esp8266/

Introduction

This is a basic Hello World program for interfacing the ESP8266 chip with HTTP. It will execute a GET on httpbin.org/get, and a POST to httpbin.org/post.

ESP8266_HTTP_HelloWorld

  1. change ssid and passphrase to match your wifi connection
  2. Initialize ESP8266 with DHCP enabled
  3. Get mbed.org
    1. Print the information retrieved
  4. Post to httbin.org
    1. Print the information retrieved

You can view the information retrieved through a serial terminal set to 9600 baud.

Successful Execution

you should see something like the following on the terminal if the program successfully executes. /media/uploads/mbedAustin/httpclient.png

Committer:
sarahmarshy
Date:
Fri Jun 05 16:17:26 2015 +0000
Revision:
14:1f0a842f8750
Parent:
13:5f2bb0dc134b
Child:
15:4b246689e0e2
Main program working

Who changed what in which revision?

UserRevisionLine numberNew contents of line
michaeljkoster 0:6a891da014a3 1 #include "mbed.h"
sarahmarshy 14:1f0a842f8750 2 #include "HTTPClient.h"
michaeljkoster 0:6a891da014a3 3 #include "ESP8266Interface.h"
mbedAustin 9:91fe783e1dd4 4 #include "TCPSocketConnection.h"
sarahmarshy 12:978788c2156c 5 #include "TCPSocketServer.h"
sarahmarshy 12:978788c2156c 6 #include "Websocket.h"
sarahmarshy 12:978788c2156c 7
sarahmarshy 14:1f0a842f8750 8 ESP8266Interface wifi(D1,D0,D2,"iotlab","42F67YxLX4AawRdcj",115200); // TX,RX,Reset,SSID,Password,Baud
mbedAustin 7:d2c97b20d237 9 RawSerial pc(USBTX, USBRX); // tx, rx
sarahmarshy 14:1f0a842f8750 10 HTTPClient http;
sarahmarshy 14:1f0a842f8750 11 char str[512];
sarahmarshy 14:1f0a842f8750 12 int main()
sarahmarshy 14:1f0a842f8750 13 {
mbedAustin 3:5175e53017e4 14 pc.baud(115200);
sarahmarshy 12:978788c2156c 15 wifi.init(); //Reset
sarahmarshy 12:978788c2156c 16 wifi.connect(); //Use DHCP
mbedAustin 7:d2c97b20d237 17
sarahmarshy 14:1f0a842f8750 18 //GET
sarahmarshy 14:1f0a842f8750 19 pc.printf("\nTrying to fetch page...\n");
sarahmarshy 14:1f0a842f8750 20 int ret = http.get("http://191.239.57.70/", str, 128);
sarahmarshy 14:1f0a842f8750 21 if (!ret) {
sarahmarshy 14:1f0a842f8750 22 pc.printf("Page fetched successfully - read %d characters\n", strlen(str));
sarahmarshy 14:1f0a842f8750 23 pc.printf("Result: %s\n", str);
sarahmarshy 14:1f0a842f8750 24 } else {
sarahmarshy 14:1f0a842f8750 25 pc.printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
sarahmarshy 12:978788c2156c 26 }
sarahmarshy 14:1f0a842f8750 27 //POST
sarahmarshy 14:1f0a842f8750 28 HTTPMap map;
sarahmarshy 14:1f0a842f8750 29 HTTPText inText(str, 512);
sarahmarshy 14:1f0a842f8750 30 map.put("Hello", "World");
sarahmarshy 14:1f0a842f8750 31 map.put("test", "1234");
sarahmarshy 14:1f0a842f8750 32 printf("\nTrying to post data...\n");
sarahmarshy 14:1f0a842f8750 33 ret = http.post("http://54.175.222.246/post", map, &inText);
sarahmarshy 14:1f0a842f8750 34 if (!ret)
sarahmarshy 14:1f0a842f8750 35 {
sarahmarshy 14:1f0a842f8750 36 printf("Executed POST successfully - read %d characters\n", strlen(str));
sarahmarshy 14:1f0a842f8750 37 printf("Result: %s\n", str);
sarahmarshy 14:1f0a842f8750 38 }
sarahmarshy 14:1f0a842f8750 39 else
sarahmarshy 14:1f0a842f8750 40 {
sarahmarshy 14:1f0a842f8750 41 printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
sarahmarshy 14:1f0a842f8750 42 }
sarahmarshy 14:1f0a842f8750 43
mbedAustin 9:91fe783e1dd4 44 }
sarahmarshy 14:1f0a842f8750 45