c_cpp ESP8266使用Web服务器或MQTT进行ESP-01继电器控制

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp ESP8266使用Web服务器或MQTT进行ESP-01继电器控制相关的知识,希望对你有一定的参考价值。



#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();
  
  
}


以上是关于c_cpp ESP8266使用Web服务器或MQTT进行ESP-01继电器控制的主要内容,如果未能解决你的问题,请参考以下文章

如何在esp8266上实现web server

ESP8266 Web Server (WebSocket) 实现多控件多引脚调节(PWM)

c_cpp ESP8266GCodeSender

c_cpp esp8266-webserver.ino

c_cpp esp8266.tv.ino

c_cpp esp8266oled.ino