Arduino_esp32_WiFi_mqtt代码

#include<WiFi.h>
#include<PubSubClient.h>

const char* ssid = "BlackWalnut";
const char* password = "blackwalnut";

const char* mqttServer = "192.168.1.25";
const int mqttPort = 1883;
const char* mqttUser = "admin";
const char* mqttPassword = "public";
 
WiFiClient espClient;
PubSubClient client(espClient);

void callback(char* topic, byte* payload, unsigned int length)
{
    Serial.print("来自订阅的主题:");
    Serial.println(topic);
    Serial.print("信息:");
    for (int i = 0; i< length; i++)
    {
        Serial.print((char)payload[i]);
    }
    Serial.println();
    Serial.println("-----------------------");
}
void setup()
{
    Serial.begin(115200);
    while (WiFi.status() != WL_CONNECTED)
    {
        Serial.println("WiFi连接中...");
        WiFi.begin(ssid,password);
        delay(2500);
    }
    Serial.println("WiFi连接成功");
    
    client.setServer(mqttServer,mqttPort);
    client.setCallback(callback);
    while (!client.connected())
    {
        Serial.println("MQTT服务器连接中");
        if (client.connect("ESP32Client",mqttUser, mqttPassword ))
        {
            Serial.println("MQTT服务器连接成功");
        }
        else
        {
            Serial.print("MQTT服务器连接失败,正在尝试重新连接");
            Serial.print(client.state());
            delay(2000);
        }
    }
    client.subscribe("ESP32");
    Serial.print("已订阅主题,等待主题消息....");
    client.publish("/World","Hello from ESP32");
}
 
void loop()
{
  client.loop();                            
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
设备端与阿里云IoT平台建立MQTT通道的CONNECT报文有三个关键参数:客户端标识符 (ClientId) ,用户名(User Name),密码(Password)。这三个参数由设备三元组(productKey,deviceName,deviceSecret)按照阿里云IoT签名规则生成。 参数 生成方式 描述 username deviceName+"&"+productKey password sign_hmac(deviceSecret,content) sign_hmac为mqttClientId中的signmethod content为 "clientId${id}deviceName${deviceName}productKey${productKey}timestamp${timestamp}" 其中 id:表示客户端ID,64字符内。 timestamp:表示当前时间毫秒值。 clientId id+"|securemode=3,signmethod=hmacsha1,timestamp="+timestamp+"|" id:表示客户端ID,64字符内。 timestamp:表示当前时间毫秒值。 securemode:表示安全模式:可选值有2(TLS加密)和3(非加密) signmethod:表示签名算法类型。支持hmacmd5,hmacsha1和hmacsha256 2. 三元组接入示例 2.1 设备三元组信息 设备三元组 productKey = a14Xib5kdYd deviceName = light1983432 deviceSecret = oLyaKqVxtRvjH284LdhqVgVUx1UPy6zq 建立MQTT连接时参数 clientId = SN1928339 timestamp = 1539421321846 signmethod = hmacsha1 2.2 参数生成结果 mqttClientId = SN1928339|securemode=3,signmethod=hmacsha1,timestamp=1539421321846| mqttUsername = light1983432&a14Xib5kdYd mqttPassword = b2488041f64f425016b467ee1c94959ebd592ad1 生成password的content content=clientIdSN1928339deviceNamelight1983432productKeya14Xib5kdYdtimestamp1539421321846 2.3 建立连接 mqttClientId作为MQTT客户端标识符 (ClientId) mqttUsername作为MQTT用户名(User Name) mqttPassword作为MQTT密码(Password) 在线Password生成算法验证
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值