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 13:19:19 2016 +0000
Revision:
1:4403f2ed1c1f
Child:
2:7abdaa5a9209
- Temperature sensor with injected channel conversion

Who changed what in which revision?

UserRevisionLine numberNew contents of line
defrost 1:4403f2ed1c1f 1 // ***************
defrost 1:4403f2ed1c1f 2 // * globals.cpp *
defrost 1:4403f2ed1c1f 3 // ***************
defrost 1:4403f2ed1c1f 4 //
defrost 1:4403f2ed1c1f 5 // Created: 2015/03/19
defrost 1:4403f2ed1c1f 6 // By: Damien Frost
defrost 1:4403f2ed1c1f 7
defrost 1:4403f2ed1c1f 8 #include "mbed.h"
defrost 1:4403f2ed1c1f 9 #include "globals.h"
defrost 1:4403f2ed1c1f 10
defrost 1:4403f2ed1c1f 11 //#define DEBUG
defrost 1:4403f2ed1c1f 12 #define INFOMESSAGES
defrost 1:4403f2ed1c1f 13 #define WARNMESSAGES
defrost 1:4403f2ed1c1f 14 #define ERRMESSAGES
defrost 1:4403f2ed1c1f 15 #define FUNCNAME "GBL"
defrost 1:4403f2ed1c1f 16 #include "messages.h"
defrost 1:4403f2ed1c1f 17
defrost 1:4403f2ed1c1f 18 char* wifissid = "SC";
defrost 1:4403f2ed1c1f 19 char* wifipassword = "smartcellshield";
defrost 1:4403f2ed1c1f 20
defrost 1:4403f2ed1c1f 21 Serial pc(USBTX, USBRX);
defrost 1:4403f2ed1c1f 22 InterruptIn UIBut1(USER_BUTTON);
defrost 1:4403f2ed1c1f 23 Timer DisplayTimer;
defrost 1:4403f2ed1c1f 24
defrost 1:4403f2ed1c1f 25 WiflyInterface eth(D8, D2, D6, LED1, wifissid, wifipassword, WPA2);
defrost 1:4403f2ed1c1f 26
defrost 1:4403f2ed1c1f 27 int ReconnectAttempts = 0;
defrost 1:4403f2ed1c1f 28 int SendCounter = 0;
defrost 1:4403f2ed1c1f 29 extern int IoT_ID = 0;
defrost 1:4403f2ed1c1f 30 float TempSensor = 0.0f;
defrost 1:4403f2ed1c1f 31
defrost 1:4403f2ed1c1f 32 Websocket ws;
defrost 1:4403f2ed1c1f 33
defrost 1:4403f2ed1c1f 34
defrost 1:4403f2ed1c1f 35 int SetupNetwork(int Tries){
defrost 1:4403f2ed1c1f 36 // Initialize the interface.
defrost 1:4403f2ed1c1f 37 // If no param is passed to init() then DHCP will be used on connect()
defrost 1:4403f2ed1c1f 38 int s = eth.init();
defrost 1:4403f2ed1c1f 39 int attempts = 1;
defrost 1:4403f2ed1c1f 40
defrost 1:4403f2ed1c1f 41 wait(1);
defrost 1:4403f2ed1c1f 42 if (s != NULL) {
defrost 1:4403f2ed1c1f 43 ERR("Could not initialise. Halting!");
defrost 1:4403f2ed1c1f 44 exit(0);
defrost 1:4403f2ed1c1f 45 }
defrost 1:4403f2ed1c1f 46
defrost 1:4403f2ed1c1f 47 INFO("Connecting to: %s", wifissid);
defrost 1:4403f2ed1c1f 48 DBG("Getting IP address...");
defrost 1:4403f2ed1c1f 49
defrost 1:4403f2ed1c1f 50 while (1) {
defrost 1:4403f2ed1c1f 51 // Connect to network:
defrost 1:4403f2ed1c1f 52 s = eth.connect();
defrost 1:4403f2ed1c1f 53 // If connection fails, retry for 5 attempts:
defrost 1:4403f2ed1c1f 54 if (s == false || s < 0) {
defrost 1:4403f2ed1c1f 55 INFO("Could not connect to network. Retrying!");
defrost 1:4403f2ed1c1f 56 attempts++;
defrost 1:4403f2ed1c1f 57 wait(1);
defrost 1:4403f2ed1c1f 58 } else {
defrost 1:4403f2ed1c1f 59
defrost 1:4403f2ed1c1f 60 break;
defrost 1:4403f2ed1c1f 61 }
defrost 1:4403f2ed1c1f 62 if(attempts > Tries){
defrost 1:4403f2ed1c1f 63 ERR("Network connection failed after %d attempts", Tries);
defrost 1:4403f2ed1c1f 64 return 0;
defrost 1:4403f2ed1c1f 65 }
defrost 1:4403f2ed1c1f 66 }
defrost 1:4403f2ed1c1f 67 INFO("Connected to: %s", wifissid);
defrost 1:4403f2ed1c1f 68 INFO("Got IP address: %s", eth.getIPAddress());
defrost 1:4403f2ed1c1f 69 IotStatus.SetFlag(SF_WIRELESSCONNECTED);
defrost 1:4403f2ed1c1f 70 return 1;
defrost 1:4403f2ed1c1f 71
defrost 1:4403f2ed1c1f 72 }
defrost 1:4403f2ed1c1f 73
defrost 1:4403f2ed1c1f 74 void SendNetworkData(void){
defrost 1:4403f2ed1c1f 75 char msg_buffer[CHARMSGBUFF];
defrost 1:4403f2ed1c1f 76 int intresult;
defrost 1:4403f2ed1c1f 77
defrost 1:4403f2ed1c1f 78 if(IotStatus.CheckFlag(SF_SERVERCONNECTED)){
defrost 1:4403f2ed1c1f 79 sprintf(msg_buffer, "%d,%d,%.5f", IoT_ID, SendCounter,TempSensor);
defrost 1:4403f2ed1c1f 80 INFO("Sending: %s", msg_buffer); // When this line is commented out, the mbed never tries to reconnect to the server after one try. SUPER. Keeping this here also uses precious CPU time
defrost 1:4403f2ed1c1f 81 intresult = ws.send(msg_buffer);
defrost 1:4403f2ed1c1f 82 }else{
defrost 1:4403f2ed1c1f 83 intresult = -1;
defrost 1:4403f2ed1c1f 84 }
defrost 1:4403f2ed1c1f 85 DBG("intresult: %d", intresult);
defrost 1:4403f2ed1c1f 86
defrost 1:4403f2ed1c1f 87 if(intresult < 0){
defrost 1:4403f2ed1c1f 88 // Clear a status flag:
defrost 1:4403f2ed1c1f 89 IotStatus.ClearFlag(SF_SERVERCONNECTED);
defrost 1:4403f2ed1c1f 90 // Check to see if the wireless is still connected:
defrost 1:4403f2ed1c1f 91 DBG("Checking network status...");
defrost 1:4403f2ed1c1f 92 if(eth.checkNetworkStatus() != 3){
defrost 1:4403f2ed1c1f 93 IotStatus.ClearFlag(SF_WIRELESSCONNECTED);
defrost 1:4403f2ed1c1f 94 // Connect to the wireless network:
defrost 1:4403f2ed1c1f 95 if(IotStatus.CheckFlag(SF_AUTOCONNECT)){
defrost 1:4403f2ed1c1f 96 INFO("Reconnecting to Network...");
defrost 1:4403f2ed1c1f 97 if(SetupNetwork(1)>0){
defrost 1:4403f2ed1c1f 98 IotStatus.SetFlag(SF_WIRELESSCONNECTED);
defrost 1:4403f2ed1c1f 99 INFO("Connected to Network.");
defrost 1:4403f2ed1c1f 100 }else{
defrost 1:4403f2ed1c1f 101 WARN("Could not re-connect to the wireless network.");
defrost 1:4403f2ed1c1f 102 }
defrost 1:4403f2ed1c1f 103 }
defrost 1:4403f2ed1c1f 104 }else{
defrost 1:4403f2ed1c1f 105 DBG("Network connected.");
defrost 1:4403f2ed1c1f 106 }
defrost 1:4403f2ed1c1f 107
defrost 1:4403f2ed1c1f 108 if(IotStatus.CheckFlag(SF_AUTOCONNECT) && IotStatus.CheckFlag(SF_WIRELESSCONNECTED)){
defrost 1:4403f2ed1c1f 109 // Server connection was closed, try to reconnect:
defrost 1:4403f2ed1c1f 110 INFO("Reconnecting to Websocket Server on ws://%s:%d/ws...", SERVER_IP, WS_PORT);
defrost 1:4403f2ed1c1f 111 if(!ws.connect()){
defrost 1:4403f2ed1c1f 112 WARN("Could not connect to the server again...");
defrost 1:4403f2ed1c1f 113 IotStatus.ClearFlag(SF_SERVERCONNECTED);
defrost 1:4403f2ed1c1f 114 ReconnectAttempts++;
defrost 1:4403f2ed1c1f 115 if(ReconnectAttempts > 4){
defrost 1:4403f2ed1c1f 116 INFO("Failed after %d reconnect attempts. Resetting the Wifi Shield...", ReconnectAttempts);
defrost 1:4403f2ed1c1f 117 SetupNetwork(1);
defrost 1:4403f2ed1c1f 118 ReconnectAttempts = 0;
defrost 1:4403f2ed1c1f 119 }
defrost 1:4403f2ed1c1f 120 }else{
defrost 1:4403f2ed1c1f 121 INFO("Connected to ws://%s:%d/ws", SERVER_IP, WS_PORT);
defrost 1:4403f2ed1c1f 122 // Set a status flag:
defrost 1:4403f2ed1c1f 123 IotStatus.SetFlag(SF_SERVERCONNECTED);
defrost 1:4403f2ed1c1f 124 }
defrost 1:4403f2ed1c1f 125 }
defrost 1:4403f2ed1c1f 126 }
defrost 1:4403f2ed1c1f 127
defrost 1:4403f2ed1c1f 128 return;
defrost 1:4403f2ed1c1f 129 }
defrost 1:4403f2ed1c1f 130
defrost 1:4403f2ed1c1f 131 void ReceiveNetworkData(unsigned int * wifi_cmd, unsigned int * var, float * value){
defrost 1:4403f2ed1c1f 132 char msg_buffer[CHARMSGBUFF];
defrost 1:4403f2ed1c1f 133 char msg_buffer2[CHARMSGBUFF];
defrost 1:4403f2ed1c1f 134 // Check for data on the websocket:
defrost 1:4403f2ed1c1f 135 if(ws.readmsg(msg_buffer)){
defrost 1:4403f2ed1c1f 136 INFO("Received: %s", msg_buffer);
defrost 1:4403f2ed1c1f 137 sscanf(msg_buffer, "%d,%s", wifi_cmd, msg_buffer2);
defrost 1:4403f2ed1c1f 138 if(*wifi_cmd == CHANGEVAR_WIFI_CMD){
defrost 1:4403f2ed1c1f 139 // Get two more values:
defrost 1:4403f2ed1c1f 140 sscanf(msg_buffer2, "%d,%f", var, value);
defrost 1:4403f2ed1c1f 141 }else{
defrost 1:4403f2ed1c1f 142 // Get one:
defrost 1:4403f2ed1c1f 143 sscanf(msg_buffer2, "%f", value);
defrost 1:4403f2ed1c1f 144 }
defrost 1:4403f2ed1c1f 145 }else{
defrost 1:4403f2ed1c1f 146 //DBG("Did not receive anything :(\n\r");
defrost 1:4403f2ed1c1f 147 *wifi_cmd = NO_WIFI_CMD;
defrost 1:4403f2ed1c1f 148 *var = 0;
defrost 1:4403f2ed1c1f 149 *value = 0.0f;
defrost 1:4403f2ed1c1f 150 }
defrost 1:4403f2ed1c1f 151 return;
defrost 1:4403f2ed1c1f 152 }
defrost 1:4403f2ed1c1f 153
defrost 1:4403f2ed1c1f 154 void ModifyVariable(unsigned int wifi_var, float wifi_data){
defrost 1:4403f2ed1c1f 155 // modifies something in the SCS Controller:
defrost 1:4403f2ed1c1f 156 switch(wifi_var){
defrost 1:4403f2ed1c1f 157 case CV_LED:
defrost 1:4403f2ed1c1f 158
defrost 1:4403f2ed1c1f 159 break;
defrost 1:4403f2ed1c1f 160
defrost 1:4403f2ed1c1f 161 default:
defrost 1:4403f2ed1c1f 162 break;
defrost 1:4403f2ed1c1f 163 }
defrost 1:4403f2ed1c1f 164 return;
defrost 1:4403f2ed1c1f 165 }
defrost 1:4403f2ed1c1f 166
defrost 1:4403f2ed1c1f 167
defrost 1:4403f2ed1c1f 168