ESP8266 / Mbed 2 deprecated ESP8266_HTTP_HelloWorld

Dependencies:   ESP8266Interface HTTPClient-SSL WebSocketClient mbed-rtos mbed

Fork of ESP8266_WebSockets_HelloWorld by ESP8266

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "HTTPClient.h"
00003 #include "ESP8266Interface.h"
00004 #include "TCPSocketConnection.h"
00005 #include "Websocket.h"
00006 
00007 ESP8266Interface wifi(D1,D0,D2,"demossid","password",115200); // TX,RX,Reset,SSID,Password,Baud
00008 RawSerial pc(USBTX, USBRX); // tx, rx
00009 HTTPClient http;
00010 char str[512];
00011 int main()
00012 {
00013     pc.baud(9600);
00014     wifi.init(); //Reset
00015     wifi.connect(); //Use DHCP
00016     
00017     //GET
00018     pc.printf("\nTrying to fetch page using GET...\n\r");
00019     int ret = http.get("http://54.175.222.246/get", str, 512);//IP address is httpbin.org/get
00020     if (!ret) {
00021         pc.printf("Page fetched successfully - read %d characters\n\r", strlen(str));
00022         pc.printf("Result: %s\n\r", str);
00023     } else {
00024         pc.printf("Error - ret = %d - HTTP return code = %d\n\r", ret, http.getHTTPResponseCode());
00025     }
00026     //POST
00027     HTTPMap map;
00028     HTTPText inText(str, 512);
00029     map.put("Hello", "World");
00030     map.put("test", "1234");
00031     printf("\nTrying to POST data to httpbin.org/post...\n\r");
00032     ret = http.post("http://54.175.222.246/post", map, &inText);//IP address is httpbin.org/post
00033     if (!ret)
00034     {
00035       printf("Executed POST successfully - read %d characters\n\r", strlen(str));
00036       printf("Result: %s\n\r", str);
00037     }
00038     else
00039     {
00040       printf("Error - ret = %d - HTTP return code = %d\n\r", ret, http.getHTTPResponseCode());
00041     }
00042 
00043 }
00044