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:
michaeljkoster
Date:
Mon Apr 20 20:27:34 2015 +0000
Revision:
1:f61c1001ee60
Parent:
0:6a891da014a3
Child:
3:5175e53017e4
ESP8266 test program

Who changed what in which revision?

UserRevisionLine numberNew contents of line
michaeljkoster 0:6a891da014a3 1 #include "mbed.h"
michaeljkoster 0:6a891da014a3 2 #include "ESP8266Interface.h"
michaeljkoster 0:6a891da014a3 3 #include "nsdl_support.h"
michaeljkoster 0:6a891da014a3 4 #include "dbg.h"
michaeljkoster 0:6a891da014a3 5 #include "UDPSocket.h"
michaeljkoster 0:6a891da014a3 6 #include "Endpoint.h"
michaeljkoster 0:6a891da014a3 7
michaeljkoster 0:6a891da014a3 8 // Include resources
michaeljkoster 0:6a891da014a3 9 #include "light.h"
michaeljkoster 0:6a891da014a3 10
michaeljkoster 0:6a891da014a3 11 Serial pc(USBTX, USBRX); // tx, rx
michaeljkoster 0:6a891da014a3 12
michaeljkoster 0:6a891da014a3 13 // ****************************************************************************
michaeljkoster 0:6a891da014a3 14 // Configuration section
michaeljkoster 0:6a891da014a3 15
michaeljkoster 0:6a891da014a3 16
michaeljkoster 0:6a891da014a3 17 // Ethernet configuration
michaeljkoster 0:6a891da014a3 18 /* Define this to enable DHCP, otherwise manual address configuration is used */
michaeljkoster 0:6a891da014a3 19 #define DHCP
michaeljkoster 0:6a891da014a3 20
michaeljkoster 0:6a891da014a3 21 /* Manual IP configurations, if DHCP not defined */
michaeljkoster 0:6a891da014a3 22 #define IP "10.0.0.199"
michaeljkoster 0:6a891da014a3 23 #define MASK "255.255.255.0"
michaeljkoster 0:6a891da014a3 24 #define GW "10.0.0.1"
michaeljkoster 0:6a891da014a3 25
michaeljkoster 0:6a891da014a3 26
michaeljkoster 0:6a891da014a3 27 // NSP configuration
michaeljkoster 0:6a891da014a3 28 /* Change this IP address to that of your NanoService Platform installation */
michaeljkoster 0:6a891da014a3 29 //static const char* NSP_ADDRESS = "191.239.5.150"; // barista2.cloudapp.net
michaeljkoster 0:6a891da014a3 30 static const char* NSP_ADDRESS = "108.201.184.41"; // smartobjectservice.com
michaeljkoster 0:6a891da014a3 31 //static const char* NSP_ADDRESS = "192.168.1.200"; // local mDS server
michaeljkoster 0:6a891da014a3 32 static const int NSP_PORT = 5683;
michaeljkoster 0:6a891da014a3 33 char endpoint_name[24] = "mbedDEMO-";
michaeljkoster 0:6a891da014a3 34 uint8_t ep_type[] = {"DEMO"};
michaeljkoster 0:6a891da014a3 35 uint8_t lifetime_ptr[] = {"60"};
michaeljkoster 0:6a891da014a3 36
michaeljkoster 0:6a891da014a3 37 // ****************************************************************************
michaeljkoster 0:6a891da014a3 38 // Ethernet initialization
michaeljkoster 0:6a891da014a3 39
michaeljkoster 0:6a891da014a3 40
michaeljkoster 1:f61c1001ee60 41 //ESP8266Interface wifi(PA_15,PB_7,PA_14,"demo","ARMDEMO1"); //hack wire
michaeljkoster 1:f61c1001ee60 42 ESP8266Interface wifi(D8,D3,D10,"demo","ARMDEMO1"); //LED strip board
michaeljkoster 0:6a891da014a3 43
michaeljkoster 0:6a891da014a3 44
michaeljkoster 0:6a891da014a3 45 static void ethernet_init()
michaeljkoster 0:6a891da014a3 46 {
michaeljkoster 0:6a891da014a3 47 pc.printf("init\r\n");
michaeljkoster 0:6a891da014a3 48 wifi.init(); // initialize the interface, reset the module
michaeljkoster 0:6a891da014a3 49
michaeljkoster 0:6a891da014a3 50 pc.printf("join\r\n");
michaeljkoster 0:6a891da014a3 51 wifi.join(); // join AP and get DHCP settings
michaeljkoster 0:6a891da014a3 52 }
michaeljkoster 0:6a891da014a3 53
michaeljkoster 0:6a891da014a3 54 // ****************************************************************************
michaeljkoster 0:6a891da014a3 55 // NSP initialization
michaeljkoster 0:6a891da014a3 56
michaeljkoster 0:6a891da014a3 57 UDPSocket server;
michaeljkoster 0:6a891da014a3 58 Endpoint nsp;
michaeljkoster 0:6a891da014a3 59 bool UDP_blocking = false;
michaeljkoster 0:6a891da014a3 60 unsigned int UDP_timeout = 100;
michaeljkoster 0:6a891da014a3 61
michaeljkoster 0:6a891da014a3 62 uint8_t macbytes[6] = {0};
michaeljkoster 1:f61c1001ee60 63 char MAC[20] = "665544332211";
michaeljkoster 0:6a891da014a3 64
michaeljkoster 0:6a891da014a3 65 static void nsp_init()
michaeljkoster 0:6a891da014a3 66 {
michaeljkoster 0:6a891da014a3 67 //pc.printf("socket init\r\n");
michaeljkoster 0:6a891da014a3 68 server.init();
michaeljkoster 0:6a891da014a3 69 //pc.printf("socket bind\r\n");
michaeljkoster 0:6a891da014a3 70 server.bind(0);
michaeljkoster 0:6a891da014a3 71 pc.printf("set blocking option\r\n");
michaeljkoster 0:6a891da014a3 72 server.set_blocking(UDP_blocking, UDP_timeout);
michaeljkoster 0:6a891da014a3 73
michaeljkoster 0:6a891da014a3 74 pc.printf("set ep address\r\n");
michaeljkoster 0:6a891da014a3 75 nsp.set_address(NSP_ADDRESS, NSP_PORT);
michaeljkoster 0:6a891da014a3 76
michaeljkoster 0:6a891da014a3 77 strncat(endpoint_name, MAC, 12);
michaeljkoster 0:6a891da014a3 78
michaeljkoster 0:6a891da014a3 79 NSDL_DEBUG("name: %s", endpoint_name);
michaeljkoster 0:6a891da014a3 80 NSDL_DEBUG("NSP=%s - port %d\n", NSP_ADDRESS, NSP_PORT);
michaeljkoster 0:6a891da014a3 81
michaeljkoster 0:6a891da014a3 82 }
michaeljkoster 0:6a891da014a3 83
michaeljkoster 0:6a891da014a3 84 // ****************************************************************************
michaeljkoster 0:6a891da014a3 85 // Resource creation
michaeljkoster 0:6a891da014a3 86
michaeljkoster 0:6a891da014a3 87 static int create_resources()
michaeljkoster 0:6a891da014a3 88 {
michaeljkoster 0:6a891da014a3 89 sn_nsdl_resource_info_s *resource_ptr = NULL;
michaeljkoster 0:6a891da014a3 90 sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
michaeljkoster 0:6a891da014a3 91
michaeljkoster 0:6a891da014a3 92 NSDL_DEBUG("Creating resources");
michaeljkoster 0:6a891da014a3 93
michaeljkoster 0:6a891da014a3 94 /* Create resources */
michaeljkoster 0:6a891da014a3 95 resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s));
michaeljkoster 0:6a891da014a3 96 if(!resource_ptr)
michaeljkoster 0:6a891da014a3 97 return 0;
michaeljkoster 0:6a891da014a3 98 memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s));
michaeljkoster 0:6a891da014a3 99
michaeljkoster 0:6a891da014a3 100 resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s));
michaeljkoster 0:6a891da014a3 101 if(!resource_ptr->resource_parameters_ptr)
michaeljkoster 0:6a891da014a3 102 {
michaeljkoster 0:6a891da014a3 103 nsdl_free(resource_ptr);
michaeljkoster 0:6a891da014a3 104 return 0;
michaeljkoster 0:6a891da014a3 105 }
michaeljkoster 0:6a891da014a3 106 memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s));
michaeljkoster 0:6a891da014a3 107
michaeljkoster 0:6a891da014a3 108 // Static resources
michaeljkoster 0:6a891da014a3 109 nsdl_create_static_resource(resource_ptr, sizeof("3/0/0")-1, (uint8_t*) "3/0/0", 0, 0, (uint8_t*) "mbedDEMO", sizeof("mbedDEMO")-1);
michaeljkoster 0:6a891da014a3 110 nsdl_create_static_resource(resource_ptr, sizeof("3/0/1")-1, (uint8_t*) "3/0/1", 0, 0, (uint8_t*) "DEMO", sizeof("DEMO")-1);
michaeljkoster 0:6a891da014a3 111
michaeljkoster 0:6a891da014a3 112 // Dynamic resources
michaeljkoster 0:6a891da014a3 113 create_light_resource(resource_ptr);
michaeljkoster 0:6a891da014a3 114
michaeljkoster 0:6a891da014a3 115 /* Register with NSP */
michaeljkoster 0:6a891da014a3 116 endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
michaeljkoster 0:6a891da014a3 117 if(sn_nsdl_register_endpoint(endpoint_ptr) != 0)
michaeljkoster 0:6a891da014a3 118 pc.printf("NSP registering failed\r\n");
michaeljkoster 0:6a891da014a3 119 else
michaeljkoster 0:6a891da014a3 120 pc.printf("NSP registering OK\r\n");
michaeljkoster 0:6a891da014a3 121 nsdl_clean_register_endpoint(&endpoint_ptr);
michaeljkoster 0:6a891da014a3 122
michaeljkoster 0:6a891da014a3 123 nsdl_free(resource_ptr->resource_parameters_ptr);
michaeljkoster 0:6a891da014a3 124 nsdl_free(resource_ptr);
michaeljkoster 0:6a891da014a3 125 return 1;
michaeljkoster 0:6a891da014a3 126 }
michaeljkoster 0:6a891da014a3 127
michaeljkoster 0:6a891da014a3 128 // ****************************************************************************
michaeljkoster 0:6a891da014a3 129 // Program entry point
michaeljkoster 0:6a891da014a3 130
michaeljkoster 0:6a891da014a3 131 int main()
michaeljkoster 0:6a891da014a3 132 {
michaeljkoster 1:f61c1001ee60 133 pc.baud(9600); // why do we need to match the ESP8266 serial rate?
michaeljkoster 0:6a891da014a3 134 pc.printf("SystemCoreClock=: %d\r\n", SystemCoreClock / 1000000) ;
michaeljkoster 0:6a891da014a3 135 NSDL_DEBUG("Hello mDS Demo Endpoint Application\n");
michaeljkoster 0:6a891da014a3 136
michaeljkoster 0:6a891da014a3 137 // Initialize Ethernet interface first
michaeljkoster 0:6a891da014a3 138 ethernet_init();
michaeljkoster 0:6a891da014a3 139
michaeljkoster 0:6a891da014a3 140 // Initialize NSP node
michaeljkoster 0:6a891da014a3 141 nsp_init();
michaeljkoster 0:6a891da014a3 142
michaeljkoster 0:6a891da014a3 143 // Initialize NSDL stack
michaeljkoster 0:6a891da014a3 144 nsdl_init();
michaeljkoster 0:6a891da014a3 145
michaeljkoster 0:6a891da014a3 146 // Create NSDL resources
michaeljkoster 0:6a891da014a3 147 create_resources();
michaeljkoster 0:6a891da014a3 148
michaeljkoster 0:6a891da014a3 149 // Run the NSDL event loop (never returns)
michaeljkoster 0:6a891da014a3 150 nsdl_event_loop();
michaeljkoster 0:6a891da014a3 151 }