当前位置:   article > 正文

ESP8266/ESP32构建MQTT协议连接进入EMQX服务器进行服务器指令通讯(客户端控制灯)_esp32用python连接emqx

esp32用python连接emqx

一、配置Arduino IDE以及esp8266/esp32环境

Arduino IDE 2.0安装,ESP8266/ESP32环境配置(Win11)_JASON丶LI的博客-CSDN博客https://blog.csdn.net/weixin_61908666/article/details/127828616?spm=1001.2014.3001.5501

二、搭建emqx环境

win11搭建EMQX环境_JASON丶LI的博客-CSDN博客https://blog.csdn.net/weixin_61908666/article/details/127944288?spm=1001.2014.3001.5502

三、点灯测试

输入下列代码上传到开发板(作者用的是ESP8266

  1. void setup() {
  2. pinMode(LED_BUILTIN, OUTPUT);
  3. }
  4. void loop() {
  5. digitalWrite(LED_BUILTIN, LOW);
  6. delay(1000);
  7. digitalWrite(LED_BUILTIN, HIGH);
  8. delay(2000);
  9. }

看到ESP8266上蓝色的呼吸灯一闪一闪就成功啦!!!

四、代码实现将ESP8266接入服务器并订阅对应信息

  1. const char *ssid = "Jason"; // 输入你的WiFi名称
  2. const char *password = "88888888"; // 输入你的WiFi密码
  3. const char *mqtt_broker = "10.12.3.89"; //输入你的无线局域网适配器IPv4地址
  4. const int mqtt_port = 1883; //默认为1883

 Win+R打开运行框-->输入cmd-->输入ipconfig-->查找IPv4地址

 ESP8266接入服务器

创建WebSocket客户端

 下发指令

 查看ESP8266接收指令情况

 

源代码:

  1. #include <ESP8266WiFi.h> //esp8266
  2. #include <PubSubClient.h>
  3. //#include <WiFi.h> // esp32
  4. const char *ssid = "Jason"; // 输入你的WiFi名称
  5. const char *password = "88888888"; // 输入你的WiFi密码
  6. const char *mqtt_broker = "10.12.3.89"; //输入你的无线局域网适配器IPv4地址
  7. const int mqtt_port = 1883; //默认为1883
  8. WiFiClient espClient;
  9. PubSubClient client(espClient);
  10. void setup() {
  11. pinMode(LED_BUILTIN, OUTPUT);
  12. Serial.begin(115200); // 频段
  13. WiFi.begin(ssid, password);
  14. while (WiFi.status() != WL_CONNECTED) {
  15. delay(500);
  16. Serial.println("Connecting to WiFi..");
  17. }
  18. Serial.println("Connected to the WiFi network");
  19. client.setServer(mqtt_broker, mqtt_port);
  20. client.setCallback(callback);
  21. while (!client.connected()) {
  22. Serial.println("Connecting to public emqx mqtt broker.....");
  23. if (client.connect("esp8266-client")) {
  24. Serial.println("Public emqx mqtt broker connected");
  25. } else {
  26. Serial.print("failed with state ");
  27. Serial.print(client.state());
  28. delay(2000);
  29. }
  30. }
  31. // publish and subscribe
  32. client.publish("esp8266/test", "hello emqx");
  33. client.subscribe("Android");
  34. }
  35. void callback(char *topic, byte *payload, unsigned int length) {
  36. Serial.print("Message arrived in topic: ");
  37. Serial.println(topic);
  38. Serial.print("Message:");
  39. String message;
  40. for (int i = 0; i < length; i++) {
  41. message = message + (char) payload[i]; // convert *byte to string
  42. }
  43. Serial.print(message);
  44. if (message == "lon") {
  45. digitalWrite(LED_BUILTIN, LOW);// LED on
  46. Serial.print(" on ready");
  47. }
  48. if (message == "loff"){
  49. digitalWrite(LED_BUILTIN, HIGH);// LED off
  50. Serial.print(" on ready");
  51. }
  52. Serial.println();
  53. Serial.println("-----------------------");
  54. }
  55. void loop() {
  56. // digitalWrite(LED_BUILTIN, LOW);
  57. // delay(1000);
  58. // digitalWrite(LED_BUILTIN, HIGH);
  59. // delay(2000);
  60. client.loop();
  61. }

本文为作者独立编写

 本BLOG上所有的原创文章未经本人许可,不得用于商业用途及传统媒体。网络媒体转载请注明出处,否则属于侵权行为。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/580742
推荐阅读
相关标签
  

闽ICP备14008679号