translate ARDUINO to Espeasy

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
espgraf
Normal user
Posts: 22
Joined: 11 Jun 2024, 20:13
Location: Germany

translate ARDUINO to Espeasy

#1 Post by espgraf » 31 Jul 2024, 13:13

I want to monitor the external powersupply and battery for an ESP32-DevKit-Lipo-EA from Olimex.
ESPEasy with ESP_Easy_mega_20240414_collection_B_ESP32_4M316k is running fine.

i found an ARDUINO code piece and not sure how the setup in Espeasy should be:

Code: Select all

//demonstration for power sense and battery sense
//for Olimex ESP32-POE and ESP32-POE-ISO
//for other boards you might need to change pins in define
//some other boards might require hardware adjustments to
//enable power sense or/and battery sense

#define POWER_SENSE 39
#define BATTERY 35

void setup()
{
  Serial.begin (115200);
  pinMode (POWER_SENSE, INPUT);
  pinMode (BATTERY, INPUT);
}

void loop()
{
  Serial.print ("External power sense: ");
  Serial.println (digitalRead (POWER_SENSE));

  Serial.print ("Battery measurement: ");
  Serial.print (analogReadMilliVolts (BATTERY)*2);
  Serial.println (" mV");

  Serial.println ();

  delay (250);
}
I already tried it with devices - Analog input - internal for both cases.
At least for POWER_SENSE i get values >4000 when power is provided via USB. A bit wondering as in the script above it uses digitalRead for this.
For the Battery the values are always 0

Any hint how to translate the code into ESPEasy (most probably devices)?

User avatar
Ath
Normal user
Posts: 4330
Joined: 10 Jun 2018, 12:06
Location: NL

Re: translate ARDUINO to Espeasy

#2 Post by Ath » 31 Jul 2024, 13:56

POWER_SENSE (pin 39) is most likely an On/Off like signal, to indicate what the current power-source is.
BATTERY (pin 35) would be the current battery-voltage-level, that is read via analogReadMilliVolts(), and multiplied by 2 to get the actual voltage, AFAICS.

So if you change your analog task configuration to read GPIO 35 you should be seeing a more acceptable value.
/Ton (PayPal.me)

TD-er
Core team member
Posts: 9884
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: translate ARDUINO to Espeasy

#3 Post by TD-er » 31 Jul 2024, 14:59

Also make sure to only use analog pins which are wired internally to ADC1 and not ADC2.
ADC2 is shared with WiFi, and thus if you have WiFi enabled, you will get nonsense values.

espgraf
Normal user
Posts: 22
Joined: 11 Jun 2024, 20:13
Location: Germany

Re: translate ARDUINO to Espeasy

#4 Post by espgraf » 31 Jul 2024, 15:53

Thanks Ath,
not sure what should i change for the Battery, here is my current setup:
Screenshot 2024-07-31 154507.png
Screenshot 2024-07-31 154507.png (131.94 KiB) Viewed 7858 times
for POWER_SENSE you are talking about On/Off signal, is there an specific device type what i should use or is it about to interpret to values eg. eversing above 3500 is treated as on,
here my current setup for this:
Attachments
Screenshot 2024-07-31 154757.png
Screenshot 2024-07-31 154757.png (132.57 KiB) Viewed 7858 times

User avatar
Ath
Normal user
Posts: 4330
Joined: 10 Jun 2018, 12:06
Location: NL

Re: translate ARDUINO to Espeasy

#5 Post by Ath » 31 Jul 2024, 16:02

For POWER_SENSE you could use a Switch task, that can represent the Off or On state of the GPIO, showing a 0 or 1.
Most likely it will change from On to Off when the external power is removed, and the device is actually running from battery-power (assuming a sufficiently charged rechargeable battery is connected).
/Ton (PayPal.me)

espgraf
Normal user
Posts: 22
Joined: 11 Jun 2024, 20:13
Location: Germany

Re: translate ARDUINO to Espeasy

#6 Post by espgraf » 06 Aug 2024, 14:16

i tried a Switch task, but does not work most probably as the lower voltage is around 350 mV.
I tried the "Regulator - Level Control" and it works like expected. Here you can set the level for on/off
Attachments
Screenshot 2024-08-03 151021.png
Screenshot 2024-08-03 151021.png (51.07 KiB) Viewed 7538 times

User avatar
Ath
Normal user
Posts: 4330
Joined: 10 Jun 2018, 12:06
Location: NL

Re: translate ARDUINO to Espeasy

#7 Post by Ath » 06 Aug 2024, 15:14

Hm, you are trying to use GPIO 39 as an output pin from the Regulator, but that pin is restricted to input only :o The selector shouldn't offer input-only pins as an option, so that's a code-change waiting to happen 8-) (but there is already a PR on that plugin so that will have to wait a bit).
For the regulator to work you don't need to configure a GPIO pin, it can also be handled by rules (only).
/Ton (PayPal.me)

espgraf
Normal user
Posts: 22
Joined: 11 Jun 2024, 20:13
Location: Germany

Re: translate ARDUINO to Espeasy

#8 Post by espgraf » 06 Aug 2024, 16:49

this is input only, the board provides via this GPIO the external voltage to check whether there is an external power supply available

User avatar
Ath
Normal user
Posts: 4330
Joined: 10 Jun 2018, 12:06
Location: NL

Re: translate ARDUINO to Espeasy

#9 Post by Ath » 06 Aug 2024, 19:31

espgraf wrote: 06 Aug 2024, 16:49 this is input only, ...
In that case, that setting should be "none"!
/Ton (PayPal.me)

espgraf
Normal user
Posts: 22
Joined: 11 Jun 2024, 20:13
Location: Germany

Re: translate ARDUINO to Espeasy

#10 Post by espgraf » 06 Aug 2024, 21:45

after reading the documentation carefully again your are right, the GPIO-Pin should not be updated, i just want the reporting via MQTT.

Post Reply

Who is online

Users browsing this forum: No registered users and 14 guests