![](/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.
Diff: main.cpp
- Revision:
- 2:7abdaa5a9209
- Parent:
- 1:4403f2ed1c1f
- Child:
- 3:f20e114eb2ee
--- a/main.cpp Tue Oct 04 13:19:19 2016 +0000 +++ b/main.cpp Tue Oct 04 13:59:13 2016 +0000 @@ -5,7 +5,7 @@ #include "Websocket.h" #include "ADC.h" -#define DEBUG +//#define DEBUG #define INFOMESSAGES #define WARNMESSAGES #define ERRMESSAGES @@ -17,6 +17,7 @@ unsigned int wifi_cmd = NO_WIFI_CMD; float wifi_data = 0.0f; unsigned int wifi_var = 0; + unsigned int ADCRaw; // Set the IoT ID: IoT_ID = 1; @@ -27,7 +28,7 @@ INFO("Starting up..."); INFO("CPU SystemCoreClock is %d Hz", SystemCoreClock); - + ConfigureADC(); SetupNetwork(5000); // Configure the baud rate of the wifi shield: ws.setBaud(115200); @@ -84,9 +85,10 @@ // Sample the internal temperature sensor: STARTADCCONVERSION; while(!ADCCONVERSIONCOMPLETE); - TempSensor = ((((float)ADC1->JDR1)/ADC_MAX)*IT_VMAX - IT_V25)/IT_AVG_SLOPE + 25.0f; + ADCRaw = ADC1->DR; + TempSensor = ((((float)ADCRaw)/ADC_MAX)*IT_VMAX - IT_V25)/IT_AVG_SLOPE + 25.0f; DBG("TempSensor = %.5f", TempSensor); - DBG("ADC1->JDR1 = %d", ADC1->JDR1); + DBG("ADC1->DR = %d", ADCRaw); // Send data over network: SendNetworkData(); @@ -96,6 +98,12 @@ // Reset the timer: DisplayTimer.reset(); + + if(Led == 1){ + Led = 0; + }else{ + Led = 1; + } }