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.
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 }