Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jeroavf/07f48ebd6994a4b913d0 to your computer and use it in GitHub Desktop.
Save jeroavf/07f48ebd6994a4b913d0 to your computer and use it in GitHub Desktop.
ESP8266 ESP-01 relay control using Web server or MQTT
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
const char* wifi_ssid = "XXXXX";
const char* wifi_password = "XXXXX";
char* mqtt_server = "iot.eclipse.org";
char* mqtt_user = "";
char* mqtt_password = "";
WiFiClient wifiClient;
PubSubClient mqtt_client(mqtt_server, 1883, mqtt_callback, wifiClient);
void mqtt_callback(char* topic, byte* payload, unsigned int len) {
String tmp=topic;
Serial.print("mqtt_callback topic=");
Serial.println(tmp);
if(tmp.indexOf("/murilo/esp/rele/on")>=0){
Serial.println("relay on");
digitalWrite(E2, 1);
}
if(tmp.indexOf("/murilo/esp/rele/off")>=0){
Serial.println("relay off");
digitalWrite(E2, 0);
}
}
String macToStr(const uint8_t* mac)
{
String result;
for (int i = 0; i < 6; ++i) {
result += String(mac[i], 16);
if (i < 5)
result += ':';
}
return result;
}
ESP8266WebServer webserver(80);
void web_handle_root() {
Serial.println("web root");
String html_payload = "WiFi.macAddress=";
uint8_t mac[6];
WiFi.macAddress(mac);
html_payload += macToStr(mac);
webserver.send(200, "text/plain", html_payload);
}
void web_handle_ron() {
Serial.println("web ron");
webserver.send(200, "text/plain", "Relay on");
digitalWrite(E2, 1);
}
void web_handle_roff() {
Serial.println("web roff");
webserver.send(200, "text/plain", "Relay off");
digitalWrite(E2, 0);
}
void web_setup() {
webserver.on("/", web_handle_root);
webserver.on("/on", web_handle_ron);
webserver.on("/off", web_handle_roff);
webserver.begin();
}
void mqtt_setup() {
if (!mqtt_client.connected()) {
uint8_t mac[6];
WiFi.macAddress(mac);
String clientName;
clientName += "esp8266-";
clientName += macToStr(mac);
if (mqtt_client.connect( (char*) clientName.c_str())) {
Serial.println("mqtt connected");
if (mqtt_client.subscribe("/murilo/esp/rele/on")) {
Serial.println("subcribe /murilo/esp/rele/on ok");
} else {
Serial.println("subcribe /murilo/esp/rele/on fail");
}
if (mqtt_client.subscribe("/murilo/esp/rele/off")) {
Serial.println("subcribe /murilo/esp/rele/off ok");
} else {
Serial.println("subcribe /murilo/esp/rele/off fail");
}
} else {
Serial.println("mqtt connect fail");
}
}
else {
static int contador=0;
contador++;
String payload="";
payload = contador;
mqtt_client.publish("/murilo/esp/rele/count",(char*)payload.c_str());
}
}
void setup() {
//
Serial.begin(115200);
//
pinMode(E2, OUTPUT);
//
WiFi.begin(wifi_ssid, wifi_password);
//
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
//
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
web_setup();
}
void loop() {
mqtt_setup();
//
webserver.handleClient();
//
mqtt_client.loop();
}
@habuild
Copy link

habuild commented Feb 9, 2018

WiFiClient wifiClient;
PubSubClient mqtt_client(mqtt_server, 1883, mqtt_callback, wifiClient);

Need to move down to line 48 so they are after the whole function of void callback. Otherwise a callback has not been defined error occurs on compilation using Arduino IDE 1.8.5 and pubsubclient 2.6 (Nick O'Leary)

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