当前位置:网站首页>第七天训练
第七天训练
2022-07-17 09:00:00 【Andrewjianlei】
1.TCP改用UDP发送数据
//uint16_t data_len = 0;
uint8_t wifi_data[200] = {0};
uint8_t rx_data[200] = {0};
//uint8_t tx_data[200] = "AT\r\n";
// 0.检测是否更新WIFI信息
Key_Update_WIFI((char *)wifi_data, 2000);
// 1.启动串口2接收(中断方式)
USART2_StartRx();
// 2.串口2发送AT指令,启动TCP服务
//USART2_Transmit(tx_data, sizeof(tx_data), 500);
ESP8266_SendCommand("AT+RST", "OK", 500);
HAL_Delay(1000);
ESP8266_SendCommand("AT+CWMODE=1", "OK", 500);
ESP8266_SendCommand((char *)wifi_data, "OK", 5000);
ESP8266_SendCommand("AT+CIPSTART=\"UDP\",\"192.168.1.1\",9999,9999,2", "OK", 500);
ESP8266_SendCommand("AT+CIFSR", "OK", 500);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
// 3.1 按键按下,发送门铃信号
if (Key_GetFlag() == 1)
{
ESP8266_Send_UDP("{\"data\":\"doorbell\",\"status\":\"1\"}");
}
// 3.2 收到数据,控制灯
if (USART2_Receive(rx_data)) //接收到数据
{
if (strstr((char *)rx_data, "\"dev\":\"led\",\"status\":\"0\""))
{
printf("led off\n\r");
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
USART2_ClearBuf();
memset(rx_data, 0, 200);
}
else if (strstr((char *)rx_data, "\"dev\":\"led\",\"status\":\"1\""))
{
printf("led on\n\r");
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
USART2_ClearBuf();
memset(rx_data, 0, 200);
}
}
}
2.实现用串口更改WIFI账户密码
// 更新WIFI连接信息
void Key_Update_WIFI(char *cmd, uint16_t time)
{
char ch = 0;
char len = 0;
char ssid_buf[20] = "100ask";
char pwd_buf[20] = "100ask100ask";
char wifi_buf[100] = "AT+CWJAP_DEF=\"";
// 1.设置等待时间
HAL_Delay(time);
// 2.检测按键是否按下(是否需要更新WIFI)
if (Key_GetFlag() == 1)
{
// 2.1获取WIFI账号
memset(ssid_buf, 0, 20);
printf("Please enter SSID:");
while(ch != '\r')
{
ch = getchar();
if(ch > ' ')
{
printf("%c",ch);
ssid_buf[len] = ch;
len++;
}
}
// 2.1获取WIFI密码
ch = 0;
len = 0;
memset(pwd_buf, 0, 20);
printf("\r\nPlease enter Password:");
while(ch != '\r')
{
ch = getchar();
if(ch > ' ')
{
printf("%c",ch);
pwd_buf[len] = ch;
len++;
}
}
}
// 3.组合指令
strcat(wifi_buf, ssid_buf);
strcat(wifi_buf, "\",\"");
strcat(wifi_buf, pwd_buf);
strcat(wifi_buf, "\"");
memcpy(cmd, wifi_buf, strlen(wifi_buf));
}
3.优化IP地址回显
// 优化IP地址显示
ip_offset = strstr(buf, "STAIP"); //获取字符串偏移
if(ip_offset)
{
memcpy(buf, ip_offset+6, strlen(ip_offset)); //拷贝开始部分字符串
while(buf[len] != '\r') //拷贝改行数据
{
ip_buf[len] = buf[len]; //保存IP地址
len++;
}
printf("ESP8266 IP: %s \r\n", ip_buf); //打印IP地址
}
return 0;
}
else
{
timeout--;
HAL_Delay(1);
}
边栏推荐
- [handwritten numeral recognition] handwritten numeral recognition based on lenet network with matlab code
- QT serial communication
- 2022年P气瓶充装考试练习题模拟考试平台操作
- 使用<pre>和JSON.stringify处理网页展示JSON的格式
- Leetcode sword finger offer II 041 Average value of sliding window: low space consumption solution
- cut,sort,uniq,xargs
- 分布式事务-可靠消息最终一致性解决方案
- 2022年上海市安全员C证国家题库及答案
- [leetcode] general operation summary
- TP5 wechat withdrawal merchants transfer to change (copying is available)
猜你喜欢

Collation of exercises of shallow neural networks in the third week of in-depth study

Distributed transaction reliable message final consistency solution

SQL优化

vscode下载历史版本

Solutions to license invalidation caused by MATLAB update

cut,sort,uniq,xargs

树状数组
![[handwritten numeral recognition] handwritten numeral recognition based on lenet network with matlab code](/img/e7/68fa042cae0bd10a06f08bba738c12.png)
[handwritten numeral recognition] handwritten numeral recognition based on lenet network with matlab code
分库分表

QR分解求矩阵逆--c工程实现
随机推荐
Basic steps for creating a static library
项目代码训练教程
cut,sort,uniq,xargs
MySQL索引(二)
Leetcode sword finger offer II 041 Average value of sliding window: low space consumption solution
HCIP --- OSPF的综合实验
LeetCode 0116.填充每个节点的下一个右侧节点指针
LabVIEW用了多线程,程序是不是会跑的更快些
Bean的作用域和生命周期
Solutions to license invalidation caused by MATLAB update
ROS common toolkit commands
Hausdorff loss function for semantic segmentation
2022t elevator repair examination question bank and answers
Introduction to flutter flutter calculates the last 1 month, 3 months, half a year, 12 months
二进制安装 mysql 初始化密码问题
Shell notes
【虹科】GenICam协议入门
对ogg用户进行加密
AuthTalk 第一期预告 | 全面拆解多租户解决方案
Qt之Qprocess