Created
February 12, 2016 10:40
-
-
Save jamesabruce/69e75c0f878d627a8901 to your computer and use it in GitHub Desktop.
Simple MQTT controlled relay running on NodeMCU dev board on pin d2/gpio4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include <WiFiClient.h> | |
#include <ESP8266mDNS.h> | |
#include <WiFiUdp.h> | |
#include <PubSubClient.h> | |
#define RELAY 4 // pin labelled d2 | |
// Update these with values suitable for your network. | |
const char* ssid = "YOURWIFI"; | |
const char* password = "YOURPASSWORD!"; | |
const char* host = "bedroomplug"; // the name of your fixture, and the base channel to listen to | |
IPAddress MQTTserver(192, 168, 1, 99); // MQTT broker | |
// NO NEED TO EDIT FURTHER BEYOND HERE | |
#define BUFFER_SIZE 100 | |
WiFiClient wclient; | |
PubSubClient client(wclient, MQTTserver); | |
void callback(const MQTT::Publish& pub) { | |
uint16_t i, j; | |
String myMessage = String(pub.payload_string()); | |
// handle message arrived | |
Serial.print(pub.topic()); | |
Serial.print(" => "); | |
String myTopic = String(pub.topic()); | |
if(myTopic == host) | |
{ | |
Serial.println(pub.payload_string()); | |
if(pub.payload_string() == "on") | |
{ | |
digitalWrite(RELAY,HIGH); | |
Serial.println("Turning on"); | |
} | |
else | |
{ | |
digitalWrite(RELAY, LOW); | |
Serial.println("Turning off"); | |
} | |
} | |
} | |
void setup() { | |
Serial.begin(115200); | |
Serial.println("Booting"); | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(ssid, password); | |
while (WiFi.waitForConnectResult() != WL_CONNECTED) { | |
Serial.println("Connection Failed! Rebooting..."); | |
delay(5000); | |
ESP.restart(); | |
} | |
pinMode(RELAY,OUTPUT); | |
Serial.println("Ready"); | |
Serial.print("IP address: "); | |
Serial.println(WiFi.localIP()); | |
// MQTT callback | |
client.set_callback(callback); | |
} | |
void loop() { | |
if (WiFi.status() == WL_CONNECTED) { | |
if (!client.connected()) { | |
if (client.connect("ESP8266: Fountain")) { | |
client.publish("outTopic",(String)"hello world, I'm "+host); | |
client.subscribe(host+(String)"/#"); | |
} | |
} | |
if (client.connected()) | |
client.loop(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment