Voice Controlled Lights From Anywhere with Jason app
Tags: iot, arduino, android, electronics
Published: 2018-07-25 22:58:28

---
title: Voice Controlled Lights From Anywhere with Jason app
published: true

description: AC lights that are controlled from anywhere with internet connection using NodeMCU (ESP8266) and Jason (Android App).

tags: IoT, arduino, android, electronics
cover_image: https://thepracticaldev.s3.amazonaws.com/i/vhd8zhqir1mopvqugkhr.png
---

##Description

Jason is a voice controlled assistant app that I coded for Android devices to control the electrical state of an AC appliance, until now it can control lights. You can control the lights from anywhere in the world as long as you have internet connection. This is possible by using an IoT broker, in this case we are using Ubidots.

To use it you need to build the hardware module that connects to the light bulb, (which instructions are in this tutorial) and you will also need to create an Ubidots account.

So let's get started...

###Components

- NodeMCU
- NPN transistor
- Solid State Relay 5V (can be a mechanical relay)
- Switch (Can be an electronics hobby switch, like a toggle, or a wall switch)
- 1k Ohm resistor
- 2.2k ohm resistor
- Breadboard
- USB-A to micro-USB cable
- A/C Light Bulb
- Light Bulb Socket
- Power Cord
- 5V Phone charger
- Jumper wires

###Software and Services

- [Arduino IDE](https://www.arduino.cc/en/main/software)
- [Jason](https://play.google.com/store/apps/details?id=com.zablah.jason)
- [Ubidots] (https://ubidots.com/education/)

###Ubidots Account

The first thing you need to do is go to the [Ubidots for Education website](https://app.ubidots.com/accounts/signup/) and create an account. You can sign in directly if you already have a Twitter, Github, Google or Facebook account.

![Ubidots sign up website](https://hackster.imgix.net/uploads/attachments/530388/ubidots_register_ms86QxY4f7.PNG?auto=compress%2Cformat&w=1280&h=960&fit=max)

When you have already created your account you will have access to your token, bu clicking on your username at the top right corner and clicking on API Credentials. Save your token, as we are going to use later on.

![Ubidots Dashboard](https://hackster.imgix.net/uploads/attachments/530397/ubidotsdashboard_smcDlh0j8B.PNG?auto=compress%2Cformat&w=680&h=510&fit=max)

![Ubidots Keys](https://hackster.imgix.net/uploads/attachments/530401/token_57BYXVvEi5.png?auto=compress%2Cformat&w=680&h=510&fit=max)

###Jason App

The app can be downloaded from the [Play Store](https://play.google.com/store/apps/details?id=com.zablah.jason), it is available in english and spanish.

Copy your Ubidots token into the app, by tapping the settings tab, pasting it in the Ubidots key field and tap the save button.

![Settings tab](https://hackster.imgix.net/uploads/attachments/530415/whatsapp_image_2018-07-21_at_7_48_24_pm_htr3T60Cd6.jpeg?auto=compress%2Cformat&w=680&h=510&fit=max)


![Ubidots key field](https://hackster.imgix.net/uploads/attachments/530418/whatsapp_image_2018-07-21_at_7_48_24_pm_(2)_cduYWnHhvr.jpeg?auto=compress%2Cformat&w=680&h=510&fit=max)


![Saved Key in Jason](https://hackster.imgix.net/uploads/attachments/530422/whatsapp_image_2018-07-21_at_7_48_24_pm_(1)_T1rBzAgKJr.jpeg?auto=compress%2Cformat&w=680&h=510&fit=max)

##Making the Hardware

###Safety First

![High Voltage danger](https://hackster.imgix.net/uploads/attachments/530922/caution-high-voltage-300x450_WfXuok0iXU.png?auto=compress%2Cformat&w=680&h=510&fit=max)

In this project we are working with mains voltage (A/C voltage) which is dangerous if you don't know what you're doing, be very careful. NEVER touch ANY part of the circuit or work with it if it is connected to the wall power. If you don't know what you are doing, stop right here or get some help from professionals.

I am only posting this educational tutorial and I am by no means responsible for any injuries or damage you may cause.

###Schematics

![Schematics](https://hackster.imgix.net/uploads/attachments/530710/schematic_EHL21EeSdU.png?auto=compress%2Cformat&w=680&h=510&fit=max)

- Power the NodeMCU by connecting **VIN** to **VCC** (5V) and **GND pin** to **GND**.
- Connect **D8** to one **end of the switch** and to a **2.2K Ohm resistor** conected to **GND**.
- Connect the other **end of the switch** to **3.3V** as the NodeMCU can only handle that - voltage in its I/O Pins.
- **D1** to **2.2k Ohm resistor** to the **base** of the NPN transistor
- **Negative DC** of the relay to the **colector** of the transistor.
- Transistor **emitter** to **GND**.
- **Positive DC** of the relay to **5V**.
- **Negative** of light bulb to one **AC pin** of the relay.
- **Positive** of bulb to **AC Live** (AC Positive).
- Other **AC pin** of relay to **Neutral** (AC Negative).
NOTE: **VCC 5V** is going to be supplied from an usb cable connected to a simple phone transformer charger.

###Breadboard

![Breadboard from top](https://hackster.imgix.net/uploads/attachments/530775/whatsapp_image_2018-07-22_at_2_17_06_pm_(1)_vvTgCj6VlN.jpeg?auto=compress%2Cformat&w=680&h=510&fit=max)

![Breadboard from side](https://hackster.imgix.net/uploads/attachments/530768/breadboardnoext1_plq9g4h8LD.jpeg?auto=compress%2Cformat&w=680&h=510&fit=max)

![Breadboard from other side](https://hackster.imgix.net/uploads/attachments/530769/breadorardnoext2_XUExNJM0gi.jpeg?auto=compress%2Cformat&w=680&h=510&fit=max)

![Switch Front](https://hackster.imgix.net/uploads/attachments/530772/whatsapp_image_2018-07-22_at_2_17_06_pm_xsVLo7aTlf.jpeg?auto=compress%2Cformat&w=680&h=510&fit=max)

![Switch Back](https://hackster.imgix.net/uploads/attachments/530773/whatsapp_image_2018-07-22_at_2_17_05_pm_(2)_dyvvk58ycq.jpeg?auto=compress%2Cformat&w=680&h=510&fit=max)


The switch that I used has double throw, we only need one, so I connected its pin 1 to 3V of the NodeMCU and pin 2 of switch to NodeMCU pin D8.

![Switch Back and breadboard](https://hackster.imgix.net/uploads/attachments/530774/whatsapp_image_2018-07-22_at_2_17_05_pm_(3)_k5ToKxqRMr.jpeg?auto=compress%2Cformat&w=680&h=510&fit=max)

The power supply is going to be a phone wall charger of 5V with a stripped usb cable.

![5 volts transformer](https://hackster.imgix.net/uploads/attachments/530776/whatsapp_image_2018-07-22_at_2_17_05_pm_(4)_EChhLVAVuR.jpeg?auto=compress%2Cformat&w=680&h=510&fit=max)

By controlling the ground connection with the relay we can control the AC status of the light bulb

![Light bulb conecctions](https://hackster.imgix.net/uploads/attachments/530780/whatsapp_image_2018-07-22_at_2_17_04_pm_lNFuRCmyMT.jpeg?auto=compress%2Cformat&w=680&h=510&fit=max)

![Solid State Relay connections](https://hackster.imgix.net/uploads/attachments/530781/whatsapp_image_2018-07-22_at_2_17_04_pm_(1)_uGuSZaE24t.jpeg?auto=compress%2Cformat&w=680&h=510&fit=max)

##Code

Before you use the source code, you need to download some libraries:

- [Arduino core for ESP8266](https://github.com/esp8266/Arduino#installing-with-boards-manager) (Read the "Installing with Boards Manager" step)
- [Ubidots ESP MQTT](https://github.com/ubidots/ubidots-mqtt-esp)

Note: If you don't know how to add libraries to the arduino IDE, you can follow [this](https://www.arduino.cc/en/Guide/Libraries)easy tutorial.

Set your development board to NodeMCU 1.0 (ESP-12E Module).

You need to change some variables in the code:

- Your SSID (Name of your home Wi-Fi network)
- Password of your Wi-FI network
- Your Ubidots token


```c++

//Include Libraries

#include "UbidotsESPMQTT.h"

//Define Constants

/*Replace the empty string
with your Ubidots TOKEN*/

#define TOKEN ""

/*Replace the empty string
with your ssid, eg: "MyHomeNewtwork"*/

#define WIFINAME "" 

/*Replace the empty string with your
wi-fi password, eg: "mypass123456"*/

#define WIFIPASS "" 

/*Replace with any random string,
Ubidots requires it to be unique*/

#define MQTTCLIENTNAME ""


Ubidots client(TOKEN, MQTTCLIENTNAME);

/**********************
  Auxiliar Functions
 *********************/

/*NOTE PINS ARE FROM THE 
INTERNAL ESP8266 MODULE 
NOT THE DIGITAL PINS FROM THE 
NODEMCU, MORE AT INFO: 
http://www.electronicwings.com/nodemcu/nodemcu-gpio-with-arduino-ide
*/
int outputs[] = {2,4,5,12};
int inputs[] = {13,14,15,16};
int btnStates[] = {0,0,0,0};

//This is called every time we 
//receive an update from the Ubidots server
void callback(char* topic, byte* payload, unsigned int length) {
  //Print the topic (pin) and its server state
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i=0;i<length;i++) {
    Serial.print((char)payload[i]);
  }
  bool reading = true;
  int count = 20;
  String topicPin = "";
  String payloadState = "";
  Serial.println(" ");
  while(reading){
    if((char)topic[count] != '/'){
      topicPin += (char)topic[count];
      Serial.print((char)topic[count]);
      count++;
    }
    else{
       Serial.println(" STRING: " + topicPin);
      reading = false;
    }
  }
  for (int i = 0; i < length; i++) {
    payloadState += (char)payload[i];
    Serial.print((char)payload[i]);
  }
  //Apply server value to pin
  int ledpin = topicPin.toInt();
  int state = payloadState.toInt();
  digitalWrite(ledpin, state);
  Serial.println();
}

/*********************
 Main Functions
 **********************/

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  client.wifiConnection(WIFINAME, WIFIPASS);
  client.begin(callback);
  //Subscribe to all of out Outputs pins
  for(int i=0;i<4;i++){
    char b[2];
    String str;
    str=String(outputs[i]);
    str.toCharArray(b,2);
     //Insert the dataSource and Variable's Labels
    client.ubidotsSubscribe("esp32",b); 
  }
  //Set all of our pinModes and save button states
  for(int i = 0; i<4; i++){
    pinMode(outputs[i], OUTPUT);
    pinMode(inputs[i], INPUT);
    int buttonState = digitalRead(inputs[i]);
    btnStates[i] = buttonState;
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  if(!client.connected()){
      client.reconnect();
      //Subscribe to all of out Outputs pins
      for(int i=0;i<4;i++){
        char b[2];
        String str;
        str=String(outputs[i]);
        str.toCharArray(b,2);
         //Insert the dataSource and Variable's Labels
        client.ubidotsSubscribe("esp32",b);
      }
  }
  //Check if switch has changed state
  //If it did chnage, change the light state and save it to array
  for(int i = 0; i<4; i++){
    int buttonState = digitalRead(inputs[i]);
    if(buttonState != btnStates[i]){
      digitalWrite(outputs[i], buttonState);
      btnStates[i] = buttonState;
    }
  }
  client.loop();
}
```

And finally upload your code to the board.
{% youtube NAoc2JeHu2g %}

It works! If you have any questions or suggestions please leave them at the comments section.