1. Component list
-
Arduino UNO board (http://arduino.cc/en/Main/arduinoBoardUno)
- Arduino Ethernet shield (http://arduino.cc/en/Main/ArduinoEthernetShield)
- Common anode RGB led x 1
- 330 ohm resisters x 3
Arduino UNO board |
Ethernet shield |
RGB LED |
2. Overall idea
We already have discussed few ways to monitor the temperature and humidity in a remote site using Arduino or Raspberry pi with the support of GadgetKeeper. Also we have discussed, how to trigger email alert when exceed the pre-configured threshold levels. So this is the next step.You can easily setup this simple but useful circuit for visualized notifications. This tutorial shows how to setup the blink notifier and how to use it for notifications. The unit consists of Arduino UNO board and Ethernet shield with a RGB led connected to it. The unit keep on connected to the internet and subscribe with GadgetKeeper mqtt service and listen for commands. Once it receives a valid command from the GadgetKeeper via mqtt service, it analyzes the command contain data and activate the LED. The basic json formatted instruction set contains the following information at the moment. You can easily follow the sample code and extend it to support more settings or even more RGB LEDs. Not only that you can connect a buzzer with few modification in the code and trigger some sounds parallel with the LED notification.
current json data format as follows.
You can set the following parameters via mqtt command.
- colour (red, green, blue)
- led fading speed (in millisecond)
- notification duration (in millisecond)
3. How to configure GadgetKeeper to use with Arduino blinker
Basically you need to follow the steps given below to prepare the GadgetKeeper for this test. All the necessary steps are given below with details,
- Create the BlinkRequest data type.
- Create blink thing.
- Create blink method.
- Create the mqttBlink connection to communicate with blink hardware.
- Create the thermometer thing.
- Create the mqtt connection to communicate with the temperature sensor hardware.
- Create the temperature property.
- Create the temperatureChanged event.
- Create the highTemperatureBlink trigger.
- Add the action script to trigger.
-
Create the BlinkRequest data type.
Click on Data Types icon and define the data format as shown below. You should at least define colour (String), fadeSpeed (int) and duration (int) to communicate with blink hardware at the moment as shown below. - Create blink thing.
Then create the blink thing as shown below.
- Create blink method.
Next step is to create the blink method as shown below.
-
Create the mqttBlink connection to communicate with blink hardware.
Then create the mqtt connection as shown below. This is the connection GadgetKeeper use to communicate with blink hardware.
- Create the thermometer thing.
Next step is to create the thermometer thing.
-
Create the mqtt connection to communicate with the temperature sensor hardware.
Since we are connecting a mqtt supported thermometer with the GadgetKeeper, we need to create this mqtt connection to communicate with that thermometer. Make sure to set the timeout parameter to 10000ms to avoid common network timeout problems.
- Create the temperature property.
next add the temperature property to thermometer thing as shown below. Make sure to set the Data Type to float and Type to remote.
- Create the temperatureChanged event.
Create the temperatureChanged event as shown below.
- Create the highTemperatureBlink trigger.
Then create the highTemperatureBlink trigger as shown in the figure. Make sure to select the thing and event correctly as shown in the sample snap.
-
Add the action script to trigger.
We have already discussed in this step by step tutorial about how to configure the email notification on temperatureChanged event. But we are not sending email notification in this stage. So we should invoke the blink method in the blink thing instead. Then it will send the blink command to Arduino via mqtt connection and trigger the LED. So lets change the action script as shown below.
Now the GadgetKeeper configuration part is finished. Next step is to prepare the Arduino hardware, compile the sketch and upload it to Arduino.
4. Prepare the blinker hardware
It is so simple circuit. You can setup it easily. We only use the Arduino D3, D5, D6 pins to drive R G B colours respectively. Also please make sure to use 330 ohm resisters with each R G B terminals to control the current to LED. Have a look at the following prototype circuit.
5. Arduino libraries required
We have used Arduino 1.5.6-r2 version as the IDE. You can download it from this url.(http://arduino.cc/en/Main/Software). Also make sure to download and include following libraries to the sketch if not already exists. (Most of the libraries comes with the Arduino IDE by default)
-
SPI.h
-
Ethernet.h
-
PubSubClient.h
-
JsonParser.h
Note*** - You need to make small modification on one of the Arduino library you used for this tutorial.
First open the PubSubClient.h library header file and make the following simple modification in that file. (increase the packet size as shown below). The GadgetKeeper MQTT request is bigger in size. So the default packet size defined in Arduino library is not enough to handle the MQTT request at Arduino side. So increase the packet size to 256 as shown below. Then restart the Arduino IDE.
6. Arduino sketch
Check more details on implementing JSON-RPC handler with JSON-RPC over Messaging
Use the following Arduino sketch with necessary modifications to match with your settings.
Please note that we have specially used millis(); function to manage long delay periods within the void loop() section in the script. We can't use standard delay(); function for this with long time periods. Because it will stop listening to mqtt messages while the delay function is called. However still we have used the standard delay(); function to handle short time periods in the sketch.
7. How to test
Make sure to connect the Arduino blink module to Gadgetkeeper before testing. Then click on the event in thermometer thing and click on Push Event button. Now you can see the blink module LED is getting blink and notify the high temperature state. Also you can further modify the action script and control the different LED blink status as shown below.
function onEvent(event){
var blink=context.things[ "blink" ].me;
if (event.value> 30 ){
blink.blink({colour: "red" , fadeSpeed: "5" , duration: "100000" });
} else if (event.value> 25 ){
blink.blink({colour: "red" , fadeSpeed: "10" , duration: "200000" });
}
}
|
8. How to debug the blinker device
Open the serial terminal in 9600 baud rate. Then you can see all the arduino debug messages from your blinker application. Please check the example below.
(Arduino capture the instruction set and blink according to the instruction received.)
9. TODO
- Connect buzzer to Arduino and ring it when receive blink commands from GadgetKeeper.
- Modify the sketch to support for different colours like orange, yellow etc.