Prototype working, please review code
-
Hello all!
I just got my M5StickCPlus and made a little program to control the screen via a web server. Everything works fine, but I wanted to see if folks had any feedback on the code. I am writing this with the Arduino IDE.
Thanks for any feedback!
#include <esp_http_server.h> // https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/protocols/esp_http_server.html?highlight=webserver#overview #include <HTTP_Method.h> #include <Uri.h> #include <WiFi.h> #include <WiFiMulti.h> #include <M5StickCPlus.h> esp_err_t green_handler(httpd_req_t *req) { const char resp[] = "ok"; httpd_resp_send(req, resp, HTTPD_RESP_USE_STRLEN); M5.Lcd.fillScreen(GREEN); return ESP_OK; } httpd_uri_t uri_green = { .uri = "/green", .method = HTTP_GET, .handler = green_handler, .user_ctx = NULL }; esp_err_t red_handler(httpd_req_t *req) { const char resp[] = "ok"; httpd_resp_send(req, resp, HTTPD_RESP_USE_STRLEN); M5.Lcd.fillScreen(RED); M5.Lcd.setTextColor(BLACK, RED); M5.Lcd.setCursor(0, 0); M5.Lcd.print("\nIN MEETING\n"); return ESP_OK; } httpd_uri_t uri_red = { .uri = "/red", .method = HTTP_GET, .handler = red_handler, .user_ctx = NULL }; esp_err_t off_handler(httpd_req_t *req) { const char resp[] = "ok"; httpd_resp_send(req, resp, HTTPD_RESP_USE_STRLEN); M5.Lcd.fillScreen(BLACK); M5.Lcd.setTextColor(WHITE, BLACK); // TODO: actually turn off screen: https://forum.m5stack.com/topic/1025/m5stickc-turn-off-screen-completely/11 return ESP_OK; } httpd_uri_t uri_off = { .uri = "/off", .method = HTTP_GET, .handler = off_handler, .user_ctx = NULL }; httpd_handle_t start_webserver(void) { httpd_config_t config = HTTPD_DEFAULT_CONFIG(); httpd_handle_t server = NULL; if (httpd_start(&server, &config) == ESP_OK) { httpd_register_uri_handler(server, &uri_green); httpd_register_uri_handler(server, &uri_red); httpd_register_uri_handler(server, &uri_off); } return server; } httpd_handle_t server; WiFiMulti wifiMulti; bool wifi_connected_ran; void setup(){ M5.begin(); M5.Lcd.setRotation(3); M5.Lcd.setTextSize(3); wifiMulti.addAP("Station_slow", "..."); M5.Lcd.print("\nConnecting Wifi...\n"); } void showIP() { M5.Lcd.fillScreen(BLACK); M5.Lcd.setTextColor(WHITE, BLACK); M5.Lcd.setCursor(0, 0); if (wifi_connected_ran) { M5.Lcd.print(WiFi.localIP()); } else { M5.Lcd.print("no wifi (yet?)\n"); } M5.Lcd.print("\n"); } void loop() { M5.update(); if (M5.BtnA.wasReleased()) showIP(); if ((wifiMulti.run() == WL_CONNECTED)) { if (!wifi_connected_ran) { // is this the best way to do this? wifi_connected_ran = true; server = start_webserver(); showIP(); } } delay(1000); // should I do this? }
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login