You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

221 lines
5.7 KiB

5 years ago
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "freertos/FreeRTOS.h"
  4. #include "freertos/task.h"
  5. #include "freertos/event_groups.h"
  6. #include "esp_system.h"
  7. #include "esp_spi_flash.h"
  8. #include "u8g2_esp32_hal.h"
  9. #include "esp_wifi.h"
  10. #include "esp_event_loop.h"
  11. #include "esp_log.h"
  12. #include "nvs_flash.h"
  13. #include "lwip/netdb.h"
  14. #include "lwip/sockets.h"
  15. #include "config.h"
  16. #define PIN_SDA 5
  17. #define PIN_SCL 4
  18. static u8g2_t u8g2;
  19. // Event group
  20. static EventGroupHandle_t wifi_event_group;
  21. const int CONNECTED_BIT = BIT0;
  22. static void print_screen(char *s1, char *s2, char *s3);
  23. // HTTP request
  24. static const char *REQUEST = "GET "CONFIG_PATH" HTTP/1.1\n"
  25. "Host: "CONFIG_URL"\n"
  26. "User-Agent: ESP32\n"
  27. "\n";
  28. static void print_screen(char *s1, char *s2, char *s3)
  29. {
  30. u8g2_ClearBuffer(&u8g2);
  31. u8g2_DrawStr(&u8g2, 2,17,s1);
  32. u8g2_DrawStr(&u8g2, 2,37,s2);
  33. u8g2_DrawStr(&u8g2, 2,57,s3);
  34. u8g2_SendBuffer(&u8g2);
  35. vTaskDelay(300 / portTICK_RATE_MS);
  36. }
  37. // Wifi event handler
  38. static esp_err_t event_handler(void *ctx, system_event_t *event)
  39. {
  40. switch(event->event_id) {
  41. case SYSTEM_EVENT_STA_START:
  42. esp_wifi_connect();
  43. break;
  44. case SYSTEM_EVENT_STA_GOT_IP:
  45. xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
  46. break;
  47. case SYSTEM_EVENT_STA_DISCONNECTED:
  48. xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
  49. break;
  50. default:
  51. break;
  52. }
  53. return ESP_OK;
  54. }
  55. // Main task
  56. void main_task(void *pvParameter)
  57. {
  58. // wait for connection
  59. xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY);
  60. printf("connected!\n");
  61. print_screen("wifi connected", "", "");
  62. printf("\n");
  63. // print the local IP address
  64. tcpip_adapter_ip_info_t ip_info;
  65. ESP_ERROR_CHECK(tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info));
  66. printf("IP Address: %s\n", ip4addr_ntoa(&ip_info.ip));
  67. printf("Subnet mask: %s\n", ip4addr_ntoa(&ip_info.netmask));
  68. printf("Gateway: %s\n", ip4addr_ntoa(&ip_info.gw));
  69. printf("\n");
  70. print_screen("IP Address:", ip4addr_ntoa(&ip_info.ip), "");
  71. // define connection parameters
  72. const struct addrinfo hints = {
  73. .ai_family = AF_INET,
  74. .ai_socktype = SOCK_STREAM,
  75. };
  76. // address info struct and receive buffer
  77. struct addrinfo *res;
  78. char recv_buf[100];
  79. // resolve the IP of the target website
  80. int result = getaddrinfo(CONFIG_URL, CONFIG_PORT, &hints, &res);
  81. if((result != 0) || (res == NULL)) {
  82. printf("Unable to resolve IP for target website %s\n", CONFIG_URL);
  83. while(1) vTaskDelay(1000 / portTICK_RATE_MS);
  84. }
  85. printf("Target website's IP resolved\n");
  86. // create a new socket
  87. int s = socket(res->ai_family, res->ai_socktype, 0);
  88. if(s < 0) {
  89. printf("Unable to allocate a new socket\n");
  90. while(1) vTaskDelay(1000 / portTICK_RATE_MS);
  91. }
  92. printf("Socket allocated, id=%d\n", s);
  93. // connect to the specified server
  94. result = connect(s, res->ai_addr, res->ai_addrlen);
  95. if(result != 0) {
  96. printf("Unable to connect to the target website\n");
  97. close(s);
  98. while(1) vTaskDelay(1000 / portTICK_RATE_MS);
  99. }
  100. printf("Connected to the target website\n");
  101. print_screen("Connected to the", "target website","");
  102. // send the request
  103. result = write(s, REQUEST, strlen(REQUEST));
  104. if(result < 0) {
  105. printf("Unable to send the HTTP request\n");
  106. close(s);
  107. while(1) vTaskDelay(1000 / portTICK_RATE_MS);
  108. }
  109. printf("HTTP request sent\n");
  110. print_screen("http request", "", "");
  111. // print the response
  112. printf("HTTP response:\n");
  113. printf("--------------------------------------------------------------------------------\n");
  114. int r;
  115. do {
  116. bzero(recv_buf, sizeof(recv_buf));
  117. r = read(s, recv_buf, sizeof(recv_buf) - 1);
  118. for(int i = 0; i < r; i++) {
  119. putchar(recv_buf[i]);
  120. }
  121. } while(r > 0);
  122. printf("--------------------------------------------------------------------------------\n");
  123. close(s);
  124. printf("Socket closed\n");
  125. while(1) {
  126. vTaskDelay(5000 / portTICK_RATE_MS);
  127. }
  128. }
  129. void app_main() {
  130. // initialize the u8g2 hal
  131. u8g2_esp32_hal_t u8g2_esp32_hal = U8G2_ESP32_HAL_DEFAULT;
  132. u8g2_esp32_hal.sda = PIN_SDA;
  133. u8g2_esp32_hal.scl = PIN_SCL;
  134. u8g2_esp32_hal_init(u8g2_esp32_hal);
  135. // initialize the u8g2 library
  136. u8g2_Setup_ssd1306_i2c_128x64_noname_f(
  137. &u8g2,
  138. U8G2_R0,
  139. u8g2_esp32_i2c_byte_cb,
  140. u8g2_esp32_gpio_and_delay_cb);
  141. // set the display address
  142. u8x8_SetI2CAddress(&u8g2.u8x8, 0x78);
  143. // initialize the display
  144. u8g2_InitDisplay(&u8g2);
  145. // wake up the display
  146. u8g2_SetPowerSave(&u8g2, 0);
  147. // set font
  148. u8g2_SetFont(&u8g2, u8g2_font_timR14_tf);
  149. print_screen("screen ready", "", "");
  150. // initialize NVS
  151. ESP_ERROR_CHECK(nvs_flash_init());
  152. // -- WIFI --
  153. // disable the default wifi logging
  154. esp_log_level_set("wifi", ESP_LOG_NONE);
  155. // create the event group to handle wifi events
  156. wifi_event_group = xEventGroupCreate();
  157. // initialize the tcp stack
  158. tcpip_adapter_init();
  159. // initialize the wifi event handler
  160. ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
  161. // initialize the wifi stack in STAtion mode with config in RAM
  162. wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT();
  163. ESP_ERROR_CHECK(esp_wifi_init(&wifi_init_config));
  164. ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
  165. ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
  166. // configure the wifi connection and start the interface
  167. wifi_config_t wifi_config = {
  168. .sta = {
  169. .ssid = CONFIG_WIFI_SSID,
  170. .password = CONFIG_WIFI_PASS,
  171. },
  172. };
  173. ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
  174. ESP_ERROR_CHECK(esp_wifi_start());
  175. /* char buffer[50]; */
  176. /* snprintf(buffer, sizeof(buffer), "Connecting to\n%s... ", CONFIG_WIFI_SSID); */
  177. /* print_screen(buffer, "", ""); */
  178. print_screen("Connectiong to:", CONFIG_WIFI_SSID, "");
  179. // start the main task
  180. xTaskCreate(&main_task, "main_task", 2048, NULL, 5, NULL);
  181. }