🤖Have you ever tried Chat.M5Stack.com before asking??😎
    M5Stack Community
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    Prototype working, please review code

    M5 Stick/StickC
    1
    1
    740
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • F
      frioux
      last edited by

      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?
      }
      
      1 Reply Last reply Reply Quote 0
      • First post
        Last post