arduino IDE开发ESP8266开发版终于可以手机远程控制LED点亮了
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
-
- // 设置wifi接入信息(请根据您的WiFi信息进行修改)
- const char* ssid = "可用WiFi";
- const char* password = "密码";
- const char* mqttServer = "";
- // 如以上MQTT服务器无法正常连接,请前往以下页面寻找解决方案
- WiFiClient wifiClient;
- PubSubClient mqttClient(wifiClient);
-
- // ****************************************************
- // 注意!以下需要用户根据然也物联平台信息进行修改!否则无法工作!
- // ****************************************************
- const char* mqttUserName = "自己的"; // 服务端连接用户名(需要修改)
- const char* mqttPassword = "自己的"; // 服务端连接密码(需要修改)
- const char* clientId = "自己的"; // 客户端id (需要修改)
- const char* subTopic = "自己/led_kz"; // 订阅主题(需要修改)
- const char* pubTopic = "自己/led_zt"; // 订阅主题(需要修改)
- const char* willTopic = "自己/led_yz"; // 遗嘱主题名称(需要修改)
- // ****************************************************
-
- //遗嘱相关信息
- const char* willMsg = "esp8266 offline"; // 遗嘱主题信息
- const int willQos = 0; // 遗嘱QoS
- const int willRetain = false; // 遗嘱保留
-
- const int subQoS = 1; // 客户端订阅主题时使用的QoS级别(截止2020-10-07,仅支持QoS = 1,不支持QoS = 2)
- const bool cleanSession = false; // 清除会话(如QoS>0必须要设为false)
-
- bool ledStatus = HIGH;
- //int pushButton =D3;
- void setup() {
- pinMode(LED_BUILTIN, OUTPUT); // 设置板上LED引脚为输出模式
- //pinMode(pushButton, INPUT);
- digitalWrite(LED_BUILTIN, ledStatus); // 启动后关闭板上LED
- Serial.begin(9600); // 启动串口通讯
-
- //设置ESP8266工作模式为无线终端模式
- WiFi.mode(WIFI_STA);
-
- // 连接WiFi
- connectWifi();
-
- // 设置MQTT服务器和端口号
- mqttClient.setServer(mqttServer, 1883);
- mqttClient.setCallback(receiveCallback);
-
- // 连接MQTT服务器
- connectMQTTserver();
- }
-
- void loop() {
- // 如果开发板未能成功连接服务器,则尝试连接服务器
- if (!mqttClient.connected()) {
- connectMQTTserver();
- }
-
- // 处理信息以及心跳
- mqttClient.loop();
- /* pushButton = digitalRead(LED_BUILTIN); //读取引脚2电平状态并将其赋值给布尔变量
-
- if (!pushButton){ //对布尔变量进行逻辑非运算,并根据运算结构点亮或者熄灭LED
- digitalWrite(LED_BUILTIN, HIGH); //布尔变量数值为假(false)时点亮LED
- } else {
- digitalWrite(LED_BUILTIN, LOW); //布尔变量数值为真(true)时熄灭LED
- }*/
-
- }
-
- // 连接MQTT服务器并订阅信息
- void connectMQTTserver(){
- // 根据ESP8266的MAC地址生成客户端ID(避免与其它ESP8266的客户端ID重名)
-
-
- /* 连接MQTT服务器
- boolean connect(const char* id, const char* user,
- const char* pass, const char* willTopic,
- uint8_t willQos, boolean willRetain,
- const char* willMessage, boolean cleanSession);
- 若让设备在离线时仍然能够让qos1工作,则connect时的cleanSession需要设置为false
- */
- if (mqttClient.connect(clientId, mqttUserName,
- mqttPassword, willTopic,
- willQos, willRetain, willMsg, cleanSession)) {
- Serial.print("MQTT Server Connected. ClientId: ");
- Serial.println(clientId);
- Serial.print("MQTT Server: ");
- Serial.println(mqttServer);
-
- subscribeTopic(); // 订阅指定主题
- } else {
- Serial.print("MQTT Server Connect Failed. Client State:");
- Serial.println(mqttClient.state());
- delay(5000);
- }
- }
-
- // 收到信息后的回调函数
- void receiveCallback(char* topic, byte* payload, unsigned int length) {
- Serial.print("Message Received [");
- Serial.print(topic);
- Serial.print("] ");
- for (int i = 0; i < length; i++) {
- Serial.print((char)payload[i]);
- }
- Serial.println("");
- Serial.print("Message Length(Bytes) ");
- Serial.println(length);
-
- if ((char)payload[0] == '1') { // 如果收到的信息以“1”为开始
- ledStatus = LOW;
- digitalWrite(BUILTIN_LED, ledStatus); // 则点亮LED。
- } else {
- ledStatus = HIGH;
- digitalWrite(BUILTIN_LED, ledStatus); // 否则熄灭LED。
- }
-
- pubMQTTmsg();
- }
-
- // 订阅指定主题
- void subscribeTopic(){
-
-
- // 通过串口监视器输出是否成功订阅主题以及订阅的主题名称
- // 请注意subscribe函数第二个参数数字为QoS级别。这里为QoS = 1
- if(mqttClient.subscribe(subTopic, subQoS)){
- Serial.print("Subscribed Topic: ");
- Serial.println(subTopic);
- } else {
- Serial.print("Subscribe Fail...");
- }
- }
-
- // 发布信息
- void pubMQTTmsg(){
- char* pubMessage;
-
- if (ledStatus == LOW){
- pubMessage = "LED ON";
- } else {
- pubMessage = "LED OFF";
- }
-
- // 实现ESP8266向主题发布信息
- if(mqttClient.publish(pubTopic, pubMessage)){
- Serial.println("Publish Topic:");Serial.println(pubTopic);
- Serial.println("Publish message:");Serial.println(pubMessage);
- } else {
- Serial.println("Message Publish Failed.");
- }
- }
-
- // ESP8266连接wifi
- void connectWifi(){
-
- WiFi.begin(ssid, password);
-
- //等待WiFi连接,成功连接后输出成功信息
- while (WiFi.status() != WL_CONNECTED) {
- delay(1000);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi Connected!");
- Serial.println("");
- }
复制代码
|