DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Data Engineering
  3. IoT
  4. Light Control With NodeMcu and Ubidots

Light Control With NodeMcu and Ubidots

Leave your lights on? With IoT, it's not a problem. Ubidots' cloud and NodeMcu, as well as a light sensor, can set up an automated light switch.

Maria Hernandez user avatar by
Maria Hernandez
·
Jan. 29, 17 · Tutorial
Like (5)
Save
Tweet
Share
9.06K Views

Join the DZone community and get the full member experience.

Join For Free

How many times have you accidentally left your lights turned on? That's why we figured out how to design an automatic system that can shut off (and turn on) your lights depending on the daylight.

The system works with a light sensor that will be responsible for detecting the amount and clarity of daylight. If the sensor detects daylight, the system will turn off your lights and raise the blinds. If the sensor detects that there is no light, it will turn on the light and lower the blinds. Also, using Ubidots, you can turn on or off an extra light whenever you want. 

It's very simple to make, and more importantly, it helps you save energy.

Overview

This post explains how to use the NodeMcu with Ubidots. NodeMcu is an Internet of Things (IoT) device with GPIO, PWM, IIC, 1-Wire, and ADC all in one board, and it has a Wi-Fi connection. It's easy to use — you just need the Arduino IDE to learn to program it. 

Requirements

  • NodeMcu version 1.0
  • Grove Base Shield for NodeMcu
  • Grove Relay
  • Grove Light Sensor
  • Grove white LED 
  • Servo motor
  • UbidotsESPMQTT library
  • Ubidots account

Setup

  1. First, couple the NodeMcu to its base shield and then connect the components. 

  2. Components

    Base Shield Pins

    Relay

    D3

    LED

    D5

    Light Sensor

    A0

    Servomotor


    • Yellow Wire - D8
    • Red Wire - 3.3 V
    • Black Wire - GND


    It should look like this:

    Image title

  3. Go to the Arduino IDE, click on Files --> Preferences and enter http://arduino.esp8266.com/stable/package_esp8266com_index.json into the "Additional Board Manager URLs" field. You can add multiple URLs, separating them with commas.

  4. Open "Boards Manager" from Tools --> Board menu and install the esp8266 platform. Don’t forget to select your NodeMCU 1.0 board from the menu after installation.

  5. Download the UbidotsESPMQTT library if you haven't already. Now, click on Sketch --> Include Library --> Add .ZIP Library

  6. Select the ZIP file of Ubidots ESPMQTT and then “Accept” or “Choose” for all libraries. If you can't add the library, try manually: unzip the downloaded RAR/ZIP and copy the folder from the library to the path C:\Users\ubidots\Documents\Arduino\libraries.

  7. Close the Arduino IDE and open it again.

Setting Up Ubidots

  1. Add a new Data Source called "manual" and a new Variable called "extra_light" (as seen below).
    Image title

  2. Verify that the name of the Data Source and Variable is the same as their API labels. This is important because our variable will be subscribed through the API Label, which allows communication between Ubidots and the NodeMcu. After you verified the Data Source and Variable Labels, you can edit their name with just one click.
    Image title

  3. Create the switch that allows you turn the light on or off. To do this, go to Dashboard and in the upper right of the page, click Add widget. Select Control --> Switch --> manual (data source) -->  light(variable) --> finish. 

Program the ESP NodeMcu

Once everything is connected correctly, we will go to the IDE and write the following code:

/****************************************
 * Include Libraries
 ****************************************/
#include "UbidotsESPMQTT.h"
#include <Servo.h>

/****************************************
 * Define Constants
 ****************************************/
#define TOKEN "...." // Your Ubidots TOKEN
#define WIFINAME "...." //Your SSID
#define WIFIPASS "...." // Your Wifi Pass
#define MQTTCLIENTNAME "..." // Your MQTT Client Name, it must be unique so we recommend to choose a random ASCCI name

Servo myservo;

int sensorPin = A0;
int ledPin = D5;
int relayPin = D3;

int state;
int lightState;

Ubidots client(TOKEN, MQTTCLIENTNAME);

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

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");

  for (int i=0;i<length;i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();
   if ((char)payload[0]=='1'){
    digitalWrite(relayPin, HIGH);
  }
   else if ((char)payload[0]=='0'){
    digitalWrite(relayPin, LOW);
  } 
}

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

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  client.wifiConnection(WIFINAME, WIFIPASS);
  client.begin(callback);
  myservo.attach(D8);
  pinMode(ledPin, OUTPUT);
  pinMode(relayPin, OUTPUT);
  client.ubidotsSubscribe("manual", "extra_light"); //Insert the dataSource and Variable's Labels
  }

void loop() {
  // put your main code here, to run repeatedly:
  if(!client.connected()){
      client.reconnect();
      client.ubidotsSubscribe("manual", "extra_light"); //Insert the dataSource and Variable's Labels
      }

      state = analogRead(sensorPin);
      lightState = digitalRead(ledPin);

      if (state <= 50)
      {
        digitalWrite(ledPin, HIGH);
        client.add("light", lightState);
        myservo.write(180);
        client.add("blind", 0);
        client.ubidotsPublish("Control");
        Serial.println("Night - Light ON, Blinds down.");
      }
      else 
      {
        digitalWrite(ledPin, LOW);
        client.add("light", lightState);
        myservo.write(0);
        client.add("blind", 1);
        client.ubidotsPublish("Control");
        Serial.println("Day - Light OFF, Blinds up.");
      }  
  client.add("luminosity", state); //Insert your variable Labels and the value to be sent
  client.ubidotsPublish("Control");
  client.loop();
  }

 

Results

After flashing your code, go to Ubidots and refresh the page. You will see the new Data Source "control" created.

Image title

If you want to check the state of the blinds or light you can build your dashboard as you desire. If you don't know how, check this tutorial.

Image title

We've just shown you how to design a light control based on the Internet of Things, which proves how seemingly complex things can be done very easily with the right tools.

Light (web browser) arduino

Published at DZone with permission of Maria Hernandez, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • OpenVPN With Radius and Multi-Factor Authentication
  • Strategies for Kubernetes Cluster Administrators: Understanding Pod Scheduling
  • Asynchronous Messaging Service
  • How To Set Up and Run Cypress Test Cases in CI/CD TeamCity

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: