标题:
阿里云MQTT数据云流转,实现多个终端控制某一个设备
[打印本页]
作者:
leanco
时间:
2022-11-14 00:03
标题:
阿里云MQTT数据云流转,实现多个终端控制某一个设备
一、在阿里云注册登录,并在控制台建好项目
二、在arduinoIDE上传代码至arduino系列单片机或者ESP8266或ESP32
三、可以在本地按键操作硬件,也可以在阿里云端进行设备模拟查看最终效果。
附上代码,仅供参考
#include <ESP8266WiFi.h>
#include <PubSubClient.h>//这个没有回调函数
#include <ArduinoJson.h>
#include "DHT.h"
WiFiClient espClient;
PubSubClient client(espClient);
PubSubClient mqttClient(espClient);
#define sensor_Pin 0
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
float h;
float t;
long s;
// 连接WIFI和密码
#define WIFI_SSID "-------------"
#define WIFI_PASSWD "-----------"
//设备的三元组信息
#define PRODUCT_KEY "-----------"
#define DEVICE_NAME "---------"
#define DEVICE_SECRET "-----------"
#define REGION_ID "cn-shanghai"
//不需要改
#define MQTT_SERVER PRODUCT_KEY ".iot-as-mqtt." REGION_ID ".aliyuncs diancom"
#define MQTT_PORT 1883
/////
#define MQTT_USRNAME DEVICE_NAME "&" PRODUCT_KEY
#define CLIENT_ID "h8v70q9rDqp.center|securemode=2,signmethod=hmacsha256,timestamp=1652631576537|"//这里使用技小新可以生成,注意密码和密钥一定要对否则连不上MQTT "FESA234FBDS24|securemode=3,timestamp=789,signmethod=hmacsha1|"
#define MQTT_PASSWD "180866dbe114bb5cedd3a3dc1f27dccdadeaf0dbc51bd3873a26c88bef98cf08"
#define ALINK_TOPIC_PROP_SET "/sys/" PRODUCT_KEY "/" DEVICE_NAME "/thing/service/property/set"
#define ALINK_BODY_FORMAT "{\"id\":\"ESP8266\",\"version\":\"1.0\",\"method\":\"thing.event.property.post\",\"params\":%s}"
#define ALINK_TOPIC_PROP_POST "/sys/" PRODUCT_KEY "/" DEVICE_NAME "/thing/event/property/post"
////a1JKlnw6EP0/postion1/user/update
unsigned long lastMs = 0;
float soil_data = 0;
//连接wifi
void wifiInit()
{
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWD);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.println("WiFi not Connect");
}
client.setServer(MQTT_SERVER, MQTT_PORT); //连接MQTT服务器
}
//mqtt连接
void mqttCheckConnect()
{
while (!client.connected())
{
client.connect(CLIENT_ID, MQTT_USRNAME, MQTT_PASSWD);
}
Serial.println("MQTT connect succeed!");
// client.subscribe(ALINK_TOPIC_PROP_POSTRSP);
mqttClient.subscribe(ALINK_TOPIC_PROP_SET);
Serial.println("subscribe done");
}
void mqttIntervalPost()
{
soil_data = analogRead(sensor_Pin); //模拟数据读取(A0脚输入的数据)385-1024
soil_data -= 385; //0-639
soil_data /= 6.39; //0.00-100.00,变为百分比
soil_data = 100 - soil_data; //修改为百分比越大湿度越大
char param[32];
char jsonBuf[128];
read_data();
//这里\"**"\要选择对应的标识符,否则数据上报不了。
sprintf(param, "{\"wendu\":%f}", t);
sprintf(jsonBuf, ALINK_BODY_FORMAT, param);
Serial.println(jsonBuf);
boolean e = client.publish(ALINK_TOPIC_PROP_POST, jsonBuf);
sprintf(param, "{\"CurrentHumidity\":%f}", h);
sprintf(jsonBuf, ALINK_BODY_FORMAT, param);
Serial.println(jsonBuf);
boolean f = client.publish(ALINK_TOPIC_PROP_POST, jsonBuf);
///以下///上报给另一个设备///////////
}
void callback(char *topic, byte *payload, unsigned int length)//mqtt回调函数
{
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
payload[length] = '\0';
Serial.println((char *)payload);
if (strstr(topic, ALINK_TOPIC_PROP_SET))
{
StaticJsonBuffer<100> jsonBuffer;
JsonObject &root = jsonBuffer.parseObject(payload);
if (!root.success())
{
Serial.println("parseObject() failed");
return;
}
}
}
void setup()
{
Serial.begin(115200);
wifiInit();
dht.begin();
}
void read_data()
{
float read_h = dht.readHumidity();//湿度
float read_t = dht.readTemperature();//温度
h = read_h;
t = read_t;
Serial.print("湿度:");
Serial.print(h);
Serial.println("%");
Serial.print("温度:");
Serial.println(t);
}
void loop()
{
s = millis();
delay(500);
read_data();
Serial.println(millis() - s);
if (millis() - lastMs >= 5000)
{
lastMs = millis();
mqttCheckConnect();
mqttIntervalPost();
}
client.loop();
delay(2000);
}
复制代码
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1