jim hamblen / Mbed OS 4180_TCPSocket_Example

Files at this revision

API Documentation at this revision

Comitter:
4180_1
Date:
Wed Feb 17 18:55:05 2021 +0000
Parent:
5:66c4a71d22e9
Commit message:
ver 1.2 Added Basic IoT demo controlling LEDs from web page data

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Mon Feb 15 18:19:25 2021 +0000
+++ b/main.cpp	Wed Feb 17 18:55:05 2021 +0000
@@ -1,15 +1,16 @@
 #include "mbed.h"
 #include "EthernetInterface.h"
-
+BusOut leds(LED1,LED2,LED3,LED4); //LEDs are controlled by web page text data
 // Network interface
 EthernetInterface net;
 
 // Socket demo
-int main() {
+int main()
+{
     // Show MAC in case it is needed to enable DHCP on a secure network
     char mac[6];
     mbed_mac_address(mac);
-    printf("\r\rmbed's MAC address is %02x:%02x:%02x:%02x:%02x:%02x\n\r", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); 
+    printf("\r\rmbed's MAC address is %02x:%02x:%02x:%02x:%02x:%02x\n\r", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
     // Bring up the ethernet interface
     printf("Waiting for IP address from DHCP Server\n\r");
     wait(1.0);
@@ -35,11 +36,21 @@
     char rbuffer[400]; //enough for a very short text page - almost out of RAM!
     int rcount = socket.recv(rbuffer, sizeof rbuffer);
     rbuffer[rcount] = 0; //terminate to print as a C string;
+    //Print packet read from HTTP web page server
     printf("recv %d [%.*s]\n\r", rcount, strstr(rbuffer, "\r\n"), rbuffer);
 
     // Close the socket to return its memory and bring down the network interface
     socket.close();
 
+    // Basic IoT demo controlling mbeds 4 LEDs from Internet web page's ASCII text data
+    // Web page demo contains a line "Data:0101"
+    char *data;
+    data = strstr(rbuffer,"Data:"); //Find Data: line on web page with '0's or '1's
+    data = data + 5; //Skip to data
+    for (int i=0; i<=3; i++) //Parse 4 web page characters to control LEDs
+        leds[i] = data[i] - '0'; //convert ASCII '0' or '1's to binary to control 4 leds
+    //Another device could change the web page contents to control LEDs from anywhere
+
     // Bring down the ethernet interface
     net.disconnect();
     printf("Done\n\r");