数码之家

 找回密码
 立即注册

QQ登录

只需一步,快速开始

微信登录

微信扫一扫,快速登录

搜索
查看: 1971|回复: 1

[Arduino] ESP8266 arduino MQTT发布温度的示范源码

[复制链接]
发表于 2020-4-11 10:48:18 | 显示全部楼层 |阅读模式

爱科技、爱创意、爱折腾、爱极致,我们都是技术控

您需要 登录 才可以下载或查看,没有账号?立即注册 微信登录

x
全球开放订阅——有电的话,每隔约16分钟发布1次。
mosquitto_sub -h test.mosquitto.org -t "zh.gd.cn/temperature1"   



源码——是发布的源码,如果要订阅使用PubSubClient 也非常轻松,注册一个回调函数。假如要实现远程遥控,不是难事。。。。。只是要注意保密通讯资料。

const char* location         = "E113.5835,N22.2767"; //length=18,ZhuHai bd map location
void mqttSendFree() {
  const char* mqttId         = "arduinoTemp2";
  const char* mqttHost       = "test.mosquitto.org"; //此服务器够标准、还是开放的,不像内地某些所谓大牌厂家太多“本土化”了
  const char* topTemp1       = "zh.gd.cn/temperature1";
  PubSubClient mqtt(mqttHost, 1883, wifiClient); //wifiClient是ESP8266的WifiClient
  char bb[26];
  int t;

  Serial.printf("MQTT2 Connecting to %s ... ", mqttHost);
  if (false == mqtt.connect(mqttId) ) {// Connect to MQTT Broker
    Serial.println(" fail");
    return ;
  }
  Serial.println(" OK");

  t = readTemperature(); //读DS18B20,在Arduino中使用库很easy,放大了100倍,所以取整了。

  bb[0] = (t >= 0) ? '+' : '-';
  bb[1] = ((t / 1000) % 10) + '0'; //原温度的十位
  bb[2] = ((t / 100) % 10) + '0'; //原温度的个位
  bb[3] =   '.'; //
  bb[4] = ((t / 10) % 10) + '0'; //原温度的第1小数
  bb[5] = (t  % 10) + '0'; //原温度的第2小数
  bb[6] = ',';
  for (int i = 0; i < 18; i++)bb[i + 7] = location[i];
  bb[25] = 0;
  mqtt.publish(topTemp1, bb);
  Serial.printf("MQTT2 sent Temperature=%s\r\n", bb);
  mqtt.disconnect();
}



打赏

参与人数 2家元 +25 收起 理由
lemontreenm + 20
人艰不拆了 + 5

查看全部打赏

 楼主| 发表于 2020-4-11 11:02:34 | 显示全部楼层

C:\mosquitto\mosquitto_sub -h test.mosquitto.org -t "zh.gd.cn/temperature1"
+25.00,E113.5835,N22.2767
+25.50,E113.5835,N22.2767
+24.50,E113.5835,N22.2767
+24.00,E113.5835,N22.2767
+24.50,E113.5835,N22.2767
+24.50,E113.5835,N22.2767
+25.00,E113.5835,N22.2767
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册 微信登录

本版积分规则

APP|手机版|小黑屋|关于我们|联系我们|法律条款|技术知识分享平台

闽公网安备35020502000485号

闽ICP备2021002735号-2

GMT+8, 2025-7-22 05:54 , Processed in 0.390001 second(s), 13 queries , Redis On.

Powered by Discuz!

© 2006-2025 MyDigit.Net

快速回复 返回顶部 返回列表