stm32毕设项目-基于stm32的智能药箱--通过ESP8266连接阿里云

1、前提准备

   需要先在阿里云平台建立设备实例,对学生来说还是挺不错的,有一定限度的免费使用,只是拿来做毕设或者简单的学习使用,绰绰有余了。

提前准备好的有:

1、WIFI ,这里注意wifi需要2.4G最好不要用5G的,否则很有可能搜索不到。

2、阿里云设备实例:需要获取到CID号,用户名和密码

#include "./BSP/ESP/esp.h"
#include "./SYSTEM/usart/usart.h"
#include "./SYSTEM/delay/delay.h"
#include "./BSP/LCD/lcd.h"
int Temperature;
int Shidu;

uint8_t Switch2;
extern char RECS[250];
const char* WIFI ="1MI9";  //WIFI名,每个人的不一样
const char* WIFIASSWORD="11111111"; //wifi密码
const char* ClintID="a1lzbFHmhfC.test1|securemode=2\\,signmethod=hmacsha256\\,timestamp=1678437715518|";  // 设备CID号
const char* username="test1&a1lzbFHmhfC";  //设备账号,用于登录设备,获取设备信息
设备密码,用于登录设备,获取设备信息
const char* passwd="562e68809d86622e2e7b1d96ead6f0a33260195c524a7393cff46536d6af272e";
//阿里云物联网平台域名
const char* Url="a1lzbFHmhfC.iot-as-mqtt.cn-shanghai.aliyuncs.com";
//通讯事件选择账号中的设备
const char* pubtopic="/sys/a1lzbFHmhfC/test1/thing/event/property/post";
//设置通讯事件 为上传数据
const char* subtopic="thing.event.property.post";
//自定义函数获取温度
const char* func1="temperature";
//自定义函数获取湿度
const char* func2="Humidity";
//获取报警开关
const char* func3="AlarmSwitch";



//初始化,开机时连接阿里云,显示连接进度
char esp_Init(void)
{

printf("AT\r\n");  
	delay_ms(500);
	lcd_show_num(160, 160,1, 3, 16, RED);
	printf("AT+RST\r\n");  //重启
	delay_ms(500);
	lcd_show_num(160, 160,2, 3, 16, RED);
	
	printf("ATE0\r\n");    //关闭回显
	delay_ms(100);
lcd_show_num(160, 160,3, 3, 16, RED);
	printf("AT+CWMODE=1\r\n"); //Station模式
	lcd_show_num(160, 160,4, 3, 16, RED);
	delay_ms(500);

	
printf("AT+CIPSNTPCFG=1,8,\"ntp1.aliyun.com\"\r\n"); //连接阿里云域名
	lcd_show_num(160, 160,5, 3, 16, RED);
	delay_ms(1000);
	
	printf("AT+CWJAP=\"%s\",\"%s\"\r\n",WIFI,WIFIASSWORD); //连接热点
	lcd_show_num(160, 160,6, 3, 16, RED);
	delay_ms(1000);

	

	printf("AT+MQTTUSERCFG=0,1,\"NULL\",\"%s\",\"%s\",0,0,\"\"\r\n",username,passwd);//用户信息配置
	lcd_show_num(160, 160,7, 3, 16, RED);
	delay_ms(1500);

	printf("AT+MQTTCLIENTID=0,\"%s\"\r\n",ClintID);//用户信息配置
		lcd_show_num(160, 160,8, 3, 16, RED);
	delay_ms(2000);

	printf("AT+MQTTCONN=0,\"%s\",1883,1\r\n",Url); //连接服务器
	lcd_show_num(160, 160,9, 3, 16, RED);
	delay_ms(2000);
printf("AT+CIPSNTPCFG=1,8\r\n");

	//printf("AT+MQTTSUB=0,\"%s\",1\r\n",subtopic); //订阅消息

	return 0;
}
//断开与阿里云的连接
void disconnect(void)
{
	printf("AT+MQTTCLEAN=0\r\n");
		delay_ms(1200);
	
}
//连接阿里云
void connect(void)
{
printf("AT+CIPSNTPCFG=1,8,\"ntp1.aliyun.com\"\r\n"); //连接阿里云域名

	delay_ms(500);

	printf("AT+CWJAP=\"%s\",\"%s\"\r\n",WIFI,WIFIASSWORD); //连接热点

	delay_ms(1000);

	

	printf("AT+MQTTUSERCFG=0,1,\"NULL\",\"%s\",\"%s\",0,0,\"\"\r\n",username,passwd);//用户信息配置

	delay_ms(500);

	printf("AT+MQTTCLIENTID=0,\"%s\"\r\n",ClintID);//用户信息配置

	delay_ms(500);

	printf("AT+MQTTCONN=0,\"%s\",1883,1\r\n",Url); //连接服务器

	delay_ms(500);
printf("AT+CIPSNTPCFG=1,8\r\n");
	delay_ms(500);
}
/*向阿里云传输数据,传输数据格式遵循阿里云的格式,可能会被阿里修改,如果不能正常传输,可以去阿里云上模拟一次数据传输,看它的格式是什么样的,再仿照它的格式来写,输出指定符号需要换行符,所以代码中有较多斜杠*/
void Sent_info(int Temperature,int Shidu,int Switch2)
{
	
	printf("AT+MQTTPUB=0,\"%s\",\"{\\\"method\\\":\\\"thing.event.property.set\\\"\\,\\\"params\\\":{\\\"%s\\\":%d\\,\\\"%s\\\":%d}}\",0,0\r\n",pubtopic,func1,Temperature,func2,Shidu);
	delay_ms(1000);
	//Temperature++;
}
void sendEatMedic(int Temperature,int Shidu,int Switch2)
{
	
		printf("AT+MQTTPUB=0,\"%s\",\"{\\\"method\\\":\\\"thing.event.property.set\\\"\\,\\\"params\\\":{\\\"%s\\\":%d\\,\\\"%s\\\":%d\\,\\\"%s\\\":%d}}\",0,0\r\n",pubtopic,func1,Temperature,func2,Shidu,func3,Switch2);
	delay_ms(100);
	
}

单片机与ESP8266为串口通讯,通过printf()的方式,实际是串口输出给ESP8266信息,即通过串口实现AT指令,让esp8266实现对应的功能。

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值