ESP8266 MQTT服务器+阿里云
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
/*WIFI 相关变量*/
const char * G_WifiName = "XY054789";
const char * G_WifiPasswd = "12345678";
uint16_t G_WaitTime=0;
/*MQTT 相关变量*/
char * MqttServer ="47.109.27.255";
uint16_t MqttServerPort = 1883;
const char * MqttUser = "wangju";
const char * MqttPaw = "root.2018";
const char * MqttTopicRecv = "stm32_client_r";
const char * MqttTopicSend = "stm32_client_s";
char * MqttClientId = "mqttx_c61ef910";
//定义WIFI 对象
WiFiClient wic;
//定义MQtt PC端对象
PubSubClient mqttpc(wic);
/****************************串口初始化***************************************/
void SerialInit(void)
{
Serial.begin(9600);
Serial.printf("\n");
}
/*****************************************************************************************/
/***********************************WIFI初始化**********************************************/
uint8_t WifiInit(const char * WifiName,const char * WifiPasswd,int WaitTime)
{
//设置Wifi模式为sta
WiFi.mode(WIFI_STA);
//清除配置
WiFi.disconnect();
//开始连接WiFi
WiFi.begin(WifiName,WifiPasswd);
//设置超时时间
G_WaitTime=1;
//测试是否在超时时间内连接
while((WiFi.status() != WL_CONNECTED))
{
delay(1000);
Serial.printf("connected wifi %s wait %d s \r\n",WifiName,G_WaitTime);
if(G_WaitTime++ > WaitTime)
{
Serial.printf("connected wifi %s faild wait timeout \r\n",WifiName);
return 0;
}
}
Serial.printf("connected wifi %s success \r\n",WifiName);
Serial.printf("connect WIFI %s success,local IP is %s\r\n",WiFi.SSID().c_str(),WiFi.localIP().toString().c_str());
return 1;
}
/*****************************************************************************************/
/*******************************MQTT初始化***************************************************/
//消息回调函数,处理接收到的消息
void getMQTT(char* topic,byte* payload,unsigned int length){
Serial.printf("get data from %s\r\n",topic); //输出调试信息,得知是哪个主题发来的消息
for(unsigned int i=0;i<length;++i){ //读出信息里的每个字节
Serial.print((char)payload[i]); //以文本形式读取就这样,以16进制读取的话就把(char)删掉
}
Serial.println();
}
uint8_t MqttInit(void)
{
if(WiFi.status() != WL_CONNECTED)
{
Serial.printf("wifi disconnected !!!\r\n");
return 0;
}
//设置服务器信息
mqttpc.setServer("47.109.27.255",MqttServerPort);
//连接mqtt服务器
if(!mqttpc.connect(MqttClientId,MqttUser,MqttPaw))
{
Serial.printf("mqtt connected failed !!!\r\n");
return 0;
}
//订阅主题
mqttpc.subscribe(MqttTopicRecv);
//设置回调函数
mqttpc.setCallback(getMQTT);
Serial.printf("mqtt connected success !!!\r\n");
mqttpc.publish(MqttTopicSend,"mqtt connected success !!! node:stm32_1");
return 1;
}
/*****************************************************************************************/
void setup() {
// put your setup code here, to run once:
SerialInit();
WifiInit(G_WifiName,G_WifiPasswd,15);
MqttInit();
}
void loop() {
// put your main code here, to run repeatedly:
if(WiFi.status() != WL_CONNECTED)
{
WifiInit(G_WifiName,G_WifiPasswd,15);
MqttInit();
}
else
{
//wifi 连接正常
//MQTT 连接
if(mqttpc.connected())
{
mqttpc.loop(); //发送心跳信息
mqttpc.publish(MqttTopicSend,"node:stm32_1 data:test");
}
else
{
MqttInit();
}
}
}