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
Parent:
0:c5607b31fb07
Child:
2:7abdaa5a9209
- Temperature sensor with injected channel conversion

Who changed what in which revision?

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