Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for ESPOE32 board #401

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,41 @@ upload_speed = 115200
lib_deps =
M5StickCPlus2
PubSubClient

build_flags = "-D ARDUINO_M5Stick_C_Plus2"

[env:wt32-eth01]
platform = espressif32
board = wt32-eth01
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 =
PubSubClient

build_flags = "-D WT32_ETH01"

[env:espoe32]
platform = espressif32
board = m5stack-core-esp32
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 =
PubSubClient

build_flags = "-D ESPOE32"

[env:native]
# Used to run unit test; g++ must be in PATH.
platform = native
Expand Down
78 changes: 71 additions & 7 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@
#endif
#include <HardwareSerial.h>

#ifdef WT32_ETH01
#include <ETH.h>
#endif

#ifdef ESPOE32
#include <ETH.h>
#define ETH_ADDR 1
#define ETH_POWER 5
#define ETH_MDC 23
#define ETH_MDIO 18
#endif

#include <PubSubClient.h>
#include <ArduinoOTA.h>

Expand All @@ -35,6 +47,8 @@ Converter converter;
char registryIDs[32]; //Holds the registries to query
bool busy = false;

static bool eth_connected = false;

#if defined(ARDUINO_M5Stick_C) || defined(ARDUINO_M5Stick_C_Plus)
long LCDTimeout = 40000;//Keep screen ON for 40s then turn off. ButtonA will turn it On again.
#endif
Expand Down Expand Up @@ -170,7 +184,7 @@ void get_wifi_bssid(const char *ssid, uint8_t *bssid, uint32_t *wifi_channel)
void checkWifi()
{
int i = 0;
while (WiFi.status() != WL_CONNECTED)
while (!WiFi.isConnected())
{
delay(500);
Serial.print(".");
Expand All @@ -182,11 +196,56 @@ void checkWifi()
}
}

void setup_wifi()
#if defined(WT32_ETH01) || defined(ESPOE32)
void WiFiEvent(WiFiEvent_t event)
{
switch (event) {
case ARDUINO_EVENT_ETH_START:
Serial.println("ETH Started");
// The hostname must be set after the interface is started, but needs
// to be set before DHCP, so set it from the event handler thread.
ETH.setHostname(HOSTNAME);
break;
case ARDUINO_EVENT_ETH_CONNECTED:
Serial.println("ETH Connected");
break;
case ARDUINO_EVENT_ETH_GOT_IP:
Serial.println("ETH Got IP");
eth_connected = true;
break;
case ARDUINO_EVENT_ETH_DISCONNECTED:
Serial.println("ETH Disconnected");
eth_connected = false;
break;
case ARDUINO_EVENT_ETH_STOP:
Serial.println("ETH Stopped");
eth_connected = false;
break;
default:
break;
}
}

void setupEthernet()
{
WiFi.onEvent(WiFiEvent);
#ifdef WT32_ETH01
ETH.begin();
#elif ESPOE32
ETH.begin(ETH_ADDR, ETH_POWER, ETH_MDC, ETH_MDIO, ETH_PHY_IP101, ETH_CLOCK_GPIO0_IN);
#endif

if (ETH.linkUp()) {
Serial.printf("Connected. IP Address: %s\n", ETH.localIP().toString().c_str());
}
}
#endif

void setupWifi()
{
delay(10);
// We start by connecting to a WiFi network
mqttSerial.printf("Connecting to %s\n", WIFI_SSID);
Serial.printf("Connecting to %s\n", WIFI_SSID);

#if defined(WIFI_IP) && defined(WIFI_GATEWAY) && defined(WIFI_SUBNET)
IPAddress local_IP(WIFI_IP);
Expand Down Expand Up @@ -228,7 +287,7 @@ void setup_wifi()
WiFi.begin(WIFI_SSID, WIFI_PWD, 0, 0, true);
}
checkWifi();
mqttSerial.printf("Connected. IP Address: %s\n", WiFi.localIP().toString().c_str());
Serial.printf("Connected. IP Address: %s\n", WiFi.localIP().toString().c_str());
}

void initRegistries(){
Expand Down Expand Up @@ -329,9 +388,14 @@ void setup()

EEPROM.begin(10);
readEEPROM();//Restore previous state
#if defined(WT32_ETH01) || defined(ESPOE32)
mqttSerial.print("Setting up ethernet...");
setupEthernet();
#else
mqttSerial.print("Setting up wifi...");
setup_wifi();
ArduinoOTA.setHostname("ESPAltherma");
setupWifi();
#endif
ArduinoOTA.setHostname(HOSTNAME);
ArduinoOTA.onStart([]() {
busy = true;
});
Expand Down Expand Up @@ -376,7 +440,7 @@ void waitLoop(uint ms){
void loop()
{
unsigned long start = millis();
if (WiFi.status() != WL_CONNECTED)
if (!eth_connected && !WiFi.isConnected())
{ //restart board if needed
checkWifi();
}
Expand Down
8 changes: 8 additions & 0 deletions src/setup.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//Default device hostname:
#define HOSTNAME "ESPAltherma"

//Setup your credentials and mqtt info here:
//only change the value between the " " leave the rest of the line untouched.
#define WIFI_SSID "SSID"//**Your SSID here**
Expand All @@ -24,9 +27,14 @@
#define TX_PIN 26// Pin connected to the RX pin of X10A
#else
//Default GPIO PINs for Serial2:
#if defined(WT32_ETH01)
#define RX_PIN 5// Pin connected to the TX pin of X10A
#define TX_PIN 17// Pin connected to the RX pin of X10A
#else
#define RX_PIN 16// Pin connected to the TX pin of X10A
#define TX_PIN 17// Pin connected to the RX pin of X10A
#endif
#endif

#define PIN_THERM 0// Pin connected to the thermostat relay (normally open)

Expand Down