LED connection with Arduino MKR1000
Connect Arduino MKR1000 with a strip LED lights as visualized in the schematic.
Register MKR1000 to IBM Watson IoT
In order to register your Arduino MKR1000 board to IBM Watson IoT, you can read the following step-by-step guide:
Once finished, note down credentials for the registered device (org-id, device-type, device-id), the API-key and Authentication token.
Import NodeRed application
- From Bluemix dashboard, create a new application by selecting¬†‚ÄúInternet of Things Starter‚ÄĚ boilerplate; then launch the application and open the NodeRED flow editor. You can read detailed instructions here:
- From NodeRED flow editor, go to Import –> Clipboard
- Open the txt file available in¬†
- Copy the content of the file and paste it in NodeRED as shown below.
- Click import
Configure and start NodeRED application
- The NodeRED application is composed by two flows (see picture below).
- First flow performs sentiment analysis and sends result to Arduino board
- Input node of first flow¬†connects to an email account periodically and fetches the last unread emails; configuration of the input node depends on email account selected (i.e. gmail). Each email received is sent to a sentiment analysis node, which returns a score (less or above 0) based on negative/positive content of the text analyzed (see this link https://github.com/thisandagain/sentiment/blob/develop/README.md¬†for more details).
- Sentiment score is sent to a simple function node which calculates the average of the last data received and pushes the result to the next node.
- Finally the last node sends a message containing the value of the sentiment score to the connected Arduino device using MQTT protocol; this block needs to be configured with the credentials generated during device registration process in step 2.
- The second flow is used for testing purposes to visualize the status of the Arduino board
- Input node receives device status from Arduino board and pushes it to IBM quickstart web page where it can¬†be visualized¬†(https://quickstart.internetofthings.ibmcloud.com/).
- Status messages contains the sentiment score and the RGB combination for LED currently used in Arduino board.
- Click deploy button to activate the flow
Configure Arduino sketch
- Download the sketch available in https://github.com/EmbeddedIoT/ConnectedMug/blob/master/Arduino/ConnectedMug.ino.¬†This sketch requires the following libraries:
- Open the sketch with Arduino IDE (https://www.arduino.cc/en/main/software)
- The code is composed by three parts:
- setup() : connect with WiFi AP and IBM MQTT broker; register a callback for messages received from IBM Watson IoT
- loop() : set RGB pin to control LED lights; send to IBM Watson IoT the status of the device (RGB and sentiment score)
- (…) : callback called upon reception of messages with sentiment score from Watson IoT application.
- The sentiment score is mapped to RGB values (negative: red; positive: green). Configure the code based on credentials generated during the device registration procedure described in step 2 (org-id, device-type, device-id)
- char *client_id = “d:your-org-id:your-device-type:your-device-id”;
- char *user_id = “use-token-auth”;
- char *pwd = “your-pwd”;
- char *ibm_hostname = “your-org-id.messaging.internetofthings.ibmcloud.com”;
- The application subscribes to topic iot-2/cmd/+/fmt/string and messageReceived callback parses messages for command type sentiment. Status messages are published to topic: iot-2/evt/status/fmt/json
- Connect the Arduino board to the PC, compile and flash the board in Arduino IDE as shown below
Launch the solution
- Power up the Arduino board and the LED strip lights. See connections in step 1.
- At start the Arduino application connects to the WiFi AccessPoint and later to the MQTT end point configured in the code.
- Once received the sentiment analysis score from IBM Watson IoT, the Arduino board will change the light of the LED accordingly (green for emails with positive messages, red for negative messages).
- It is possible to check in the IBM Quickstart web page the status in real time of the Arduino application (RGB code being displayed and the sentiment score received); open the web browser at page https://quickstart.internetofthings.ibmcloud.com and insert the device id to visualize the status for your device
- See more on this project here:¬†https://sites.google.com/view/make-iot/connectedmug¬†