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

Committer:
defrost
Date:
2016-10-04
Revision:
3:f20e114eb2ee
Parent:
2:7abdaa5a9209
Child:
5:0c7d131e6089

File content as of revision 3:f20e114eb2ee:

#include "mbed.h"
#include "globals.h"
#include "WiflyInterface.h"
#include "Commands.h"
#include "Websocket.h"
#include "ADC.h"

//#define DEBUG
#define INFOMESSAGES
#define WARNMESSAGES
#define ERRMESSAGES
#define FUNCNAME "IoT"
#include "messages.h"

// Main Loop!
int main() {
    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;
    
    // Set the Auto reconnect flag:
    IotStatus.SetFlag(SF_AUTOCONNECT);
    
    // Send a startup message to serial port:
    INFO("");
    INFO("");
    INFO("Starting up...");
    INFO("CPU SystemCoreClock is %d Hz", SystemCoreClock); 
        
    ConfigureADC();
    SetupNetwork(5000);
    // Configure the baud rate of the wifi shield:
    ws.setBaud(115200);
    wait(0.5f);
    
    
    
    char msg[128];
    if(IotStatus.CheckFlag(SF_WIRELESSCONNECTED)){
        sprintf(msg, "ws://%s:%d/ws", SERVER_IP, WS_PORT);
        ws.Initialize(msg);
        INFO("Connecting to Websocket Server on %s...", msg);
        if(ws.connect()){
            // Set a status flag:
            INFO("Connected.");
            IotStatus.SetFlag(SF_SERVERCONNECTED);
        }else{
            // We could not connect right now..
            IotStatus.ClearFlag(SF_SERVERCONNECTED);
            INFO("Could not connect to server, will try again later.");
            ReconnectAttempts++;
            if(ReconnectAttempts > 5){
                INFO("Failed after %d reconnect attempts. Resetting the Wifi Shield...", ReconnectAttempts);
                SetupNetwork(1);
                ReconnectAttempts = 0;
            }
        }
    }
    
    
    
    DisplayTimer.start();
    // Inifinite loop:
    while(1) {
        
        // Process the wifi command:
        if(wifi_cmd > NO_WIFI_CMD){
            switch(wifi_cmd){
                case CHANGEVAR_WIFI_CMD:
                    ModifyVariable(wifi_var, wifi_data);
                    break;
                default:
                    break;
            }
            wifi_cmd = NO_WIFI_CMD;
        }
        
        // Check for new wifi data:
        if((wifi_cmd == NO_WIFI_CMD)){
            ReceiveNetworkData(&wifi_cmd, &wifi_var, &wifi_data);
        }
        
        // Send the network data every 3 seconds:
        if(DisplayTimer.read()>(3.0f)){
            // Sample the internal temperature sensor:
            STARTADCCONVERSION;
            while(!ADCCONVERSIONCOMPLETE);
            ADCRaw = ADC1->DR;
            TempSensor = ((((float)ADCRaw)/ADC_MAX)*IT_VMAX - IT_V25)/IT_AVG_SLOPE + 25.0f;
            DBG("TempSensor = %.5f", TempSensor);
            DBG("ADC1->DR = %d", ADCRaw);
            
            // Send data over network:
            SendNetworkData();               
            
            // Increment a counter:
            SendCounter++;
            
            // Reset the timer:
            DisplayTimer.reset();
            
        }
        
       
    }
}