Skip to content

Instantly share code, notes, and snippets.

@prackky
Created July 1, 2017 14:32
Show Gist options
  • Select an option

  • Save prackky/3f44238419f494f0572da479ffcf30b2 to your computer and use it in GitHub Desktop.

Select an option

Save prackky/3f44238419f494f0572da479ffcf30b2 to your computer and use it in GitHub Desktop.
Arduino code to control relay using NodeMCU and CloudMQTT.
#include <EEPROM.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
// Connect to the WiFi
const char *ssid = "YOUR_WIFI_SSID"; // wifi SSID
const char *password = "YOUR_WIFI_PASSWORD"; // wifi password
const char *mqtt_server = "m45.cloudmqtt.com"; // change this with your server name
const char *device_id = "uniqueID"; // write any unique ID
WiFiClient espClient;
PubSubClient client(espClient);
const byte ledPin5 = 14;
char message_buff[100];
void callback(char *topic, byte *payload, unsigned int length)
{
//payload[length] = '\0';
Serial.print("Message arrived [");
Serial.print(topic);
Serial.println("] ");
int i;
for (i = 0; i & lt; length; i++)
{
message_buff[i] = payload[i];
}
message_buff[i] = '\0';
String msgString = String(message_buff);
Serial.println(msgString);
if (strcmp(topic, "uniqueID/topic") == 0)
{ // write your unique ID here
if (msgString == "1")
{
digitalWrite(ledPin5, LOW); // PIN HIGH will switch OFF the relay
}
if (msgString == "0")
{
digitalWrite(ledPin5, HIGH); // PIN LOW will switch ON the relay
}
}
}
void reconnect()
{
// Loop until we're reconnected
while (!client.connected())
{
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(device_id, "cloud_username", "cloud_password"))
{ // enter cloudmqtt provided username and password in console
Serial.println("connected");
// ... and subscribe to topic
client.subscribe("uniqueID/topic"); // write your unique ID here
}
else
{
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup()
{
Serial.begin(115200);
client.setServer(mqtt_server, 1883); // change port number as mentioned in your cloudmqtt console
client.setCallback(callback);
pinMode(ledPin5, OUTPUT);
}
void loop()
{
if (!client.connected())
{
reconnect();
}
client.loop();
}
@somesickjoke

Copy link
Copy Markdown

Hi i'm getting a build error for the undeclared lt variable any solution other than declaring it myself.

@Monrey-lab

Copy link
Copy Markdown

Hi, My message is getting received in my COM12 serial monitor.
But the LED on-off is not working.
What will be the reason? please help.

@prackky

prackky commented Sep 3, 2019 •

Copy link
Copy Markdown
Author

Hi, My message is getting received in my COM12 serial monitor.
But the LED on-off is not working.
What will be the reason? please help.
@Monrey-lab
in the example D5 corresponds to GPIO14. please check you are connecting to correct pin.
Refer this : https://www.teachmemicro.com/wp-content/uploads/2018/04/NodeMCUv1.0-pinout.jpg

@prackky

prackky commented Sep 3, 2019

Copy link
Copy Markdown
Author

Hi i'm getting a build error for the undeclared lt variable any solution other than declaring it myself.

@somesickjoke could you please post the error?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment