Practice Exercise: Events and Networking
Table of contents
As part of this exercise you’ll practice developing paired devices and networked interactions with
Before you begin, review the concepts introduced in the section and make sure you understand how events work.
While you will reuse the circuit, you should start a new code project for this exercise.
As part of this exercise, you’ll need to
Use the paired device template provided as a starting point
Agree and define an event name to trigger responses in your paired devices. Modify the event name in the template in all locations.
Modify the code to setup and configure your component i.e. add global variables, appropriate
setupand any necessary libraries to your project.
Change the example so that it will trigger an event to be published when the button is pushed. Ensure an event is not published more than once every 5 seconds.
Finally add some code so that when an event is received from another device that it triggers a response from your component e.g. the Neopixel should fade up, the Solenoid should tap and the piezo should beep.
Use the Particle Event console to test your events are working:
Press the button and check to see that an event with the correct name is published from your device
Click the green arrow icon to send a test event. Type in the event name you have chosen and click
Publish. Ensure your device responds.
When the other paired devices are complete, test the interaction together.
Submit the following:
The completed code
A brief video of the interaction working
Optional: Exploring More
Once you’ve achieved the outcome, you can try some of the below and experiment to improve your coding skills
Update the paired device to only publish after the button is released. Use
millis() to store a timestamp when the button was first pushed and measure the duration it’s pressed. Send the duration in seconds to the paired devices and update the response to show this e.g. if the button is pushed for 5 seconds, the solenoid is held for 5 seconds; perhaps the neopixel lights up a proportional number of pixels.
Hint You can’t send a raw number in a Particle.publish. use
String( value ) to convert a number to a string.
Extend the behavior so that it allows for a pseudo morse code message to be transmitted. Sample the button over 10 seconds to see if the button is pressed or released and build a short string to send to the other device e.g.
11110000111101111101111 would show 4 presses. After 10 seconds publish the event to the other devices and have them display it as follows: for each 1 the actuator will throw, for each 1 the piezo will beep and the neopixel will display the sequence in illiminated pixels (1 is on, 0 is off)