nodemcu Arduino mqtt控制板载灯

ESP8266 代码编写

需要 ESP8266WiFiPubSubClient ,还有ArduinoJson库,ESP8266WiFi 库能够将 ESP8266 连接到 Wi-Fi 网络,PubSubClient 库能使 ESP8266 连接到 MQTT 服务器发布消息及订阅主题,ArduinoJson来处理json。
 

#include 
#include 
#include 
WiFiClient espClient;
PubSubClient client(espClient);
  // put your setup code here, to run once:
// WiFi
const char *ssid = "a5"; // Enter your WiFi name
const char *password = "******";  // Enter WiFi password

// MQTT Broker
const char *mqtt_broker = "broker-cn.emqx.io";
const char *topic = "esp8266/ctrl";
const char *mqtt_username = "";
const char *mqtt_password = "";
const int mqtt_port = 1883;

String client_id = "esp8266-client-";
    
void setup() {
 pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
// connecting to a WiFi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting to WiFi..");
}
client_id += String(WiFi.macAddress());
client.setServer(mqtt_broker, mqtt_port);
client.setCallback(callback);

while (!client.co

你可能感兴趣的:(javascript,arduino,html,c语言,javascript)