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:
Tue Jun 09 17:49:47 2015 +0000
Revision:
18:3209e51b731a
Parent:
17:59097e8b5c60
Unincluded TCPSocketServer-not needed

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 "Websocket.h"
sarahmarshy 12:978788c2156c 6
sarahmarshy 17:59097e8b5c60 7 ESP8266Interface wifi(D1,D0,D2,"demossid","password",115200); // TX,RX,Reset,SSID,Password,Baud
mbedAustin 7:d2c97b20d237 8 RawSerial pc(USBTX, USBRX); // tx, rx
sarahmarshy 14:1f0a842f8750 9 HTTPClient http;
sarahmarshy 14:1f0a842f8750 10 char str[512];
sarahmarshy 14:1f0a842f8750 11 int main()
sarahmarshy 14:1f0a842f8750 12 {
sarahmarshy 16:e3d152c9f6e0 13 pc.baud(9600);
sarahmarshy 12:978788c2156c 14 wifi.init(); //Reset
sarahmarshy 12:978788c2156c 15 wifi.connect(); //Use DHCP
mbedAustin 7:d2c97b20d237 16
sarahmarshy 14:1f0a842f8750 17 //GET
sarahmarshy 16:e3d152c9f6e0 18 pc.printf("\nTrying to fetch page using GET...\n\r");
sarahmarshy 17:59097e8b5c60 19 int ret = http.get("http://54.175.222.246/get", str, 512);//IP address is httpbin.org/get
sarahmarshy 14:1f0a842f8750 20 if (!ret) {
sarahmarshy 16:e3d152c9f6e0 21 pc.printf("Page fetched successfully - read %d characters\n\r", strlen(str));
sarahmarshy 16:e3d152c9f6e0 22 pc.printf("Result: %s\n\r", str);
sarahmarshy 14:1f0a842f8750 23 } else {
sarahmarshy 16:e3d152c9f6e0 24 pc.printf("Error - ret = %d - HTTP return code = %d\n\r", ret, http.getHTTPResponseCode());
sarahmarshy 12:978788c2156c 25 }
sarahmarshy 14:1f0a842f8750 26 //POST
sarahmarshy 14:1f0a842f8750 27 HTTPMap map;
sarahmarshy 14:1f0a842f8750 28 HTTPText inText(str, 512);
sarahmarshy 14:1f0a842f8750 29 map.put("Hello", "World");
sarahmarshy 14:1f0a842f8750 30 map.put("test", "1234");
sarahmarshy 16:e3d152c9f6e0 31 printf("\nTrying to POST data to httpbin.org/post...\n\r");
sarahmarshy 16:e3d152c9f6e0 32 ret = http.post("http://54.175.222.246/post", map, &inText);//IP address is httpbin.org/post
sarahmarshy 14:1f0a842f8750 33 if (!ret)
sarahmarshy 14:1f0a842f8750 34 {
sarahmarshy 16:e3d152c9f6e0 35 printf("Executed POST successfully - read %d characters\n\r", strlen(str));
sarahmarshy 16:e3d152c9f6e0 36 printf("Result: %s\n\r", str);
sarahmarshy 14:1f0a842f8750 37 }
sarahmarshy 14:1f0a842f8750 38 else
sarahmarshy 14:1f0a842f8750 39 {
sarahmarshy 16:e3d152c9f6e0 40 printf("Error - ret = %d - HTTP return code = %d\n\r", ret, http.getHTTPResponseCode());
sarahmarshy 14:1f0a842f8750 41 }
sarahmarshy 14:1f0a842f8750 42
mbedAustin 9:91fe783e1dd4 43 }
sarahmarshy 14:1f0a842f8750 44