Skip to content

Commit

Permalink
Merge pull request #403 from smart40-githib/m5stickcplus2
Browse files Browse the repository at this point in the history
Add support for M5StickC Plus2
  • Loading branch information
raomin authored Mar 6, 2024
2 parents ccd2d36 + 81ecc2f commit 54f8512
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 16 deletions.
7 changes: 6 additions & 1 deletion include/mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ PubSubClient client(espClient);
void sendValues()
{
Serial.printf("Sending values in MQTT.\n");
#ifdef ARDUINO_M5Stick_C
#ifdef ARDUINO_M5Stick_C_Plus2
//Add Power values
// getBatteryVoltage returns battery voltage [mV] as an int16_t
float batteryVoltage = (float) M5.Power.getBatteryVoltage() / 1000; // convert to V as a float
snprintf(jsonbuff + strlen(jsonbuff),MAX_MSG_SIZE - strlen(jsonbuff) , "\"%s\":\"%.3gV\",", "M5BatV", batteryVoltage);
#elif ARDUINO_M5Stick_C
//Add M5 APX values
snprintf(jsonbuff + strlen(jsonbuff),MAX_MSG_SIZE - strlen(jsonbuff) , "\"%s\":\"%.3gV\",\"%s\":\"%gmA\",", "M5VIN", M5.Axp.GetVinVoltage(),"M5AmpIn", M5.Axp.GetVinCurrent());
snprintf(jsonbuff + strlen(jsonbuff),MAX_MSG_SIZE - strlen(jsonbuff) , "\"%s\":\"%.3gV\",\"%s\":\"%gmA\",", "M5BatV", M5.Axp.GetBatVoltage(),"M5BatCur", M5.Axp.GetBatCurrent());
Expand Down
4 changes: 3 additions & 1 deletion include/mqttserial.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
#define mqttSerial_h
#include "Stream.h"
#include <PubSubClient.h>
#ifdef ARDUINO_M5Stick_C_Plus
#ifdef ARDUINO_M5Stick_C_Plus2
#include <M5StickCPlus2.h>
#elif ARDUINO_M5Stick_C_Plus
#include <M5StickCPlus.h>
#elif ARDUINO_M5Stick_C
#include <M5StickC.h>
Expand Down
17 changes: 17 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,23 @@ lib_deps =

build_flags = "-D ARDUINO_M5Stick_C_Plus"

[env:m5stickcplus2]
platform = espressif32
board = m5stick-c
framework = arduino
monitor_speed = 115200
upload_speed = 115200
; Uncomment this line to allow for remote upgrade. If name resolution does not work for you, replace with the IP of ESPAltherma
; upload_port = ESPAltherma.local
; Uncomment this line if you want to define the protocol. Autodetected otherwise.
; upload_protocol = espota

lib_deps =
M5StickCPlus2
PubSubClient

build_flags = "-D ARDUINO_M5Stick_C_Plus2"

[env:native]
# Used to run unit test; g++ must be in PATH.
platform = native
Expand Down
28 changes: 16 additions & 12 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#ifdef ARDUINO_M5Stick_C_Plus
#ifdef ARDUINO_M5Stick_C_Plus2
#include <M5StickCPlus2.h>
#elif ARDUINO_M5Stick_C_Plus
#include <M5StickCPlus.h>
#elif ARDUINO_M5Stick_C
#include <M5StickC.h>
Expand Down Expand Up @@ -93,12 +95,12 @@ void extraLoop()
ArduinoOTA.handle();
}

#if defined(ARDUINO_M5Stick_C) || defined(ARDUINO_M5Stick_C_Plus)
#if defined(ARDUINO_M5Stick_C) || defined(ARDUINO_M5Stick_C_Plus) || defined(ARDUINO_M5Stick_C_Plus2)
if (M5.BtnA.wasPressed()){//Turn back ON screen
M5.Axp.ScreenBreath(12);
M5.Display.wakeup();
LCDTimeout = millis() + 30000;
}else if (LCDTimeout < millis()){//Turn screen off.
M5.Axp.ScreenBreath(0);
} else if (LCDTimeout < millis()) { //Turn screen off.
M5.Display.sleep();
}
M5.update();
#endif
Expand Down Expand Up @@ -245,21 +247,23 @@ void initRegistries(){
}

void setupScreen(){
#if defined(ARDUINO_M5Stick_C) || defined(ARDUINO_M5Stick_C_Plus)
#if defined(ARDUINO_M5Stick_C) || defined(ARDUINO_M5Stick_C_Plus) || defined(ARDUINO_M5Stick_C_Plus2)
M5.begin();
#if !defined(ARDUINO_M5Stick_C_Plus2)
M5.Axp.EnableCoulombcounter();
#endif
M5.Lcd.setRotation(1);
M5.Axp.ScreenBreath(12);
M5.Lcd.setBrightness(127);
M5.Lcd.fillScreen(TFT_WHITE);
M5.Lcd.setFreeFont(&FreeSansBold12pt7b);
m5.Lcd.setTextDatum(MC_DATUM);
M5.Lcd.setFont(&FreeSansBold12pt7b);
M5.Lcd.setTextDatum(MC_DATUM);
int xpos = M5.Lcd.width() / 2; // Half the screen width
int ypos = M5.Lcd.height() / 2; // Half the screen width
M5.Lcd.setTextColor(TFT_DARKGREY);
M5.Lcd.drawString("ESPAltherma", xpos,ypos,1);
M5.Lcd.drawString("ESPAltherma", xpos,ypos);
delay(2000);
M5.Lcd.fillScreen(TFT_BLACK);
M5.Lcd.setTextFont(1);
M5.Lcd.setFont(&Font0);
M5.Lcd.setTextColor(TFT_GREEN);
#endif
}
Expand All @@ -269,7 +273,7 @@ void setup()
Serial.begin(115200);
setupScreen();
MySerial.begin(9600, SERIAL_CONFIG, RX_PIN, TX_PIN);
pinMode(PIN_THERM, OUTPUT);
pinMode(PIN_THERM, OUTPUT);
digitalWrite(PIN_THERM, HIGH);

#ifdef PIN_SG1
Expand Down
4 changes: 2 additions & 2 deletions src/setup.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

#define FREQUENCY 30000 //query values every 30 sec

#if defined(ARDUINO_M5Stick_C) || defined(ARDUINO_M5Stick_C_Plus)
//Values used when **M5StickC** or **M5STickCPlus** environment is selected:
#if defined(ARDUINO_M5Stick_C) || defined(ARDUINO_M5Stick_C_Plus) || defined(ARDUINO_M5Stick_C_Plus2)
// Values used when M5StickC, M5STickCPlus or M5Stick_C_Plus2 environment is selected:
#define RX_PIN 36// Pin connected to the TX pin of X10A
#define TX_PIN 26// Pin connected to the RX pin of X10A
#else
Expand Down

0 comments on commit 54f8512

Please sign in to comment.