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