IOT Relay and ESP8266 to Control Lighting

Here is a quick project that I did to control lighting for our seedlings.  I wanted something that could be controlled via MQTT and then managed in Thingsboard.  On this initial prototype, I am using Adafruit’s IOT http://io.adafruit.com.  It works pretty well, but is limited in some of the features that I want (like scheduling events on a timed basis).

The components of the project are:

  • NodeMCU ESP8266 module.  I am developing in c++
  • Visual Micro plugin for Visual Studio.  Much easier for debugging and features than just the straight Arduino IDE
  • IOT Relay (See on Amazon):  This is an enclosed relay for both AC and DC switching.  And it has a simple port that allows you to control from a microcontroller.

NOTE:  I have sample code below the video.

Quick video of it in action

The workflow that am currently doing

  • Connect a digital pin, from the NodeMCU, to the low voltage controlling pin on the IOT Relay
  • NodeMCU powers up and sets the digital pin to low to make sure that the relay is turned off
  • NodeMCU subscribes to an MQTT feed on io.adafruit.com (will switch to ThingsBoard)
  • Whenever a message with a “1” comes in on the subscribed MQTT feed, the NodeMCU sets the digital pin to high to switch the relay to the on position (thus turning on the lights).  Everything else passed to the feed will cause the NodeMCU to switch the digital pin to low (turning off the switch)

You can also see the below code on GitHub here: Github Link

#include <PubSubClient.h>
#include <ESP8266WiFi.h>

#define WIFI_AP "YOURWiFiAP"
#define WIFI_PASSWORD "YourWiFiPassword"

#define USERNAME "Your io.adafruit.com username"
#define TOKEN "Your io.adafruit.com password"
#define TOPIC "Your io.adafruit.com topic"

char thingsboardServer[] = "io.adafruit.com";

WiFiClient wifiClient;

PubSubClient client(wifiClient);

int status = WL_IDLE_STATUS;

void setup()
{
	pinMode(D0, OUTPUT);     // Initialize the LED_BUILTIN pin as an output
	digitalWrite(D0, LOW);

	Serial.begin(115200);
	delay(10);
	InitWiFi();
	client.setServer(thingsboardServer, 1883);

	client.setCallback(callback);
}

void loop()
{
	if (!client.connected()) {
		reconnect();
	}

	client.loop();
}

void InitWiFi()
{
	Serial.println("Connecting to AP ...");
	// attempt to connect to WiFi network

	WiFi.begin(WIFI_AP, WIFI_PASSWORD);
	while (WiFi.status() != WL_CONNECTED) {
		delay(500);
		Serial.print(".");
	}
	Serial.println("Connected to AP");
}

void reconnect() {
	// Loop until we're reconnected
	while (!client.connected()) {
		status = WiFi.status();
		if (status != WL_CONNECTED) {
			WiFi.begin(WIFI_AP, WIFI_PASSWORD);
			while (WiFi.status() != WL_CONNECTED) {
				delay(500);
				Serial.print(".");
			}
			Serial.println("Connected to AP");
		}
		Serial.print("Connecting to MQTT server ...");
		// Attempt to connect (clientId, username, password)
		if (client.connect("ESP8266 Device", USERNAME, TOKEN)) {
			Serial.println("[DONE]");

			client.subscribe(TOPIC);
		}
		else {
			Serial.print("[FAILED] [ rc = ");
			Serial.print(client.state());
			Serial.println(" : retrying in 5 seconds]");
			// Wait 5 seconds before retrying
			delay(5000);
		}
	}
}

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') {
		Serial.println("Turn Switch On");
		digitalWrite(D0, HIGH);
	}
	else {
		Serial.println("Turn Switch Off");
		digitalWrite(D0, LOW);
	}

}

Advertisements

One thought on “IOT Relay and ESP8266 to Control Lighting

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s