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 Lolin C3 mini #620

Merged
merged 4 commits into from
Sep 19, 2022
Merged
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
7 changes: 6 additions & 1 deletion interface/src/project/SettingsApplication.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,18 @@ const SettingsApplication: FC = () => {
name="board_profile"
label={LL.BOARD_PROFILE()}
value={data.board_profile}
disabled={processingBoard}
disabled={processingBoard || data.board_profile === 'C3MINI'}
variant="outlined"
onChange={changeBoardProfile}
margin="normal"
select
>
{boardProfileSelectItems()}
{data.board_profile === 'C3MINI' && (
<MenuItem key={'C3MINI'} value={'C3MINI'}>
C3 Mini
</MenuItem>
)}
<Divider />
<MenuItem key={'CUSTOM'} value={'CUSTOM'}>
Custom&hellip;
Expand Down
2 changes: 1 addition & 1 deletion interface/src/project/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const GPIO_VALIDATOR = {
if (
value &&
(value === 1 ||
(value >= 6 && value <= 12) ||
(value >= 10 && value <= 12) ||
(value >= 14 && value <= 15) ||
value === 20 ||
value === 24 ||
Expand Down
2 changes: 1 addition & 1 deletion lib/ESPAsyncWebServer/AsyncWebSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ void AsyncWebSocketClient::binary(AsyncWebSocketMessageBuffer * buffer)

IPAddress AsyncWebSocketClient::remoteIP() {
if(!_client) {
return IPAddress(0U);
return IPAddress((uint32_t)0);
}
return _client->remoteIP();
}
Expand Down
3 changes: 3 additions & 0 deletions lib/framework/APSettingsService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ void APSettingsService::startAP() {
WiFi.softAPConfig(_state.localIP, _state.gatewayIP, _state.subnetMask);
esp_wifi_set_bandwidth((wifi_interface_t)ESP_IF_WIFI_AP, WIFI_BW_HT20);
WiFi.softAP(_state.ssid.c_str(), _state.password.c_str(), _state.channel, _state.ssidHidden, _state.maxClients);
#ifdef ARDUINO_LOLIN_C3_MINI
WiFi.setTxPower(WIFI_POWER_8_5dBm); //https://www.wemos.cc/en/latest/c3/c3_mini.html#about-wifi
#endif
if (!_dnsServer) {
IPAddress apIp = WiFi.softAPIP();
emsesp::EMSESP::logger().info(F("Starting Access Point with captive portal on %s"), apIp.toString().c_str());
Expand Down
3 changes: 3 additions & 0 deletions lib/framework/NetworkSettingsService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ void NetworkSettingsService::manageSTA() {
});

WiFi.begin(_state.ssid.c_str(), _state.password.c_str()); // attempt to connect to the network
#ifdef ARDUINO_LOLIN_C3_MINI
WiFi.setTxPower(WIFI_POWER_8_5dBm); //https://www.wemos.cc/en/latest/c3/c3_mini.html#about-wifi
#endif
}
}

Expand Down
11 changes: 11 additions & 0 deletions mock-api/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -1152,6 +1152,17 @@ rest_server.post(EMSESP_BOARDPROFILE_ENDPOINT, (req, res) => {
data.eth_power = 12
data.eth_phy_addr = 0
data.eth_clock_mode = 3
} else if (board_profile == 'C3MINI') {
// Lolin C3 mini
data.led_gpio = 7
data.dallas_gpio = 2
data.rx_gpio = 4
data.tx_gpio = 5
data.pbutton_gpio = 9
data.phy_type = 0
data.eth_power = 0
data.eth_phy_addr = 0
data.eth_clock_mode = 0
}

console.log('boardProfile POST. Sending back, profile: ' + board_profile + ', ' + 'data: ' + JSON.stringify(data))
Expand Down
11 changes: 11 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,14 @@ board_upload.flash_size = 16MB
board_build.partitions = esp32_partition_16M.csv
build_flags = ${common.build_flags}
build_unflags = ${common.unbuild_flags}

[env:lolin_c3_mini]
extra_scripts =
pre:scripts/build_interface.py
scripts/rename_fw.py
board = lolin_c3_mini
platform = espressif32
board_upload.flash_size = 4MB
board_build.partitions = esp32_partition_4M.csv
build_flags = ${common.build_flags}
build_unflags = ${common.unbuild_flags}
10 changes: 8 additions & 2 deletions src/analogsensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,11 @@ void AnalogSensor::reload() {
} else if (sensor.type() == AnalogType::COUNTER) {
LOG_DEBUG(F("Adding analog I/O Counter sensor on GPIO%d"), sensor.gpio());
pinMode(sensor.gpio(), INPUT_PULLUP);
#ifndef ARDUINO_LOLIN_C3_MINI
if (sensor.gpio() == 25 || sensor.gpio() == 26) {
dacWrite(sensor.gpio(), 255);
}
#endif
sensor.polltime_ = 0;
sensor.poll_ = digitalRead(sensor.gpio());
publish_sensor(sensor);
Expand Down Expand Up @@ -160,7 +162,9 @@ void AnalogSensor::reload() {
} else if (sensor.offset() < 0) {
sensor.set_offset(0);
}
dacWrite(sensor.gpio(), sensor.offset());
#ifndef ARDUINO_LOLIN_C3_MINI
dacWrite(sensor.gpio(), sensor.offset());
#endif
sensor.set_value(sensor.offset());
} else {
digitalWrite(sensor.gpio(), sensor.offset() > 0 ? 1 : 0);
Expand Down Expand Up @@ -572,7 +576,9 @@ bool AnalogSensor::command_setvalue(const char * value, const int8_t gpio) {
sensor.set_offset(v);
sensor.set_value(v);
pinMode(sensor.gpio(), OUTPUT);
dacWrite(sensor.gpio(), sensor.offset());
#ifndef ARDUINO_LOLIN_C3_MINI
dacWrite(sensor.gpio(), sensor.offset());
#endif
publish_sensor(sensor);
return true;
} else if (v == 0 || v == 1) {
Expand Down
2 changes: 1 addition & 1 deletion src/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ void Console::load_system_commands(unsigned int context) {
std::vector<int8_t> data; // led, dallas, rx, tx, button, phy_type, eth_power, eth_phy_addr, eth_clock_mode
std::string board_profile = Helpers::toUpper(arguments.front());
if (!EMSESP::system_.load_board_profile(data, board_profile)) {
shell.println(F("Invalid board profile (S32, E32, MH-ET, NODEMCU, OLIMEX, OLIMEXPOE, CUSTOM)"));
shell.println(F("Invalid board profile (S32, E32, MH-ET, NODEMCU, OLIMEX, OLIMEXPOE, C3MINI, CUSTOM)"));
return;
}
EMSESP::webSettingsService.update(
Expand Down
2 changes: 1 addition & 1 deletion src/emsdevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ void EMSdevice::generate_values_web(JsonObject & output) {
if (dv.numeric_operator > 0) {
obj["s"] = Helpers::render_value(s, (float)1 / dv.numeric_operator, 1);
} else if (dv.numeric_operator < 0) {
obj["s"] = Helpers::render_value(s, (-1) * dv.numeric_operator, 0);
obj["s"] = Helpers::render_value(s, (float)(-1) * dv.numeric_operator, 0);
}

int16_t dv_set_min, dv_set_max;
Expand Down
4 changes: 2 additions & 2 deletions src/mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1321,8 +1321,8 @@ void Mqtt::publish_ha_climate_config(uint8_t tag, bool has_roomtemp, bool remove
doc["curr_temp_tpl"] = currtemp_s;
}

doc["min_temp"] = Helpers::render_value(min_s, 5, 0, EMSESP::system_.fahrenheit() ? 2 : 0);
doc["max_temp"] = Helpers::render_value(max_s, 30, 0, EMSESP::system_.fahrenheit() ? 2 : 0);
doc["min_temp"] = Helpers::render_value(min_s, (uint32_t)5, 0, EMSESP::system_.fahrenheit() ? 2 : 0);
doc["max_temp"] = Helpers::render_value(max_s, (uint32_t)30, 0, EMSESP::system_.fahrenheit() ? 2 : 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and also what is the reason behind adding these two casts?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

doc["temp_step"] = "0.5";

// the HA climate component only responds to auto, heat and off
Expand Down
12 changes: 12 additions & 0 deletions src/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,15 @@ void System::wifi_tweak() {
#endif
}

#ifdef ARDUINO_LOLIN_C3_MINI
// https://www.wemos.cc/en/latest/c3/c3_mini.html
bool System::is_valid_gpio(uint8_t pin) {
if ((pin >= 11 && pin <= 19) || (pin > 21)) {
return false; // bad pin
}
return true;
}
#else
// check for valid ESP32 pins. This is very dependent on which ESP32 board is being used.
// Typically you can't use 1, 6-11, 12, 14, 15, 20, 24, 28-31 and 40+
// we allow 0 as it has a special function on the NodeMCU apparently
Expand All @@ -364,6 +373,7 @@ bool System::is_valid_gpio(uint8_t pin) {
}
return true;
}
#endif

// Starts up the UART Serial bridge
void System::start() {
Expand Down Expand Up @@ -1224,6 +1234,8 @@ bool System::load_board_profile(std::vector<int8_t> & data, const std::string &
data = {0, 0, 36, 4, 34, PHY_type::PHY_TYPE_LAN8720, -1, 0, 0}; // Olimex ESP32-EVB (uses U1TXD/U1RXD/BUTTON, no LED or Dallas)
} else if (board_profile == "OLIMEXPOE") {
data = {0, 0, 36, 4, 34, PHY_type::PHY_TYPE_LAN8720, 12, 0, 3}; // Olimex ESP32-POE
} else if (board_profile == "C3MINI") {
data = {7, 2, 4, 5, 9, PHY_type::PHY_TYPE_NONE, 0, 0, 0}; // Lolin C3 Mini
} else if (board_profile == "CUSTOM") {
// send back current values
data = {(int8_t)EMSESP::system_.led_gpio_,
Expand Down
6 changes: 5 additions & 1 deletion src/uart/emsuart_esp32.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@

#define EMS_MAXBUFFERSIZE 33 // max size of the buffer. EMS packets are max 32 bytes, plus extra for BRK

#define EMSUART_NUM UART_NUM_2 // on the ESP32 we're using UART2
#ifdef ARDUINO_LOLIN_C3_MINI
#define EMSUART_NUM UART_NUM_1 // on C3 mini we're using UART1
#else
#define EMSUART_NUM UART_NUM_2 // on the ESP32 we're using UART2
#endif
#define EMSUART_BAUD 9600 // uart baud rate for the EMS circuit

#define EMS_TXMODE_DEFAULT 1
Expand Down
6 changes: 5 additions & 1 deletion src/web/WebSettingsService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ void WebSettings::read(WebSettings & settings, JsonObject & root) {
StateUpdateResult WebSettings::update(JsonObject & root, WebSettings & settings) {
// load default GPIO configuration based on board profile
std::vector<int8_t> data; // // led, dallas, rx, tx, button, phy_type, eth_power, eth_phy_addr, eth_clock_mode
settings.board_profile = root["board_profile"] | EMSESP_DEFAULT_BOARD_PROFILE;
#ifdef ARDUINO_LOLIN_C3_MINI
settings.board_profile = "C3MINI";
#else
settings.board_profile = root["board_profile"] | EMSESP_DEFAULT_BOARD_PROFILE;
#endif
if (!System::load_board_profile(data, settings.board_profile.c_str())) {
settings.board_profile = "CUSTOM";
EMSESP::logger().info("No board profile found. Re-setting to %s", settings.board_profile.c_str());
Expand Down