esp8266 mqtt 使用arduino开发教程

esp8266的开发可以使用arduino的IDE进行开发,或者使用SDK开发
使用arduino的IDE开发是最简单的。

使用arduino的IDE开发,又可以分为直接在模块上开发或者通过arduino软连接开发

以esp826601s 使用mqtt传输数据为例
下面介绍如何使用arduino 开发esp8266 mqtt

arduino 配置IDE

  1. 在附加开发板管理器网址中加入:
    http://arduino.esp8266.com/stable/package_esp8266com_index.json

  2. 安装pubsubclient (支持mqtt协议的库 2.6.0)
    enter description here
    示例中有 mqtt_esp8266 的测试代码可以进行测试

  3. 打开工具 开发板 开发板管理器 搜索esp8266 下载esp8266 by ESP8266 Community (2.4.2)
    enter description here

  4. 工具选择开发板 generic esp8266 module 波特率115200 flash size 1M no SPIFS
    其他不变
    (代码在下面)
    上传代码 GPIO0 要低电位
    可以使用上传工具ttl或者专门的esp模块下载器(省事)
    enter description here

  5. 上传代码 后连接线路
    vcc 和en 接3.3v电源
    **GPIO0高电平
    GND接地

这是以esp826601s为例
根据使用的esp模块进行相应的配置

使用arduino IDE连接wifi以及mqtt服务

代码解析

适用于MQTT的Arduino客户端
API文档

  1. 下载mqtt支持库 Pubsubclient,以及esp8266wifi库,以及esp8266的开发板

/ #include <ESP8266WiFi.h>
#include <PubSubClient.h>

  1. 添加wifi信息以及mqtt服务器的信息
    声明全局变量,用于后面进行连接的时候使用

const char* ssid = “YourNetworkName”;
const char* password = “YourNetworkPassword”;
const char* mqttServer = “m11.cloudmqtt.com”;
const int mqttPort = 12948;
const char* mqttUser = “YourMqttUser”;
const char* mqttPassword = “YourMqttUserPassword”;
(如果没有用户名以及密码设置 可以不添加后两个)

  1. 声明类WIFIClien的对象,该对象允许建立到特点ip和端口的连接
    声明类PubSubClient的对象,接受先前定义的WiFiClient 作为构造函数的输入

WiFiClient espClient;
PubSubClient client(espClient);

  1. 连接wifi,打开串行连接,能够在串口中查看输出信息(可删)

Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(“Connecting to WiFi…”);
}
Serial.println(“Connected to the WiFi network”);

  1. 指定MQTT服务器的IP和端口,使用setcallback函数当接受到订阅的消息时,执行相应的函数

client.setServer(mqttServer,mqttport);
client.setCallback(callback);

  1. 连接MQTT服务器,使用while循环如果连接不上,持续连接,client.connected()返回ture或false
    使用connect方法,进行实际的连接。ESP8266Client是连接到服务器时要使用的客户端ID,如果有用户名密码 添加到后面的参数里,如果没有可以不添加。
    state函数能够返回连接失败的原因。

while (!client.connected()) {
Serial.println(“Connecting to MQTT…”);
if (client.connect(“ESP8266Client”, mqttUser, mqttPassword )) {
Serial.println(“connected”);
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}

  1. 订阅以及发布主题

client.publish(“test”,“Hello Word”);
client.subscribe(test/status);

  1. callback函数用来处理订阅主题接受到的消息。参数是主题的名称,发布内容,和发布内容的长度。可以添加其他函数在此基础上修改添加。

void callback(char* topic, byte* payload, unsigned int length) {
Serial.print(“Message arrived in topic: “);
Serial.println(topic);
Serial.print(“Message:”);
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println(”-----------------------”);
}

  1. 主循环中调用PubSubClient的loop方法,定期调用函数,实现与mqtt服务器的消息接受发送。

大致流程是这样,使用不同的功能 不同的模块时要适当修改代码。

完整代码

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
 
const char* ssid = "nihen_pc";
const char* password = "****";
const char* mqtt_server = "192.168.137.41";
const int mqttPort = 1883;

 
WiFiClient espClient;
PubSubClient client(espClient);
 
void setup() {
 
  Serial.begin(115200);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting to WiFi..");
  }
  Serial.println("Connected to the WiFi network");
 
  client.setServer(mqtt_server, mqttPort);
  client.setCallback(callback);
 
  while (!client.connected()) {
    Serial.println("Connecting to MQTT...");
 
    if (client.connect("ESP8266Client")) {
      Serial.println("connected");  
 
    } else {
 
      Serial.print("failed with state ");
      Serial.print(client.state());
      delay(2000);
 
    }
  }
 
  client.publish("esp/test", "Hello from ESP8266");
  client.subscribe("esp/test");
 
}
 
void callback(char* topic, byte* payload, unsigned int length) {
 
  Serial.print("Message arrived in topic: ");
  Serial.println(topic);
 
  Serial.print("Message:");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
 
}
 
void loop() {
  client.loop();
}

参考文章
参考文章

arduino 软串口连接

上面的方法是使用esp8266板子 直接上传到esp上面执行
下面我们介绍使用arduino控制esp(有时候需要联合esp使用)
需要将esp模块的波特率修改为9600 初始化为115200
使用"SoftwareSerial.h" 设置软串口 进行与arduino之间的通信

使用WiFiESP这个底层是AT指令封装的库 进行连接wifi

缺点很明显 反应很慢 会经常断开连接

参考 http://xcx1024.com/ArtInfo/117805.html

使用sdk 开发参考链接

参考代码链接

GitHub地址

更多学习教程

ESP开发学习基础知识

基础知识包括对esp模块的认识与了解 mqtt协议的了解,arduino IDE运用代码编写等等。

  1. arduino基础学习
  2. esp系列模块的介绍
  3. mqtt协议的介绍与使用
  4. 利用mqtt esp模块 基于arduino IDE开发方法
  5. esp模块的AT指令 刷固件
  6. esp模块睡眠模式使用
  7. esp8266-01s介绍与使用
  8. esp8266-12f介绍与使用
  9. NodeMcu介绍与使用
esp开发IOT应用

基于esp8266的模块以及其他模块根据实际的应用场景与需求制作的物联网应用

  1. 基于FRID arduino 继电器 电磁锁开发的FRID门禁系统
  2. esp32-cam获取视频流图像处理
  3. 基于步进电机 esp8266 mqtt开发的自动窗帘控制
  4. 基于DHT11 Esp8266 mqtt获取室内温湿度
  5. 基于CCS811 esp8266 mqtt 获取室内空气质量
  6. 基于红外模块 esp8266 mqtt开发的智能遥控控制
  7. 基于ws2812 esp8266 mqtt开发的智能多级照明灯
  8. 基于ws2812 esp8266 mqtt开发的智能多模式氛围灯
  9. 基于mp3player esp8266 mqtt开发的智能语音播报系统
  10. IOT综合应用之智慧教室项目开发
发布了46 篇原创文章 · 获赞 59 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/Nirvana_6174/article/details/104402855