Skip to content

Commit

Permalink
configurable PWM value
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkucera committed Aug 18, 2023
1 parent 53e56ba commit 10cf2d5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
2 changes: 2 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ platform = espressif32
board = esp32-c3-devkitm-1
build_flags =
-DRESET_PIN=20
-DPWM_PIN=6

lib_deps =
https://github.com/tzapu/WiFiManager
https://github.com/guido4096/espsoftwareserial.git#add-startbit-timestamp
Expand Down
40 changes: 37 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#include "main.hpp"
#include "enhanced.hpp"
#include "bus.hpp"
#include <Preferences.h>

Preferences preferences;

#ifdef ESP32
#include <esp_task_wdt.h>
Expand All @@ -22,6 +25,7 @@
TaskHandle_t Task1;

WiFiManager wifiManager(Serial1);
WiFiManagerParameter param_pwm_value("pwm_value", "PWM value", "", 6);

WiFiServer wifiServer(3333);
WiFiServer wifiServerRO(3334);
Expand Down Expand Up @@ -83,6 +87,19 @@ inline void enableTX() {
#endif
}

void set_pwm(uint8_t value){
#ifdef PWM_PIN
ledcWrite(PWM_CHANNEL, value);
#endif
}

uint32_t get_pwm(){
#ifdef PWM_PIN
return ledcRead(PWM_CHANNEL);
#endif
return 0;
}

void reset(){
disableTX();
ESP.restart();
Expand Down Expand Up @@ -158,6 +175,15 @@ void data_loop(void * pvParameters){
}
}

void saveParamsCallback () {
uint8_t new_pwm_value = atoi(param_pwm_value.getValue());
if (new_pwm_value > 0){
set_pwm(new_pwm_value);
preferences.putUInt("pwm_value", new_pwm_value);
}
Serial1.printf("pwm_value set: %s %d\n", param_pwm_value.getValue(), new_pwm_value);
}

void setup() {
check_reset();

Expand All @@ -174,15 +200,23 @@ void setup() {

disableTX();

preferences.begin("esp-ebus", false);

#ifdef PWM_PIN
ledcSetup(PWM_CHANNEL, PWM_FREQ, PWM_RESOLUTION);
ledcAttachPin(6, PWM_CHANNEL);
ledcWrite(PWM_CHANNEL, 120);
ledcAttachPin(PWM_PIN, PWM_CHANNEL);
#endif

set_pwm(preferences.getUInt("pwm_value", 130));

WiFi.enableAP(false);
WiFi.begin();

WiFi.onEvent(on_connected, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_CONNECTED);

wifiManager.setSaveParamsCallback(saveParamsCallback);
wifiManager.addParameter(&param_pwm_value);

wifiManager.setHostname(HOSTNAME);
wifiManager.setConfigPortalTimeout(120);
wifiManager.setWiFiAPChannel(random_ch());
Expand Down Expand Up @@ -237,7 +271,7 @@ bool handleStatusServerRequests() {
client.printf("nbr won2: %i\r\n", (int)Bus._nbrWon2);
client.printf("nbr late: %i\r\n", (int)Bus._nbrLate);
client.printf("nbr errors: %i\r\n", (int)Bus._nbrErrors);
client.printf("pwm_value: %i\r\n", pwm_value);
client.printf("pwm_value: %i\r\n", get_pwm());

client.flush();
client.stop();
Expand Down

0 comments on commit 10cf2d5

Please sign in to comment.