WIFI_handle.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #include "main.h"
  2. //#include "WIFI_config.h"
  3. //#include "WIFI_func.h"
  4. //#include "cJSON.h"
  5. static uint8_t weatherCJSON_handle(char s[], WIFIWeatherStruct *WIFIWeather1);
  6. static uint8_t timeCJSONToString(char s[], uint8_t *timeBuff1, uint8_t hexOrDec1);
  7. static void timeJsonTotimeInt(char *str1, uint8_t *timeBuff2, uint8_t hexOrDec2);
  8. static int stringToInt(char *str);
  9. /**
  10. * @brief WIFI初始化
  11. * @param None
  12. * @retval None
  13. * @note None
  14. */
  15. void WIFI_Init(void)
  16. {
  17. mem_init();
  18. WIFI_USART_Init(115200);
  19. WIFI_TIM3_Init();
  20. }
  21. /**
  22. * @brief WIFI联网
  23. * @param None
  24. * @retval None
  25. * @note None
  26. */
  27. void WIFI_Connect(void)
  28. {
  29. WIFI_RESET_GPIO_Congfig();
  30. ESP8266_clear();
  31. ESP8266_quit_trans(); // 关闭透传与TCP模式
  32. Connect_wifi();
  33. }
  34. /**
  35. * @brief WIFI联网获取实时时间数据
  36. * @param timeBuff: 时间存放地址
  37. * @param hexOrDec: 转化为十六进制表示还是十进制表示 输入16或10
  38. * @retval 0: 解析成功
  39. * @note None
  40. */
  41. uint8_t WIFI_RealTime_handle(uint8_t *timeBuff, uint8_t hexOrDec)
  42. {
  43. uint8_t result = 1;
  44. Connect_time_port();
  45. result = timeCJSONToString(WIFI_USART_RX_BUF, timeBuff, hexOrDec);
  46. ESP8266_quit_trans(); // 必须放在解析数据之后
  47. return result;
  48. }
  49. /**
  50. * @brief WIFI联网获取实时天气数据
  51. * @param None
  52. * @retval None
  53. * @note None
  54. */
  55. uint8_t WIFI_realWeather_handle(WIFIWeatherStruct *WIFIWeather1)
  56. {
  57. uint8_t result = 1;
  58. Connect_wea_port();
  59. result = weatherCJSON_handle(WIFI_USART_RX_BUF, WIFIWeather1);
  60. ESP8266_quit_trans(); // 必须放在解析数据之后
  61. return result;
  62. }
  63. /**
  64. * @brief WIFI天气CJSON数据处理
  65. * @param None
  66. * @retval None
  67. * @note 将CJSON数据转换为可用数据格式
  68. */
  69. static uint8_t weatherCJSON_handle(char s[], WIFIWeatherStruct *WIFIWeather2)
  70. {
  71. uint8_t result = 1;
  72. char t[] ="[";
  73. char r[] ="]";
  74. cJSON *res,*location, *tq ,*qw, *name, *now;
  75. cJSON *cjson;
  76. Delete_char(s,t[0]);
  77. Delete_char(s,r[0]);
  78. cjson = cJSON_Parse(s);
  79. if(cjson)
  80. {
  81. res = cJSON_GetObjectItem(cjson, "results");
  82. location = cJSON_GetObjectItem(res, "location");
  83. name = cJSON_GetObjectItem(location, "name");
  84. now = cJSON_GetObjectItem(res, "now");
  85. tq = cJSON_GetObjectItem(now, "code");
  86. qw = cJSON_GetObjectItem(now, "temperature");
  87. WIFIWeather2->weatherCode = stringToInt(tq->valuestring);
  88. WIFIWeather2->weatherTemp[0] = *(qw->valuestring);
  89. WIFIWeather2->weatherTemp[1] = *((qw->valuestring) + 1);
  90. WIFIWeather2->weatherTemp[2] = '\0';
  91. if(WIFIWeather2->weatherTemp[0] < '0') WIFIWeather2->tempNum = 0;
  92. else WIFIWeather2->tempNum = stringToInt(qw->valuestring);
  93. result = 0;
  94. #ifdef DEBUG_printf
  95. printf("cityName is %s\n",name->valuestring);
  96. printf("tianqi is %s\n",tq->valuestring);
  97. printf("tem is %s\n",qw->valuestring);
  98. #endif
  99. }
  100. cJSON_Delete(cjson);
  101. myfree(s);
  102. return result;
  103. }
  104. /**
  105. * @brief 时间json转string
  106. * @param s[]:json数据
  107. * @param timeBuff1: 时间存放地址
  108. * @param hexOrDec1: 转化为十六进制表示还是十进制表示 输入16或10
  109. * @retval 0: 解析成功
  110. * @note None
  111. */
  112. static uint8_t timeCJSONToString(char s[], uint8_t *timeBuff1, uint8_t hexOrDec1)
  113. {
  114. uint8_t result = 1;
  115. cJSON *res, *time, *week;
  116. cJSON *cjson;
  117. cjson = cJSON_Parse(s);
  118. if(cjson)
  119. {
  120. res = cJSON_GetObjectItem(cjson, "result");
  121. time = cJSON_GetObjectItem(res, "datetime_1");
  122. week = cJSON_GetObjectItem(res, "week_1");
  123. if(stringToInt(week->valuestring) == 0) {
  124. WIFITimeData[7] = 0x07;
  125. } else {
  126. WIFITimeData[7] = stringToInt(week->valuestring);
  127. }
  128. // 解析为int方便初始化DS1302
  129. timeJsonTotimeInt(time->valuestring, timeBuff1, hexOrDec1);
  130. result = 0;
  131. #ifdef DEBUG_printf
  132. printf("WIFI time: datetime is %s; week is %s\n", time->valuestring, week->valuestring);
  133. #endif
  134. }
  135. cJSON_Delete(cjson);
  136. myfree(s);
  137. return result;
  138. }
  139. /**
  140. * @brief 解析json数据中的time
  141. * @param str1:字符串地址
  142. * @param timeBuff2: 时间存放地址
  143. * @param hexOrDec2: 转化为十六进制表示还是十进制表示 输入16或10
  144. * @retval None
  145. * @note None
  146. */
  147. static void timeJsonTotimeInt(char *str1, uint8_t *timeBuff2, uint8_t hexOrDec2)
  148. {
  149. char *str = str1;
  150. char temp[5];
  151. char i = 0,j = 0;
  152. int timeTemp = 0;
  153. memset(temp, 0, sizeof(temp)/sizeof(temp[0]));
  154. for(j = 0; j < 6; j ++) {
  155. while(*str != '-' && *str != ' ' && *str != ':') {
  156. if(*str == '\0') break;
  157. temp[i] = *str;
  158. i++;
  159. str++;
  160. }
  161. timeTemp = stringToInt(temp);
  162. switch(j) {
  163. case 0:
  164. timeBuff2[0] = timeTemp/100/10*hexOrDec2 + timeTemp/100%10;
  165. timeBuff2[1] = timeTemp%100/10*hexOrDec2 + timeTemp%100%10;
  166. break;
  167. case 1:
  168. timeBuff2[2] = timeTemp/10*hexOrDec2 + timeTemp%10;
  169. break;
  170. case 2:
  171. timeBuff2[3] = timeTemp/10*hexOrDec2 + timeTemp%10;
  172. break;
  173. case 3:
  174. timeBuff2[4] = timeTemp/10*hexOrDec2 + timeTemp%10;
  175. break;
  176. case 4:
  177. timeBuff2[5] = timeTemp/10*hexOrDec2 + timeTemp%10;
  178. break;
  179. case 5:
  180. timeBuff2[6] = timeTemp/10*hexOrDec2 + timeTemp%10 + 2; // 加2s补时
  181. break;
  182. }
  183. memset(temp, 0, sizeof(temp)/sizeof(temp[0]));
  184. i = 0;
  185. str++;
  186. }
  187. #ifdef DEBUG_printf
  188. if(hexOrDec2 == 16)
  189. {
  190. printf("timeBuff: %x%x-%x-%x %x:%x:%x %x\n", timeBuff2[0], timeBuff2[1], timeBuff2[2], timeBuff2[3], timeBuff2[4], timeBuff2[5], timeBuff2[6], timeBuff2[7]);
  191. }
  192. else if(hexOrDec2 == 10)
  193. {
  194. printf("timeBuff: %d%d-%d-%d %d:%d:%d %d\n", timeBuff2[0], timeBuff2[1], timeBuff2[2], timeBuff2[3], timeBuff2[4], timeBuff2[5], timeBuff2[6], timeBuff2[7]);
  195. }
  196. #endif
  197. }
  198. /**
  199. * @brief string转换int
  200. * @param *str:字符串地址
  201. * @retval 返回int型
  202. * @note None
  203. */
  204. static int stringToInt(char *str)
  205. {
  206. char *p = str;
  207. int nNUM = 0;
  208. while (*p != '\0')
  209. {
  210. nNUM = nNUM * 10 + (*p - '0');
  211. p++;
  212. }
  213. return nNUM;
  214. }
  215. /**
  216. * @brief 根据天气代码显示天气情况
  217. * @param temp:天气代码
  218. * @retval None
  219. * @note None
  220. */
  221. void tianqiDisplay(uint8_t weatherCode)
  222. {
  223. uint8_t x1 = 48;
  224. uint8_t x2 = 64;
  225. uint8_t y = 4;
  226. if(weatherCode <= 3) {
  227. OLED_ShowChinese(x1,y,27); // 晴
  228. OLED_ShowString(x2,y," ",16);
  229. } else if(weatherCode <= 8) {
  230. OLED_ShowChinese(x1,y,28); // 多云
  231. OLED_ShowChinese(x2,y,29);
  232. } else if(weatherCode == 9) {
  233. OLED_ShowChinese(x1,y,30); // 阴
  234. OLED_ShowString(x2,y," ",16);
  235. } else if(weatherCode == 10) {
  236. OLED_ShowChinese(x1,y,36); // 阵雨
  237. OLED_ShowChinese(x2,y,31);
  238. } else if(weatherCode <= 12) {
  239. OLED_ShowChinese(x1,y,35); // 雷雨
  240. OLED_ShowChinese(x2,y,31);
  241. } else if(weatherCode == 13) {
  242. OLED_ShowChinese(x1,y,34); // 小雨
  243. OLED_ShowChinese(x2,y,31);
  244. } else if(weatherCode == 14) {
  245. OLED_ShowChinese(x1,y,33); // 中雨
  246. OLED_ShowChinese(x2,y,31);
  247. } else if(weatherCode <= 19) {
  248. OLED_ShowChinese(x1,y,32); // 大雨
  249. OLED_ShowChinese(x2,y,31);
  250. } else if(weatherCode <= 21) {
  251. OLED_ShowChinese(x1,y,36); // 阵雪
  252. OLED_ShowChinese(x2,y,39);
  253. } else if(weatherCode == 22) {
  254. OLED_ShowChinese(x1,y,34); // 小雪
  255. OLED_ShowChinese(x2,y,39);
  256. } else if(weatherCode == 23) {
  257. OLED_ShowChinese(x1,y,33); // 中雪
  258. OLED_ShowChinese(x2,y,39);
  259. } else if(weatherCode <= 25) {
  260. OLED_ShowChinese(x1,y,32); // 大雪
  261. OLED_ShowChinese(x2,y,39);
  262. } else if(weatherCode == 30) {
  263. OLED_ShowChinese(x1,y,37); // 雾
  264. OLED_ShowString(x2,y," ",16);
  265. } else if(weatherCode == 31) {
  266. OLED_ShowChinese(x1,y,38); // 霾
  267. OLED_ShowString(x2,y," ",16);
  268. } else {
  269. OLED_ShowString(x1,y,"N/A ",16); // N/A
  270. }
  271. }