M5StickC and 18650C hat problem



  • Hi,
    I am using M5StickC and 18650C hat together and encountering a weird issue.
    First, when assembling them to each other with the designated screws, it seem to disrupt the on/off button or something because it can't be pressed after wise, and maybe related to the fact that my code suddenly gets stuck for no reason.
    This is my code(basically I expected to see led on while it is not in sleep mode):

    #include <WiFi.h>
    #include <M5StickC.h>
    #include "arduino_secrets.h"

    char *ssid[] = { SECRET_SSID1 , SECRET_SSID2 , SECRET_SSID3 , SECRET_SSID4 , SECRET_SSID5 };
    char *pass[] = { SECRET_PASS1 , SECRET_PASS2 , SECRET_PASS3 , SECRET_PASS4 , SECRET_PASS5 };

    //#define TIME_TO_SLEEP_SECONDS 3600
    #define TIME_TO_SLEEP_SECONDS 60

    const int LED = 10;
    int sm;
    String deviceID;
    bool sentData = false;

    void setup() {

    M5_Setup();

    wifiTask();

    }

    void loop() {

    wifiTask();

    M5.update();

    getSm();

    if(sentData)
    {
    Serial.println("Going to sleep, good night");
    Serial.flush();
    delay(3500);
    M5.Axp.DeepSleep(SLEEP_SEC(TIME_TO_SLEEP_SECONDS));
    }

    loopAnalysis();

    }

    void getSm() {
    #define SMPIN 33
    float smVal = analogRead(SMPIN);
    float smFloat = smVal * 0.01 + (float) sm * 0.99;
    sm = (int) smFloat;
    }

    int httpRequest() {
    static WiFiClient client;
    static const char WEBSITE[] = "api.pushingbox.com";
    static const String devid = "v2928F6149137678";

    client.stop();

    if (client.connect(WEBSITE, 80)) {
    Serial.println("connecting...");
    client.print("GET /pushingbox?devid=" + devid
    + "&deviceID=" + (String) deviceID
    + "&sm=" + (String) sm
    );
    client.println(" HTTP/1.1");
    client.print("Host: ");
    client.println(WEBSITE);
    client.println("Connection: close");
    client.println();
    Serial.println("Sent data");
    sentData = true;
    return 1;
    } else {
    Serial.println("failed to connect to http");
    return 0;
    }
    }

    void wifiTask() {
    static int state = 0;
    static int wifiConnectTry = 0;
    static int wifiRestartTry = 0;
    static int network = 0;
    static int wifiStatus = WL_IDLE_STATUS;
    static int httpStatus = 0;
    static int httpConnectTry = 0;
    static unsigned long previousMillis = 0;
    unsigned long currentMillis = 0;

    #define WIFI_CONNECT_TIMEOUT 10000 // seconds waiting between re-connection attempts
    #define HTTP_CONNECT_TIMEOUT 5000
    #define HTTP_CONNECTED 1
    #define HTTP_DISCONNECTED 0
    

    enum WIFI_STATE_TYPE { WIFI_CONNECT,
    HTTP_CONNECT,
    HTTP_POLL,
    WIFI_STATE_RESTART = 255
    };

    switch ( state )
    {
    case WIFI_CONNECT:
    if ( wifiStatus == WL_CONNECTED )
    {
    Serial.println("WIFI Connected");
    printWifiStatus();
    state++;
    digitalWrite( LED , LOW );
    break;
    }
    if ( millis() - previousMillis < WIFI_CONNECT_TIMEOUT && wifiConnectTry > 0 )
    {
    break;
    }

    if ( wifiConnectTry > 9 )
    {
      state = WIFI_STATE_RESTART;
      break;
    }
    
    if ( wifiRestartTry > 1 )
    {
      wifiRestartTry = 0;
      network++;
      if (network == 5) {
        network = 0;
      }
      Serial.println( "Switched to network: " + String(network));
      break;  
    }
    
    wifiStatus = WiFi.begin( ssid[network], pass[network] );
    previousMillis = millis();
    wifiConnectTry++;
    Serial.print( "Try: " );
    Serial.print( wifiConnectTry );
    Serial.print( " Status: " );
    Serial.println( wifiStatus );
    break;
    

    case HTTP_CONNECT:
    if (httpStatus == HTTP_CONNECTED) {
    state++;
    break;
    }
    if ( millis() - previousMillis < HTTP_CONNECT_TIMEOUT && httpConnectTry > 0 )
    {
    break;
    }

    if ( httpConnectTry > 10 )
    {
      state = WIFI_STATE_RESTART;
      break;
    }
    httpStatus = httpRequest();
    previousMillis = millis();
    httpConnectTry++;
    Serial.print( "http Try: " );
    Serial.print( httpConnectTry );
    Serial.print( " http Status: " );
    Serial.println( httpStatus );
    break;
    

    case HTTP_POLL:
    httpStatus = httpRequest();
    if (httpStatus == HTTP_DISCONNECTED) {
    if (WiFi.status() == WL_CONNECTED) {
    state = HTTP_CONNECT;
    break;
    }
    else {
    state = WIFI_STATE_RESTART;
    break;
    }
    }
    break;
    default:
    state = 0;
    wifiConnectTry = 0;
    wifiStatus = WL_IDLE_STATUS;
    httpConnectTry = 0;
    httpStatus = HTTP_DISCONNECTED;
    WiFi.disconnect();
    Serial.println( "WiFi restart" );
    wifiRestartTry++;
    break;
    }

    }

    void printWifiStatus() {
    static byte mac[6];
    Serial.print("SSID: ");
    Serial.println(WiFi.SSID());

    IPAddress ip = WiFi.localIP();
    Serial.print("IP Address: ");
    Serial.println(ip);

    long rssi = WiFi.RSSI();
    Serial.print("signal strength (RSSI):");
    Serial.print(rssi);
    Serial.println(" dBm");

    WiFi.macAddress(mac);
    String mac0,mac1,mac2,mac3,mac4,mac5;
    mac0 = String(mac[0],HEX);
    mac1 = String(mac[1],HEX);
    mac2 = String(mac[2],HEX);
    mac3 = String(mac[3],HEX);
    mac4 = String(mac[4],HEX);
    mac5 = String(mac[5],HEX);
    deviceID = String(mac5 + ":" + mac4 + ":" + mac3 + ":" + mac2 + ":" + mac1 + ":" + mac0);
    Serial.print("mac address: "); Serial.println(deviceID);
    }

    void loopAnalysis()
    {
    static unsigned long previousMillis = 0;
    static unsigned long lastMillis = 0;
    static unsigned long minLoopTime = 0xFFFFFFFF;
    static unsigned long maxLoopTime = 0;
    static unsigned long loopCounter = 0;

    #define INTERVAL 1000

    unsigned long currentMillis = millis();
    if ( currentMillis - previousMillis > INTERVAL )
    {
    Serial.print( "Loops: " );
    Serial.print( loopCounter );
    Serial.print( " ( " );
    Serial.print( minLoopTime );
    Serial.print( " / " );
    Serial.print( maxLoopTime );
    Serial.println( " )" );
    previousMillis = currentMillis;
    loopCounter = 0;
    minLoopTime = 0xFFFFFFFF;
    maxLoopTime = 0;
    }
    loopCounter++;
    unsigned long loopTime = currentMillis - lastMillis;
    lastMillis = currentMillis;
    if ( loopTime < minLoopTime )
    {
    minLoopTime = loopTime;
    }
    if ( loopTime > maxLoopTime )
    {
    maxLoopTime = loopTime;
    }

    }

    void M5_Setup() {
    M5.begin();
    M5.Axp.ScreenBreath(0);
    pinMode(LED,OUTPUT);
    digitalWrite(LED,LOW);
    }

    Thanks!