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
Ubidots Account
The first thing you need to do is go to the Ubidots for Education website and create an account. You can sign in directly if you already have a Twitter, Github, Google or Facebook account.
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.
Jason App
The app can be downloaded from the Play Store, 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.
Making the Hardware
Safety First
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
- 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
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.
The power supply is going to be a phone wall charger of 5V with a stripped usb cable.
By controlling the ground connection with the relay we can control the AC status of the light bulb
Code
Before you use the source code, you need to download some libraries:
- Arduino core for ESP8266 (Read the "Installing with Boards Manager" step)
- Ubidots ESP MQTT
Note: If you don't know how to add libraries to the arduino IDE, you can follow thiseasy 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
//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.
It works! If you have any questions or suggestions please leave them at the comments section.
Top comments (8)
Amazing project !
Es posible dar órdenes en español también ?
What Arduino's board did you use ?
Thanks for sharing, I bought an Arduino and this kind of projects are so good to play.
¡Claro que sí! La app funciona en ingles y español. You just have to have your android's phone language to spanish.
Greetings from México!
Fernando I edited my comment, can you answer the question please ?
Saludos desde Argentina!
Sorry, didn't read correctly, already answered your question below.
Oh and I used the NodeMCU which can be programmed as an arduino but instead of the arduino atmega328 chip, it has the esp8266 which has wifi connectivity.
Oh, I see, I read again the post after I made the comment and I didn't see any wifi module for Arduino. Thanks for the clarification !
I'm gonna try with Arduino UNO and esp8266.
Good Luck! if you have any doubt just message me.
Wow, so cool!