![](/media/cache/profiles/c19a46c712c75b70a137f619a0916436.jpg.50x50_q85.png)
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.
main.cpp@3:f20e114eb2ee, 2016-10-04 (annotated)
- 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?
User | Revision | Line number | New 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 | 2:7abdaa5a9209 | 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 | 2:7abdaa5a9209 | 20 | unsigned int ADCRaw; |
defrost | 0:c5607b31fb07 | 21 | |
defrost | 0:c5607b31fb07 | 22 | // Set the IoT ID: |
defrost | 0:c5607b31fb07 | 23 | IoT_ID = 1; |
defrost | 0:c5607b31fb07 | 24 | |
defrost | 3:f20e114eb2ee | 25 | // Set the Auto reconnect flag: |
defrost | 3:f20e114eb2ee | 26 | IotStatus.SetFlag(SF_AUTOCONNECT); |
defrost | 3:f20e114eb2ee | 27 | |
defrost | 0:c5607b31fb07 | 28 | // Send a startup message to serial port: |
defrost | 1:4403f2ed1c1f | 29 | INFO(""); |
defrost | 1:4403f2ed1c1f | 30 | INFO(""); |
defrost | 0:c5607b31fb07 | 31 | INFO("Starting up..."); |
defrost | 0:c5607b31fb07 | 32 | INFO("CPU SystemCoreClock is %d Hz", SystemCoreClock); |
defrost | 0:c5607b31fb07 | 33 | |
defrost | 2:7abdaa5a9209 | 34 | ConfigureADC(); |
defrost | 0:c5607b31fb07 | 35 | SetupNetwork(5000); |
defrost | 0:c5607b31fb07 | 36 | // Configure the baud rate of the wifi shield: |
defrost | 0:c5607b31fb07 | 37 | ws.setBaud(115200); |
defrost | 0:c5607b31fb07 | 38 | wait(0.5f); |
defrost | 0:c5607b31fb07 | 39 | |
defrost | 0:c5607b31fb07 | 40 | |
defrost | 0:c5607b31fb07 | 41 | |
defrost | 0:c5607b31fb07 | 42 | char msg[128]; |
defrost | 0:c5607b31fb07 | 43 | if(IotStatus.CheckFlag(SF_WIRELESSCONNECTED)){ |
defrost | 0:c5607b31fb07 | 44 | sprintf(msg, "ws://%s:%d/ws", SERVER_IP, WS_PORT); |
defrost | 0:c5607b31fb07 | 45 | ws.Initialize(msg); |
defrost | 0:c5607b31fb07 | 46 | INFO("Connecting to Websocket Server on %s...", msg); |
defrost | 0:c5607b31fb07 | 47 | if(ws.connect()){ |
defrost | 0:c5607b31fb07 | 48 | // Set a status flag: |
defrost | 0:c5607b31fb07 | 49 | INFO("Connected."); |
defrost | 0:c5607b31fb07 | 50 | IotStatus.SetFlag(SF_SERVERCONNECTED); |
defrost | 0:c5607b31fb07 | 51 | }else{ |
defrost | 0:c5607b31fb07 | 52 | // We could not connect right now.. |
defrost | 0:c5607b31fb07 | 53 | IotStatus.ClearFlag(SF_SERVERCONNECTED); |
defrost | 0:c5607b31fb07 | 54 | INFO("Could not connect to server, will try again later."); |
defrost | 0:c5607b31fb07 | 55 | ReconnectAttempts++; |
defrost | 0:c5607b31fb07 | 56 | if(ReconnectAttempts > 5){ |
defrost | 0:c5607b31fb07 | 57 | INFO("Failed after %d reconnect attempts. Resetting the Wifi Shield...", ReconnectAttempts); |
defrost | 0:c5607b31fb07 | 58 | SetupNetwork(1); |
defrost | 0:c5607b31fb07 | 59 | ReconnectAttempts = 0; |
defrost | 0:c5607b31fb07 | 60 | } |
defrost | 0:c5607b31fb07 | 61 | } |
defrost | 0:c5607b31fb07 | 62 | } |
defrost | 0:c5607b31fb07 | 63 | |
defrost | 0:c5607b31fb07 | 64 | |
defrost | 3:f20e114eb2ee | 65 | |
defrost | 0:c5607b31fb07 | 66 | DisplayTimer.start(); |
defrost | 0:c5607b31fb07 | 67 | // Inifinite loop: |
defrost | 0:c5607b31fb07 | 68 | while(1) { |
defrost | 0:c5607b31fb07 | 69 | |
defrost | 0:c5607b31fb07 | 70 | // Process the wifi command: |
defrost | 0:c5607b31fb07 | 71 | if(wifi_cmd > NO_WIFI_CMD){ |
defrost | 0:c5607b31fb07 | 72 | switch(wifi_cmd){ |
defrost | 0:c5607b31fb07 | 73 | case CHANGEVAR_WIFI_CMD: |
defrost | 0:c5607b31fb07 | 74 | ModifyVariable(wifi_var, wifi_data); |
defrost | 0:c5607b31fb07 | 75 | break; |
defrost | 0:c5607b31fb07 | 76 | default: |
defrost | 0:c5607b31fb07 | 77 | break; |
defrost | 0:c5607b31fb07 | 78 | } |
defrost | 0:c5607b31fb07 | 79 | wifi_cmd = NO_WIFI_CMD; |
defrost | 0:c5607b31fb07 | 80 | } |
defrost | 0:c5607b31fb07 | 81 | |
defrost | 0:c5607b31fb07 | 82 | // Check for new wifi data: |
defrost | 0:c5607b31fb07 | 83 | if((wifi_cmd == NO_WIFI_CMD)){ |
defrost | 0:c5607b31fb07 | 84 | ReceiveNetworkData(&wifi_cmd, &wifi_var, &wifi_data); |
defrost | 0:c5607b31fb07 | 85 | } |
defrost | 0:c5607b31fb07 | 86 | |
defrost | 0:c5607b31fb07 | 87 | // Send the network data every 3 seconds: |
defrost | 0:c5607b31fb07 | 88 | if(DisplayTimer.read()>(3.0f)){ |
defrost | 1:4403f2ed1c1f | 89 | // Sample the internal temperature sensor: |
defrost | 1:4403f2ed1c1f | 90 | STARTADCCONVERSION; |
defrost | 1:4403f2ed1c1f | 91 | while(!ADCCONVERSIONCOMPLETE); |
defrost | 2:7abdaa5a9209 | 92 | ADCRaw = ADC1->DR; |
defrost | 2:7abdaa5a9209 | 93 | TempSensor = ((((float)ADCRaw)/ADC_MAX)*IT_VMAX - IT_V25)/IT_AVG_SLOPE + 25.0f; |
defrost | 1:4403f2ed1c1f | 94 | DBG("TempSensor = %.5f", TempSensor); |
defrost | 2:7abdaa5a9209 | 95 | DBG("ADC1->DR = %d", ADCRaw); |
defrost | 0:c5607b31fb07 | 96 | |
defrost | 0:c5607b31fb07 | 97 | // Send data over network: |
defrost | 0:c5607b31fb07 | 98 | SendNetworkData(); |
defrost | 0:c5607b31fb07 | 99 | |
defrost | 0:c5607b31fb07 | 100 | // Increment a counter: |
defrost | 0:c5607b31fb07 | 101 | SendCounter++; |
defrost | 0:c5607b31fb07 | 102 | |
defrost | 0:c5607b31fb07 | 103 | // Reset the timer: |
defrost | 0:c5607b31fb07 | 104 | DisplayTimer.reset(); |
defrost | 2:7abdaa5a9209 | 105 | |
defrost | 0:c5607b31fb07 | 106 | } |
defrost | 0:c5607b31fb07 | 107 | |
defrost | 0:c5607b31fb07 | 108 | |
defrost | 0:c5607b31fb07 | 109 | } |
defrost | 0:c5607b31fb07 | 110 | } |