Basic websocket demo esp8266
Dependencies: ESP8266Interface HTTPClient-SSL WebSocketClient mbed-rtos mbed-dev
Fork of ESP8266_HTTP_HelloWorld by
main.cpp@6:9ca92d1d90dd, 2015-04-30 (annotated)
- 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?
User | Revision | Line number | New 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 | } |