Example program to create IoT devices for a local network, which connect to a local server.

Dependencies:   WebSocketClient WiflyInterface mbed messages

This code is used in the second part of my Internet of Things (IoT) blog post available here. The code is fairly simple, but its real value is in its reliability. I have worked hard to try to make the wireless connection as reliable, and as fast, as possible. There are a few lines of code that must be modified before it will work correctly, and those are described in the following Wiki pages.

It is designed to work with a Python WebSocket Server running on a PC, the source code of which is available here.

Once operating with the server, each microcontroller, or IoT device, will broadcast a counter and its internal temperature to your WebSocket Server.

Revision:
0:c5607b31fb07
Child:
1:4403f2ed1c1f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Oct 04 11:34:47 2016 +0000
@@ -0,0 +1,94 @@
+#include "mbed.h"
+#include "iQ_globals.h"
+#include "WiflyInterface.h"
+#include "iQ_Commands.h"
+#include "Websocket.h"
+
+//#define DEBUG
+#define INFOMESSAGES
+#define WARNMESSAGES
+#define ERRMESSAGES
+#define FUNCNAME "IoT"
+#include "messages.h"
+
+// Main Loop!
+int main() {
+    unsigned int wifi_cmd = NO_WIFI_CMD;
+    float wifi_data = 0.0f;
+    unsigned int wifi_var = 0;
+    
+    // Set the IoT ID:
+    IoT_ID = 1;
+    
+    // Send a startup message to serial port:
+    INFO("Starting up...");
+    INFO("CPU SystemCoreClock is %d Hz", SystemCoreClock); 
+        
+    
+    SetupNetwork(5000);
+    // Configure the baud rate of the wifi shield:
+    ws.setBaud(115200);
+    wait(0.5f);
+    
+    
+    
+    char msg[128];
+    if(IotStatus.CheckFlag(SF_WIRELESSCONNECTED)){
+        sprintf(msg, "ws://%s:%d/ws", SERVER_IP, WS_PORT);
+        ws.Initialize(msg);
+        INFO("Connecting to Websocket Server on %s...", msg);
+        if(ws.connect()){
+            // Set a status flag:
+            INFO("Connected.");
+            IotStatus.SetFlag(SF_SERVERCONNECTED);
+        }else{
+            // We could not connect right now..
+            IotStatus.ClearFlag(SF_SERVERCONNECTED);
+            INFO("Could not connect to server, will try again later.");
+            ReconnectAttempts++;
+            if(ReconnectAttempts > 5){
+                INFO("Failed after %d reconnect attempts. Resetting the Wifi Shield...", ReconnectAttempts);
+                SetupNetwork(1);
+                ReconnectAttempts = 0;
+            }
+        }
+    }
+    
+    
+    DisplayTimer.start();
+    // Inifinite loop:
+    while(1) {
+        
+        // Process the wifi command:
+        if(wifi_cmd > NO_WIFI_CMD){
+            switch(wifi_cmd){
+                case CHANGEVAR_WIFI_CMD:
+                    ModifyVariable(wifi_var, wifi_data);
+                    break;
+                default:
+                    break;
+            }
+            wifi_cmd = NO_WIFI_CMD;
+        }
+        
+        // Check for new wifi data:
+        if((wifi_cmd == NO_WIFI_CMD)){
+            ReceiveNetworkData(&wifi_cmd, &wifi_var, &wifi_data);
+        }
+        
+        // Send the network data every 3 seconds:
+        if(DisplayTimer.read()>(3.0f)){
+            
+            // Send data over network:
+            SendNetworkData();               
+            
+            // Increment a counter:
+            SendCounter++;
+            
+            // Reset the timer:
+            DisplayTimer.reset();
+        }
+        
+       
+    }
+}