Prerequisites
STM32F4 DiscoverySTM32F4 Discovery
STM32F4 Discovery WiFi BoardSTM32F4 Discovery WiFi Board
UART8000-U CableUART8000-U Cable
Introduction
We are going to use the STM32F4 Discovery BoardSTM32F4 Discovery Board and the STM32F4 Discover WiFiSTM32F4 Discover WiFi add on module to create a program that will send a signal when a certain movement is detected. So, imagine a scenario like this. I have a secret door in my house with no visible way to open it. Instead of a secret book on a self that you have to pull in order to open it, I have a magic gesture that I do with my Discovery board in order to open it. When I move the board in a specific way, the door will open.
Ok, so I don't have a secret door. However, this project will show you how to use the accelerometer to detect a specific movement and then transmit a signal over WiFi indicating that the movement occurred. This could be used for opening a secret door, turning on/off a light, changing the channel on TV, or many other things like that.
Setting Up WiFi - Initial Configuration
The STM32F4 Discovery WiFi board's default behavior is to act as a wireless access point. Since this is the case, we can test out the WiFi board and make sure that it is working correctly before we attach it to the STM32F4 board. Less variables means less things to potentially go wrong. So, we'll start out by using this default behavior to connect to it and do some initial configuration. In doing this, we will verify that WiFi board is working as expected and that we can connect to a wireless network.
The first step is to power the device. The easiest way to do this is to plug in a mini-USB cable into the board:
Once you do that, the LED next to V1 should light up red indicating that there is power to the board. Now, since the board starts up as a wireless access point, we can connect to it with any WiFi device. Since, I had my smart phone handy, that's what I used.
Start by searching for WiFi networks and look for one that starts with “Murata-WS”. For me, it was called “Murata-WS-418135”. After you connect to that network, then open up a browser and navigate to sn8200.com. This will present you with a page that looks like this:
Select the second tab called “WiFi scan and join”, and you will get a web page that lists all of the available networks:
Navigating the UI should be pretty straight forward. Enter in a password, if your network has one – and it should, then click join. You will get a message like “Result: Join pending. Allow 10 seconds for the status to refresh”. After waiting 10 seconds (or so) click OK and then navigate back to the initial tab “WiFi STA Status”. On this page you should now see a Status of “In network”, and the SSID should be populated with the SSID from your network.
WiFi Demo Example Program
With the STM32F4 Discovery WiFi board comes a code sample to demonstrate some of the functionality that the board provides. So, as a first step, we'll get that code sample up and running.
Unfortunately, this sample is not readily available. It requires an access code that comes with the STM32F4 Discovery WiFi board's documentation. In there should be a piece of paper that has the heading “Setting Up the System”. In the “Kit Contents:” section, there should be an “Access Code”.
So, armed with that access code, go to:
http://element14.com/stm32f4-expansion
And click on the “Download Your Software” button. After the download completes, unzip the file and you'll notice that there are quite a few goodies in there. The one that we will mostly be concerned with is what is in the “software” directory. Inside there will be two installers “Setup_client.msi” and “Setup_server.msi”, you can go ahead and install these now, as we will need them later.
There is also a project file for the Keil MDK-ARM IDE, in the software/ST-Discovery-Wifi/Project/WiFi_Demo_V2.2_20130620/MDK-ARM directory. This is what we will use to compile the example program. So, if you don't already have the Keil IDE installed, now would be a good time to do that. It can be downloaded from:
https://www.keil.com/demo/eval/arm.htm
Now the latest Keil IDE is on version 5 and the example code was created on version 4, so you will have to convert the project upon opening it. During the conversion, it will ask if you would like to install legacy support, which you do want to install. It will automatically take you to:
http://www2.keil.com/mdk5/legacy/
Download and install the first option: “Cortex M Devices”.
Now, if you try and compile the code now, you will run into an error because a file called rtl.h is missing. For whatever reason, this file does not come with the sample. Fortunately, there are a couple of places on the internet where the file can be found:
https://github.com/x893/CMSIS-DAP/blob/master/Firmware/STM32/USBStack/inc/RTL.h
http://www.element14.com/community/thread/30912?start=15&tstart=0
Copy and paste that code into a file called rtl.h and place it in the software/ST-Discovery-Wifi/Project/WiFi_Demo_V2.2_20130620/User directory.
Once that is accomplished, you should be able to successfully build the project from within the Keil IDE!
Wiring Up WiFi
Now that we got the sample code compiling, it is time to get it up and running on the STM32F4 Discovery device. To do this, we need to wire together the STM32F4 Discovery board to the STM32F4 Discovery WiFi board, and then we need to connect the STM32F4 Discovery board to our PC so that we can see what is running on the board.
First, let's wire together the two boards. To do this, make the following connections:
STM32F4 Board | WiFi Board |
---|---|
PB6 | J6 - pin 3 |
PB7 | J6 - pin 5 |
GND | J6 - pin 11 |
GND | J6 - pin 12 |
The we also need to connect the UART8000-UUART8000-U cable to the STM32F4 Discovery Board and the computer The USB end goes into the computer that's pretty easy The other connections look like this
UART8000-UUART8000-U | STM32F4 Discovery Board |
---|---|
Green | PC10 |
Red | PC11 |
Black | GND |
Nice, once that is connected, and the USB end is plugged into your computer, open up Device Manager (Start – Control Panel – System and Security – System – Device Manager). In there you should see a section called ports, expand that and in there you should see your STM32F4 board.
Now, the name does not make it obvious what it actually is; it is called “USB Serial Port (COM5)”. If you are unsure which entry is your STM32F4 board, one simple way to figure it out is to unplug and then reinsert the USB into your PC. Removing it should cause the Device Manager to refresh and whatever one went away when you unplugged it and then came back when you plugged it back in is your STM32F4.
Now, the important piece of information is the COM port that the STM32F4 board is connected to. In my case this is COM5. With this information, we can connect to the board using putty.
Fire up putty and start by going to the Connection – Serial settings page. On that page, make the settings look like this:
You'll want to change the entry from COM5 to whatever COM port that your STM32F4 is connected to. However, the Speed (baud), Data bits, Stop bits, Parity and Flow control settings should be changed to match the picture above.
Once those settings are complete, go to the Session page and choose a connection type of Serial. This should pull in the settings that we configured in the previous step:
Optional: Now it is probably a good idea to name the session and save it. To do this, add a name in the Saved Sessions text box and click Save. This way you don't have to reconfigure the Connection – Serial settings every time that you open up putty.
Once everything is set up, click the open button. This will open up a terminal to the STM32F4 Discovery board. Now, at first this will come up blank. In order to get it to do something more exciting, we need to load the WiFi demo program that we compiled earlier on to the board. To do this, open up the Keil IDE and click on the load button:
In the Build Output window you can watch the progress of the program getting loaded on to the board. When it is complete, you should see a message like “Programming Done. Verify OK.”
Once this is complete, if you go back to your putty window, you should see a new menu appear:
As you can see, the demo application has quite a bit of functionality in it! As a test to make sure that everything is working like we expect it to, type in a 0 to Get WiFi status. This should return a message like this:
Awesome, this returned the information about the network that we connected to in the previous section. Now we can be confident that the boards are properly wired together and that we can successfully connect to a network.
Note: There are also options within the menu in order to connect to a network, in case you had difficulty doing that in the previous section.
Note: There are also some test programs in the User Manual to make sure that your device is working properly and can communicate with your computer.
Using the Accelerometer
Fortunately, there is a great library out there to interface with the accelerometer on the STM32F4 Discovery board:
https://github.com/MaJerle/stm32f429
So, start by cloning this repository, and then navigate to the folder:
35-STM32F4_LIS3DSH_LIS302DL
Inside there should be a Keil IDE project called project.uvprojx. Simply open that up, compile it, and load it on the device. It compiles without any errors or warnings.
This program lights up LEDs indicating which way the board is tilted. So, after the program is loaded and running, tilt the board around and watch the various colored LEDs light up. Here's a quick video demonstrating the behavior:
More information about the library can be found on the creator's website:
http://stm32f4-discovery.com/2014/09/library-35-lis302dl-or-lis3dsh-accelerometer/
Combining the Projects (Accelerometer into WiFi)
Now for the tricky part. We want to combine these two projects into one project so that we can use the WiFi board and the accelerometer together! The easiest way that I found to do this was to start with the WiFi project and slowly add in the features from the accelerometer library. Here's the steps that I took:
1) Rename <WiFi>/software/ST-Discovery-Wifi/Project/WiFi_Demo_V2.2_20130620/User/main.c to main.c.old. We're going to copy over the accelerometer project's version of the file and we don't want the names to collide and the file to get erased.
2) From <Accelerometer>/35-STM32F4_LIS3DSH_LIS302DL/User copy main.c, defines.h, stm32f4xx_it.c, and stm32f4xx_it.h into <WiFi>/software/ST-Discovery-Wifi/Project/WiFi_Demo_V2.2_20130620/User.
3) From <Accelerometer>/00-STM32F429_LIBRARIES copy attributes.h, tm_stm32f4_gpio.h, tm_stm32f4_gpio.c, tm_stm32f4_delay.h, tm_stm32f4_delay.c, tm_stm32f4_disco.h, tm_stm32f4_disco.c, tm_stm32f4_spi.h, tm_stm32f4_spi.c, tm_stm32f4_lis302dl_lis3dsh.h, and tm_stm32f4_lis302dl_lis3dsh.c <WiFi>/software/ST-Discovery-Wifi/Project/WiFi_Demo_V2.2_20130620/User. Since these are libraries, I probably should have copied them over into another directly and then added that directory to my library path, but I didn't.
4) Open the <WiFi>/software/ST-Discovery-Wifi/Project/WiFi_Demo_V2.2_20130620/MDK-ARM/DiscoverWIFI.uvproj within the Keil IDE.
5) Right click on the User folder and choose the option “Add Existing Files to Group: 'User'...” and then within the file dialog, select the following files: stm32f4xx_it.c, tm_stm32f4_delay.c, tm_stm32f4_disco.c, tm_stm32f4_gpio.c, tm_stm32f4_lis302dl_lis3dsh.c, and tm_stm32f4_spi.c
6) Right click on the STM32F4xx_StdPeriph_Driver floder and choose the option “Add Existing Files to Group: 'STM32F4xx_StdPeriph_Driver'...” and within the file dialog, select the following file: stm32f4xx_spi.h
7) Replace all instances of mdelay with Delayms
8) In main.c and sn8200_api.c change #include “delay.h” to #include “tm_stm32f4_delay.h”
9) In tm_stm32f4_delay.c add:
10) Right click on folder STM32F4-Discovery and choose “Options for Target 'STM32F4-Discovery'...”. Within the C/C++ tab, there should be a section called Preprocessor Symbols, and within there a text box labeled as “Define:”. Add KEIL_IDE,TM_DISCO_STM32F4_DISCOVERY to the end of the list.
Now you should be able to successfully compile the program and run it. This should produce a program that has the same behavior as the accelerometer program, but has some additional libraries included so that we can add some WiFi behavior when the time comes.
Creating a Program
Now that we have the two projects combined, we can edit the program to include the functionality of both demos. To do this, make the main.c file look like this:
Looking at the code, most of it is the same as the two projects, just slightly tweaked to make them work together. I made it so that the lights on the Discovery board still light up when the board is tilted. Then when the board is tilted in the direction of the red LED, a message is sent to the server. In this case, the message is “Hello”.
Now, we could add a more difficult gesture recognition, however that would just complicate the example. So, in this case, I decided to keep it simple and just have the message be sent over the network when the board is tilted towards the red LED.
Note: In order to get it to work on your network, you'll probably want to change the IP Address from 192.168.1.9 to whatever the appropriate value is for your computer.
Server to Receive the Message
Now that we have the Discovery board sending a message, we need someone on the other end to receive the message. So, to do this, we'll set up a very simple server that will just log when a connection occurs and then write out any message that it receives to the console. Here's the code for a simple server:
Now this server is based on the great example that is given here:
http://www.linuxhowtos.org/C_C++/socket.htm
I just made some small modifications to work on Windows and to be slightly more suited towards my needs.
Running the Program
Now for the fun part, where all of your hard work is rewarded, running the program and watching it succeed! Load up the program on to the Discovery board and fire up the server on your PC and watch them send a message when the board is tilted towards the red LED.
Here's a quick video demonstrating the program:
Conclusion
By combining the power of the accelerometer and the WiFi module of the STM32F4 Discovery board, we were able to create a program that sent a signal based on detecting a movement pattern. This signal could be used for many different applications like opening a secret door, turning on/off a light, changing the channel on the tv, or many, many more applications. The gesture could also be as complex or as simple as you would like.
Next Steps
After getting the accelerometer and WiFi working together, I used it to create a simple game:
Top Comments