Simple HTTP GET and POST with ESP8266.
Dependencies: ESP8266Interface HTTPClient-SSL WebSocketClient mbed-rtos mbed
Fork of ESP8266_HTTP_HelloWorld by
Diff: main.cpp
- Revision:
- 3:5175e53017e4
- Parent:
- 1:f61c1001ee60
- Child:
- 4:d02fdd139307
--- a/main.cpp Mon Apr 20 20:29:26 2015 +0000
+++ b/main.cpp Tue Apr 28 20:18:04 2015 +0000
@@ -1,151 +1,64 @@
#include "mbed.h"
+#define DEBUG 1
#include "ESP8266Interface.h"
-#include "nsdl_support.h"
-#include "dbg.h"
#include "UDPSocket.h"
#include "Endpoint.h"
-// Include resources
-#include "light.h"
-
-Serial pc(USBTX, USBRX); // tx, rx
-
-// ****************************************************************************
-// Configuration section
-
-
-// Ethernet configuration
-/* Define this to enable DHCP, otherwise manual address configuration is used */
-#define DHCP
-
-/* Manual IP configurations, if DHCP not defined */
-#define IP "10.0.0.199"
-#define MASK "255.255.255.0"
-#define GW "10.0.0.1"
-
-
-// NSP configuration
-/* Change this IP address to that of your NanoService Platform installation */
-//static const char* NSP_ADDRESS = "191.239.5.150"; // barista2.cloudapp.net
-static const char* NSP_ADDRESS = "108.201.184.41"; // smartobjectservice.com
-//static const char* NSP_ADDRESS = "192.168.1.200"; // local mDS server
-static const int NSP_PORT = 5683;
-char endpoint_name[24] = "mbedDEMO-";
-uint8_t ep_type[] = {"DEMO"};
-uint8_t lifetime_ptr[] = {"60"};
-
-// ****************************************************************************
-// Ethernet initialization
-
-
-//ESP8266Interface wifi(PA_15,PB_7,PA_14,"demo","ARMDEMO1"); //hack wire
-ESP8266Interface wifi(D8,D3,D10,"demo","ARMDEMO1"); //LED strip board
-
-
-static void ethernet_init()
-{
- pc.printf("init\r\n");
- wifi.init(); // initialize the interface, reset the module
-
- pc.printf("join\r\n");
- wifi.join(); // join AP and get DHCP settings
-}
-
-// ****************************************************************************
-// NSP initialization
+RawSerial pc(USBTX, USBRX); // tx, rx
+ESP8266Interface wifi(D1,D0,D10,"demossid","password",115200); // TX,RX,Reset,SSID,Password
UDPSocket server;
-Endpoint nsp;
bool UDP_blocking = false;
unsigned int UDP_timeout = 100;
uint8_t macbytes[6] = {0};
char MAC[20] = "665544332211";
-static void nsp_init()
-{
- //pc.printf("socket init\r\n");
- server.init();
- //pc.printf("socket bind\r\n");
- server.bind(0);
- pc.printf("set blocking option\r\n");
- server.set_blocking(UDP_blocking, UDP_timeout);
-
- pc.printf("set ep address\r\n");
- nsp.set_address(NSP_ADDRESS, NSP_PORT);
-
- strncat(endpoint_name, MAC, 12);
-
- NSDL_DEBUG("name: %s", endpoint_name);
- NSDL_DEBUG("NSP=%s - port %d\n", NSP_ADDRESS, NSP_PORT);
-
-}
-
-// ****************************************************************************
-// Resource creation
-
-static int create_resources()
-{
- sn_nsdl_resource_info_s *resource_ptr = NULL;
- sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
-
- NSDL_DEBUG("Creating resources");
-
- /* Create resources */
- resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s));
- if(!resource_ptr)
- return 0;
- memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s));
-
- resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s));
- if(!resource_ptr->resource_parameters_ptr)
- {
- nsdl_free(resource_ptr);
- return 0;
- }
- memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s));
-
- // Static resources
- nsdl_create_static_resource(resource_ptr, sizeof("3/0/0")-1, (uint8_t*) "3/0/0", 0, 0, (uint8_t*) "mbedDEMO", sizeof("mbedDEMO")-1);
- nsdl_create_static_resource(resource_ptr, sizeof("3/0/1")-1, (uint8_t*) "3/0/1", 0, 0, (uint8_t*) "DEMO", sizeof("DEMO")-1);
-
- // Dynamic resources
- create_light_resource(resource_ptr);
-
- /* Register with NSP */
- endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
- if(sn_nsdl_register_endpoint(endpoint_ptr) != 0)
- pc.printf("NSP registering failed\r\n");
- else
- pc.printf("NSP registering OK\r\n");
- nsdl_clean_register_endpoint(&endpoint_ptr);
-
- nsdl_free(resource_ptr->resource_parameters_ptr);
- nsdl_free(resource_ptr);
- return 1;
-}
-
-// ****************************************************************************
-// Program entry point
-
int main()
{
- pc.baud(9600); // why do we need to match the ESP8266 serial rate?
- pc.printf("SystemCoreClock=: %d\r\n", SystemCoreClock / 1000000) ;
- NSDL_DEBUG("Hello mDS Demo Endpoint Application\n");
-
- // Initialize Ethernet interface first
- ethernet_init();
-
- // Initialize NSP node
- nsp_init();
-
- // Initialize NSDL stack
- nsdl_init();
-
- // Create NSDL resources
- create_resources();
-
- // Run the NSDL event loop (never returns)
- nsdl_event_loop();
+ 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);
+ wifi.sendCommand("AT+CWJAP?", NULL, NULL, 1000);
+
+ check = wifi.is_connected();
+ pc.printf("\r\nwifi connected: %d,\r\n",check);
+
+ char * ip = NULL;
+ ip = wifi.getIPAddress();
+ pc.printf("\n\rIP = %s \n\r",ip);
+
+ pc.printf("\r\nStarting while1 loop\r\n");
+ while(1) {
+ if(pc.readable()) {
+ char c = pc.getc();
+ //pc.putc(c); // echo character back to computer for sanity
+ wifi.putc(c);
+ }
+ if(wifi.readable()) {
+ char c = wifi.getc();
+ pc.putc(c);
+ }
+ }
}
