ESP8266-连接阿里云示例

1:环境

ESP8266
STM42F407
wifi:手机热点
阿里云

2:目的

通过在F407中运行代码,让其与ESP8266通过uart3来进行通信。通过uart3发送AT指令。也接收返回的数据。
实现功能:1)可以向阿里云的物模型持续发送数据
2)可以通过阿里云上的开关来控制开发板上LED灯的亮灭

3:主要代码

#include "common.h"
u8 atk_8266_aliyun_test(void)
{
	u8 netpro;
	u8 key=0;
	u8 timex=0; 
	u8 ipbuf[16]; 	//IP缓存
	u8 *p;
	u8 *data;
	u16 t=0;
	u8 res=0;
	u16 rlen=0;
	u8 constate=0;	//连接状态
	u8 i =0;
	char *str;
	p=mymalloc(SRAMIN,100);							
	data=mymalloc(SRAMIN,1000);
	res = atk_8266_send_cmd("AT","OK",50);//检测状态
	res = atk_8266_send_cmd("AT+CWMODE=1","OK",50);//设置WIFI STA mode
	if(res){
		printf("AT+CWMODE=1 error\n");
	}
	res = atk_8266_send_cmd("AT+CIPSNTPCFG=1,8,\"ntp1.aliyun.com\"", "OK", 50); //设置阿里云的参数
	if(res){
		printf("AT+CIPSNTPCFG=1 error\n");
	}
	sprintf((char*)p,"AT+CWJAP=\"%s\",\"%s\"",wifista_ssid,wifista_password);//连接wifi
	res =  atk_8266_send_cmd(p,"OK",1000);	
	if(res){
		printf("AT+CWJAP error\r\n");
	}
	/*设置阿里云上的设备的用户用和密码,可以用阿里云配置工具生成*/
	res =  atk_8266_send_cmd("AT+MQTTUSERCFG=0,1,\"NULL\",\"smart-door&a1vlcNvprSS\",\"FA5EC5AB4CB947695E16C77F718B8EC5669496EE\",0,0,\"\"", "OK", 50); //aliyun
	if(res){
		printf("MQTTUSERCFG error\r\n");
	}
	/*设置阿里云的clinetID,可以用阿里云配置工具生成*/
	res =  atk_8266_send_cmd("AT+MQTTCLIENTID=0,\"kayshi|securemode=3\\,signmethod=hmacsha1\\,timestamp=4546|\"", "OK", 100); //aliyun 
	if(res){
		printf("MQTTCLIENTID error\r\n");
	}
	/*连接阿里云*/
	res =  atk_8266_send_cmd("AT+MQTTCONN=0,\"a1Py84SEWWJ.iot-as-mqtt.cn-shanghai.aliyuncs.com\",1883,1", "OK", 100); //aliyun
	if(res){
		printf("AT+MQTTCONN=0 error\r\n");
	}
	/*订阅一个主题*/
	res =  atk_8266_send_cmd("AT+MQTTSUB=0,\"/a1vlcNvprSS/smart-door/user/test\",1", "OK", 100);
	if(res){
		printf("AT+MQTTSUB error\r\n");
	}
	/*订阅物模型的主题*/
	res =  atk_8266_send_cmd("AT+MQTTSUB=0,\"/sys/smart-door/smart-door/thing/event/property/post_reply\",1", "OK", 100);
	if(res){
		printf("AT+MQTTSUB error\r\n");
	}
//	res =  atk_8266_send_cmd("AT+MQTTPUB=0,\"/sys/a1vlcNvprSS/smart-door/thing/event/property/post\",\"{params:{\\\"LightSwitch\\\":1}}\",0,0", "OK", 100); //发布topic数据
//	if(res){
//		printf("AT+MQTTPUB error\r\n");
//	}
//	res =  atk_8266_send_cmd("AT+MQTTPUB=0,\"/a1vlcNvprSS/smart-door/user/test\",\"data\",0,0", "OK", 100); //发布topic数据
//	if(res){
//		printf("AT+MQTTPUB error\r\n");
//	}
	while(1) {
		/*每过1秒,物模型主题发送一次数据*/
		if((t%100) == 0) {
			sprintf((char*)p,"AT+MQTTPUB=0,\"/sys/a1vlcNvprSS/smart-door/thing/event/property/post\",\"{params:{\\\"CurrentTemperature_14\\\":%d}}\",0,0", i);//设置无线参数:ssid,密码
			res =  atk_8266_send_cmd(p, "OK", 100); //aliyun 
			if(res){
				printf("AT+MQTTPUB wumo error\r\n");
			}
			i++;
			if(i > 31) 
				i = 0;
		}
		/*通过uart3 接受ESP8266数据*/
		if(USART3_RX_STA&0X8000)		//接收到一次数据了
		{ 
			rlen=USART3_RX_STA&0X7FFF;	//得到本次接收到的数据长度
			USART3_RX_BUF[rlen]=0;		//添加结束符 
			printf("%s",USART3_RX_BUF);	//发送到串口   
//			sprintf((char*)p,"收到%d字节,内容如下",rlen);//接收到的字节数 
			str = strtok(USART3_RX_BUF, ",");//以逗号分隔字符串
			while(str){
				printf("%s\r\n",str);//打印分隔字符串
				str = strtok(NULL, ",");//继续分隔,这里设置为NULL是为了分隔剩下的字符串
				if (strstr(str, "LightSwitch")){//判断字符串中是否由有LightSwitch,如何存在则根据后面的值是0或1来决定开关灯
					printf("===============>%s\r\n",str);
					if(str[24] == '1') {
						/*开灯*/
						printf("===============>%c\r\n",str[24]);
						GPIO_ResetBits(GPIOF,GPIO_Pin_9 | GPIO_Pin_10);//GPIOF9,F10设置高,灯灭
					}else{
						/*关灯*/
						printf("===============>%c\r\n",str[24]);
						GPIO_SetBits(GPIOF,GPIO_Pin_9 | GPIO_Pin_10);//GPIOF9,F10设置高,灯灭
					}
				}
			}
			
			USART3_RX_STA=0;
		}
		t++;
		delay_ms(10);
	}
	return 0;
} 


4:设备上电查看结果

1)产品下查看物模型的值
在这里插入图片描述

2)在Iot studio的wed开发中,创建项目,添加控件关联上面的物模型
可以看到计数的值在不断变化
而且可以通过开关按钮来进行控制led灯的开关
在这里插入图片描述

代码:链接

  • 6
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
你可以按照以下步骤来连接ESP8266-01S与阿里云的MQTT服务: 1. 配置阿里云IoT平台: - 在阿里云控制台上创建一个物联网实例。 - 在物联网实例中创建设备,获取设备的三元组信息(DeviceName、DeviceSecret和ProductKey)。 2. 编写Arduino代码: - 在Arduino IDE中打开一个新的Sketch。 - 引入ESP8266WiFi库和PubSubClient库。 - 设置WiFi连接信息。 - 定义阿里云MQTT服务器的地址和端口。 - 定义设备的三元组信息。 - 在setup()函数中连接WiFi和阿里云MQTT服务器。 - 在loop()函数中保持MQTT连接,并处理订阅和发布消息的逻辑。 下面是一个简单的示例代码: ```cpp #include <ESP8266WiFi.h> #include <PubSubClient.h> const char* ssid = "your_wifi_ssid"; const char* password = "your_wifi_password"; const char* mqtt_server = "your_mqtt_server"; const int mqtt_port = 1883; const char* product_key = "your_product_key"; const char* device_name = "your_device_name"; const char* device_secret = "your_device_secret"; WiFiClient espClient; PubSubClient client(espClient); void setup() { Serial.begin(115200); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); client.setServer(mqtt_server, mqtt_port); } void callback(char* topic, byte* payload, unsigned int length) { Serial.print("Message received: "); for (int i = 0; i < length; i++) { Serial.print((char)payload[i]); } Serial.println(); } void reconnect() { while (!client.connected()) { String clientId = "ESP8266Client-"; clientId += String(random(0xffff), HEX); if (client.connect(clientId.c_str(), device_name, device_secret)) { Serial.println("Connected to MQTT server"); client.subscribe("your_topic"); } else { Serial.print("Failed to connect to MQTT server, rc="); Serial.print(client.state()); Serial.println(" retrying in 5 seconds..."); delay(5000); } } } void loop() { if (!client.connected()) { reconnect(); } client.loop(); } ``` 3. 替换示例代码中的`your_wifi_ssid`和`your_wifi_password`为你的WiFi网络的名称和密码。 替换`your_mqtt_server`为你的阿里云MQTT服务器地址。 替换`your_product_key`、`your_device_name`和`your_device_secret`为你的设备三元组信息。 4. 将代码上传到ESP8266-01S开发板。 这样,你的ESP8266-01S就可以连接阿里云的MQTT服务了。记得根据你的实际需求修改代码中的订阅和发布逻辑。希望对你有帮助!如果有任何问题,请随时向我提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值