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

Revision:
7:d2c97b20d237
Parent:
6:9ca92d1d90dd
Child:
8:af3b0b5438ad
diff -r 9ca92d1d90dd -r d2c97b20d237 main.cpp
--- a/main.cpp	Thu Apr 30 19:07:47 2015 +0000
+++ b/main.cpp	Thu Apr 30 21:26:41 2015 +0000
@@ -1,78 +1,35 @@
 #include "mbed.h"
-#define DEBUG 1
 #include "ESP8266Interface.h"
-//#include "UDPSocket.h"
 #include "TCPSocketConnection.h"
-#include "Endpoint.h"
-
-RawSerial pc(USBTX, USBRX); // tx, rx
+ 
+const char* ECHO_SERVER_ADDRESS = "192.168.2.4";
+const int ECHO_SERVER_PORT = 8080;
 ESP8266Interface wifi(D1,D0,D10,"demossid","password",115200); // TX,RX,Reset,SSID,Password
-
-TCPSocketConnection tcp;
-//bool UDP_blocking = false;
-//unsigned int UDP_timeout = 100;
-
-
-int main()
-{
+RawSerial pc(USBTX, USBRX); // tx, rx
+ 
+int main() {
     pc.baud(115200);
-    int check = 0;
-    pc.printf("\n\rSystemCoreClock=: %d", SystemCoreClock / 1000000) ;
-
-    // Init ESP8266
-    pc.printf("\r\nwifi.init: ");
-    check = wifi.init(); // initialize the interface, reset the module
-    pc.printf("%d",check);
-
-    // Initialize Socket
-    pc.printf("\r\nwifi.connect: ");
-    check = wifi.connect(); // join AP and get DHCP settings
-    pc.printf("%d",check);
-
-//    printf("\r\nsocket init: ");
-//    check = server.init();
-//    printf("%d",check);
-//
-//    printf("\r\nsocket bind: ");
-//    check = server.bind(0);
-//    printf("%d",check);
-//
-//    printf("\r\nset blocking option");
-//    server.set_blocking(UDP_blocking, UDP_timeout);
-
-    check = tcp.connect("192.168.2.4",8080);
-    printf("\r\tcp.connect() result is: %d \r\n",check);
-
-    char testString[] = "Hello There!!";
-   // check = tcp.send((char *)testString, sizeof(testString));
-//    printf("\r\tcp.send() result is : %d",check);
-
-//    if(wifi.is_connected()) {
-//        pc.printf("\r\nConnected to Wifi!\r\n");
-//    } else {
-//        pc.printf("\r\nFailed to Connect to Wifi...\r\n");
-//    }
-
-    //pc.printf("\n\rIP = %s \n\r",wifi.getIPAddress());
-
-    char receiveString[30] = {0};
-    while(1) {
-        check = tcp.send((char *)testString, sizeof(testString));
-        check = tcp.receive(receiveString,sizeof(receiveString));
-        for(int x =0; x<check; x++) {
-            pc.putc(receiveString[x]);
-        }
+    wifi.init(); //Use DHCP
+    wifi.connect();
+    pc.printf("IP Address is %s\n", wifi.getIPAddress());
+    
+    TCPSocketConnection socket;
+    while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) {
+        pc.printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
+        wait(1);
     }
-
-    pc.printf("\r\n#####Starting Interactive Terminal:#####\r\n");
-    while(1) {
-        if(pc.readable()) {
-            char c = pc.getc();
-            wifi.putc(c);
-        }
-        if(wifi.readable()) {
-            char c = wifi.getc();
-            pc.putc(c);
-        }
-    }
+    
+    char hello[] = "Hello World\n";
+    socket.send_all(hello, sizeof(hello) - 1);
+    
+    char buf[256];
+    int n = socket.receive(buf, 256);
+    buf[n] = '\0';
+    pc.printf("%s", buf);
+    
+    socket.close();
+    wifi.disconnect();
+    
+    while(true) {}
 }
+ 
\ No newline at end of file