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 16:20:14 2016 +0000
Revision:
3:f20e114eb2ee
Parent:
2:7abdaa5a9209
Child:
5:0c7d131e6089
- Works well with server

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 2:7abdaa5a9209 24 DigitalOut Led(LED1);
defrost 1:4403f2ed1c1f 25
defrost 3:f20e114eb2ee 26 WiflyInterface eth(D8, D2, D6, D5, wifissid, wifipassword, WPA2);
defrost 1:4403f2ed1c1f 27
defrost 1:4403f2ed1c1f 28 int ReconnectAttempts = 0;
defrost 1:4403f2ed1c1f 29 int SendCounter = 0;
defrost 1:4403f2ed1c1f 30 extern int IoT_ID = 0;
defrost 1:4403f2ed1c1f 31 float TempSensor = 0.0f;
defrost 1:4403f2ed1c1f 32
defrost 1:4403f2ed1c1f 33 Websocket ws;
defrost 1:4403f2ed1c1f 34
defrost 1:4403f2ed1c1f 35
defrost 1:4403f2ed1c1f 36 int SetupNetwork(int Tries){
defrost 1:4403f2ed1c1f 37 // Initialize the interface.
defrost 1:4403f2ed1c1f 38 // If no param is passed to init() then DHCP will be used on connect()
defrost 1:4403f2ed1c1f 39 int s = eth.init();
defrost 1:4403f2ed1c1f 40 int attempts = 1;
defrost 1:4403f2ed1c1f 41
defrost 1:4403f2ed1c1f 42 wait(1);
defrost 1:4403f2ed1c1f 43 if (s != NULL) {
defrost 1:4403f2ed1c1f 44 ERR("Could not initialise. Halting!");
defrost 1:4403f2ed1c1f 45 exit(0);
defrost 1:4403f2ed1c1f 46 }
defrost 1:4403f2ed1c1f 47
defrost 1:4403f2ed1c1f 48 INFO("Connecting to: %s", wifissid);
defrost 1:4403f2ed1c1f 49 DBG("Getting IP address...");
defrost 1:4403f2ed1c1f 50
defrost 1:4403f2ed1c1f 51 while (1) {
defrost 1:4403f2ed1c1f 52 // Connect to network:
defrost 1:4403f2ed1c1f 53 s = eth.connect();
defrost 1:4403f2ed1c1f 54 // If connection fails, retry for 5 attempts:
defrost 1:4403f2ed1c1f 55 if (s == false || s < 0) {
defrost 1:4403f2ed1c1f 56 INFO("Could not connect to network. Retrying!");
defrost 1:4403f2ed1c1f 57 attempts++;
defrost 1:4403f2ed1c1f 58 wait(1);
defrost 1:4403f2ed1c1f 59 } else {
defrost 1:4403f2ed1c1f 60
defrost 1:4403f2ed1c1f 61 break;
defrost 1:4403f2ed1c1f 62 }
defrost 1:4403f2ed1c1f 63 if(attempts > Tries){
defrost 1:4403f2ed1c1f 64 ERR("Network connection failed after %d attempts", Tries);
defrost 1:4403f2ed1c1f 65 return 0;
defrost 1:4403f2ed1c1f 66 }
defrost 1:4403f2ed1c1f 67 }
defrost 1:4403f2ed1c1f 68 INFO("Connected to: %s", wifissid);
defrost 1:4403f2ed1c1f 69 INFO("Got IP address: %s", eth.getIPAddress());
defrost 1:4403f2ed1c1f 70 IotStatus.SetFlag(SF_WIRELESSCONNECTED);
defrost 1:4403f2ed1c1f 71 return 1;
defrost 1:4403f2ed1c1f 72
defrost 1:4403f2ed1c1f 73 }
defrost 1:4403f2ed1c1f 74
defrost 1:4403f2ed1c1f 75 void SendNetworkData(void){
defrost 1:4403f2ed1c1f 76 char msg_buffer[CHARMSGBUFF];
defrost 1:4403f2ed1c1f 77 int intresult;
defrost 1:4403f2ed1c1f 78
defrost 1:4403f2ed1c1f 79 if(IotStatus.CheckFlag(SF_SERVERCONNECTED)){
defrost 1:4403f2ed1c1f 80 sprintf(msg_buffer, "%d,%d,%.5f", IoT_ID, SendCounter,TempSensor);
defrost 1:4403f2ed1c1f 81 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 82 intresult = ws.send(msg_buffer);
defrost 1:4403f2ed1c1f 83 }else{
defrost 1:4403f2ed1c1f 84 intresult = -1;
defrost 1:4403f2ed1c1f 85 }
defrost 1:4403f2ed1c1f 86 DBG("intresult: %d", intresult);
defrost 1:4403f2ed1c1f 87
defrost 1:4403f2ed1c1f 88 if(intresult < 0){
defrost 1:4403f2ed1c1f 89 // Clear a status flag:
defrost 1:4403f2ed1c1f 90 IotStatus.ClearFlag(SF_SERVERCONNECTED);
defrost 1:4403f2ed1c1f 91 // Check to see if the wireless is still connected:
defrost 1:4403f2ed1c1f 92 DBG("Checking network status...");
defrost 1:4403f2ed1c1f 93 if(eth.checkNetworkStatus() != 3){
defrost 1:4403f2ed1c1f 94 IotStatus.ClearFlag(SF_WIRELESSCONNECTED);
defrost 1:4403f2ed1c1f 95 // Connect to the wireless network:
defrost 1:4403f2ed1c1f 96 if(IotStatus.CheckFlag(SF_AUTOCONNECT)){
defrost 1:4403f2ed1c1f 97 INFO("Reconnecting to Network...");
defrost 1:4403f2ed1c1f 98 if(SetupNetwork(1)>0){
defrost 1:4403f2ed1c1f 99 IotStatus.SetFlag(SF_WIRELESSCONNECTED);
defrost 1:4403f2ed1c1f 100 INFO("Connected to Network.");
defrost 1:4403f2ed1c1f 101 }else{
defrost 1:4403f2ed1c1f 102 WARN("Could not re-connect to the wireless network.");
defrost 1:4403f2ed1c1f 103 }
defrost 1:4403f2ed1c1f 104 }
defrost 1:4403f2ed1c1f 105 }else{
defrost 1:4403f2ed1c1f 106 DBG("Network connected.");
defrost 1:4403f2ed1c1f 107 }
defrost 1:4403f2ed1c1f 108
defrost 1:4403f2ed1c1f 109 if(IotStatus.CheckFlag(SF_AUTOCONNECT) && IotStatus.CheckFlag(SF_WIRELESSCONNECTED)){
defrost 1:4403f2ed1c1f 110 // Server connection was closed, try to reconnect:
defrost 1:4403f2ed1c1f 111 INFO("Reconnecting to Websocket Server on ws://%s:%d/ws...", SERVER_IP, WS_PORT);
defrost 1:4403f2ed1c1f 112 if(!ws.connect()){
defrost 1:4403f2ed1c1f 113 WARN("Could not connect to the server again...");
defrost 1:4403f2ed1c1f 114 IotStatus.ClearFlag(SF_SERVERCONNECTED);
defrost 1:4403f2ed1c1f 115 ReconnectAttempts++;
defrost 1:4403f2ed1c1f 116 if(ReconnectAttempts > 4){
defrost 1:4403f2ed1c1f 117 INFO("Failed after %d reconnect attempts. Resetting the Wifi Shield...", ReconnectAttempts);
defrost 1:4403f2ed1c1f 118 SetupNetwork(1);
defrost 1:4403f2ed1c1f 119 ReconnectAttempts = 0;
defrost 1:4403f2ed1c1f 120 }
defrost 1:4403f2ed1c1f 121 }else{
defrost 1:4403f2ed1c1f 122 INFO("Connected to ws://%s:%d/ws", SERVER_IP, WS_PORT);
defrost 1:4403f2ed1c1f 123 // Set a status flag:
defrost 1:4403f2ed1c1f 124 IotStatus.SetFlag(SF_SERVERCONNECTED);
defrost 1:4403f2ed1c1f 125 }
defrost 1:4403f2ed1c1f 126 }
defrost 1:4403f2ed1c1f 127 }
defrost 1:4403f2ed1c1f 128
defrost 1:4403f2ed1c1f 129 return;
defrost 1:4403f2ed1c1f 130 }
defrost 1:4403f2ed1c1f 131
defrost 1:4403f2ed1c1f 132 void ReceiveNetworkData(unsigned int * wifi_cmd, unsigned int * var, float * value){
defrost 1:4403f2ed1c1f 133 char msg_buffer[CHARMSGBUFF];
defrost 1:4403f2ed1c1f 134 char msg_buffer2[CHARMSGBUFF];
defrost 2:7abdaa5a9209 135 int resp;
defrost 2:7abdaa5a9209 136 if(IotStatus.CheckFlag(SF_SERVERCONNECTED)){
defrost 2:7abdaa5a9209 137 // Check for data on the websocket:
defrost 2:7abdaa5a9209 138 resp = ws.readmsg(msg_buffer);
defrost 2:7abdaa5a9209 139 if(resp == 1){
defrost 2:7abdaa5a9209 140 INFO("Received: %s", msg_buffer);
defrost 2:7abdaa5a9209 141 sscanf(msg_buffer, "%d,%s", wifi_cmd, msg_buffer2);
defrost 2:7abdaa5a9209 142 if(*wifi_cmd == CHANGEVAR_WIFI_CMD){
defrost 3:f20e114eb2ee 143 // Get one more value:
defrost 2:7abdaa5a9209 144 sscanf(msg_buffer2, "%f", value);
defrost 2:7abdaa5a9209 145 }
defrost 2:7abdaa5a9209 146 }else if(resp == -1){
defrost 2:7abdaa5a9209 147 // Connection to the server is lost:
defrost 2:7abdaa5a9209 148 IotStatus.ClearFlag(SF_SERVERCONNECTED);
defrost 1:4403f2ed1c1f 149 }else{
defrost 2:7abdaa5a9209 150 //DBG("Did not receive anything :(\n\r");
defrost 2:7abdaa5a9209 151 *wifi_cmd = NO_WIFI_CMD;
defrost 2:7abdaa5a9209 152 *var = 0;
defrost 2:7abdaa5a9209 153 *value = 0.0f;
defrost 1:4403f2ed1c1f 154 }
defrost 1:4403f2ed1c1f 155 }
defrost 1:4403f2ed1c1f 156 return;
defrost 1:4403f2ed1c1f 157 }
defrost 1:4403f2ed1c1f 158
defrost 1:4403f2ed1c1f 159 void ModifyVariable(unsigned int wifi_var, float wifi_data){
defrost 1:4403f2ed1c1f 160 // modifies something in the SCS Controller:
defrost 1:4403f2ed1c1f 161 switch(wifi_var){
defrost 1:4403f2ed1c1f 162 case CV_LED:
defrost 3:f20e114eb2ee 163 if(wifi_data > 0){
defrost 3:f20e114eb2ee 164 Led = 1;
defrost 3:f20e114eb2ee 165 }else{
defrost 3:f20e114eb2ee 166 Led = 0;
defrost 3:f20e114eb2ee 167 }
defrost 1:4403f2ed1c1f 168 break;
defrost 1:4403f2ed1c1f 169
defrost 1:4403f2ed1c1f 170 default:
defrost 1:4403f2ed1c1f 171 break;
defrost 1:4403f2ed1c1f 172 }
defrost 1:4403f2ed1c1f 173 return;
defrost 1:4403f2ed1c1f 174 }
defrost 1:4403f2ed1c1f 175
defrost 1:4403f2ed1c1f 176
defrost 1:4403f2ed1c1f 177