<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Prototype working, please review code]]></title><description><![CDATA[<p dir="auto">Hello all!</p>
<p dir="auto">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.</p>
<p dir="auto">Thanks for any feedback!</p>
<pre><code>#include &lt;esp_http_server.h&gt;
// https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/protocols/esp_http_server.html?highlight=webserver#overview
#include &lt;HTTP_Method.h&gt;
#include &lt;Uri.h&gt;
#include &lt;WiFi.h&gt;
#include &lt;WiFiMulti.h&gt;
#include &lt;M5StickCPlus.h&gt;

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(&amp;server, &amp;config) == ESP_OK) {
        httpd_register_uri_handler(server, &amp;uri_green);
        httpd_register_uri_handler(server, &amp;uri_red);
        httpd_register_uri_handler(server, &amp;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?
}
</code></pre>
]]></description><link>https://community.m5stack.com/topic/5889/prototype-working-please-review-code</link><generator>RSS for Node</generator><lastBuildDate>Fri, 17 Apr 2026 22:48:40 GMT</lastBuildDate><atom:link href="https://community.m5stack.com/topic/5889.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 15 Dec 2023 01:44:15 GMT</pubDate><ttl>60</ttl></channel></rss>