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.

Committer:
defrost
Date:
Tue Oct 04 11:34:47 2016 +0000
Revision:
0:c5607b31fb07
Child:
1:4403f2ed1c1f
- Initial Commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
defrost 0:c5607b31fb07 1 #include "mbed.h"
defrost 0:c5607b31fb07 2 #include "iQ_globals.h"
defrost 0:c5607b31fb07 3 #include "WiflyInterface.h"
defrost 0:c5607b31fb07 4 #include "iQ_Commands.h"
defrost 0:c5607b31fb07 5 #include "Websocket.h"
defrost 0:c5607b31fb07 6
defrost 0:c5607b31fb07 7 //#define DEBUG
defrost 0:c5607b31fb07 8 #define INFOMESSAGES
defrost 0:c5607b31fb07 9 #define WARNMESSAGES
defrost 0:c5607b31fb07 10 #define ERRMESSAGES
defrost 0:c5607b31fb07 11 #define FUNCNAME "IoT"
defrost 0:c5607b31fb07 12 #include "messages.h"
defrost 0:c5607b31fb07 13
defrost 0:c5607b31fb07 14 // Main Loop!
defrost 0:c5607b31fb07 15 int main() {
defrost 0:c5607b31fb07 16 unsigned int wifi_cmd = NO_WIFI_CMD;
defrost 0:c5607b31fb07 17 float wifi_data = 0.0f;
defrost 0:c5607b31fb07 18 unsigned int wifi_var = 0;
defrost 0:c5607b31fb07 19
defrost 0:c5607b31fb07 20 // Set the IoT ID:
defrost 0:c5607b31fb07 21 IoT_ID = 1;
defrost 0:c5607b31fb07 22
defrost 0:c5607b31fb07 23 // Send a startup message to serial port:
defrost 0:c5607b31fb07 24 INFO("Starting up...");
defrost 0:c5607b31fb07 25 INFO("CPU SystemCoreClock is %d Hz", SystemCoreClock);
defrost 0:c5607b31fb07 26
defrost 0:c5607b31fb07 27
defrost 0:c5607b31fb07 28 SetupNetwork(5000);
defrost 0:c5607b31fb07 29 // Configure the baud rate of the wifi shield:
defrost 0:c5607b31fb07 30 ws.setBaud(115200);
defrost 0:c5607b31fb07 31 wait(0.5f);
defrost 0:c5607b31fb07 32
defrost 0:c5607b31fb07 33
defrost 0:c5607b31fb07 34
defrost 0:c5607b31fb07 35 char msg[128];
defrost 0:c5607b31fb07 36 if(IotStatus.CheckFlag(SF_WIRELESSCONNECTED)){
defrost 0:c5607b31fb07 37 sprintf(msg, "ws://%s:%d/ws", SERVER_IP, WS_PORT);
defrost 0:c5607b31fb07 38 ws.Initialize(msg);
defrost 0:c5607b31fb07 39 INFO("Connecting to Websocket Server on %s...", msg);
defrost 0:c5607b31fb07 40 if(ws.connect()){
defrost 0:c5607b31fb07 41 // Set a status flag:
defrost 0:c5607b31fb07 42 INFO("Connected.");
defrost 0:c5607b31fb07 43 IotStatus.SetFlag(SF_SERVERCONNECTED);
defrost 0:c5607b31fb07 44 }else{
defrost 0:c5607b31fb07 45 // We could not connect right now..
defrost 0:c5607b31fb07 46 IotStatus.ClearFlag(SF_SERVERCONNECTED);
defrost 0:c5607b31fb07 47 INFO("Could not connect to server, will try again later.");
defrost 0:c5607b31fb07 48 ReconnectAttempts++;
defrost 0:c5607b31fb07 49 if(ReconnectAttempts > 5){
defrost 0:c5607b31fb07 50 INFO("Failed after %d reconnect attempts. Resetting the Wifi Shield...", ReconnectAttempts);
defrost 0:c5607b31fb07 51 SetupNetwork(1);
defrost 0:c5607b31fb07 52 ReconnectAttempts = 0;
defrost 0:c5607b31fb07 53 }
defrost 0:c5607b31fb07 54 }
defrost 0:c5607b31fb07 55 }
defrost 0:c5607b31fb07 56
defrost 0:c5607b31fb07 57
defrost 0:c5607b31fb07 58 DisplayTimer.start();
defrost 0:c5607b31fb07 59 // Inifinite loop:
defrost 0:c5607b31fb07 60 while(1) {
defrost 0:c5607b31fb07 61
defrost 0:c5607b31fb07 62 // Process the wifi command:
defrost 0:c5607b31fb07 63 if(wifi_cmd > NO_WIFI_CMD){
defrost 0:c5607b31fb07 64 switch(wifi_cmd){
defrost 0:c5607b31fb07 65 case CHANGEVAR_WIFI_CMD:
defrost 0:c5607b31fb07 66 ModifyVariable(wifi_var, wifi_data);
defrost 0:c5607b31fb07 67 break;
defrost 0:c5607b31fb07 68 default:
defrost 0:c5607b31fb07 69 break;
defrost 0:c5607b31fb07 70 }
defrost 0:c5607b31fb07 71 wifi_cmd = NO_WIFI_CMD;
defrost 0:c5607b31fb07 72 }
defrost 0:c5607b31fb07 73
defrost 0:c5607b31fb07 74 // Check for new wifi data:
defrost 0:c5607b31fb07 75 if((wifi_cmd == NO_WIFI_CMD)){
defrost 0:c5607b31fb07 76 ReceiveNetworkData(&wifi_cmd, &wifi_var, &wifi_data);
defrost 0:c5607b31fb07 77 }
defrost 0:c5607b31fb07 78
defrost 0:c5607b31fb07 79 // Send the network data every 3 seconds:
defrost 0:c5607b31fb07 80 if(DisplayTimer.read()>(3.0f)){
defrost 0:c5607b31fb07 81
defrost 0:c5607b31fb07 82 // Send data over network:
defrost 0:c5607b31fb07 83 SendNetworkData();
defrost 0:c5607b31fb07 84
defrost 0:c5607b31fb07 85 // Increment a counter:
defrost 0:c5607b31fb07 86 SendCounter++;
defrost 0:c5607b31fb07 87
defrost 0:c5607b31fb07 88 // Reset the timer:
defrost 0:c5607b31fb07 89 DisplayTimer.reset();
defrost 0:c5607b31fb07 90 }
defrost 0:c5607b31fb07 91
defrost 0:c5607b31fb07 92
defrost 0:c5607b31fb07 93 }
defrost 0:c5607b31fb07 94 }