Skeleton program for Federico's 4YP project.
Dependencies: WebSocketClient WiflyInterface mbed messages
Fork of IoT_Ex by
main.cpp
- Committer:
- defrost
- Date:
- 2016-10-04
- Revision:
- 2:7abdaa5a9209
- Parent:
- 1:4403f2ed1c1f
- Child:
- 3:f20e114eb2ee
File content as of revision 2:7abdaa5a9209:
#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; // 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(); if(Led == 1){ Led = 0; }else{ Led = 1; } } } }