demo.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. #include "freertos/FreeRTOS.h"
  2. #include "freertos/task.h"
  3. #include "esp_err.h"
  4. #include "esp_log.h"
  5. #include "nvs_flash.h"
  6. #include "esp_wifi.h"
  7. #include "esp_event.h"
  8. #include "esp_netif.h"
  9. #include "lwip/sockets.h"
  10. #include "lwip/err.h"
  11. #include "lwip/sockets.h"
  12. #include "lwip/sys.h"
  13. #include <lwip/netdb.h>
  14. #include "esp_camera.h"
  15. #include "esp_http_server.h"
  16. #include "cJSON.h"
  17. #include "esp_log.h"
  18. #include "mqtt_client.h"
  19. #include "sdkconfig.h"
  20. #include "esp_event.h"
  21. #include "esp_netif.h"
  22. // #include "protocol_examples_common.h"
  23. #include "usart.h"
  24. #include "wifi_softap_sta.h"
  25. #include "lwip/sockets.h"
  26. #include "lwip/dns.h"
  27. #include "lwip/netdb.h"
  28. #include "esp_log.h"
  29. #include <stdio.h>
  30. #include "esp_system.h"
  31. #include <string.h>
  32. #include "public.h"
  33. // #include "protocol_examples_common.h"
  34. #define CONFIG_EXAMPLE_IPV4
  35. static const char *TAG = "example";
  36. //在esp_camera.h中定义了CONFIG_OV2640_SUPPORT 用于选择摄像头型号
  37. //#define CONFIG_OV2640_SUPPORT 1
  38. #define CAM_PIN_PWDN -1 //该引脚接地
  39. #define CAM_PIN_RESET -1 //software reset will be performed
  40. #define CAM_PIN_SIOD 26
  41. #define CAM_PIN_SIOC 27
  42. #define CAM_PIN_D7 35
  43. #define CAM_PIN_D6 34
  44. #define CAM_PIN_D5 39
  45. #define CAM_PIN_D4 36
  46. #define CAM_PIN_D3 21
  47. #define CAM_PIN_D2 19
  48. #define CAM_PIN_D1 18
  49. #define CAM_PIN_D0 5
  50. #define CAM_PIN_VSYNC 25
  51. #define CAM_PIN_HREF 23
  52. #define CAM_PIN_PCLK 22
  53. int udp_socket;
  54. struct sockaddr_in remote_addr;//配置UDP远端地址变量
  55. #define JPEG_PORT 8888 //传输图像
  56. #define DATA_PORT 9999 //传输命令数据
  57. #define left_forward_gpio 33 //左轮前进GPIO
  58. #define left_back_gpio 14 //左轮后退GPIO
  59. #define right_forward_gpio 17 //右轮前进GPIO
  60. #define right_back_gpio 16 //右轮后退GPIO
  61. void gpio_init(){
  62. //gpio配置结构体
  63. gpio_config_t io_conf;
  64. //禁止中断
  65. io_conf.intr_type = GPIO_PIN_INTR_DISABLE;
  66. //输出模式
  67. io_conf.mode = GPIO_MODE_OUTPUT;
  68. //配置要设置的引脚
  69. io_conf.pin_bit_mask = (((unsigned long long)1<<left_forward_gpio) | ((unsigned long long)1<<left_back_gpio) | ((unsigned long long)1<<right_forward_gpio) | ((unsigned long long)1<<right_back_gpio));
  70. //禁止下拉
  71. io_conf.pull_down_en = 0;
  72. //禁止上拉
  73. io_conf.pull_up_en = 0;
  74. //配置gpio(不设置上下拉默认输出低电平)
  75. gpio_config(&io_conf);
  76. }
  77. //前进
  78. void car_forward(){
  79. gpio_set_level(left_forward_gpio, 1); gpio_set_level(left_back_gpio, 0); gpio_set_level(right_forward_gpio, 1); gpio_set_level(right_back_gpio, 0);
  80. }
  81. //后退
  82. void car_back(){
  83. gpio_set_level(left_forward_gpio, 0); gpio_set_level(left_back_gpio, 1); gpio_set_level(right_forward_gpio, 0); gpio_set_level(right_back_gpio, 1);
  84. }
  85. //左转
  86. void car_left(){
  87. gpio_set_level(left_forward_gpio, 0); gpio_set_level(left_back_gpio, 0); gpio_set_level(right_forward_gpio, 1); gpio_set_level(right_back_gpio, 0);
  88. }
  89. //右转
  90. void car_right(){
  91. gpio_set_level(left_forward_gpio, 1); gpio_set_level(left_back_gpio, 0); gpio_set_level(right_forward_gpio, 0); gpio_set_level(right_back_gpio, 0);
  92. }
  93. //停止
  94. void car_stop(){
  95. gpio_set_level(left_forward_gpio, 0); gpio_set_level(left_back_gpio, 0); gpio_set_level(right_forward_gpio, 0); gpio_set_level(right_back_gpio, 0);
  96. }
  97. static void udp_server_jpeg_task(void *pvParameters)
  98. {
  99. camera_fb_t *fb = NULL;
  100. esp_err_t res = ESP_OK;
  101. size_t _jpg_buf_len;
  102. uint8_t *_jpg_buf;
  103. char rx_buffer[128];
  104. char addr_str[128];
  105. int addr_family = (int)pvParameters;
  106. int ip_protocol = 0;
  107. struct sockaddr_in6 dest_addr;
  108. while (1) {
  109. if (addr_family == AF_INET) {
  110. struct sockaddr_in *dest_addr_ip4 = (struct sockaddr_in *)&dest_addr;
  111. dest_addr_ip4->sin_addr.s_addr = htonl(INADDR_ANY);
  112. dest_addr_ip4->sin_family = AF_INET;
  113. dest_addr_ip4->sin_port = htons(JPEG_PORT);
  114. ip_protocol = IPPROTO_IP;
  115. } else if (addr_family == AF_INET6) {
  116. bzero(&dest_addr.sin6_addr.un, sizeof(dest_addr.sin6_addr.un));
  117. dest_addr.sin6_family = AF_INET6;
  118. dest_addr.sin6_port = htons(JPEG_PORT);
  119. ip_protocol = IPPROTO_IPV6;
  120. }
  121. int sock = socket(addr_family, SOCK_DGRAM, ip_protocol);
  122. if (sock < 0) {
  123. ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);
  124. break;
  125. }
  126. ESP_LOGI(TAG, "Socket created");
  127. #if defined(CONFIG_EXAMPLE_IPV4) && defined(CONFIG_EXAMPLE_IPV6)
  128. if (addr_family == AF_INET6) {
  129. // Note that by default IPV6 binds to both protocols, it is must be disabled
  130. // if both protocols used at the same time (used in CI)
  131. int opt = 1;
  132. setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
  133. setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &opt, sizeof(opt));
  134. }
  135. #endif
  136. int err = bind(sock, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
  137. if (err < 0) {
  138. ESP_LOGE(TAG, "Socket unable to bind: errno %d", errno);
  139. }
  140. ESP_LOGI(TAG, "Socket bound, port %d", JPEG_PORT);
  141. while (1) {
  142. struct sockaddr_in6 source_addr; // Large enough for both IPv4 or IPv6
  143. socklen_t socklen = sizeof(source_addr);
  144. int len = recvfrom(sock, rx_buffer, sizeof(rx_buffer) - 1, 0, (struct sockaddr *)&source_addr, &socklen);
  145. // Error occurred during receiving
  146. if (len < 0) {
  147. ESP_LOGE(TAG, "recvfrom failed: errno %d", errno);
  148. break;
  149. }
  150. // Data received
  151. else {
  152. // Get the sender's ip address as string
  153. if (source_addr.sin6_family == PF_INET) {
  154. inet_ntoa_r(((struct sockaddr_in *)&source_addr)->sin_addr.s_addr, addr_str, sizeof(addr_str) - 1);
  155. } else if (source_addr.sin6_family == PF_INET6) {
  156. inet6_ntoa_r(source_addr.sin6_addr, addr_str, sizeof(addr_str) - 1);
  157. }
  158. /*检测是否采集完了摄像头图像*/
  159. fb = esp_camera_fb_get();
  160. if (!fb)
  161. {
  162. ESP_LOGI(TAG,"Camera capture failed");
  163. res = ESP_FAIL;
  164. }
  165. else
  166. {
  167. if (fb->format != PIXFORMAT_JPEG)
  168. {
  169. bool jpeg_converted = frame2jpg(fb, 80, &_jpg_buf, &_jpg_buf_len);
  170. if (!jpeg_converted)
  171. {
  172. ESP_LOGI(TAG,"JPEG compression failed");
  173. esp_camera_fb_return(fb);
  174. res = ESP_FAIL;
  175. }
  176. }
  177. else
  178. {
  179. _jpg_buf_len = fb->len;
  180. _jpg_buf = fb->buf;
  181. res = ESP_OK;
  182. }
  183. }
  184. if (res == ESP_OK)
  185. {
  186. if(len==4)
  187. {
  188. if (rx_buffer[0]==0x55 && rx_buffer[1]==0xAA && rx_buffer[2]==0x01 &&rx_buffer[3]==0x02)
  189. {
  190. int err = sendto(sock, _jpg_buf, _jpg_buf_len, 0, (struct sockaddr *)&source_addr, sizeof(source_addr));
  191. if (err < 0) {
  192. ESP_LOGE(TAG, "Error occurred during sending: errno %d", errno);
  193. break;
  194. }
  195. }
  196. }
  197. }
  198. if (fb->format != PIXFORMAT_JPEG)
  199. {
  200. free(_jpg_buf);
  201. }
  202. esp_camera_fb_return(fb);//重新开始采集
  203. }
  204. }
  205. if (sock != -1) {
  206. ESP_LOGE(TAG, "Shutting down socket and restarting...");
  207. shutdown(sock, 0);
  208. close(sock);
  209. }
  210. }
  211. vTaskDelete(NULL);
  212. }
  213. static void udp_server_data_task(void *pvParameters)
  214. {
  215. char rx_buffer[128];
  216. char addr_str[128];
  217. int addr_family = (int)pvParameters;
  218. int ip_protocol = 0;
  219. struct sockaddr_in6 dest_addr;
  220. while (1) {
  221. if (addr_family == AF_INET) {
  222. struct sockaddr_in *dest_addr_ip4 = (struct sockaddr_in *)&dest_addr;
  223. dest_addr_ip4->sin_addr.s_addr = htonl(INADDR_ANY);
  224. dest_addr_ip4->sin_family = AF_INET;
  225. dest_addr_ip4->sin_port = htons(DATA_PORT);
  226. ip_protocol = IPPROTO_IP;
  227. } else if (addr_family == AF_INET6) {
  228. bzero(&dest_addr.sin6_addr.un, sizeof(dest_addr.sin6_addr.un));
  229. dest_addr.sin6_family = AF_INET6;
  230. dest_addr.sin6_port = htons(DATA_PORT);
  231. ip_protocol = IPPROTO_IPV6;
  232. }
  233. int sock = socket(addr_family, SOCK_DGRAM, ip_protocol);
  234. if (sock < 0) {
  235. ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);
  236. break;
  237. }
  238. ESP_LOGI(TAG, "Socket created");
  239. #if defined(CONFIG_EXAMPLE_IPV4) && defined(CONFIG_EXAMPLE_IPV6)
  240. if (addr_family == AF_INET6) {
  241. // Note that by default IPV6 binds to both protocols, it is must be disabled
  242. // if both protocols used at the same time (used in CI)
  243. int opt = 1;
  244. setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
  245. setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &opt, sizeof(opt));
  246. }
  247. #endif
  248. int err = bind(sock, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
  249. if (err < 0) {
  250. ESP_LOGE(TAG, "Socket unable to bind: errno %d", errno);
  251. }
  252. ESP_LOGI(TAG, "Socket bound, port %d", DATA_PORT);
  253. while (1) {
  254. struct sockaddr_in6 source_addr; // Large enough for both IPv4 or IPv6
  255. socklen_t socklen = sizeof(source_addr);
  256. int len = recvfrom(sock, rx_buffer, sizeof(rx_buffer) - 1, 0, (struct sockaddr *)&source_addr, &socklen);
  257. // Error occurred during receiving
  258. if (len < 0) {
  259. ESP_LOGE(TAG, "recvfrom failed: errno %d", errno);
  260. break;
  261. }
  262. // Data received
  263. else {
  264. // Get the sender's ip address as string
  265. if (source_addr.sin6_family == PF_INET) {
  266. inet_ntoa_r(((struct sockaddr_in *)&source_addr)->sin_addr.s_addr, addr_str, sizeof(addr_str) - 1);
  267. } else if (source_addr.sin6_family == PF_INET6) {
  268. inet6_ntoa_r(source_addr.sin6_addr, addr_str, sizeof(addr_str) - 1);
  269. }
  270. if(len==3)
  271. {
  272. if (rx_buffer[0]==0x55 && rx_buffer[1]==0xAA)
  273. {
  274. if (rx_buffer[2]==0x00)
  275. {
  276. car_stop();
  277. }
  278. else if(rx_buffer[2]==0x01)
  279. {
  280. ESP_LOGI(TAG, "↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑");
  281. car_forward();
  282. }
  283. else if(rx_buffer[2]==0x02)
  284. {
  285. ESP_LOGI(TAG, "↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓");
  286. car_back();
  287. }
  288. else if(rx_buffer[2]==0x03)
  289. {
  290. ESP_LOGI(TAG, "←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←");
  291. car_left();
  292. }
  293. else if(rx_buffer[2]==0x04)
  294. {
  295. ESP_LOGI(TAG, "→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→");
  296. car_right();
  297. }
  298. }
  299. }
  300. }
  301. }
  302. if (sock != -1) {
  303. ESP_LOGE(TAG, "Shutting down socket and restarting...");
  304. shutdown(sock, 0);
  305. close(sock);
  306. }
  307. }
  308. vTaskDelete(NULL);
  309. }
  310. uint8_t mqtt_sendCnt=0;
  311. //是否连接服务器
  312. bool isConnectMqttServer = false;
  313. //mqtt
  314. static esp_mqtt_client_handle_t client;
  315. char deviceUUID[17];
  316. char MqttTopicSub[50], MqttTopicPub[50];
  317. #define ali_MQTT_TopicPub "/a1E4l5c9AoW/lh_light/user/update"
  318. #define ali_MQTT_TopicSub "/a1E4l5c9AoW/lh_light/user/get"
  319. #define APP_TAG "APP_MAIN"
  320. /*
  321. * @Description: MQTT服务器的下发消息回调
  322. * @param:
  323. * @return:
  324. */
  325. esp_err_t MqttCloudsCallBack(esp_mqtt_event_handle_t event)
  326. {
  327. int msg_id;
  328. client = event->client;
  329. switch (event->event_id)
  330. {
  331. //连接成功
  332. case MQTT_EVENT_CONNECTED:
  333. ESP_LOGI(APP_TAG, "MQTT_EVENT_CONNECTED");
  334. msg_id = esp_mqtt_client_subscribe(client, ali_MQTT_TopicSub, 1);
  335. ESP_LOGI(APP_TAG, "sent subscribe[%s] successful, msg_id=%d", ali_MQTT_TopicSub, msg_id);
  336. ESP_LOGI(APP_TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size());
  337. //post_data_to_clouds();
  338. isConnectMqttServer = true;
  339. break;
  340. //断开连接回调
  341. case MQTT_EVENT_DISCONNECTED:
  342. ESP_LOGI(APP_TAG, "MQTT_EVENT_DISCONNECTED");
  343. isConnectMqttServer = false;
  344. break;
  345. //订阅成功
  346. case MQTT_EVENT_SUBSCRIBED:
  347. ESP_LOGI(APP_TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id);
  348. break;
  349. //订阅失败
  350. case MQTT_EVENT_UNSUBSCRIBED:
  351. ESP_LOGI(APP_TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id);
  352. break;
  353. //推送发布消息成功
  354. case MQTT_EVENT_PUBLISHED:
  355. ESP_LOGI(APP_TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id);
  356. break;
  357. //服务器下发消息到本地成功接收回调
  358. case MQTT_EVENT_DATA:
  359. {
  360. printf("TOPIC=%.*s \r\n", event->topic_len, event->topic);
  361. printf("DATA=%.*s \r\n\r\n", event->data_len, event->data);
  362. break;
  363. }
  364. default:
  365. break;
  366. }
  367. return ESP_OK;
  368. }
  369. //ProductSecret:YAJUJnp2mSt3Ja6P
  370. //ProductKey:a1E4l5c9AoW
  371. //DeviceName:lh_light
  372. #define ProductKey "a1E4l5c9AoW"
  373. #define DeviceName "lh_light"
  374. #define ProductSecret "092ea6988775cb980f5cb2f45c19dd22"
  375. // {
  376. // "ProductKey": "a1E4l5c9AoW",
  377. // "DeviceName": "lh_light",
  378. // "DeviceSecret": "092ea6988775cb980f5cb2f45c19dd22"
  379. // }
  380. esp_err_t ret;
  381. uint16_t step=0;
  382. uint16_t getListCnt=0;
  383. // uint8_t pcWriteBuffer[1024]={0};
  384. char RegisterOpte[128]={0};
  385. char MqttClientID[512]={0};
  386. char MqttName[128]={0};
  387. char MainBuff[512]={0};
  388. char MqttPass[128]={0};
  389. uint32_t Random;
  390. uint32_t len=0;
  391. uint8_t wifi_net=0;
  392. void app_main()
  393. {
  394. //初始化 NVS(配置WiFi的参数存储需要用到NVS)
  395. esp_err_t err = nvs_flash_init();
  396. if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  397. ESP_ERROR_CHECK(nvs_flash_erase());
  398. err = nvs_flash_init();
  399. }
  400. ESP_ERROR_CHECK(err);
  401. //初始化内部的lwip
  402. ESP_ERROR_CHECK(esp_netif_init());
  403. //创建系统事件任务
  404. ESP_ERROR_CHECK(esp_event_loop_create_default());
  405. camera_config_t camera_config = {
  406. .ledc_timer = LEDC_TIMER_0,
  407. .ledc_channel = LEDC_CHANNEL_0,
  408. .pin_d7 = CAM_PIN_D7,
  409. .pin_d6 = CAM_PIN_D6,
  410. .pin_d5 = CAM_PIN_D5,
  411. .pin_d4 = CAM_PIN_D4,
  412. .pin_d3 = CAM_PIN_D3,
  413. .pin_d2 = CAM_PIN_D2,
  414. .pin_d1 = CAM_PIN_D1,
  415. .pin_d0 = CAM_PIN_D0,
  416. .pin_pclk = CAM_PIN_PCLK,
  417. .pin_vsync = CAM_PIN_VSYNC,
  418. .pin_href = CAM_PIN_HREF,
  419. .pin_sscb_sda = CAM_PIN_SIOD,
  420. .pin_sscb_scl = CAM_PIN_SIOC,
  421. //.pin_pwdn = CAM_PIN_PWDN,//硬件上没有该引脚
  422. //.pin_reset = CAM_PIN_RESET, //硬件上没有该引脚
  423. //.pin_xclk = CAM_PIN_XCLK,//硬件上没有该引脚
  424. //XCLK 20MHz or 10MHz for OV2640 double FPS (Experimental)
  425. .xclk_freq_hz = 20000000,
  426. .pixel_format = PIXFORMAT_JPEG, //YUV422,GRAYSCALE,RGB565,JPEG
  427. .frame_size = FRAMESIZE_QVGA,
  428. .jpeg_quality = 15, //0-63 lower number means higher quality
  429. .fb_count = 2 //if more than one, i2s runs in continuous mode. Use only with JPEG
  430. };
  431. //摄像头初始化
  432. //gpio_install_isr_service(ESP_INTR_FLAG_LEVEL1); //设置中断优先级最低
  433. // err = esp_camera_init(&camera_config);
  434. // if (err != ESP_OK)
  435. // {
  436. // printf("Camera init failed with error 0x%x\r\n", err);
  437. // return;
  438. // }
  439. setvbuf(stdout, NULL, _IONBF, 0);
  440. gpio_init();
  441. wifi_init_softap();//初始化模组连接的路由器
  442. //获取mac地址(station模式)
  443. uint8_t mac[6];
  444. esp_read_mac(mac, ESP_MAC_WIFI_STA);
  445. sprintf(deviceUUID, "%02x%02x%02x%02x%02x%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  446. //组建MQTT订阅的主题
  447. sprintf(MqttTopicSub, "/esp32-c3/%s/devSub", deviceUUID);
  448. //组建MQTT推送的主题
  449. sprintf(MqttTopicPub, "/esp32-c3/%s/devPub", deviceUUID);
  450. ESP_LOGI(APP_TAG, "deviceUUID: %s", deviceUUID);
  451. ESP_LOGI(APP_TAG, "MqttTopicSub: %s", MqttTopicSub);
  452. ESP_LOGI(APP_TAG, "MqttTopicPub: %s", MqttTopicPub);
  453. // ESP_ERROR_CHECK(esp_netif_init());
  454. // ESP_ERROR_CHECK(esp_event_loop_create_default());
  455. // /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
  456. // * Read "Establishing Wi-Fi or Ethernet Connection" section in
  457. // * examples/protocols/README.md for more information about this function.
  458. // */
  459. // ESP_ERROR_CHECK(example_connect());
  460. //连接的配置参数
  461. // esp_mqtt_client_config_t mqtt_cfg = {
  462. // .host = "www.xiaohaodianzi.cn", //连接的域名 ,请务必修改为您的
  463. // .port = 1883, //端口,请务必修改为您的
  464. // .username = "xhdz", //用户名,请务必修改为您的
  465. // .password = "1520747634", //密码,请务必修改为您的
  466. // .client_id = deviceUUID,
  467. // .event_handle = MqttCloudsCallBack, //设置回调函数
  468. // .keepalive = 120, //心跳
  469. // .disable_auto_reconnect = true, //开启自动重连
  470. // .disable_clean_session = false, //开启 清除会话
  471. // };
  472. printf("free_heap_size = %d\n", esp_get_free_heap_size());
  473. // Random=esp_timer_get_time()%1024;
  474. // sprintf(RegisterOpte,"|securemode=2,authType=regnwl,random=%d,signmethod=hmacsha1|",Random);
  475. // printf("\r\nRegisterOpte:%s\r\n",RegisterOpte);
  476. // memset(MqttClientID,0,sizeof(MqttClientID));
  477. // sprintf(MqttClientID,"%s.%s%s",(char*)ProductKey,(char*)DeviceName,RegisterOpte);
  478. // printf("\r\nMqttClientID:%s\r\n",MqttClientID);
  479. // sprintf(MqttName,"%s&%s",(char*)DeviceName,(char*)ProductKey);
  480. // sprintf(MainBuff,"deviceName%sproductKey%srandom%d",DeviceName,ProductKey,Random);
  481. // printf("\r\nMainBuff:%s\r\n",MainBuff);
  482. // utils_hmac_sha1(MainBuff,strlen(MainBuff),MqttPass,ProductSecret,strlen(ProductSecret));
  483. // printf("\r\nMqttPass:%s\r\n",MqttPass);
  484. // esp_mqtt_client_config_t mqtt_cfg = {
  485. // .host = "a1E4l5c9AoW.iot-as-mqtt.cn-shanghai.aliyuncs.com", //连接的域名 ,请务必修改为您的
  486. // //.uri = "a1E4l5c9AoW.iot-as-mqtt.cn-shanghai.aliyuncs.com:1883",
  487. // .port = 1883, //端口,请务必
  488. // .username = MqttName, //用户名,请务必
  489. // .password = MqttPass, //密码,请务必
  490. // .client_id = MqttClientID,
  491. // .event_handle = MqttCloudsCallBack, //设置回调函数
  492. // .keepalive = 120, //心跳
  493. // .disable_auto_reconnect = false, //开启自动重连
  494. // .disable_clean_session = false, //开启 清除会话
  495. // };
  496. esp_mqtt_client_config_t mqtt_cfg = {
  497. .host = "a1E4l5c9AoW.iot-as-mqtt.cn-shanghai.aliyuncs.com", //连接的域名 ,请务必修改为您的
  498. //.uri = "a1E4l5c9AoW.iot-as-mqtt.cn-shanghai.aliyuncs.com:1883",
  499. .port = 1883, //端口,请务必修改为您的
  500. .username = "lh_light&a1E4l5c9AoW", //用户名,请务必修改为您的
  501. .password = "810639EB23072D05F9213FD636BD2FF311202F42", //密码,请务必修改为您的
  502. .client_id = "esp32_test|securemode=3,signmethod=hmacsha1,timestamp=120|",
  503. .event_handle = MqttCloudsCallBack, //设置回调函数
  504. .keepalive = 120, //心跳
  505. .disable_auto_reconnect = false, //开启自动重连
  506. .disable_clean_session = false, //开启 清除会话
  507. };
  508. #ifdef CONFIG_EXAMPLE_IPV4
  509. xTaskCreate(udp_server_jpeg_task, "udp_server_jpeg_task", 4096, (void*)AF_INET, 5, NULL);
  510. #endif
  511. #ifdef CONFIG_EXAMPLE_IPV6
  512. xTaskCreate(udp_server_jpeg_task, "udp_server_jpeg_task", 4096, (void*)AF_INET6, 5, NULL);
  513. #endif
  514. #ifdef CONFIG_EXAMPLE_IPV4
  515. xTaskCreate(udp_server_data_task, "udp_server_data_task", 4096, (void*)AF_INET, 6, NULL);
  516. #endif
  517. #ifdef CONFIG_EXAMPLE_IPV6
  518. xTaskCreate(udp_server_data_task, "udp_server_data_task", 4096, (void*)AF_INET6, 6, NULL);
  519. #endif
  520. while (1)
  521. {
  522. // printf("hello camera!\r\n");
  523. vTaskDelay(100);
  524. if(wifi_net)
  525. {
  526. wifi_net=0;
  527. client = esp_mqtt_client_init(&mqtt_cfg);
  528. esp_mqtt_client_start(client);
  529. }
  530. // ag_timer_sched();
  531. // vTaskDelay(1000 / portTICK_PERIOD_MS);
  532. // /* Blink off (output low) */
  533. if(++step>10)
  534. {
  535. step=0;
  536. if(isConnectMqttServer)
  537. {
  538. esp_mqtt_client_publish(client,ali_MQTT_TopicPub,"hello_world\r\n",strlen("hello_world\r\n"),0,00);
  539. ESP_LOGI(APP_TAG,"MQTT_PUB:hello_world\r\n");
  540. }
  541. }
  542. }
  543. }