Example program to create IoT devices for a local network, which connect to a local server.

Dependencies:   WebSocketClient WiflyInterface mbed messages

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 /**
00002 * @author Damien Frost
00003 *
00004 * @section LICENSE
00005 *
00006 *   Copyright (c) 2016 Damien Frost
00007 *
00008 *   Permission is hereby granted, free of charge, to any person obtaining a copy
00009 *   of this software and associated documentation files (the "Software"), to deal
00010 *   in the Software without restriction, including without limitation the rights
00011 *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00012 *   copies of the Software, and to permit persons to whom the Software is
00013 *   furnished to do so, subject to the following conditions:
00014 *
00015 *   The above copyright notice and this permission notice shall be included in
00016 *   all copies or substantial portions of the Software.
00017 *
00018 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00019 *   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00020 *   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00021 *   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00022 *   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00023 *   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00024 *   THE SOFTWARE.
00025 *
00026 * @file "main.cpp"
00027 *
00028 * @section DESCRIPTION
00029 *   Simple Internet of Things main program. The device sends data every 3
00030 *   seconds, and can receive data from a server.
00031 *
00032 */
00033 
00034 #include "mbed.h"
00035 #include "globals.h "
00036 #include "WiflyInterface.h"
00037 #include "Commands.h "
00038 #include "Websocket.h"
00039 #include "ADC.h "
00040 
00041 //#define DEBUG
00042 #define INFOMESSAGES
00043 #define WARNMESSAGES
00044 #define ERRMESSAGES
00045 #define FUNCNAME "IoT"
00046 #include "messages.h"
00047 
00048 
00049 // Main Loop!
00050 int main() {
00051     unsigned int wifi_cmd = NO_WIFI_CMD;
00052     float wifi_data = 0.0f;
00053     unsigned int ADCRaw;
00054     char msg[128];
00055     
00056     // Set the IoT ID:
00057     IoT_ID = 5;
00058     
00059     // Set the Auto reconnect flag:
00060     IotStatus.SetFlag(SF_AUTOCONNECT);
00061     
00062     // Send a startup message to serial port:
00063     INFO("");
00064     INFO("");
00065     INFO("Starting up...");
00066     INFO("CPU SystemCoreClock is %d Hz", SystemCoreClock); 
00067     
00068     // Configure the ADC to sample the internal temperature sensor. You cannot
00069     // use AnalogIn() unfortunately...
00070     ConfigureADC();
00071     
00072     // Connect to the wifi network. It will basically get stuck here until it
00073     // connects to the network.
00074     SetupNetwork(5000);
00075     
00076     // Configure the baud rate of the wifi shield:
00077     // This will make our wireless transmissions much faster.
00078     ws.setBaud(115200);
00079     wait(0.5f);
00080     
00081     // Check to see we are connected to the network:
00082     if(IotStatus.CheckFlag(SF_WIRELESSCONNECTED)){
00083         // Try to connect to the WebSocket server:
00084         sprintf(msg, "ws://%s:%d/ws", SERVER_IP, WS_PORT);
00085         ws.Initialize(msg);
00086         INFO("Connecting to Websocket Server on %s...", msg);
00087         if(ws.connect()){
00088             // Set a status flag:
00089             INFO("Connected.");
00090             IotStatus.SetFlag(SF_SERVERCONNECTED);
00091         }else{
00092             // We could not connect right now..
00093             IotStatus.ClearFlag(SF_SERVERCONNECTED);
00094             INFO("Could not connect to server, will try again later.");
00095             ReconnectAttempts++;
00096         }
00097     }
00098     
00099     // Start the display timer which will send data to the server every
00100     // 3 seconds.
00101     DisplayTimer.start();
00102     
00103     // Inifinite main loop:
00104     while(1) {
00105         
00106         // Process the wifi command:
00107         if(wifi_cmd > NO_WIFI_CMD){
00108             // Modify the desired variable:
00109             ModifyVariable(wifi_cmd, wifi_data);
00110             // Reset the command:
00111             wifi_cmd = NO_WIFI_CMD;
00112         }
00113         
00114         // Check for new wifi data:
00115         if((wifi_cmd == NO_WIFI_CMD)){
00116             ReceiveNetworkData(&wifi_cmd, &wifi_data);
00117         }
00118         
00119         // Send the network data every 3 seconds:
00120         if(DisplayTimer.read()>(3.0f)){
00121             // Sample the internal temperature sensor:
00122             STARTADCCONVERSION;
00123             // Wait for the conversion to complete:
00124             while(!ADCCONVERSIONCOMPLETE);
00125             // Save the raw value from the ADC:
00126             ADCRaw = ADC1->DR;
00127             // Calculate the temperature using information from the datasheet:
00128             TempSensor = ((((float)ADCRaw)/ADC_MAX)*IT_VMAX - IT_V25)/IT_AVG_SLOPE + 25.0f;
00129             // Output the result:
00130             DBG("TempSensor = %.5f", TempSensor);
00131             DBG("ADC1->DR = %d", ADCRaw);
00132             
00133             // Send data over network:
00134             SendNetworkData();               
00135             
00136             // Increment a counter:
00137             SendCounter++;
00138             
00139             // Reset the timer:
00140             DisplayTimer.reset();
00141             
00142         }
00143     } // while(1)
00144 } // main()