Author: Krishnaraj Varma (page 1 of 9)

Home Automation using Sonoff WiFi Switch and Google Assistant

Recently I got one Sonoff WiFi Switch from Itead.cc. Sonofff WiFi Switch is an ESP8266 based wireless switch and has a 5V Relay with a voltage range of 90-250V. Itead provides a mobile application eWeLinK to operate the switch remotely. The switch also works with Amazon Alexa and Nest which is awesome. You can find more details about how to use the switch here and about Alexa integration here.

The switch comes with default ESP8266 firmware which provides the functionality to operate this remotely using the eWeLinK mobile application. But more advanced users can program theESP8266 chip on it. There is an unsoldered pins which brings out the Rx, Tx, GND and Vcc of the ESP8266. You can use this pins to re-program the ESP8266 which is another awesome thing about this. Following picture shows the pins.

Sonoff Pins

enter image description here

To program the ESP8266 on it, you solder headers to these pins and connect to 3.3v FTDI board. Please note that it should be 3.3v compatible, don’t use 5v FTDI board since it will damage the board.

Wiring

Sonoff FTDI
Vcc (Square Pad) Vcc
Rx Tx
Tx Rx
GND GND

Once the wiring is completed connect the FTDI board to the computer. This will power the ESP8266. But this will not put the board in programming mode, it will just power the ESP8266. In order to put the board in programming mode, press and hold the button on the board ad connect the FTDI to the computer. This will put the board in programming mode. Following picture shows the button on the board.

enter image description here

Once the board is in programming mode you can use the Arduino IDE to program it. Install the ESP8266 platform packages using Package Manager (You can find more information here) and select the board Generic ESP8266.

Arduino Board Selection

enter image description here

The GPIO pin 12 is connected to the relay and GPIO pin 13 is connected to the LED. So in order to turn on the relay you can do digitalWrite on pin 12.

Demo Application

This demo connects to a MQTT server and listens for MQTT messages. The application listen the topic “mqtteclipse/cmd” for commands. For purpose of this demo the commands are simple 1 or 0. When the the payload 1 is received the relay is turned on and for 0 the relay is turned off. For this demo I am using publicly available Eclipse MQTT Sandbox server (iot.eclipse.org). If you have any other servers, replace the server (line 13 of the firmware).

const char* mqtt_server = "iot.eclipse.org";

You can use any MQTT client to send message to the board.

Google Assistant integration

The demo application works with Google Assistant. For this purpose I am using Actions on Google. I created a simple action using Google Action SDK. I am using the following action.

{
    "actions": [
      {
        "description": "Default Welcome Intent",
        "name": "MAIN",
        "fulfillment": {
          "conversationName": "sonoff_app"
        },
        "intent": {
          "name": "actions.intent.MAIN",
          "trigger": {
            "queryPatterns": [
              "talk to sonos"
            ]
          }
        }
      },
      {
        "description": "Turn on/off intent",
        "name": "TURNONOFF",
        "fulfillment": {
          "conversationName": "sonoff_app"
        },
        "intent": {
            "name": "com.sonoff.example.home",
            "parameters": [
                {
                    "name": "cmd",
                    "type": "SchemaOrg_Text"
                }
            ],
            "trigger": {
                "queryPatterns": [
                    "Turn $SchemaOrg_Text:cmd kitchen light"
                ]
            }
        }
      }
    ],
    "conversations": {
      "sonoff_app": {
        "name": "sonoff_app",
        "url": "https://hook.io/krvarma/eclipsemqtt"
      }
    }
}

As you can see from the above action, it is pretty simple. The actions has only one query pattern "Turn $SchemaOrg_Text:cmd kitchen light". User can say Turn on kitchen light or Turn off kitchen light. You may notice the $SchemaOrg_Text:cmd on the query pattern. This a query parameter defined in action JSON file. When the user say Turn on kitchen light the query parameter cmd will contain the text “on“, and will contain the word “off” when the user says Turn off kitchen light. This link describes defining actions.

Another point of interest is the fulfillment URL used to fulfill the action. This URL will be called to process the Action. I have hosted a node.js program on on hook.io microservice hosting platform. The URL https://hook.io/krvarma/eclipsemqtt points to a Node.js script I am using. Following is the script I am using:

module['exports'] = function myService (hook) {  
    console.log(hook.params.data);

    var intent = hook.params.inputs[0].intent;

    if(intent === "assistant.intent.action.MAIN"){
        var response = {
            "expect_user_response": false,
            "final_response": {
                "speech_response": {
                    "text_to_speech": "Hello!"
                }
            }
        };


        console.log(response);

            hook.res.writeHead(200, { 'Content-Type': 'application/json'});
        hook.res.write(JSON.stringify(response));
        hook.res.end();      
    }
    else{
        var mqtt = require('mqtt')
            var mqttClient  = mqtt.connect('mqtt://iot.eclipse.org', 1883)

        mqttClient.on('connect', function () {
            var arguments = hook.params.inputs[0].arguments;
            var cmdText = '';

            for(var arg of arguments){
                console.log(arg);
                console.log(arg.name + "=" + arg.raw_text);

                if(arg.name === "cmd"){
                    cmdText = arg.raw_text;
                }
            }

            var cmd = (cmdText === "on" ? '1' : '0');

            mqttClient.publish('mqtteclipse/cmd', cmd);

            var response = {
                "expect_user_response": false,
                "final_response": {
                    "speech_response": {
                        "text_to_speech": "Done!"
                    }
                }
            };

            hook.res.writeHead(200, { 'Content-Type': 'application/json'});
            hook.res.write(JSON.stringify(response));
            hook.res.end();  
      })
    }
};

You can see that the script sends the payload 1 or 0 depending on the Action on Google request JSON data. This JSON data will contain the details of the user voice input. So if the the input argument “cmd” contains “on” then the script send payload 1 using the topic mqtteclipse/cmd otherwise it sends the payload 0. The firmware running on the device receives the message and turn on/off based on the payload.

You can follow this Google Documentation to create a project in Actions on Google. Basically you have to do following steps to define the action.

  1. Logon to Actions on Google Console
  2. Create a Project and give it a name
  3. Choose Use Action SDK options to define the action
  4. Fill the App Information page. Here you can change the app name, invocation name, etc…
  5. Deploy the hook.io microservice.
    a. Sign in or Sign up on hook.io
    b. Create a microservice using node.js as the language
    c. Name it as eclipseqmtt.
    d. Copy and paste the microservice and save the service
  6. Save and test the action using any Google Assistant platform.

This documentation describes all the necessary steps to create Actions on Google. I am using “Jarvis Home Manager” as the invocation name. But you can use any name as you want, just replace the invocation name on the Settings page of the action. Following image shows our Actions on Google project:

enter image description here

Demo Video

Interactive Voice Response system using Particle Core, GSM Breakout Board, Emic2 and DTMF Decoder

Source Code

My previous project using was a Voice Messaging system based on Particle Core, GSM Breakout Board and Emic2 TTS Module. Actually this was the project I first started but because of the unavailability of the DTMF Breakout Board, I released the Voice Messaging project.

Overview

This project is an IVR System. Following hardware components are using for this project.

  1. Particle Core as main processor and for Internet connectivity
  2. A GSM Breakout Board for Cellular connectivity
  3. SparkFun I2C/SPI-to-UART Breakout Board for SPI to UART
  4. Emic2 Text to Speech module for TTS
  5. DTMF Breakout Board

Here is the overview of the different hardware components and how they interact.

enter image description here

This project is almost similar to my previous Voice Messaging project except it has an additional DTMF Breakout Board. This is used to decode the DTMF signals. This board is used to detect which number user has pressed on other end. The Audio Out of the GSM Breakout Board is connected to the Audio Input of the DTMF Decoder and the pins DR, D0, D1, D2 and D3 are connected to Particle Core pins D2, D3, D4, D5 and D6 respectively. The Data Ready (DR) pin of the DTMF Board will be high when the boards detects the DTMF tone and it is decoded correctly. When this pin is high, we read the data pins D0…D3, this will be give us the number pressed on the other end of the telephone line.

In this sample the GSM Board is configured to answer the incoming call after 2 rings, so when a call is received it will be automatically answer after 2 rings. When the call is answered, application will speak out a welcome message and prompts the user to enter a 4 digit PIN. The default PIN is 9999, but you can change it whatever you like. When the correct PIN is entered, the application will ask you to enter 7, 8 or 9. Pressing 7 will speak out the current temperature, pressing 8 will speak out current humidity and pressing 9 will hang up the call. The sample use DHT Sensor to read temperature and humidity. This sensor is connected to the pin D0 of Particle Core.

Wiring

Particle Core and I2C/SPI-to-UART

  • Core MOSI to I2C/SPI-to-UART MOSI
  • Core MISO to I2C/SPI-to-UART MISO
  • Core SCK to I2C/SPI-to-UART SCK
  • Core SS to I2C/SPI-to-UART SS

I2C/SPI-to-UART and GSM Shield

  • I2C/SPI-to-UART Vcc to 3.3v
  • I2C/SPI-to-UART GND to GND
  • I2C/SPI-to-UART Tx to GSM Board Rx
  • I2C/SPI-to-UART Rx to GSM Board Tx
  • GSM Board 5v to 5v
  • GSM Board GND to GND

Particle Core and Emic2 TTS Module

  • Core Tx to Emic2 Rx
  • Core Rx to Emic2 Tx
  • Emic2 5v to 5v
  • Emic2 GND to GND

DTMF Board and GSM Breakout Board

  • DTMF Board Audio In to GSM Board Audio Out

DTMF Board to Particle Core

  • DTMF Board 5v to 5v
  • DTMF GND to GND
  • DTMF DR to Particle Core D2
  • DTMF D0 to Particle Core D3
  • DTMF D1 to Particle Core D4
  • DTMF D2 to Particle Core D5
  • DTMF D3 to Particle Core D6

Screenshots

enter image description here

Demo Video

https://www.youtube.com/watch?v=sPZ4FBCrNjo

Send Voice Message using Particle and GSM Breakout Board

Source Code

I bought a GSM Breakout Board and it was resting on my table for a long time. Last week I decided to work on a project using GSM and Particle Core. Actually I decided to work on 2 project, one is to deliver a voice message to a specified number and other one is an IVR system. Here is the first project I worked, send voice message to a specified telephone number. I am working on second one and will post here once completed.

Overview

This project basically call a number, when the call is picked up on the other end, deliver a voice message and then hangup. This project mainly uses 4 Hardware Components.

  1. Particle Core as main processor and for Internet connectivity.
  2. A GSM Breakout Board for Cellular connectivity.
  3. SparkFun I2C/SPI-to-UART Breakout Board for SPI to UART.
  4. Emic2 Text to Speech module for TTS.

enter image description here

Since both GSM Board and Emic2 are Serial based, you need 2 Serial to communicate to these two. So first I tried Serial1 and Serial2 on Spark Core. But only one was working at a time, either Serial1 or Serial2. I was not able to get both working at the same time. So I used SPI to Serial Breakout Board. This breakout board convert SPI or I2C to Serial. The Emic2 is connected through Serial2 and GSM Board is connected through the SPI to Serial board.

Adafruit FONA library is used to communicate to the GSM Board. Even though this is written for Adafruit FONA Breakout Board, it will work with other SIM800/SIM900 boards. FONA is based on SIM800L module. I have slightly modified the Adafruit FONA Library to include the functions to read current call status and set the auto answer.

For voice output I am using Emic2 Text to Speech module. The audio output of this module is connected to the Audio Input of the GSM Breakout Board using 3.5mm Audio connector.

The Particle Core application exposes three functions, one to set the number to which voice message has to be delivered, second one to set the message and third to set the number of rings before automatically answer the call. When we send voice message, the application place a call to the number and waits for the other end to pick up the phone. Once the phone is picked up, voice message is delivered and the application hang up. The status of the current call is obtained using the GSM900 AT Command AT+CLCC. This command returns the status of the current call. This command is explained in this document. The response string contains the status of the current call if any, if there is no active call the command returns just the string “OK”. Third field of the response string contains the status of the current call. Following are the possible values of this field.

  1. 0 Active
  2. 1 Held
  3. 2 Dialing
  4. 3 Alerting
  5. 4 Incoming
  6. 5 Waiting

Once the call is placed, the application waits for the active call status to become active and the message is delivered using the Emic2 TTS Module and hangs up the call once the TTS is completed. For incoming call if the auto answer is set, the SIM module automatically answer the call after the specified number of rings. In this case also when the call is active, the application delivers the message and hangs up.

Wiring

Particle Core and I2C/SPI-to-UART

  • Core MOSI to I2C/SPI-to-UART MOSI
  • Core MISO to I2C/SPI-to-UART MISO
  • Core SCK to I2C/SPI-to-UART SCK
  • Core SS to I2C/SPI-to-UART SS

I2C/SPI-to-UART and GSM Shield

  • I2C/SPI-to-UART Vcc to 3.3v
  • I2C/SPI-to-UART GND to GND
  • I2C/SPI-to-UART Tx to GSM Board Rx
  • I2C/SPI-to-UART Rx to GSM Board Tx
  • GSM Board 5v to 5v
  • GSM Board GND to GND

Particle Core and Emic2 TTS Module

  • Core Tx to Emic2 Rx
  • Core Rx to Emic2 Tx
  • Emic2 5v to 5v
  • Emic2 GND to GND

Screenshots

Circuit

Web Interface

Demo Video

https://www.youtube.com/watch?v=dfa3WrB7OzY&feature=youtu.be

Windows 10 IoT Core and SHT15 Sensor

Source Code

Here is another project using Window 10 IoT. In this project I am using SHT15 Temperature an Humidity sensor. This sensor give accurate temperature and humidity readings.

I used this library and converted to C#. The port was easy and simple. Only at one point I was stuck, where we wait for the result after sending a command to the sensor. In the original code, after sending temperature or humidity commands to the sensor, it waits for the data to be ready for reading. The code loops 100 times and see whether the data pin is high or not. It turns out for Windows 10 IoT looping 100 times is not enough, after I increased it to 3000 it was working. I don’t know why this is so, someone expert in this area has to find out and explain.

The project reads Temperature, Humidity and calculate the Dew Point. The Dew Point calculation is taken from Arduino DHT Library.

Wiring

  1. SHT15 Vcc to 3.3v
  2. SHT15 Gnd to GND
  3. SHT15 Data to GPIO24 (Pin #18)
  4. SHT15 Sck to GPIO23 (Pin #16)

Using the sample

Open the project in Visual Studio 2015. Start the application using the ‘Remote Device’ option under the Debug Tab. Configure the IP Address of the RPi2.

To see the sensor values, access the internal HTTP Server using the URL http://<<RPi2 IP Address>>:8080. The web page automatically refresh every 5 seconds.

RPi2 and SHT15

RPI2 and SHT15

Browser Output

Fritzing

Luminosity Sensor Sample using Windows IoT Core

Source Code

As you all know Microsoft released Insider Preview of Windows 10 IoT Core recently. I too download and installed it on my RPi2. While playing with Windows 10 IoT Core, I decided to work on TSL2561 Luminosity Sensor using I2C. Here is the sample I created this weekend.

In this project I am using TSL2561 Luminosity Sensor connected to RPi2 using I2C. This project is based on Sparkfun TSL2561 Library. This library is ported to Windows 10 IoT Core. This is simple and a straight forward porting. The sample read the luminosity and display it. You can either connect RPi2 to a monitor or access the internal HTTP server running on port 8080. The HTTP Server code is shamelessly copied from the super awesome project Hands-on-lab IoT Weather Station using Windows 10.

Wiring

  • TSL2561 3V3 to RPi2 3.3v (Pin #01)
  • TSL2561 GND to RPi2 Ground (Pin #09)
  • TSL2561 SDA to RPi2 SDA (GPIO02)
  • TSL2561 SCL to RPi2 SCL (GPIO03)

Using the sample
Open the TSL256 solution in VS2015. Start the application using the ‘Remote Device’ option under the Debug Tab. Configure the IP Address of the RPi2.

To see the sensor values, either connect the RPi2 to a monitor or access the internal HTTP Server using the URL http://<<RPi2 IP Address>>:8080. The web page automatically refresh every 3 seconds.

Screenshots

Wiring

Schematic

RPi2

Output

Basic weather station using Intel Edison, Cylon.js, Pubnub and Freeboard.io

Source Code

This is project I worked this weekend. The project use Intel Edison and BMP180 sensor for the weather data. Using Cylon.js I collect the sensor data display it on Freeboard.io using Pubnub real-time communication.

Cylon.js is a powerful JavaScript robotic framework based on Node.js and supports multiple development platform and different sensors. You can install Cylon.js on Intel Edison and use it to create powerful programs. In this project I use Pubnub for real-time communication and Freeboard.io is used to create beautiful dashboard. You can also send sensor data to IoT Cloud solutions and use it for processing. I am using Intel XDK IoT Edition for development.

Prepare and install on Intel Edison

To run this project, first you have to setup the Intel Edison, this link has the detailed instructions to setup the board. Then download install the Intel XDK Iot Edition from here. Download the project from GitHub, open and upload to the board. Please note that you have to use the XDK’s Compile/Build options, this will fetch cylon.js and all the necessary modules and install it on Edison.

Wiring

  1. BMP180 Vcc to Edison Breakout Board’s 3.3v
  2. BMP180 GND to Edison Breakout Board’s GND
  3. BMP180 SDA to Edison Breakout Board’s A4
  4. BMP180 SCL to Edison Breakout Board’s A5

Screenshots

Intel Edison

Intel Edison

Setting up Freeboard.io

Setting up Freeboard.io

Freeboard Dashboard

Freeboard Dashboard

Fritzing

Fritzing Diagram

Spark Core and Temboo Light Monitor

https://github.com/krvarma/SparkCore_Temboo

Here is a project I worked on last weekend using Spark Core and Temboo. Temboo provides easy and fast access to different APIs. It provides different libraries to connect to Temboo services and access it. There is a great Spark Core port of Temboo Arduino library which I decided to use for this project.

This project uses a low cost Light Sensor to constantly monitor the light level. If it is below a particular level then call a Temboo Choreo. For this project I am using Nexmo library (Library\Nexmo\Voice\CaptureTextToSpeechPrompt) to call a particular phone number, speaks out a message and get input (digits). Temboo library provides a easy way to call any Choreo and get the return value. The return value may vary based on the Choreo we are using. It can XML or JSON. In this project I am using JSON return string.

As everybody knows parsing JSON/XML from a memory limited MCU is not that easy. For this reason Temboo library provides Output Filters to make it very easy to process the return values. The above link provides a detailed documentation on how to use Output Filters. The Nexmo’s return value in JSON format contains an element ‘digits’ which contains the user entered digit. In this sample if the digit is ‘1’ then the LED attached to D2 is turn on. When the light level goes above the limit the LED is turned off. Instead of LED we can connect anything more meaningful like a Relay control to turn on the light, etc…

Using the project

To use this project, you should configure the Temboo Nexmo library and save it to a profile (https://www.temboo.com/library/Library/Nexmo/Voice/CaptureTextToSpeechPrompt/). You should also have one Nexmo account either trial/paid. Replace the following #defines of temboo-light.ino with actual Temboo credentials.

// Your Temboo account name 
#define TEMBOO_ACCOUNT "<<temboo account>>"  
// Your Temboo app key name
#define TEMBOO_APP_KEY_NAME "<<temboo app key name>>"  
// Your Temboo app key
#define TEMBOO_APP_KEY "<<temboo app key>>"  

Replace the Temboo profile name (which is saved previously) at line no. 111 of temboo-light.ino

Also you have Replace the Spark Core credentials in the temboo.html page.

Screenshots

enter image description here

enter image description here

enter image description here

Demo Video

https://www.youtube.com/watch?v=BORNp10bbY0

Electric Imp and BlinkM

https://github.com/krvarma/ElectricImp_BlinkM

A simple Electric Imp application that controls BlinkM Smart LED. This is only simple port of BlinKM functions. Not all the functions are included, only functions to set and fade RGB and HSB is included in this sample. The application does not include Script and other functionality. This will be added in future.

Wiring

  1. BlinkM SCL to Pin 1
  2. BlinkM SDA to Pin 2
  3. BlinkM Vcc to 3.3V
  4. BlinkM GND to GND

enter image description here

Sample Application

The sample application defines a class BlinkM which contains which defines the BlinkM functions. The application simply fade Red, Green and Blue.

XBee and Spark Core, Part 3 – Analog Input using Light Sensor

https://github.com/krvarma/XBee_SparkCore_LightSensor

Here is the part 3 of my XBee and Spark Core series. This part is about Analog Input using a Light Sensor. In this part AD1 of the Remote XBee is configured as Analog Input. AD1 is connected to Light Senor. As usual this XBee is configured as Router AT and Coordinator is connected to Spark Core via Serial. The Remote XBee is configured to send samples every 1 second and the Spark Core receives this IO Sample data and reads the Light Sensor value.

Same as the previous project, I am using the XBee library and reads the analog data using getAnalog method of the ZBRxIoSampleResponse class.

To monitor the values, in this part I created a Freeboard.io data source and configured a Display Panel.

Screenshots

enter image description here

enter image description here

enter image description here

XBee and Spark Core, Part #2 – Remote Door Monitor using XBee Digital Input

https://github.com/krvarma/Spark_XBee_Remote_Door_Monitor

This is the part 2 of my XBee and Spark Core series. In this part I worked on a simple remote door monitor using XBee, Spark Core and a Magnetic Contact Switch (Door Sensor).

In this project, the remote XBee is configured as Router AT. The DIO4 of the XBee is setup for Digital Input (3) and it is also configured to send IO Sample data every 1 second (setting IR to 3E8 hex). The Magnetic Contact Switch is connected to DIO4 of this XBee, so when the door is closed the DIO4 will be HIGH and when the door is open the DIO4 will be LOW.

A Coordinator XBee is connected to Spark Core using Serial. The Coordinator receives the IO Sample data that Remote XBee is sending and parse it to see whether DIO4 is HIGH/LOW. An event is generated based on the door state.

The companion web page listens for the above event and display some graphics based on the whether the door is open or closed.

I am using XBee Library ported by @peekay123. The Spark Core continuously reads XBee packets and look for packets with API ID ZB_IO_SAMPLE_RESPONSE. This is the API ID of the IO Sample Data. If it is IO Sample data, then read the data into ZBRxIoSampleResponse class. The ZBRxIoSampleResponse.isDigitalOn() method can be used to find whether a particular PIN is ON/OFF.

Screehshots

Spark Core and Coordinator XBee

Remote XBee and Door Sensor

Web Page

Demo Video

Demo Video