Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: WebSocketClient WiflyInterface mbed messages
Fork of IoT_Ex by
Revision 0:c5607b31fb07, committed 2016-10-04
- Comitter:
- defrost
- Date:
- Tue Oct 04 11:34:47 2016 +0000
- Child:
- 1:4403f2ed1c1f
- Commit message:
- - Initial Commit
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WebSocketClient.lib Tue Oct 04 11:34:47 2016 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/users/defrost/code/WebSocketClient/#420b83755885
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WiflyInterface.lib Tue Oct 04 11:34:47 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/teams/components/code/WiflyInterface/#8774837f5a78
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/headers/StatusReg.h Tue Oct 04 11:34:47 2016 +0000
@@ -0,0 +1,43 @@
+// ******************
+// * iQ_StatusReg.h *
+// ******************
+//
+// Created: 2016/03/24
+// By: Damien Frost
+//
+// Status register class
+
+#ifndef STATUSREG_H_
+#define STATUSREG_H_
+
+#include "StatusReg.h"
+
+ /**
+ * Interface using Wifly to connect to an IP-based network
+ */
+class StatusReg{
+public:
+ // Constructor
+ StatusReg(void);
+
+ // Set a flag:
+ void SetFlag(unsigned int flag);
+
+ // Clear a flag:
+ void ClearFlag(unsigned int flag);
+
+ // Check for flag
+ bool CheckFlag(unsigned int flag);
+
+ // Check for no flags
+ bool AllClear(void);
+
+ // Get Register
+ unsigned int GetReg(void);
+
+private:
+ unsigned int _reg;
+};
+
+
+#endif /* STATUSREG_H_ */
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/headers/iQ_Commands.h Tue Oct 04 11:34:47 2016 +0000 @@ -0,0 +1,65 @@ +// ***************** +// * iQ_Commands.h * +// ***************** +// +// Created: 2016/03/24 +// By: Damien Frost +// +// Description: +// Status registers + +#ifndef IQ_COMMANDS_H +#define IQ_COMMANDS_H + +#include "StatusReg.h" +#include "iQ_globals.h" + +#define BIT0 0x1 +#define BIT1 0x2 +#define BIT2 0x4 +#define BIT3 0x8 +#define BIT4 0x10 +#define BIT5 0x20 +#define BIT6 0x40 +#define BIT7 0x80 +#define BIT8 0x100 +#define BIT9 0x200 +#define BIT10 0x400 +#define BIT11 0x800 +#define BIT12 0x1000 +#define BIT13 0x2000 +#define BIT14 0x4000 +#define BIT15 0x8000 + +#define BIT16 0x10000 +#define BIT17 0x20000 +#define BIT18 0x40000 +#define BIT19 0x80000 +#define BIT20 0x100000 +#define BIT21 0x200000 +#define BIT22 0x400000 +#define BIT23 0x800000 +#define BIT24 0x1000000 +#define BIT25 0x2000000 +#define BIT26 0x4000000 +#define BIT27 0x8000000 +#define BIT28 0x10000000 +#define BIT29 0x20000000 +#define BIT30 0x40000000 +#define BIT31 0x80000000 + +// Status regsiter flags: +#define SF_SERVERCONNECTED BIT0 +#define SF_AUTOCONNECT BIT1 // Set this flag to automatically start connecting to the websocket server +#define SF_WIRELESSCONNECTED BIT2 // Flag to indicate whether or not the wireless network is connected + +// Wifi Commands +#define NO_WIFI_CMD 0 +#define CHANGEVAR_WIFI_CMD 1 + +// Change variable commands: +#define CV_LED 0 + +extern StatusReg IotStatus; + +#endif /* IQ_COMMANDS_H */ \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/headers/iQ_globals.h Tue Oct 04 11:34:47 2016 +0000 @@ -0,0 +1,60 @@ +// **************** +// * iQ_globals.h * +// **************** +// +// Created: 2015/03/19 +// By: Damien Frost +// +// Description: +// Provides global definitions. + +#ifndef IQ_GLOBALS_H +#define IQ_GLOBALS_H + +#include "mbed.h" +#include "WiflyInterface.h" +#include "iQ_Commands.h" +#include "Websocket.h" + + +// Wifi Interface defines: +#define TCP_SERVER_PORT 4445 +#define WIFIBAUDRATE 115200 +#define WIFINETWORK 2 +#define CHARMSGBUFF 1024 +#define TIMEOUTRECEIVEATTEMPTS 5 +#define WS_PORT 4444 +#define SERVER_IP "192.168.0.3" + +extern char* wifissid; +extern char* wifipassword; + +extern Serial pc; +extern InterruptIn UIBut1; +extern Timer DisplayTimer; + +extern WiflyInterface eth; + +extern int ReconnectAttempts; +extern int SendCounter; +extern int IoT_ID; + +extern Websocket ws; + +// Functions: +void SensorToPu(float gain, float offset, int sensor, float* result); +void InitializeStruct(struct tf_history_t* toClear); +void SetupVar(void); +void SetButtonEvent(void); +void rt_OneStep(void); +void DisplayInputs(float CPS); +void SetSCKDCParams(bool enable, float gain); +int SetupNetwork(int Tries); +bool ConnectToServer(int Tries); +void SendNetworkData(void); +void ReceiveNetworkData(unsigned int * wifi_cmd, unsigned int * var, float * value); +void ModifyVariable(unsigned int wifi_var, float wifi_data); + + +#endif /* IQ_GLOBALS_H */ +
--- /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();
+ }
+
+
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Tue Oct 04 11:34:47 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/487b796308b0 \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/source/StatusReg.cpp Tue Oct 04 11:34:47 2016 +0000
@@ -0,0 +1,49 @@
+// ********************
+// * iQ_StatusReg.cpp *
+// ********************
+//
+// Created: 2016/03/24
+// By: Damien Frost
+
+
+#include "StatusReg.h"
+
+StatusReg::StatusReg()
+{
+ _reg = 0;
+ return;
+}
+
+// Set a flag:
+void StatusReg::SetFlag(unsigned int flag){
+ _reg |= flag;
+ return;
+}
+
+// Clear a flag:
+void StatusReg::ClearFlag(unsigned int flag){
+ _reg &= (~flag);
+ return;
+}
+
+// Check for flag
+bool StatusReg::CheckFlag(unsigned int flag){
+ if((_reg & flag) > 0){
+ return true;
+ }else{
+ return false;
+ }
+}
+
+// Check for no flags
+bool StatusReg::AllClear(void){
+ if(_reg == 0){
+ return true;
+ }else{
+ return false;
+ }
+}
+
+unsigned int StatusReg::GetReg(void){
+ return _reg;
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/source/iQ_Commands.cpp Tue Oct 04 11:34:47 2016 +0000 @@ -0,0 +1,10 @@ +// ******************* +// * iQ_Commands.cpp * +// ******************* +// +// Created: 2016/03/24 +// By: Damien Frost + +#include "StatusReg.h" + +StatusReg IotStatus; \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/source/iQ_globals.cpp Tue Oct 04 11:34:47 2016 +0000
@@ -0,0 +1,167 @@
+// ******************
+// * iQ_globals.cpp *
+// ******************
+//
+// Created: 2015/03/19
+// By: Damien Frost
+
+#include "mbed.h"
+#include "iQ_globals.h"
+
+//#define DEBUG
+#define INFOMESSAGES
+#define WARNMESSAGES
+#define ERRMESSAGES
+#define FUNCNAME "GBL"
+#include "messages.h"
+
+char* wifissid = "SC";
+char* wifipassword = "smartcellshield";
+
+Serial pc(USBTX, USBRX);
+InterruptIn UIBut1(USER_BUTTON);
+Timer DisplayTimer;
+
+WiflyInterface eth(D8, D2, D6, LED1, wifissid, wifipassword, WPA2);
+
+int ReconnectAttempts = 0;
+int SendCounter = 0;
+extern int IoT_ID = 0;
+
+Websocket ws;
+
+
+int SetupNetwork(int Tries){
+ // Initialize the interface.
+ // If no param is passed to init() then DHCP will be used on connect()
+ int s = eth.init();
+ int attempts = 1;
+
+ wait(1);
+ if (s != NULL) {
+ ERR("Could not initialise. Halting!");
+ exit(0);
+ }
+
+ INFO("Connecting to: %s", wifissid);
+ DBG("Getting IP address...");
+
+ while (1) {
+ // Connect to network:
+ s = eth.connect();
+ // If connection fails, retry for 5 attempts:
+ if (s == false || s < 0) {
+ INFO("Could not connect to network. Retrying!");
+ attempts++;
+ wait(1);
+ } else {
+
+ break;
+ }
+ if(attempts > Tries){
+ ERR("Network connection failed after %d attempts", Tries);
+ return 0;
+ }
+ }
+ INFO("Connected to: %s", wifissid);
+ INFO("Got IP address: %s", eth.getIPAddress());
+ IotStatus.SetFlag(SF_WIRELESSCONNECTED);
+ return 1;
+
+}
+
+void SendNetworkData(void){
+ char msg_buffer[CHARMSGBUFF];
+ int intresult;
+
+ if(IotStatus.CheckFlag(SF_SERVERCONNECTED)){
+ sprintf(msg_buffer, "%d,%d", IoT_ID, SendCounter);
+ INFO("Sending: %s", msg_buffer); // When this line is commented out, the mbed never tries to reconnect to the server after one try. SUPER. Keeping this here also uses precious CPU time
+ intresult = ws.send(msg_buffer);
+ }else{
+ intresult = -1;
+ }
+ DBG("intresult: %d", intresult);
+
+ if(intresult < 0){
+ // Clear a status flag:
+ IotStatus.ClearFlag(SF_SERVERCONNECTED);
+ // Check to see if the wireless is still connected:
+ DBG("Checking network status...");
+ if(eth.checkNetworkStatus() != 3){
+ IotStatus.ClearFlag(SF_WIRELESSCONNECTED);
+ // Connect to the wireless network:
+ if(IotStatus.CheckFlag(SF_AUTOCONNECT)){
+ INFO("Reconnecting to Network...");
+ if(SetupNetwork(1)>0){
+ IotStatus.SetFlag(SF_WIRELESSCONNECTED);
+ INFO("Connected to Network.");
+ }else{
+ WARN("Could not re-connect to the wireless network.");
+ }
+ }
+ }else{
+ DBG("Network connected.");
+ }
+
+ if(IotStatus.CheckFlag(SF_AUTOCONNECT) && IotStatus.CheckFlag(SF_WIRELESSCONNECTED)){
+ // Server connection was closed, try to reconnect:
+ INFO("Reconnecting to Websocket Server on ws://%s:%d/ws...", SERVER_IP, WS_PORT);
+ if(!ws.connect()){
+ WARN("Could not connect to the server again...");
+ IotStatus.ClearFlag(SF_SERVERCONNECTED);
+ ReconnectAttempts++;
+ if(ReconnectAttempts > 4){
+ INFO("Failed after %d reconnect attempts. Resetting the Wifi Shield...", ReconnectAttempts);
+ SetupNetwork(1);
+ ReconnectAttempts = 0;
+ }
+ }else{
+ INFO("Connected to ws://%s:%d/ws", SERVER_IP, WS_PORT);
+ // Set a status flag:
+ IotStatus.SetFlag(SF_SERVERCONNECTED);
+ }
+ }
+ }
+
+ return;
+}
+
+void ReceiveNetworkData(unsigned int * wifi_cmd, unsigned int * var, float * value){
+ char msg_buffer[CHARMSGBUFF];
+ char msg_buffer2[CHARMSGBUFF];
+ // Check for data on the websocket:
+ if(ws.readmsg(msg_buffer)){
+ INFO("Received: %s", msg_buffer);
+ sscanf(msg_buffer, "%d,%s", wifi_cmd, msg_buffer2);
+ if(*wifi_cmd == CHANGEVAR_WIFI_CMD){
+ // Get two more values:
+ sscanf(msg_buffer2, "%d,%f", var, value);
+ }else{
+ // Get one:
+ sscanf(msg_buffer2, "%f", value);
+ }
+ }else{
+ //DBG("Did not receive anything :(\n\r");
+ *wifi_cmd = NO_WIFI_CMD;
+ *var = 0;
+ *value = 0.0f;
+ }
+ return;
+}
+
+void ModifyVariable(unsigned int wifi_var, float wifi_data){
+ // modifies something in the SCS Controller:
+ switch(wifi_var){
+ case CV_LED:
+
+ break;
+
+ default:
+ break;
+ }
+ return;
+}
+
+
+
\ No newline at end of file
