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

Dependencies:   WebSocketClient WiflyInterface mbed messages

You are viewing an older revision! See the latest version

Tour of the Code

This first part of the tour will focus on the wireless communication. At the end of this section will be a few notes on other interesting aspects of the code: the messaging system, and the manual configuration of the ADC module.

Wireless Communication and the WebSocket Client

Looking at the file structure of the IoT_Ex project, you can easily identify the two libraries necessary for the wireless communication: WiflyInterface and WebSocketClient. These two libraries were developed by other mbed users, and I have modified them slightly to improve their performance. I will not be going into detail on how they work, but on how they are being used for this project.

As shown in Figure 1, below, the Main Program interfaces with the “high level” WebSocket Client. Once a connection is established, the main program simply needs to call either ws.send() or ws.readmsg() to send or receive data from the server. After anyone of those function calls, the data goes through the WiFly Interface, then the UART module, then the physical wires to the RN-171 module. All of these lower level layers of code and hardware ensure everything is sent at the right time, and errors do not occur in the transmission, etc.

Figure 1 - Schematic diagram of the mbed software and hardware.

The next two subsections will parse through some of the important parts of main.cpp and globals.cpp that enable the wireless communication using the WebSocket Client and the WiFly Interface. I will also point out the areas of the code which you can modify to send and receive different types of data.

main.cpp

49   // Main Loop!
50   int main() {

The int main(){…} function is where most of the action takes place, so let’s take some time to parse through it. The first part of the function has some standard declarations, and variable configurations. Lines 74 and 78 is some of the first communications code we find:

72	// Connect to the wifi network. It will basically get stuck here until it
73	// connects to the network.
74	SetupNetwork(5000);
75	
76	// Configure the baud rate of the wifi shield:
77	// This will make our wireless transmissions much faster.
78	ws.setBaud(115200);

The function SetupNetwork() connects to the wireless network. It will try 5000 times before it exits. The function itself is explicity defined in globals.cpp. Once connected to the network, we know the communication between the microcontroller and the RN-171 module is working, so we increase the baud rate of the UART interface between the microcontroller and the RN-171 module to 115200 with a call to ws.setBaud() on line 78.

ws is a global instance of the WebSocketClient class, and is declared in globals.cpp.

Next, the microcontroller will try to connect to the WebSocket server on Line 87:

82	if(IotStatus.CheckFlag(SF_WIRELESSCONNECTED)){
...	    ...
87	    if(ws.connect()){
88	        // Set a status flag:
89	        INFO("Connected.");
90	        IotStatus.SetFlag(SF_SERVERCONNECTED);
91	    }else{
92	        // We could not connect right now..
93	        IotStatus.ClearFlag(SF_SERVERCONNECTED);
94	        INFO("Could not connect to server, will try again later.");
95	        ReconnectAttempts++;
96	    }
97	}

If it cannot connect, it will try again later, during a call to SendNetworkData().

Finally, we get to the infinite loop. This is where the microcontroller spends all of its processing time:

103	// Inifinite main loop:
104	while(1) {
105	    
106	    // Process the wifi command:
107	    if(wifi_cmd > NO_WIFI_CMD){
108	        // Modify the desired variable:
109	        ModifyVariable(wifi_cmd, wifi_data);
110	        // Reset the command:
111	        wifi_cmd = NO_WIFI_CMD;
112	    }
113	    
114	    // Check for new wifi data:
115	    if((wifi_cmd == NO_WIFI_CMD)){
116	        ReceiveNetworkData(&wifi_cmd, &wifi_data);
117	    }
118	    
119	    // Send the network data every 3 seconds:
120	    if(DisplayTimer.read()>(3.0f)){
...	        ...                
133	        // Send data over network:
134	        SendNetworkData();               
...	        ...                
139	        // Reset the timer:
140	        DisplayTimer.reset();
141	        
142	    }
143	} // while(1)

On Line 116, the microcontroller checks for new data from the WebSocket server. The data is parsed in the ReadNetworkData() function into the two variables: wifi_cmd and wifi_data. Within this function, is the function call to ws.readmsg(), mentioned earlier. In effect, the microcontroller is polling the websocket for any new data. You could also implement this as an interrupt, but I leave that as an exercise for the reader. Any received data is handled by the ModifyVariable() function on Line 109.

The if(){…} statement on Line 120 ensures SendNetworkData() is called once every 3 seconds. SendNetworkData() sends a comma separated string to the WebSocket Server of the form of:

IoT_ID, SendCounter, TempSensor

All of the functions listed above are found in globals.cpp.

That’s it!

144	} // main()

All wikipages