Skeleton program for Federico's 4YP project.
Dependencies: WebSocketClient WiflyInterface mbed messages
Fork of IoT_Ex by
Diff: main.cpp
- Revision:
- 0:c5607b31fb07
- Child:
- 1:4403f2ed1c1f
diff -r 000000000000 -r c5607b31fb07 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Oct 04 11:34:47 2016 +0000 @@ -0,0 +1,94 @@ +#include "mbed.h" +#include "iQ_globals.h" +#include "WiflyInterface.h" +#include "iQ_Commands.h" +#include "Websocket.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; + + // Set the IoT ID: + IoT_ID = 1; + + // Send a startup message to serial port: + INFO("Starting up..."); + INFO("CPU SystemCoreClock is %d Hz", SystemCoreClock); + + + 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)){ + + // Send data over network: + SendNetworkData(); + + // Increment a counter: + SendCounter++; + + // Reset the timer: + DisplayTimer.reset(); + } + + + } +}