### M5StickC HTTP Client Example Source: https://context7.com/m5stack/m5stickc/llms.txt Demonstrates making HTTP GET requests and displaying the response on the M5StickC. Requires WiFi connection. ```cpp #include #include #include const char* ssid = "YourSSID"; const char* password = "YourPassword"; void setup() { M5.begin(); M5.Lcd.setRotation(3); M5.Lcd.println("Connecting WiFi..."); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); M5.Lcd.print("."); } M5.Lcd.println("\nConnected!"); M5.Lcd.println(WiFi.localIP()); } void loop() { M5.update(); if (M5.BtnA.wasPressed() && WiFi.status() == WL_CONNECTED) { HTTPClient http; M5.Lcd.fillScreen(BLACK); M5.Lcd.setCursor(0, 0); M5.Lcd.println("HTTP GET..."); http.begin("http://httpbin.org/ip"); int httpCode = http.GET(); if (httpCode > 0) { M5.Lcd.printf("Code: %d\n", httpCode); if (httpCode == HTTP_CODE_OK) { String payload = http.getString(); M5.Lcd.println(payload.substring(0, 100)); } } else { M5.Lcd.printf("Error: %s\n", http.errorToString(httpCode).c_str()); } http.end(); } delay(100); } ``` -------------------------------- ### Initialize M5StickC Hardware Source: https://context7.com/m5stack/m5stickc/llms.txt Initializes all hardware subsystems including LCD, power management, and serial communication. Must be called at the start of setup(). ```cpp #include void setup() { // Full initialization - LCD, Power, and Serial enabled M5.begin(); // Selective initialization for power saving // M5.begin(LCDEnable, PowerEnable, SerialEnable) // M5.begin(true, true, false); // Disable serial output // M5.begin(false, true, true); // Disable LCD for headless operation M5.Lcd.setRotation(3); M5.Lcd.println("M5StickC Ready!"); } void loop() { M5.update(); // Required to update button states delay(100); } ``` -------------------------------- ### M5StickC SPIFFS File Storage Example Source: https://context7.com/m5stack/m5stickc/llms.txt Demonstrates using SPIFFS for persistent storage on M5StickC. Allows writing, reading, appending, and deleting files. Initializes SPIFFS with formatting on failure. ```cpp #include #include const char* filename = "/data/config.txt"; void setup() { M5.begin(); M5.Lcd.setRotation(3); // Initialize SPIFFS if (!SPIFFS.begin(true)) { // true = format if mount fails M5.Lcd.println("SPIFFS mount failed!"); return; } M5.Lcd.println("SPIFFS mounted"); // Write data to file File writeFile = SPIFFS.open(filename, "w"); if (writeFile) { writeFile.println("Sensor1=25.5"); writeFile.println("Sensor2=60.2"); writeFile.close(); M5.Lcd.println("Data written"); } // Read data from file M5.Lcd.println("\nReading file:"); if (SPIFFS.exists(filename)) { File readFile = SPIFFS.open(filename, "r"); while (readFile.available()) { M5.Lcd.println(readFile.readStringUntil('\n')); } readFile.close(); } } void loop() { M5.update(); if (M5.BtnA.wasPressed()) { // Append data File appendFile = SPIFFS.open(filename, "a"); if (appendFile) { appendFile.printf("Log=%lu\n", millis()); appendFile.close(); M5.Lcd.println("Data appended"); } } if (M5.BtnB.wasPressed()) { // Delete file SPIFFS.remove(filename); M5.Lcd.println("File deleted"); } delay(100); } ``` -------------------------------- ### Read IMU Sensor Data Source: https://context7.com/m5stack/m5stickc/llms.txt Accesses 6-axis IMU data including accelerometer, gyroscope, and AHRS orientation. Requires calling M5.IMU.Init() during setup. ```cpp #include float accX, accY, accZ; float gyroX, gyroY, gyroZ; float pitch, roll, yaw; float temp; void setup() { M5.begin(); M5.IMU.Init(); // Initialize IMU sensor M5.Lcd.setRotation(3); M5.Lcd.println("IMU Test"); } void loop() { // Read accelerometer (units: G) M5.IMU.getAccelData(&accX, &accY, &accZ); // Read gyroscope (units: degrees/second) M5.IMU.getGyroData(&gyroX, &gyroY, &gyroZ); // Read computed orientation (AHRS) M5.IMU.getAhrsData(&pitch, &roll, &yaw); // Read temperature M5.IMU.getTempData(&temp); // Display readings M5.Lcd.setCursor(0, 20); M5.Lcd.printf("Gyro: %6.1f %6.1f %6.1f\n", gyroX, gyroY, gyroZ); M5.Lcd.printf("Acc: %5.2f %5.2f %5.2f G\n", accX, accY, accZ); M5.Lcd.printf("Angle:%5.1f %5.1f %5.1f\n", pitch, roll, yaw); M5.Lcd.printf("Temp: %.1f C\n", temp); // Raw ADC values also available: // int16_t rawAx, rawAy, rawAz; // M5.IMU.getAccelAdc(&rawAx, &rawAy, &rawAz); delay(100); } ``` -------------------------------- ### M5.begin() - Initialize M5StickC Source: https://context7.com/m5stack/m5stickc/llms.txt Initializes all M5StickC hardware subsystems including LCD display, AXP192 power management, serial communication, and RTC. ```APIDOC ## M5.begin() ### Description Initializes all M5StickC hardware subsystems including LCD display, AXP192 power management, serial communication, and RTC. Parameters allow selective enabling of subsystems for power optimization. ### Parameters #### Arguments - **LCDEnable** (bool) - Optional - Enable/Disable LCD display - **PowerEnable** (bool) - Optional - Enable/Disable AXP192 power management - **SerialEnable** (bool) - Optional - Enable/Disable Serial communication ### Request Example // Full initialization M5.begin(); // Selective initialization M5.begin(true, true, false); // Disable serial output ``` -------------------------------- ### Record and Visualize Audio with I2S Source: https://context7.com/m5stack/m5stickc/llms.txt Initializes the I2S peripheral for PDM microphone input and displays a real-time waveform and volume level on the M5StickC screen. ```cpp #include #include #define PIN_CLK 0 #define PIN_DATA 34 #define SAMPLE_RATE 44100 #define BUFFER_SIZE 256 int16_t audioBuffer[BUFFER_SIZE]; void i2sInit() { i2s_config_t i2s_config = { .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_PDM), .sample_rate = SAMPLE_RATE, .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, .channel_format = I2S_CHANNEL_FMT_ALL_RIGHT, .communication_format = I2S_COMM_FORMAT_STAND_I2S, .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, .dma_buf_count = 2, .dma_buf_len = 128, }; i2s_pin_config_t pin_config = { .bck_io_num = I2S_PIN_NO_CHANGE, .ws_io_num = PIN_CLK, .data_out_num = I2S_PIN_NO_CHANGE, .data_in_num = PIN_DATA, }; i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL); i2s_set_pin(I2S_NUM_0, &pin_config); i2s_set_clk(I2S_NUM_0, SAMPLE_RATE, I2S_BITS_PER_SAMPLE_16BIT, I2S_CHANNEL_MONO); } void setup() { M5.begin(); M5.Lcd.setRotation(3); M5.Lcd.fillScreen(BLACK); M5.Lcd.println("Microphone Test"); i2sInit(); } void loop() { size_t bytesRead; i2s_read(I2S_NUM_0, audioBuffer, sizeof(audioBuffer), &bytesRead, 100); // Simple visualization - draw waveform M5.Lcd.fillRect(0, 20, 160, 60, BLACK); for (int i = 0; i < 80; i++) { int sample = audioBuffer[i * 2]; int y = map(sample, -32768, 32767, 20, 80); M5.Lcd.drawPixel(i * 2, y, GREEN); } // Calculate peak level int maxVal = 0; for (int i = 0; i < BUFFER_SIZE; i++) { maxVal = max(maxVal, abs(audioBuffer[i])); } int level = map(maxVal, 0, 32768, 0, 100); M5.Lcd.setCursor(100, 0); M5.Lcd.printf("Level:%3d%%", level); delay(50); } ``` -------------------------------- ### BeetleC Initialization Source: https://github.com/m5stack/m5stickc/blob/master/examples/Hat/BeetleC/README.md Initializes WiFi and the HTTP server for BeetleC operation. ```APIDOC ## BeetleC Initialization ### Description Initializes WiFi and the HTTP server, allowing other devices to connect and access the control page. The BeetleC will wait for control messages once started. ### Functions - `static void initWifi()`: WiFi initialization. - `static esp_err_t http_server_init()`: HTTP Server initialization. Callback functions will be set here. ``` -------------------------------- ### Configure M5StickC Component Source Directories Source: https://github.com/m5stack/m5stickc/blob/master/CMakeLists.txt Sets the source directories for the component. Ensure all necessary source files are located within these paths. ```cmake set(COMPONENT_SRCDIRS src src/Fonts src/utility) ``` -------------------------------- ### Configure M5StickC Component Include Directories Source: https://github.com/m5stack/m5stickc/blob/master/CMakeLists.txt Specifies the include directories for the component. This allows header files to be found during compilation. ```cmake set(COMPONENT_ADD_INCLUDEDIRS src) ``` -------------------------------- ### WiFi Scanning Source: https://context7.com/m5stack/m5stickc/llms.txt Demonstrates WiFi integration for network scanning using the standard ESP32 WiFi library alongside the M5StickC library. ```cpp #include #include void setup() { M5.begin(); M5.Lcd.setRotation(3); WiFi.mode(WIFI_STA); WiFi.disconnect(); M5.Lcd.setTextColor(GREEN); M5.Lcd.println("WiFi Scanner"); M5.Lcd.println("Press A to scan"); } void scanNetworks() { M5.Lcd.fillScreen(BLACK); M5.Lcd.setCursor(0, 0); M5.Lcd.setTextColor(YELLOW); M5.Lcd.println("Scanning..."); int numNetworks = WiFi.scanNetworks(); M5.Lcd.fillScreen(BLACK); M5.Lcd.setCursor(0, 0); M5.Lcd.setTextColor(WHITE); M5.Lcd.printf("Found: %d\n", numNetworks); for (int i = 0; i < min(numNetworks, 5); i++) { String ssid = WiFi.SSID(i); if (ssid.length() > 12) ssid = ssid.substring(0, 12) + "..."; M5.Lcd.printf("%d)%s %ddBm\n", i+1, ssid.c_str(), WiFi.RSSI(i)); } } void loop() { M5.update(); if (M5.BtnA.wasPressed()) { scanNetworks(); } delay(100); } ``` -------------------------------- ### Configure M5StickC Private Include Directories Source: https://github.com/m5stack/m5stickc/blob/master/CMakeLists.txt Defines the private include directories for the component. These are typically used for internal headers. ```cmake set(COMPONENT_PRIV_INCLUDEDIRS src/Fonts src/Fonts/Custom src/Fonts/GFXFF src/Fonts/TrueType src/utility) ``` -------------------------------- ### BeetleC Control Methods Source: https://github.com/m5stack/m5stickc/blob/master/examples/Hat/BeetleC/README.md Functions for motor control, HTTP request handling, and system initialization. ```c carLRcontrol(int8_t left, int8_t right) ``` ```c esp_err_t control(httpd_req_t *req) ``` ```c esp_err_t test_handler(httpd_req_t *req) ``` ```c static esp_err_t http_server_init() ``` ```c static void initWifi() ``` ```c void blink() ``` -------------------------------- ### Generate QR Codes on LCD Source: https://context7.com/m5stack/m5stickc/llms.txt Renders QR codes on the display, useful for sharing URLs or configuration data. Version parameter should be adjusted based on data length. ```cpp #include void setup() { M5.begin(); M5.Lcd.setRotation(0); // Portrait mode for square QR M5.Lcd.fillScreen(WHITE); // Generate QR code at position (0,0) with width 80 pixels // qrcode(data, x, y, width, version) M5.Lcd.qrcode("http://www.m5stack.com", 0, 0, 80, 7); // For longer data, increase version (1-40) // String ssid = "WiFi:S:MyNetwork;T:WPA;P:password123;;"; // M5.Lcd.qrcode(ssid, 0, 0, 80, 10); } void loop() { } ``` -------------------------------- ### M5.Lcd.qrcode() - QR Code Generation Source: https://context7.com/m5stack/m5stickc/llms.txt Generates and displays QR codes directly on the LCD screen. ```APIDOC ## M5.Lcd.qrcode() ### Description Generates and displays QR codes directly on the LCD. Useful for sharing URLs, WiFi credentials, or device information. ### Parameters #### Arguments - **data** (String) - Required - The string content to encode - **x** (int) - Required - X coordinate - **y** (int) - Required - Y coordinate - **width** (int) - Required - Width of the QR code in pixels - **version** (int) - Required - QR version (1-40) ### Request Example M5.Lcd.qrcode("http://www.m5stack.com", 0, 0, 80, 7); ``` -------------------------------- ### Handle Button Inputs with M5StickC Source: https://context7.com/m5stack/m5stickc/llms.txt Requires calling M5.update() in the main loop to refresh button states. Supports detection of presses, releases, and timed long-press events. ```cpp #include void setup() { M5.begin(); M5.Lcd.setRotation(3); M5.Lcd.setTextColor(YELLOW); M5.Lcd.println("Button Test"); M5.Lcd.println("A: Print 'A'"); M5.Lcd.println("B: Print 'B'"); M5.Lcd.println("B (700ms): Clear"); } void loop() { M5.update(); // REQUIRED: Update button states // Detect button release (recommended for single press) if (M5.BtnA.wasReleased()) { M5.Lcd.print('A'); } if (M5.BtnB.wasReleased()) { M5.Lcd.print('B'); } // Long press detection (700ms) if (M5.BtnB.wasReleasefor(700)) { M5.Lcd.fillScreen(BLACK); M5.Lcd.setCursor(0, 0); } // Other button methods: // M5.BtnA.isPressed() - Currently pressed // M5.BtnA.isReleased() - Currently released // M5.BtnA.wasPressed() - Just pressed this cycle // M5.BtnA.pressedFor(ms) - Held for specified milliseconds // M5.BtnA.releasedFor(ms)- Released for specified milliseconds delay(10); } ``` -------------------------------- ### Perform LCD Display Operations Source: https://context7.com/m5stack/m5stickc/llms.txt Demonstrates text rendering, geometric shapes, and color fills on the 80x160 pixel display using TFT_eSPI functions. ```cpp #include void setup() { M5.begin(); M5.Lcd.setRotation(3); // Landscape mode (160x80) // Screen fill operations M5.Lcd.fillScreen(BLACK); // Text configuration M5.Lcd.setCursor(0, 0); M5.Lcd.setTextColor(WHITE, BLACK); // Text color, background color M5.Lcd.setTextSize(1); // Text size multiplier M5.Lcd.setTextFont(2); // Font selection (1-4) // Text output M5.Lcd.print("Hello "); M5.Lcd.println("World!"); M5.Lcd.printf("Value: %d\n", 42); // Centered text M5.Lcd.drawCentreString("Centered", 80, 40, 2); // Drawing primitives M5.Lcd.drawRect(10, 50, 30, 20, BLUE); // Outline rectangle M5.Lcd.fillRect(50, 50, 30, 20, RED); // Filled rectangle M5.Lcd.drawCircle(100, 60, 15, GREEN); // Outline circle M5.Lcd.fillCircle(140, 60, 10, YELLOW); // Filled circle M5.Lcd.drawLine(0, 75, 159, 75, WHITE); // Line M5.Lcd.drawTriangle(10, 70, 30, 50, 50, 70, CYAN); // Triangle M5.Lcd.drawPixel(80, 40, WHITE); // Single pixel } void loop() { // Random triangles demo M5.Lcd.fillTriangle( random(M5.Lcd.width()), random(M5.Lcd.height()), random(M5.Lcd.width()), random(M5.Lcd.height()), random(M5.Lcd.width()), random(M5.Lcd.height()), random(0xFFFF) ); delay(100); } ``` -------------------------------- ### Register M5StickC Component Source: https://github.com/m5stack/m5stickc/blob/master/CMakeLists.txt Registers the component with the build system. This command should be called after all configuration settings have been applied. ```cmake register_component() ``` -------------------------------- ### Set M5StickC Component Requirements Source: https://github.com/m5stack/m5stickc/blob/master/CMakeLists.txt Declares that the M5StickC component requires the Arduino framework. This ensures that Arduino-specific functionalities are available. ```cmake set(COMPONENT_PRIV_REQUIRES arduino) ``` -------------------------------- ### Manage Real-Time Clock (RTC) Source: https://context7.com/m5stack/m5stickc/llms.txt Uses the BM8563 chip to track time and date. Time settings persist through power cycles if battery is present. ```cpp #include RTC_TimeTypeDef rtcTime; RTC_DateTypeDef rtcDate; void setup() { M5.begin(); M5.Lcd.setRotation(3); // Set initial time RTC_TimeTypeDef setTime; setTime.Hours = 14; setTime.Minutes = 30; setTime.Seconds = 0; M5.Rtc.SetTime(&setTime); // Set initial date RTC_DateTypeDef setDate; setDate.Year = 2024; setDate.Month = 1; setDate.Date = 15; setDate.WeekDay = 1; // Monday M5.Rtc.SetData(&setDate); M5.Lcd.println("RTC Clock"); } void loop() { // Read current time and date M5.Rtc.GetTime(&rtcTime); M5.Rtc.GetData(&rtcDate); M5.Lcd.setCursor(0, 20); M5.Lcd.printf("Date: %04d-%02d-%02d\n", rtcDate.Year, rtcDate.Month, rtcDate.Date); M5.Lcd.printf("Day: %d\n", rtcDate.WeekDay); M5.Lcd.printf("Time: %02d:%02d:%02d\n", rtcTime.Hours, rtcTime.Minutes, rtcTime.Seconds); delay(1000); } ``` -------------------------------- ### BeetleC HTTP Server Source: https://github.com/m5stack/m5stickc/blob/master/examples/Hat/BeetleC/README.md Handles HTTP requests for controlling the BeetleC base and serves a control page. ```APIDOC ## BeetleC HTTP Server ### Description Handles HTTP requests for controlling the BeetleC base and serves a control page. ### Functions - `esp_err_t control(httpd_req_t *req)`: Callback function of the HTTP server, receives HTTP requests to control the BeetleC base. - `esp_err_t test_handler(httpd_req_t *req)`: Sends back a control page when someone accesses `192.168.4.1/ctl`, which can send control messages. - `static esp_err_t http_server_init()`: HTTP Server initialization. Callback functions will be set here. ``` -------------------------------- ### M5.Axp Sleep Modes - Deep Sleep and Light Sleep Source: https://context7.com/m5stack/m5stickc/llms.txt Implements ESP32 sleep modes with AXP192 power optimization. Light sleep preserves RAM state, deep sleep provides lowest power consumption with wake-up timer or button press. ```cpp #include void setup() { M5.begin(); M5.Lcd.setRotation(3); M5.Lcd.drawCentreString("Press A: Sleep", 80, 30, 2); } void loop() { M5.update(); if (M5.BtnA.wasPressed()) { // === LIGHT SLEEP (5 seconds) === M5.Lcd.drawCentreString("Light sleep 5s...", 80, 30, 1); delay(1000); // Turn off LCD backlight to save power M5.Axp.SetLDO2(false); // Enter light sleep (approx 6mA) M5.Axp.LightSleep(SLEEP_SEC(5)); // Wake up - restore LCD M5.Axp.SetLDO2(true); M5.Lcd.fillScreen(BLACK); M5.Lcd.drawCentreString("Woke from light", 80, 20, 1); delay(2000); // === DEEP SLEEP (20 seconds or button wake) === M5.Lcd.fillScreen(BLACK); M5.Lcd.drawCentreString("Deep sleep 20s", 80, 30, 1); delay(1000); // Enter deep sleep (approx 2-3mA) // Wake by timer or power button M5.Axp.DeepSleep(SLEEP_SEC(20)); // Code below never executes - device resets on wake } if (M5.BtnB.wasPressed()) { // Complete power off (cannot wake by timer) M5.Axp.PowerOff(); } delay(20); } ``` -------------------------------- ### M5.Axp LCD Control - Power Saving Source: https://context7.com/m5stack/m5stickc/llms.txt Controls LCD backlight (LDO2) and LCD controller power (LDO3) for power optimization. Useful when display is not needed or for visual effects. ```cpp #include void setup() { M5.begin(); M5.Lcd.setRotation(3); M5.Lcd.println("LCD Control Test"); M5.Lcd.println("A: Toggle backlight"); M5.Lcd.println("B: Toggle LCD"); } bool backlightOn = true; bool lcdOn = true; void loop() { M5.update(); if (M5.BtnA.wasPressed()) { backlightOn = !backlightOn; M5.Axp.SetLDO2(backlightOn); // LCD backlight Serial.printf("Backlight: %s\n", backlightOn ? "ON" : "OFF"); } if (M5.BtnB.wasPressed()) { lcdOn = !lcdOn; M5.Axp.SetLDO3(lcdOn); // LCD controller power if (lcdOn) { M5.Lcd.begin(); // Re-initialize LCD after power restore M5.Lcd.setRotation(3); M5.Lcd.println("LCD restored"); } } delay(50); } ``` -------------------------------- ### M5.Lcd - Display Operations Source: https://context7.com/m5stack/m5stickc/llms.txt Provides TFT_eSPI-based graphics functions for the 80x160 pixel ST7735S display, supporting text, shapes, and colors. ```APIDOC ## M5.Lcd ### Description The LCD object provides graphics functions for the 80x160 pixel ST7735S display. Supports text rendering, geometric shapes, color fills, and rotation. ### Methods - **setRotation(int rotation)** - Set display orientation (0-3) - **fillScreen(uint16_t color)** - Fill entire screen with color - **setCursor(int x, int y)** - Set text cursor position - **setTextColor(uint16_t color, uint16_t bg)** - Set text and background color - **setTextSize(uint8_t size)** - Set text size multiplier - **print(String text)** - Print text to display ### Request Example M5.Lcd.setRotation(3); M5.Lcd.fillScreen(BLACK); M5.Lcd.setCursor(0, 0); M5.Lcd.print("Hello World"); ``` -------------------------------- ### M5.Axp - Power Management (AXP192) Source: https://context7.com/m5stack/m5stickc/llms.txt Controls the AXP192 power management IC for battery monitoring, charging control, display brightness, sleep modes, and power rail management. Essential for battery-powered applications. ```cpp #include void setup() { M5.begin(); M5.Lcd.setRotation(3); M5.Axp.EnableCoulombcounter(); // Enable battery charge tracking // Set screen brightness (0-12, 7 default) M5.Axp.ScreenBreath(10); // Set charging current (battery is only 80mAh) M5.Axp.SetChargeCurrent(CURRENT_100MA); // Options: CURRENT_100MA, CURRENT_190MA, CURRENT_280MA, etc. } void loop() { M5.Lcd.setCursor(0, 0); // Battery information M5.Lcd.printf("Bat: %.3fV %.1fmA\n", M5.Axp.GetBatVoltage(), M5.Axp.GetBatCurrent()); M5.Lcd.printf("Power: %.1fmW\n", M5.Axp.GetBatPower()); // USB/5V input information M5.Lcd.printf("USB: %.2fV %.1fmA\n", M5.Axp.GetVBusVoltage(), M5.Axp.GetVBusCurrent()); M5.Lcd.printf("5V-In: %.2fV\n", M5.Axp.GetVinVoltage()); // AXP192 chip temperature M5.Lcd.printf("AXP Temp: %.1fC\n", M5.Axp.GetTempInAXP192()); // Power button detection (0x01=long, 0x02=short) uint8_t btnState = M5.Axp.GetBtnPress(); if (btnState == 0x02) { M5.Lcd.println("Power btn short"); } M5.update(); delay(500); } ``` -------------------------------- ### BeetleC Motor Control Source: https://github.com/m5stack/m5stickc/blob/master/examples/Hat/BeetleC/README.md Functions to control the motors of the BeetleC base. ```APIDOC ## BeetleC Motor Control ### Description Functions to control the motors of the BeetleC base. ### Functions - `carLRcontrol(int8_t left, int8_t right)`: This function will send two values to the BeetleC base to control the left and right motors. - `blink()`: Turn on the LEDs one by one for 3 times. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.