当前位置:网站首页>ESP8266_ Connect to Alibaba cloud through mqtt protocol

ESP8266_ Connect to Alibaba cloud through mqtt protocol

2022-06-11 09:37:00 Little tiger_ IOT

1、 Alibaba cloud operations

1.1、 Get ready

Log on the official website of aliyun , Registered account , Real name authentication ;

Find the product → The Internet of things IoT→ Enterprise Internet of things platform → Access control → Public examples ;

1.2、 Create a product and two devices under the product

Create products , For example, a ESP8266 Control lamp products ;

This product requires two cloud devices ,ESP8266 Itself and the client , The client sends control instructions to Alibaba cloud , The instructions are forwarded to through the cloud ESP8266 equipment ;

 


1.3、 see MQTT Connection parameters , use mqtt.fx Test whether it can be connected to the cloud

Selected device , In the device information , I circled it out “MQTT Connection parameters ”, Click on “ see ” And then there is , Copy this information to mqtt.fx Configuration file for , Can successfully connect ;


 1.4、 Create rules , Realize data transmission between devices

First, understand the topic name of the subscription publication , In the product of Topic There are... In the class list ;

  • The topic name of the default subscription :/ProductKey/DeviceName/user/get
  • The default published topic name :/ProductKey/DeviceName/user/update

And then in “ Rules engine ”、“ Cloud product flow ”、 Old edition , Add two rules , among mqttfx Forward data to esp8266 The rules are as follows ;

  After creating the rule , Start these two rules , Open two mqttfx Client to test , success ;

 

2、 Code and phenomenon

2.1、esp8266 Code

want modify mqtt_config.h Server domain name in 、 port 、ClientID、MQTT user name 、 password 、 Holder identification ;

also user_main.c Subscription topic name in 、 Publish topic name 、 received PUBLISH Message subject name ;

ClientID I can see it at a glance , How long is it spicy , It must be more than 64 Yes. , therefore hold config.h Medium “device_id” Changed to 128 position , It is also confirmed that there is no problem when using this parameter ;

download 、 function , The following information is returned :

  Look for what you have written MQTT That article ESP8266_MQTT agreement _ Little tiger _IOT The blog of -CSDN Blog , have a look CONNACK The message 32,2,0,4 What does it represent , good heavens , Invalid user name or password ;

Is the user name and password invalid ? Like Baidu cloud last time ?

Go to the equipment details “MQTT Connection parameters ” Copied the new user name 、 password 、 client ID, Then burn and run , Not yet. ;

emmm, I just forgot to change the holder logo , Change and try again , It's another meal, but it still doesn't work ;

Then I counted the length of the password , just 64 position ,emmm, Not at all. OK , Remember the password array in the code 64 position , The string has another '\0' Well , So we need to hold config.h Medium “mqtt_pass” Change to 128 position , Again , It is also confirmed that there is no problem when using this parameter , Sure enough ;

//  system parameter 
//-------------------------------------------------------------------------------------
typedef struct{
	uint32_t cfg_holder;		//  Holder identification ( Only update this value , System parameters will be updated )
	uint8_t device_id[128];		//  client ID[128]		

	uint8_t sta_ssid[64];		// WIFI name [64]
	uint8_t sta_pwd[64];		// WIFI password [64]
	uint32_t sta_type;			// STA type 

	uint8_t mqtt_host[64];		// MQTT Server domain name [64]
	uint32_t mqtt_port;			// MQTT port 

	uint8_t mqtt_user[64];		// MQTT user name [64]		
	uint8_t mqtt_pass[128];		// MQTT password [128]		

	uint32_t mqtt_keepalive;	//  Stay connected for 
	uint8_t security;			//  Security type 
} SYSCFG;

2.2、 The phenomenon

  Make complaints : I regret mosaic the key from the first picture , Then we have to play mosaic , Whenever I advise you to send a few, don't call , No problem , It seems that we all hear another voice , no way , The front has been mosaic , And if you don't fight now, you will seem to forget , It's stupid fufu Of , No matter what I say about the other one, I won't listen , Even control my hands , This is the so-called internal friction , I split ;

原网站

版权声明
本文为[Little tiger_ IOT]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110914309645.html