This is a simple example of mbed client with PIR sensor working for motion detection with mbed OS (linux version is not provided here. You can try it by yourself). You may need to take a look at mbed-client-quickstart first to finish the device registration.
The application:
- Registers with mbed Device Connector.
- Gives mbed Device Connector access to its resources (read and write).
- Records the number of clicks on the device’s button as well as motion detected by PIR sensor and sends the number to mbed Device Connector.
- Lets you control the state of PIR sensor and blink pattern of the LED on the device (through mbed Device Connector).
Five resources are included:
Button (3200/0/5501)
. Number of presses of SW2 (GET).LED Blink (3201/0/5850)
.Blink function, blinksLED1
when executed (POST)Blink Patter (3201/0/5853)
. Used by the blink function to determine how to blink. In the format of1000:500:1000:500:1000:500
(PUT).PIR State (3300/0/5700)
. Pir state function, turn PIR sensor on or off by click the corresponding buttons (POST).Motion Detection (3300/0/5701)
. Number of motion detected by PIR sensor (GET).
- An FRDM-K64F board with PIR sensor.
- An Ethernet connection to the internet.
- An Ethernet cable.
- A micro-USB cable.
- An ARM mbed account.
- yotta - to build the example programs. To learn how to build mbed OS applications with yotta, see the user guide.
- A serial port monitor.
The application prints debug messages over the serial port, so you can monitor its activity with a serial port monitor. The application uses baud rate 115200.
Note: Instructions to set this up are located here.
After the bin file downloaded and your board reset, you should see the messages as below:
In app_start()
IP address 10.164.3.62
Device name 1b0273c6-d31f-4337-8c5b-fbc8b1fff045
SOCKET_MODE : UDP
Connecting to coap://api.connector.mbed.com:5684
Registered object successfully!
Note: Device name is the endpoint name you will need later on Testing the application chapter.
After you click the SW2
button on your board you should see messages about the value changes:
handle_button_click, new value of counter is 1
When something moves in the detecting range of PIR sensor, the monitor will output the messages like this:
Hello! I've detected 1 times since reset
By experiments you will find the sensor is really sensitive.
- After device registration finished, go to My devices > Connected devices. Your device should be listed here.
- Press the
SW2
button on the device a number of times (make a note of how many times you did that). - Go to Device Connector > API Console.
- Enter
https://api.connector.mbed.com/endpoints/DEVICE_NAME/3200/0/5501
in the URI field and click TEST API. ReplaceDEVICE_NAME
with your actual endpoint name. The device name can be found in thesource/security.h
file, see variableMBED_ENDPOINT_NAME
or it can be found from the traces Monitoring the application. - The number of times you pressed
SW2
is shown. - Enter
https://api.connector.mbed.com/endpoints/DEVICE_NAME/3300/0/5700
in the URI field and click TEST API. - The number of motion detected by the sensor is shown.
- Press the
SW3
button to unregister from mbed Device Connector. You should seeUnregistered Object Successfully
printed to the serial port and LED starts blinking. This will also stop your application. Press theRESET
button to run the program again.
NOTE: If you get an error, for example Server Response: 410 (Gone)
, clear your browser's cache, log out, and log back in.
NOTE: Only GET methods can be executed through Device Connector > API Console. For the other methods check the mbed Device Connector Quick Start.