Stm32开发板Wifi部分
- 04/30
- 这部分我做了两天出了成果,也遇到很多Bug,真的好多坑。所以只讲核心
- 定时器
- 一定要注意分配字符串之后free,free,free
- 接收是Json,所以我用了cJSON(包含cJSON.c;cJSON.h两个文件,我没怎么改)
- 两次开关灯只是好看,但是眼疼
- cJsonBUG:
- 1.把Heap_Size 搞大点,别200,200根本不够
- 2.不是每一个cJSON* 都要free
- 3.cJson可以用很复杂,但是注意你接收的长度和复杂性。越简单代表内存占的越小。
- 4.注意free
- 5.注意压力测试
.
04/30
这部分我做了两天出了成果,也遇到很多Bug,真的好多坑。所以只讲核心
定时器
定时器我这里其实是用的10s的间隔。五一整合的时候打算看看改成5s。
虽说的10s,但是我其实是进行了1次Post,2次数据流采集,所以,大概到每个数据的时间是间隔20s.
在时间中断里面:
/* USER CODE BEGIN 1 */
// ¶¨Ê±Æ÷µÄÖжϻص÷º¯Êý
int count_flag = 0;
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
//¶¨Ê±Æ÷ËÄÖжϻص÷
if(htim->Instance == TIM4)
{
switch(count_flag)
{
case 0:
Esp8266_toOnenet();
Esp8266_Get_Z3();
count_flag = 1;
break;
case 1:
Esp8266_toOnenet();
Esp8266_Post();
count_flag = 2;
break;
case 2:
Esp8266_toOnenet();
Esp8266_Get_Z4();
count_flag = 0;
break;
}
}
}
/* USER CODE END 1 */
然后,在Esp8266.h里面是指令:
void Esp8266_Post(void)
{
char *temp = calloc(100, sizeof(char));
char *temp2 = calloc(40, sizeof(char));
makePostJson(temp, Online, Temp_z3, Temp_z4, Tec, Warning);
makeContent(temp2, strlen(temp));
HAL_Delay(100);
printf("POST /devices/592808742/datapoints?type=3 HTTP/1.1\r\n");
HAL_Delay(100);
printf("api-key:Jl6WGd3z9Qmms0uGX7uGm=dq8jU=\r\n");
HAL_Delay(100);
printf("Host:api.heclouds.com\r\n");
HAL_Delay(100);
printf("%s\r\n", temp2);
HAL_Delay(100);
printf("\r\n");
HAL_Delay(100);
printf("%s\r\n", temp);
HAL_Delay(2000);
free(temp);
free(temp2);
}
void Esp8266_Get_Z3(void)
{
printf("GET /devices/592808742/datastreams/Set_z3 HTTP/1.1\r\n");
HAL_Delay(100);
printf("api-key:Jl6WGd3z9Qmms0uGX7uGm=dq8jU=\r\n");
HAL_Delay(100);
printf("Host:api.heclouds.com\r\n");
HAL_Delay(100);
printf("\r\n");
HAL_Delay(3000);
}
void Esp8266_Get_Z4(void)
{
printf("GET /devices/592808742/datastreams/Set_z4 HTTP/1.1\r\n");
HAL_Delay(100);
printf("api-key:Jl6WGd3z9Qmms0uGX7uGm=dq8jU=\r\n");
HAL_Delay(100);
printf("Host:api.heclouds.com\r\n");
HAL_Delay(100);
printf("\r\n");
HAL_Delay(3000);
}
const char *const b2s(bool b)
{
return b != 0 ? "true" : "false";
}
void makePostJson(char *temp, bool Online, int Temp_z3, int Temp_z4, bool Tec, bool Warning)
{
sprintf(temp, "{\"Online\":%s,\"Temp_z3\":%d,\"Temp_z4\":%d,\"Tec\":%s,\"Warning\":%s}",
b2s(Online), Temp_z3, Temp_z4, b2s(Tec), b2s(Warning));
}
void makeContent(char *temp2, int strlens)
{
sprintf(temp2, "Content-Length:%d",
strlens);
}
一定要注意分配字符串之后free,free,free
然后是Wifi接收:
/* USER CODE BEGIN 1 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(huart->Instance == USART3)
{
U3_Recieve_Buf[U3_RecPoint++] = U3_RecByte; // ½ÓÊÕµ½µÄ×Ö½Ú±£´æÔÚ½ÓÊÕ»º´æÇø?
if(U3_RecPoint >= U3_RECIEVE_LENGTH) // ·ÀÖ¹»º´æÇøÒç³ö
{
U3_RecPoint = 0;
U3_CheckPoint = 0;
}
HAL_UART_Receive_IT(&huart3, &U3_RecByte, 1); // ½ÓÊÕÍê³ÉÔÙ´ÎÉèÖÃ
//½ÓÊÕ×öÁËÐ޸ģº
//¸Ä³ÉÖ¡Í·£º{"errno":0, 7b 22 65 72 72 6e 6f 22 3a 30 2c
//Ö¡ "succ"} 22 73 75 63 63 22 7d
//ÔòΪһ¸öÍêÕûµÄ½ÓÊÕÖ¡ 7b 22 65 72 72 6e 6f 22 3a 30 2c ... 22 73 75 63 63 22 7dÍÛ£¬ºÃ³¤´ó¸Å18λÀ´È·¶¨£¬ºÃÏñÓе㳤
//ÆäʵÀÁ¶èµÄʱºò»¹ÊÇ¿ÉÒÔËõ¼õµÄ£º
//¸Ä³ÉÖ¡Í·£º{"er 7b 22 65 72
//Ö¡ } 7d
//ÔòÔÚÈ·±£²»´íµÄÇ°ÌáϽÓÊÕµ½µÄÊý¾ÝΪ 7b 22 65 72 ... 7d 5λßõßõßõ
switch(U3_CheckPoint)
{
case 0:
if(U3_RecByte == 0x7b)//7b
{
U3_CheckPoint = 1;
}else{
U3_CheckPoint = 0;
}
break;
case 1:
if(U3_RecByte == 0x22)//22
{
U3_CheckPoint = 2;
}else{
U3_CheckPoint = 0;
}
break;
case 2:
if(U3_RecByte == 0x65)//65
{
U3_CheckPoint = 3;
}else{
U3_CheckPoint = 0;
}
break;
case 3:
if(U3_RecByte == 0x72)//72
{
U3_CheckPoint = 4;
}else{
U3_CheckPoint = 0;
}
break;
case 4:
if(U3_RecByte == 0x7d)//7d }
{
U3_Rec_Success = SUCCESS; // ´®¿ÚÒ»½ÓÊÕÍê³ÉÒ»Ö¡Êý¾Ý
}
break;
case 5:
if(U3_RecByte == 0x63)//63 c
{
U3_CheckPoint = 6;
}else{
U3_CheckPoint = 4;
U3_RecPoint = 4;
}
break;
}
if(U3_CheckPoint == 0)
{
U3_RecPoint = 0;
memset(U3_Recieve_Buf,0, U3_RECIEVE_LENGTH);
}
//ATÖ¸ÁȷÈÏ֡β.....0D 0D 0A 0D 0A 4F 4B 0D 0A(¾ÅλУÑé)
// switch(U3_CheckPoint)
// {
// case 0:
// if(U3_RecByte == 0x0D)//0D
// {
// U3_CheckPoint = 1;
// }else{
// U3_CheckPoint = 0;
// }
// case 1:
// if(U3_RecByte == 0x0D)//0D
// {
// U3_CheckPoint = 2;
// }else{
// U3_CheckPoint = 0;
// }
// case 2:
// if(U3_RecByte == 0x0A)//0A
// {
// U3_CheckPoint = 3;
// }else{
// U3_CheckPoint = 0;
// }
// case 3:
// if(U3_RecByte == 0x0D)//0D
// {
// U3_CheckPoint = 4;
// }else{
// U3_CheckPoint = 0;
// }
// case 4:
// if(U3_RecByte == 0x0A)//0A
// {
// U3_CheckPoint = 5;
// }else{
// U3_CheckPoint = 0;
// }
// case 5:
// if(U3_RecByte == 0x4F)//4F
// {
// U3_CheckPoint = 6;
// }else{
// U3_CheckPoint = 0;
// }
// case 6:
// if(U3_RecByte == 0x4B)//4B
// {
// U3_CheckPoint = 7;
// }else{
// U3_CheckPoint = 0;
// }
// case 7:
// if(U3_RecByte == 0x0D)//0D
// {
// U3_CheckPoint = 8;
// }else{
// U3_CheckPoint = 0;
// }
// case 8:
// if(U3_RecByte == 0x0A)//0A
// {
// U3_Rec_Success = SUCCESS; // ´®¿ÚÒ»½ÓÊÕÍê³ÉÒ»Ö¡Êý¾Ý
// }else{
// U3_CheckPoint = 0;
// }
}
}
//Öض¨Ïòprintf½øÐдòÓ¡
int fputc(int c, FILE *stream)
{
HAL_UART_Transmit(&huart3,(uint8_t *)&c, 1, 1000);
return c;
}
/* USER CODE END 1 */
接收是Json,所以我用了cJSON(包含cJSON.c;cJSON.h两个文件,我没怎么改)
main导包:
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
char *Json = calloc(200, sizeof(char));
if(U3_Rec_Success == SUCCESS) // ´®¿ÚÒ»½ÓÊÕÍê³ÉÒ»Ö¡Êý¾Ý
{
U3_Rec_Success = ERROR;
// HAL_UART_Transmit(&huart1, U3_Recieve_Buf, U3_RecPoint, 3000);
sprintf(Json, "%s",
U3_Recieve_Buf);
U3_RecPoint = 0;
U3_CheckPoint = 0;
memset(U3_Recieve_Buf,0, U3_RECIEVE_LENGTH);
HAL_UART_Receive_IT(&huart3, &U3_RecByte, 1); //µÈ´ý½ÓÊܵÚÒ»¸öÊý¾Ý²¢µÈ´ý½øÈëÖжÏ
}
//¿ÉÒÔJson»ñÈ¡µ½ÁËÊý¾Ý
if(strlen(Json)>30) //Èç¹û»ñÈ¡µ½ÁËÊý¾ÝPost or Get
{
HAL_UART_Transmit(&huart1, Json, strlen(Json), 3000); //´òÓ¡Ò»ÏÂ
LED_ON();
HAL_Delay(1000);
LED_OFF();
HAL_Delay(1000);
LED_ON();
cJSON* root = cJSON_Parse(Json);
cJSON* data = cJSON_GetObjectItem(root, "data");
cJSON* id = cJSON_GetObjectItem(data, "id");
cJSON* current_value = cJSON_GetObjectItem(data, "current_value");
if(strcmp("Set_z3", id->valuestring) == 0)//ÏàµÈ
{
Set_z3 = current_value->valueint;
}
if(strcmp("Set_z4", id->valuestring) == 0)//ÏàµÈ
{
Set_z4 = current_value->valueint;
}
OLED_ShowNum(16,16,Set_z3,2,16);
OLED_ShowNum(32,32,Set_z4,2,16);
OLED_Refresh();
cJSON_Delete(root);
LED_ON();
HAL_Delay(1000);
LED_OFF();
HAL_Delay(1000);
LED_ON();
}
free(Json);
}
/* USER CODE END 3 */
两次开关灯只是好看,但是眼疼
cJsonBUG:
1.把Heap_Size 搞大点,别200,200根本不够
2.不是每一个cJSON* 都要free
这是在Vs搞出来的,发现delete的时候,从上往下,已经free了,所以只cJSON_Delete(root);
3.cJson可以用很复杂,但是注意你接收的长度和复杂性。越简单代表内存占的越小。
4.注意free
5.注意压力测试
很多时候,不注意压力测试会让你,出丑:
[00:13:41.528]收←◆{"errno":0,"data":{"unit":"int ","create_time":"2020-04-21 15:33:11","unit_symbol":"int ","updat
[00:13:41.578]收←◆e_at":"2020-04-30 00:05:20","id":"Set_z3","tags":["mobile"],"cur
[00:13:41.613]收←◆rent_value":16},"error":"succ"}
[00:14:07.149]收←◆{"errno":0,"data":{"unit":"int ","create_time":"2020-04-21 15:33:16","unit_symbol":"int ","update_at":"2020-04-21 15:51:05","id"
[00:14:07.218]收←◆:"Set_z4","tags":["mobile"],"current_value":1},"error":"succ"}
[00:14:17.151]收←◆{"errno":0,"data":{"unit":"int ","create_time":"2020-04-21 15:33:11","unit_symbol":"int ","update_at":"2020-04-30 00:05:20","id"
[00:14:17.218]收←◆:"Set_z3","tags":["mobile"],"current_value":16},"error":"succ"}
[00:14:37.150]收←◆{"errno":0,"data":{"unit":"int ","create_time":"2020-04-21 15:33:16","unit_symbol":"int ","update_at":"2020-04-30 00:14:33","id":"Set_z4","tags":["mobile"],"current_value":23},"error":"succ"}
[00:14:47.149]收←◆{"errno":0,"data":{"unit":"int ","create_time":"2020-04-21 15:33:11","unit_symbol":"int ","update_at":"2020-04-30 00:14:21","id":"Set_z3","tags":["mobile"],"current_value":24},"error":"succ"}
[00:15:07.148]收←◆{"errno":0,"data":{"unit":"int ","create_time":"2020-04-21 15:33:16","unit_symbol":"int ","update_at":"2020-04-30 00:14:34","id":"Set_z4","tags":["mobile"],"current_value":8},"error":"succ"}
[00:15:17.147]收←◆{"errno":0,"data":{"unit":"int ","create_time":"2020-04-21 15:33:11","unit_symbol":"int ","update_at":"2020-04-30 00:14:21","id":"Set_z3","tags":["mobile"],"current_value":24},"error":"succ"}
[00:15:37.145]收←◆{"errno":0,"data":{"unit":"int ","create_time":"2020-04-21 15:33:16","unit_symbol":"int ","update_at":"2020-04-30 00:14:34","id":"Set_z4","tags":["mobile"],"current_value":8},"error":"succ"}
[00:15:47.145]收←◆{"errno":0,"data":{"unit":"int ","create_time":"2020-04-21 15:33:11","unit_symbol":"int ","update_at":"2020-04-30 00:14:21","id":"Set_z3","tags":["mobile"],"current_value":24},"error":"succ"}
[00:16:07.144]收←◆{"errno":0,"data":{"unit":"int ","create_time":"2020-04-21 15:33:16","unit_symbol":"int ","update_at":"2020-04-30 00:14:34","id":"Set_z4","tags":["mobile"],"current_value":8},"error":"succ"}
[00:16:17.143]收←◆{"errno":0,"data":{"unit":"int ","create_time":"2020-04-21 15:33:11","unit_symbol":"int ","update_at":"2020-04-30 00:14:21","id":"Set_z3","tags":["mobile"],"current_value":24},"error":"succ"}
[00:16:37.141]收←◆{"errno":0,"data":{"unit":"int ","create_time":"2020-04-21 15:33:16","unit_symbol":"int ","update_at":"2020-04-30 00:14:34","id":"Set_z4","tags":["mobile"],"current_value":8},"error":"succ"}
[00:16:47.141]收←◆{"errno":0,"data":{"unit":"int ","create_time":"2020-04-21 15:33:11","unit_symbol":"int ","update_at":"2020-04-30 00:14:21","id":"Set_z3","tags":["mobile"],"current_value":24},"error":"succ"}
[00:17:07.140]收←◆{"errno":0,"data":{"unit":"int "
[00:17:07.156]收←◆,"create_time":"2020-04-21 15:33:16","unit_symbol":"int ","update_at":"2020-04-30 00:14:34","id":"Set_z4","tags":["mobile"],"current_value":8},"error":"succ"}
[00:17:17.139]收←◆{"errno":0,"data":{"unit":"int ","create_time":"2020-04-21 15:33
[00:17:17.172]收←◆:11","unit_symbol":"int ","update_at":"2020-04-30 00:14:21","id":"Set_z3","tags":["mobile"],"current_value":24},"error":"succ"}
[00:17:37.138]收←◆{"errno":0,"data":{"unit":"int "
[00:17:37.158]收←◆,"create_time":"2020-04-21 15:33:16","unit_symbol":"int ","update_at":"2020-04-30 00:14:34","id":"Set_z4","tags":["mobile"],"current_value":8},"error":"succ"}
[00:17:47.137]收←◆{"errno":0,"data":{"unit":"int ","create_time":"2020-04-21 15:33:11","unit_symbol":"int ","update_at":"2020-04-30 00:14:21","id"
[00:17:47.204]收←◆:"Set_z3","tags":["mobile"],"current_value":24},"error":"succ"}
[00:18:07.135]收←◆{"errno":0,"data":{"unit":"int ","create_time":"2020-04-21 15:33:16","unit_symbol":"int ","update_at":"2020-04-30 00:14:34","id"
[00:18:07.202]收←◆:"Set_z4","tags":["mobile"],"current_value":8},"error":"succ"}
[00:18:17.135]收←◆{"errno":0,"data":{"unit":"int ","create_time":"2020-04-21 15:33:11","unit_symbol":"int ","update_at":"2020-04-30 00:14:21","id":"Set_z3","tags":["mobile"],"cur
[00:18:17.219]收←◆rent_value":24},"error":"succ"}
[00:18:37.133]收←◆{"errno":0,"data":{"unit":"int ","create_time":"2020-04-21 15:33:16","unit_symbol":"int ","update_at":"2020-04-30 00:14:34","id":"Set_z4","tags":["mobile"],"current_value":8},"error":"succ"}
[00:18:47.133]收←◆{"errno":0,"data":{"unit":"int ","create_time":"2020-04-21 15:33:11","unit_symbol":"int ","update_at":"2020-04-30 00:14:21","id"
[00:18:47.206]收←◆:"Set_z3","tags":["mobile"],"current_value":24},"error":"succ"}
[00:19:07.131]收←◆{"errno":0,"data":{"unit":"int ","create_time":"2020-04-21 15:33:16","unit_symbol":"int ","update_at":"2020-04-30 00:14:34","id":"Set_z4","tags":["mobile"],"current_value":8},"error":"succ"}
[00:19:17.131]收←◆{"errno":0,"data":{"unit":"int ","create_time":"2020-04-21 15:33:11","unit_symbol":"int ","update_at":"2020-04-30 00:14:21","id":"Set_z3","tags":["mobile"],"current_value":24},"error":"succ"}
[00:19:37.129]收←◆{"errno":0,"data":{"unit":"int ","create_time":"2020-04-21 15:33:16","unit_symbol":"int ","update_at":"2020-04-30 00:14:34","id":"Set_z4","tags":["mobile"],"current_value":8},"error":"succ"}
[00:19:47.128]收←◆{"errno":0,"data":{"unit":"int ","create_time":"2020-04-21 15:33:11","unit_symbol":"int ","update_at":"2020-04-30 00:14:21","id":"Set_z3","tags":["mobile"],"current_value":24},"error":"succ"}
[00:20:07.127]收←◆{"errno":0,"data":{"unit":"int ","create_time":"2020-04-21 15:33:16","unit_symbol":"int ","update_at":"2020-04-30 00:14:34","id":"Set_z4","tags":["mobile"],"current_value":8},"error":"succ"}
[00:20:17.127]收←◆{"errno":0,"data":{"unit":"int ","create_time":"2020-04-21 15:33:11","unit_symbol":"int ","update_at":"2020-04-30 00:14:21","id":"Set_z3","tags":["mobile"],"current_value":24},"error":"succ"}
[00:20:37.125]收←◆{"errno":0,"data":{"unit":"int ","create_time":"2020-04-21 15:33:16","unit_symbol":"int ","update_at":"2020-04-30 00:14:34","id":"Set_z4","tags":["mobile"],"current_value":8},"error":"succ"}
[00:20:47.124]收←◆{"errno":0,"data":{"unit":"int ","create_time":"2020-04-21 15:33:11","unit_symbol":"int ","update_at":"2020-04-30 00:14:21","id":"Set_z3","tags":["mobile"],"current_value":24},"error":"succ"}
[00:21:07.123]收←◆{"errno":0,"data":{"unit":"int "
[00:21:07.140]收←◆,"create_time":"2020-04-21 15:33:16","unit_symbol":"int ","update_at":"2020-04-30 00:14:34","id":"Set_z4","tags":["mobile"],"current_value":8},"error":"succ"}
[00:21:17.122]收←◆{"errno":0,"data":{"unit":"int ","create_time":"2020-04-21 15:33:11","unit_symbol":"int ","updat
[00:21:17.172]收←◆e_at":"2020-04-30 00:14:21","id":"Set_z3","tags":["mobile"],"current_value":24},"error":"succ"}
[00:21:37.122]收←◆{"errno":0,"data":{"unit":"int ","create_time":"2020-04-21 15:33:16","unit_symbol":"int ","update_at":"2020-04-30 00:14:34","id"
[00:21:37.188]收←◆:"Set_z4","tags":["mobile"],"current_value":8},"error":"succ"}
[00:21:47.120]收←◆{"errno":0,"data":{"unit":"int ","create_time":"2020-04-21 15:33:11","unit_symbol":"int ","update_at":"2020-04-30 00:14:21","id"
[00:21:47.187]收←◆:"Set_z3","tags":["mobile"],"current_value":24},"error":"succ"}
[00:22:07.119]收←◆{"errno":0,"data":{"unit":"int ","create_time":"2020-04-21 15:33:16","unit_symbol":"int ","update_at":"2020-04-30 00:14:34","id":"Set_z4","tags":["mobile"],"cur
[00:22:07.204]收←◆rent_value":8},"error":"succ"}
[00:22:17.118]收←◆{"errno":0,"data":{"unit":"int "
[00:22:17.152]收←◆,"create_time":"2020-04-21 15:33:11","unit_symbol":"int ","update_at":"2020-04-30 00:14:21","id":"Set_z3","tags":["mobile"],"cur
[00:22:17.203]收←◆rent_value":24},"error":"succ"}
这部分其他交互会比较好办。我暂时先不加。
下面做pyqt5部分。