128天创作纪念日 - wpsshop博客
当前位置:   article > 正文

128天创作纪念日

128天创作纪念日

机缘

起初写博客,就是为了分享自己所学的东西,希望能帮助到别人,在几年前最初学一些技术类的东西时候,很艰难,没有教,又没人带,十分痛苦。后面在网上查阅的资料越来越多,特别是CSDN博客论坛上,有很多人分享的笔记文章影响到了我,所以开始学着他们的步伐,去分享自己的笔记文章,帮助更多的人在初学起步的时候,不会那么艰难。


收获

在创作的过程中,收获了490个粉丝,3万2千多个阅读量,476次点赞和457次收藏,24次评论。


日常

起初写博客文章,也是十分的艰难,没有什么idea,也不知道写什么东西,要怎么样去分享自己掌握的东西。后面因为学习的东西越来越多,发现经常会遗忘,就开始记笔记,一开始在有道云笔记上面写了大概8个月左右的内容,后面就直接到CSDN博客上开始记录笔记并且分享。写博客和学习工作方面的话,并不会冲突,这不仅可以帮助自己梳理知识体系,也可以更近一步的理解掌握这些内容。


成就

 分享一段写的还看得过去的代码

  1. #include "network.h"
  2. //ESP-IDF v5.1
  3. static const char *TAG = "wifi_station";
  4. int tcp_sock = -1;
  5. int udp_sock = -1;
  6. esp_ping_handle_t ping;
  7. int ping_flag = -1;
  8. enum {
  9. PING_IP_OK = 0,
  10. PING_IP_ERR
  11. };
  12. struct sockaddr_in dest_addr;
  13. /**
  14. * @brief 连接WiFi事件处理器---处理WiFi和IP事件
  15. * @param arg:用户定义的参数
  16. * @param event_base:事件基类型
  17. * @param event_id:事件具体ID
  18. * @param event_data:指向事件数据的指针
  19. * @retval void
  20. */
  21. void connect_wifi_ap_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
  22. {
  23. if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
  24. esp_wifi_connect();
  25. } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
  26. esp_wifi_connect();
  27. } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
  28. ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
  29. char addr_str[IP4ADDR_STRLEN_MAX];
  30. ESP_LOGI(TAG, "got ip:%s", esp_ip4addr_ntoa(&event->ip_info.ip, addr_str, IP4ADDR_STRLEN_MAX));
  31. }
  32. }
  33. /**
  34. * @brief 设备连接WiFi AP热点
  35. * @param wifi_ssid:WiFi 热点ID
  36. * @param wifi_passwd:WiFi密码
  37. * @retval void
  38. */
  39. void device_connect_wifi_ap(uint8_t *wifi_ssid, uint8_t *wifi_passwd)
  40. {
  41. esp_err_t ret = nvs_flash_init();
  42. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  43. ESP_ERROR_CHECK(nvs_flash_erase());
  44. ret = nvs_flash_init();
  45. }
  46. ESP_ERROR_CHECK(ret);
  47. ESP_ERROR_CHECK(esp_netif_init());
  48. ESP_ERROR_CHECK(esp_event_loop_create_default());
  49. esp_netif_create_default_wifi_sta();
  50. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  51. ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  52. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &connect_wifi_ap_event_handler, NULL));
  53. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &connect_wifi_ap_event_handler, NULL));
  54. wifi_config_t wifi_config;
  55. memcpy(wifi_config.sta.ssid, wifi_ssid, strlen((char *)wifi_ssid)+1);
  56. memcpy(wifi_config.sta.password, wifi_passwd, strlen((char *)wifi_passwd)+1);
  57. ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
  58. ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
  59. ESP_ERROR_CHECK(esp_wifi_start());
  60. }
  61. /**
  62. * @brief udp客户端初始化
  63. * @param None
  64. * @retval 成功返回0,失败返回-1
  65. */
  66. int udp_client_init(void)
  67. {
  68. char host_ip[] = HOST_IP_ADDR;
  69. int addr_family = 0;
  70. int ip_protocol = 0;
  71. #if defined(CONFIG_EXAMPLE_IPV4)
  72. //struct sockaddr_in dest_addr;
  73. dest_addr.sin_addr.s_addr = inet_addr(host_ip);
  74. dest_addr.sin_family = AF_INET;
  75. dest_addr.sin_port = htons(PORT);
  76. addr_family = AF_INET;
  77. ip_protocol = IPPROTO_IP;
  78. #elif defined(CONFIG_EXAMPLE_IPV6)
  79. struct sockaddr_in6 dest_addr = {0};
  80. inet6_aton(HOST_IP_ADDR, &dest_addr.sin6_addr);
  81. dest_addr.sin6_family = AF_INET6;
  82. dest_addr.sin6_port = htons(PORT);
  83. dest_addr.sin6_scope_id = esp_netif_get_netif_impl_index(EXAMPLE_INTERFACE);
  84. addr_family = AF_INET6;
  85. ip_protocol = IPPROTO_IPV6;
  86. #elif defined(CONFIG_EXAMPLE_SOCKET_IP_INPUT_STDIN)
  87. struct sockaddr_storage dest_addr = {0};
  88. ESP_ERROR_CHECK(get_addr_from_stdin(PORT, SOCK_DGRAM, &ip_protocol, &addr_family, &dest_addr));
  89. #endif
  90. extern int udp_sock;
  91. udp_sock = socket(addr_family, SOCK_DGRAM, ip_protocol);
  92. if (udp_sock < 0)
  93. {
  94. ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);
  95. return -1;
  96. }
  97. ESP_LOGI(TAG, "Socket created, sending to %s:%d", HOST_IP_ADDR, PORT);
  98. return 0;
  99. }
  100. /**
  101. * @brief tcp客户端初始化
  102. * @param None
  103. * @retval 成功返回0,失败返回-1
  104. */
  105. int tcp_client_init(char ip_addr[])
  106. {
  107. //char host_ip[128] = HOST_IP_ADDR;
  108. char host_ip[128] = {0};
  109. memcpy(host_ip, ip_addr, strlen(ip_addr));
  110. int addr_family = 0;
  111. int ip_protocol = 0;
  112. int err = 0;
  113. #if defined(CONFIG_EXAMPLE_IPV4)
  114. struct sockaddr_in dest_addr;
  115. dest_addr.sin_addr.s_addr = inet_addr(host_ip);
  116. dest_addr.sin_family = AF_INET;
  117. dest_addr.sin_port = htons(PORT);
  118. addr_family = AF_INET;
  119. ip_protocol = IPPROTO_IP;
  120. #elif defined(CONFIG_EXAMPLE_IPV6)
  121. struct sockaddr_in6 dest_addr = { 0 };
  122. inet6_aton(host_ip, &dest_addr.sin6_addr);
  123. dest_addr.sin6_family = AF_INET6;
  124. dest_addr.sin6_port = htons(PORT);
  125. dest_addr.sin6_scope_id = esp_netif_get_netif_impl_index(EXAMPLE_INTERFACE);
  126. addr_family = AF_INET6;
  127. ip_protocol = IPPROTO_IPV6;
  128. #elif defined(CONFIG_EXAMPLE_SOCKET_IP_INPUT_STDIN)
  129. struct sockaddr_storage dest_addr = { 0 };
  130. ESP_ERROR_CHECK(get_addr_from_stdin(PORT, SOCK_STREAM, &ip_protocol, &addr_family, &dest_addr));
  131. #endif
  132. extern int tcp_sock;
  133. tcp_sock = socket(addr_family, SOCK_STREAM, ip_protocol);
  134. if (tcp_sock < 0) {
  135. ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);
  136. return -1;
  137. }
  138. ESP_LOGI(TAG, "Socket created, connecting to server");
  139. DPRINTF("Socket created, connecting to %s:%d\n", host_ip, PORT);
  140. err = connect(tcp_sock, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr_in6));
  141. if (err != 0) {
  142. ESP_LOGE(TAG, "Socket unable to connect: errno %d", errno);
  143. return -1;
  144. }
  145. ESP_LOGI(TAG, "Successfully connected");
  146. return 0;
  147. }
  148. /**
  149. * @brief 设备连接服务器
  150. * @param target_ip:目标IP地址
  151. * @retval 成功返回0,失败返回-1
  152. */
  153. int device_connect_to_server(char *target_ip)
  154. {
  155. int sock = 0;
  156. struct sockaddr_in serv_addr;
  157. //#define PORT 5005
  158. DPRINTF("FUNCTION:%s\tLINE:%d\n", __FUNCTION__, __LINE__);
  159. if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  160. DPRINTF("\n Socket creation error \n");
  161. close(sock);
  162. return -1;
  163. }
  164. DPRINTF("FUNCTION:%s\tLINE:%d\n", __FUNCTION__, __LINE__);
  165. memset(&serv_addr, '0', sizeof(serv_addr));
  166. serv_addr.sin_family = AF_INET;
  167. serv_addr.sin_port = htons(PORT);
  168. if(inet_pton(AF_INET, target_ip, &serv_addr.sin_addr)<=0) {
  169. DPRINTF("\nInvalid address/ Address not supported \n");
  170. close(sock);
  171. return -1;
  172. }
  173. DPRINTF("FUNCTION:%s\tLINE:%d\n", __FUNCTION__, __LINE__);
  174. if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
  175. //send(sock, "hello", strlen("hello"), 0);
  176. DPRINTF("\nConnection Failed %s\t%d\n", target_ip, PORT);
  177. close(sock);
  178. return -1;
  179. }
  180. DPRINTF("Connected to %s:%d\n", target_ip, PORT);
  181. //char str[] = "hello server!";
  182. //send(sock, str, strlen(str)+1, 0);
  183. close(sock);
  184. return 0;
  185. }
  186. /**
  187. * @brief TCP扫描同一网段下的目标服务器设备
  188. * @param target_ip:目标IP地址
  189. * @retval 成功返回0, 失败返回-1
  190. */
  191. int device_scan_dest_server(char ip_addr[])
  192. {
  193. char ip_local_addr[128] = {0};
  194. char ip_search_addr[256] = {0};
  195. esp_netif_t *netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
  196. esp_netif_ip_info_t ip_info;
  197. esp_netif_get_ip_info(netif, &ip_info);
  198. memcpy(ip_local_addr, ip4addr_ntoa((ip4_addr_t *)&ip_info.ip), strlen(ip4addr_ntoa((ip4_addr_t *)&ip_info.ip)));
  199. //char *ip4addr_ntoa(const ip4_addr_t *addr);
  200. printf("device ip:%s\n", ip_local_addr);
  201. //192.168.1.8
  202. int i, self_ip_com = 0;
  203. int tok_cnt = 1;
  204. int ret = -1;
  205. for(i = 0; i < 128; i++)
  206. {
  207. if(ip_local_addr[i] == '.')//获取所在网段
  208. {
  209. if(tok_cnt == 3)
  210. {
  211. self_ip_com = ip_local_addr[i+1] - '0';
  212. break;
  213. }
  214. tok_cnt++;
  215. }
  216. //DPRINTF("%c %d\n", ip_local_addr[i], i);
  217. }
  218. DPRINTF("self_ip_com:%d\n", self_ip_com);
  219. DPRINTF("%s\t%d\n", __FUNCTION__,__LINE__);
  220. char ip_addr_temp[128] = {0};
  221. strncpy(ip_addr_temp, ip_local_addr, i+1);
  222. DPRINTF("%s\n", ip_addr_temp);
  223. int com;
  224. for(com = 1; com < 255; com++)
  225. {
  226. if(com == self_ip_com)
  227. continue;
  228. sprintf(ip_search_addr, "%s%d", ip_addr_temp, com);
  229. //DPRINTF("%s\n", ip_search_addr);
  230. DPRINTF("%s\t%d\n", __FUNCTION__,__LINE__);
  231. device_ping_ip_addr(ip_search_addr);//ping传入的ip地址
  232. vTaskDelay(500 / portTICK_PERIOD_MS);
  233. //ping通ip地址后,继续判断是否能够连接端口(5005)
  234. if(ping_flag == PING_IP_OK)
  235. {
  236. esp_ping_stop(ping);
  237. esp_ping_delete_session(ping);
  238. DPRINTF("%s\t%d\n", __FUNCTION__,__LINE__);
  239. ret = device_connect_to_server(ip_search_addr);
  240. if (ret == 0)
  241. {
  242. DPRINTF("Found the target server.\n");
  243. memcpy(ip_addr, ip_search_addr, strlen(ip_search_addr));
  244. break;
  245. }
  246. }else{
  247. DPRINTF("%s\t%d\n", __FUNCTION__,__LINE__);
  248. //vTaskDelay(300 / portTICK_PERIOD_MS);
  249. esp_ping_stop(ping);
  250. esp_ping_delete_session(ping);
  251. }
  252. DPRINTF("%s\t%d\n", __FUNCTION__,__LINE__);
  253. //DPRINTF("try again.\n");
  254. memset(ip_search_addr, 0, sizeof(ip_search_addr));
  255. }
  256. DPRINTF("%s\t%d\n", __FUNCTION__,__LINE__);
  257. printf("com = %d\n", com);
  258. if(com < 255)
  259. {
  260. DPRINTF("Device scan server success.\n");
  261. return 0;
  262. }else{
  263. DPRINTF("Device scan server failed.\n");
  264. return -1;
  265. }
  266. }
  267. /**
  268. * @brief ping ip地址成功的回调函数
  269. * @param handle:ping ip句柄
  270. * @param args:传入给回调函数的参数
  271. * @retval None
  272. */
  273. void ping_ip_success(esp_ping_handle_t handle, void *args)
  274. {
  275. // optionally, get callback arguments
  276. // const char* str = (const char*) args;
  277. // DPRINTF("%s\r\n", str); // "foo"
  278. ping_flag = PING_IP_OK;
  279. uint8_t ttl;
  280. uint16_t seqno;
  281. uint32_t elapsed_time, recv_len;
  282. ip_addr_t target_addr;
  283. esp_ping_get_profile(handle, ESP_PING_PROF_SEQNO, &seqno, sizeof(seqno));
  284. esp_ping_get_profile(handle, ESP_PING_PROF_TTL, &ttl, sizeof(ttl));
  285. esp_ping_get_profile(handle, ESP_PING_PROF_IPADDR, &target_addr, sizeof(target_addr));
  286. esp_ping_get_profile(handle, ESP_PING_PROF_SIZE, &recv_len, sizeof(recv_len));
  287. esp_ping_get_profile(handle, ESP_PING_PROF_TIMEGAP, &elapsed_time, sizeof(elapsed_time));
  288. printf("%ld bytes from %s icmp_seq=%d ttl=%d time=%ld ms\n",
  289. recv_len, inet_ntoa(target_addr.u_addr.ip4), seqno, ttl, elapsed_time);
  290. }
  291. /**
  292. * @brief ping ip地址超时的回调函数
  293. * @param handle:ping ip句柄
  294. * @param args:传入给回调函数的参数
  295. * @retval None
  296. */
  297. void ping_ip_timeout(esp_ping_handle_t handle, void *args)
  298. {
  299. ping_flag = PING_IP_ERR;
  300. uint16_t seqno;
  301. ip_addr_t target_addr;
  302. esp_ping_get_profile(handle, ESP_PING_PROF_SEQNO, &seqno, sizeof(seqno));
  303. esp_ping_get_profile(handle, ESP_PING_PROF_IPADDR, &target_addr, sizeof(target_addr));
  304. printf("From %s icmp_seq=%d timeout\n", inet_ntoa(target_addr.u_addr.ip4), seqno);
  305. }
  306. /**
  307. * @brief ping ip地址结束的回调函数
  308. * @param handle:ping ip句柄
  309. * @param args:传入给回调函数的参数
  310. * @retval None
  311. */
  312. void ping_ip_end(esp_ping_handle_t handle, void *args)
  313. {
  314. uint32_t transmitted;
  315. uint32_t received;
  316. uint32_t total_time_ms;
  317. ping_flag = -1;
  318. esp_ping_get_profile(handle, ESP_PING_PROF_REQUEST, &transmitted, sizeof(transmitted));
  319. esp_ping_get_profile(handle, ESP_PING_PROF_REPLY, &received, sizeof(received));
  320. esp_ping_get_profile(handle, ESP_PING_PROF_DURATION, &total_time_ms, sizeof(total_time_ms));
  321. DPRINTF("%ld packets transmitted, %ld received, time %ldms\n", transmitted, received, total_time_ms);
  322. }
  323. /**
  324. * @brief ping ip地址
  325. * @param ip_str:字符串格式的IP地址 "192.168.1.8"
  326. * @retval None
  327. */
  328. void device_ping_ip_addr(char *ip_str)
  329. {
  330. DPRINTF("%s\t%d\n", __FUNCTION__,__LINE__);
  331. ip_addr_t target_addr;
  332. esp_ping_config_t ping_config = ESP_PING_DEFAULT_CONFIG();
  333. ping_config.interval_ms = 400;
  334. ping_config.timeout_ms = 400;
  335. memset(&target_addr, 0, sizeof(ip_addr_t));
  336. //字符串IP地址转换为ping_addr
  337. if (ipaddr_aton(ip_str, &target_addr)) {
  338. //DPRINTF("转换成功.\n");
  339. ping_config.target_addr = target_addr;
  340. } else {
  341. DPRINTF("Invalid IP address\n");
  342. return ;
  343. }
  344. DPRINTF("%s\t%d\n", __FUNCTION__,__LINE__);
  345. //ping_config.target_addr = target_addr; // target IP address
  346. ping_config.count = ESP_PING_COUNT_INFINITE; // ping in infinite mode, esp_ping_stop can stop it
  347. //设置回调函数
  348. esp_ping_callbacks_t cbs;
  349. cbs.on_ping_success = ping_ip_success;
  350. cbs.on_ping_timeout = ping_ip_timeout;
  351. cbs.on_ping_end = ping_ip_end;
  352. cbs.cb_args = "ping ip"; // arguments that will feed to all callback functions, can be NULL
  353. DPRINTF("%s\t%d\n", __FUNCTION__,__LINE__);
  354. esp_ping_new_session(&ping_config, &cbs, &ping);
  355. DPRINTF("%s\t%d\n", __FUNCTION__,__LINE__);
  356. esp_ping_start(ping);
  357. DPRINTF("%s\t%d\n", __FUNCTION__,__LINE__);
  358. }

憧憬

希望能保持分享,保持创作,让更多人喜欢我写的博客文章,帮助更多人学习或解决遇到的问题。

之后在创作方面,还会继续输出一些比较基础的内容,循序渐进输出一些更高难度的知识点,一是可以帮助自己巩固此前已经掌握的技术栈,二也是让关注我文章的读者,阅读起来不会突然感到吃力,更加的流畅。

Tips

  1. 您发布的文章将会展示至 里程碑专区 ,您也可以在 专区 内查看其他创作者的纪念日文章
  2. 优质的纪念文章将会获得神秘打赏哦
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/690866
推荐阅读
相关标签
  

闽ICP备14008679号