element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Single-Board Computers
  • Products
  • Dev Tools
  • Single-Board Computers
  • More
  • Cancel
Single-Board Computers
Blog Accelerometer readings over WiFi with the STM32F4 Discovery Board
  • Blog
  • Forum
  • Documents
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Single-Board Computers to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: oneleggedredcow
  • Date Created: 21 Jun 2015 4:34 PM Date Created
  • Views 5455 views
  • Likes 7 likes
  • Comments 24 comments
  • stm32f4dis-wifi
  • accelerometers
  • stm32f4
  • wi-fi
  • accelerometer
  • stm32f407
  • wi-fi_module
  • wi-fi_solution
  • wi-fi®
  • wi_fi
  • discover_wi-fi_board
  • discover_wi‐fi
  • stm32f4discovery
  • wi-fi_connectivity
Related
Recommended

Accelerometer readings over WiFi with the STM32F4 Discovery Board

oneleggedredcow
oneleggedredcow
21 Jun 2015

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:

image

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:

image

Select the second tab called “WiFi scan and join”, and you will get a web page that lists all of the available networks:

image

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

image

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 BoardWiFi Board
PB6

J6 - pin 3

PB7J6 - pin 5
GNDJ6 - pin 11
GNDJ6 - pin 12

image

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-USTM32F4 Discovery Board

Green

PC10

RedPC11
BlackGND

 

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.

image

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:

image

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:

image

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:

image

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:

image

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:

image

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:

This video is unavailable.
You don't have permission to edit metadata of this video.

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:

This video is unavailable.
You don't have permission to edit metadata of this video.

 

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:

Lava Ball

  • Sign in to reply

Top Comments

  • Former Member
    Former Member over 10 years ago +3
    Cool WiFI SSID
  • DAB
    DAB over 10 years ago +2
    Nice detailed post. DAB
  • oneleggedredcow
    oneleggedredcow over 9 years ago in reply to derekt +1
    Glad you like the tutorials. When I had problems similar to that, it was because both libraries defined a function with the same name. In my case, both functions had the exact same purpose. So, all I had…
  • Former Member
    Former Member over 9 years ago

    Hello,

    I hope you see this reply.

    This is a very helpful tutorial.

    I have a question related to USART1.

     

    Unfortunately, I already use PB6 and PB7 for another function

    So I did change the all the related part of UART1 to UART3.

     

    When I use UART1, It works fine. However, after I changed it, it keeps hanging.

    What I am trying to say is that I write '0' to call the getstatus, In hyper terminal getstatus is displayed and the others such as mac address is not displayed.

     

    Do you know what is the reason that happened? when I change UART1 to UART3?

    I also well wired depend on what uart port I use.

     

    I am looking forward to replying your answer

     

    Thank you again

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • czyskows
    czyskows over 9 years ago

    Hi Shaun,

     

    You are a king among men.  Thanks for the detailed tutorial. So much better than that provided in the user manual.

     

    Cheers

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • derekt
    derekt over 9 years ago in reply to oneleggedredcow

    Thanks Shaun

     

    I have taken your advice and posted the question in the discussion question. I am not getting any error any more. I can't seem to get the functions to work right. When I call the the functions in the sn8200_api.c the program seem to get into an infinite loop. I have gone through your program, the following parts are what I am a bit unsure of are the following parts:

     

    • TM_USART_Init(USART1, TM_USART_PinsPack_1, 115200); What are you using USART1 for, because it seems like DBGU_Init() configures USART3 on pins PC10 and PC11. You have configured USART1 PinsPack_1 which is supposed to be for PA9 and PA10, but on USART1 your wiring diagram uses PB6 and PB7 which is supposed to be PinPack2.image
    • struct __FILE {int dummy;};FILE __stdout;intfputc(int ch, FILE *f) {TM_USART_Putc(USART1, ch);return ch;}
    • This whole above part, struct, and the dummy int, what do they do?

     

    Many thanksimage

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • oneleggedredcow
    oneleggedredcow over 9 years ago in reply to derekt

    Sure, but I definitely don't have enough information to help you effectively.  What's the compiler error that you are seeing?  (Or the problem symptoms?)  What does the code look like?

     

    Also, instead of posting the question directly in the comments, you may want to consider posting the question in the discussion section:

    http://www.element14.com/community/discussion/create.jspa?sr=cmenu&containerType=14&containerID=2463

     

    Lots more people look at the discussions than read through all of the comments on a blog post.  The more people that see your question, the more likely it will be answered.  (You should probably also put a link to the question in the comments here.  Again, the more people that see it, the better.)

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • derekt
    derekt over 9 years ago

    Hello Shaun, I still am having issues using the functions in the WiFi demo code that comes with the Discovery Wifi. I am using a virtual com port instead of the usb -> uart converter. Can you help me with ideas just to achieve my 2 way communication between my board and my client. On the client side I am ok, just the board side.

     

    Thanks in advanceimage

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube