Practice Exercise: Events and Networking

Table of contents

  1. Instructions
  2. Optional: Exploring More

As part of this exercise you’ll practice developing paired devices and networked interactions with Particle.publish

Before you begin, review the concepts introduced in the section and make sure you understand how events work.

Working in a team of three, you’ll create a set of connected devices. The devices will have a motor and a pushbutton. When you push the button on one of your team’s devices, it will trigger a response on the other two devices.

Instructions

Circuit

Reuse the circuit you prepared for the Motor’s practice exercise.

Next, add a pushbutton to that circuit.

Code Functionality

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 approach 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 pinMode statements to setup and 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:

  1. Visit https://console.particle.io/events

  2. Press the button and check to see that an event with the correct name is published from your device

  3. 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:

  1. The completed code

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

Exercise 1

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.

Exercise 2

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)