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:
mbedAustin
Date:
Thu Apr 30 19:07:47 2015 +0000
Revision:
6:9ca92d1d90dd
Parent:
4:d02fdd139307
Child:
7:d2c97b20d237
TCP Send / Receive Working

Who changed what in which revision?

UserRevisionLine numberNew contents of line
michaeljkoster 0:6a891da014a3 1 #include "mbed.h"
mbedAustin 3:5175e53017e4 2 #define DEBUG 1
michaeljkoster 0:6a891da014a3 3 #include "ESP8266Interface.h"
mbedAustin 6:9ca92d1d90dd 4 //#include "UDPSocket.h"
mbedAustin 6:9ca92d1d90dd 5 #include "TCPSocketConnection.h"
michaeljkoster 0:6a891da014a3 6 #include "Endpoint.h"
michaeljkoster 0:6a891da014a3 7
mbedAustin 3:5175e53017e4 8 RawSerial pc(USBTX, USBRX); // tx, rx
mbedAustin 3:5175e53017e4 9 ESP8266Interface wifi(D1,D0,D10,"demossid","password",115200); // TX,RX,Reset,SSID,Password
michaeljkoster 0:6a891da014a3 10
mbedAustin 6:9ca92d1d90dd 11 TCPSocketConnection tcp;
mbedAustin 6:9ca92d1d90dd 12 //bool UDP_blocking = false;
mbedAustin 6:9ca92d1d90dd 13 //unsigned int UDP_timeout = 100;
michaeljkoster 0:6a891da014a3 14
michaeljkoster 0:6a891da014a3 15
michaeljkoster 0:6a891da014a3 16 int main()
michaeljkoster 0:6a891da014a3 17 {
mbedAustin 3:5175e53017e4 18 pc.baud(115200);
mbedAustin 3:5175e53017e4 19 int check = 0;
mbedAustin 3:5175e53017e4 20 pc.printf("\n\rSystemCoreClock=: %d", SystemCoreClock / 1000000) ;
mbedAustin 3:5175e53017e4 21
mbedAustin 3:5175e53017e4 22 // Init ESP8266
mbedAustin 3:5175e53017e4 23 pc.printf("\r\nwifi.init: ");
mbedAustin 3:5175e53017e4 24 check = wifi.init(); // initialize the interface, reset the module
mbedAustin 3:5175e53017e4 25 pc.printf("%d",check);
mbedAustin 3:5175e53017e4 26
mbedAustin 3:5175e53017e4 27 // Initialize Socket
mbedAustin 3:5175e53017e4 28 pc.printf("\r\nwifi.connect: ");
mbedAustin 3:5175e53017e4 29 check = wifi.connect(); // join AP and get DHCP settings
mbedAustin 3:5175e53017e4 30 pc.printf("%d",check);
mbedAustin 3:5175e53017e4 31
mbedAustin 3:5175e53017e4 32 // printf("\r\nsocket init: ");
mbedAustin 3:5175e53017e4 33 // check = server.init();
mbedAustin 3:5175e53017e4 34 // printf("%d",check);
mbedAustin 3:5175e53017e4 35 //
mbedAustin 3:5175e53017e4 36 // printf("\r\nsocket bind: ");
mbedAustin 3:5175e53017e4 37 // check = server.bind(0);
mbedAustin 3:5175e53017e4 38 // printf("%d",check);
mbedAustin 3:5175e53017e4 39 //
mbedAustin 3:5175e53017e4 40 // printf("\r\nset blocking option");
mbedAustin 3:5175e53017e4 41 // server.set_blocking(UDP_blocking, UDP_timeout);
mbedAustin 3:5175e53017e4 42
mbedAustin 6:9ca92d1d90dd 43 check = tcp.connect("192.168.2.4",8080);
mbedAustin 6:9ca92d1d90dd 44 printf("\r\tcp.connect() result is: %d \r\n",check);
mbedAustin 6:9ca92d1d90dd 45
mbedAustin 6:9ca92d1d90dd 46 char testString[] = "Hello There!!";
mbedAustin 6:9ca92d1d90dd 47 // check = tcp.send((char *)testString, sizeof(testString));
mbedAustin 6:9ca92d1d90dd 48 // printf("\r\tcp.send() result is : %d",check);
mbedAustin 6:9ca92d1d90dd 49
mbedAustin 6:9ca92d1d90dd 50 // if(wifi.is_connected()) {
mbedAustin 6:9ca92d1d90dd 51 // pc.printf("\r\nConnected to Wifi!\r\n");
mbedAustin 6:9ca92d1d90dd 52 // } else {
mbedAustin 6:9ca92d1d90dd 53 // pc.printf("\r\nFailed to Connect to Wifi...\r\n");
mbedAustin 6:9ca92d1d90dd 54 // }
mbedAustin 6:9ca92d1d90dd 55
mbedAustin 6:9ca92d1d90dd 56 //pc.printf("\n\rIP = %s \n\r",wifi.getIPAddress());
mbedAustin 6:9ca92d1d90dd 57
mbedAustin 6:9ca92d1d90dd 58 char receiveString[30] = {0};
mbedAustin 6:9ca92d1d90dd 59 while(1) {
mbedAustin 6:9ca92d1d90dd 60 check = tcp.send((char *)testString, sizeof(testString));
mbedAustin 6:9ca92d1d90dd 61 check = tcp.receive(receiveString,sizeof(receiveString));
mbedAustin 6:9ca92d1d90dd 62 for(int x =0; x<check; x++) {
mbedAustin 6:9ca92d1d90dd 63 pc.putc(receiveString[x]);
mbedAustin 6:9ca92d1d90dd 64 }
mbedAustin 4:d02fdd139307 65 }
mbedAustin 3:5175e53017e4 66
mbedAustin 4:d02fdd139307 67 pc.printf("\r\n#####Starting Interactive Terminal:#####\r\n");
mbedAustin 3:5175e53017e4 68 while(1) {
mbedAustin 3:5175e53017e4 69 if(pc.readable()) {
mbedAustin 3:5175e53017e4 70 char c = pc.getc();
mbedAustin 3:5175e53017e4 71 wifi.putc(c);
mbedAustin 3:5175e53017e4 72 }
mbedAustin 3:5175e53017e4 73 if(wifi.readable()) {
mbedAustin 3:5175e53017e4 74 char c = wifi.getc();
mbedAustin 3:5175e53017e4 75 pc.putc(c);
mbedAustin 3:5175e53017e4 76 }
mbedAustin 3:5175e53017e4 77 }
michaeljkoster 0:6a891da014a3 78 }