Open nkgiovannivl opened 2 years ago
Hey i tried to write some code like that:
import utime
from emonlib import Emonlib
from time import sleep
import machine
import time
from math import pow, sqrt
adc = ADC(0) # Initialization ADC
ct = Emonlib() # Initialization Library
ct.current(adc, 10) #Pin, calibration
Irms = ct.calc_current_rms(1000) # Sample
while True:
# adc_value = adc.read()
print(Irms)
sleep(0.5)
but i get the following output: <generatot object 'calc_current_rms' at 3fff0870>
have you try something different?
@rokkino only async mode is allowed to use, for more information see asyncio documentation.
`import asyncio
async def main(): adc = ADC(0) # Initialization ADC ct = Emonlib() # Initialization Library
ct.current(adc, 10) #Pin, calibration
while True:
irms = await ct.calc_current_rms(1000) # Sample
asyncio.sleep(1)
loop = asyncio.get_event_loop() loop.run_until_complete() `
Hey @alisonsalmeida thank you :+1: for your answer, i modify the code accordingly :
import uasyncio
import Emonlib
import machine
import time
import esp
adc = ADC(Pin(32)) # create an ADC object acting on a pin # Initialization ADC
from machine import Pin, ADC
async def main():
ct = Emonlib() # Initialization Library
ct.current(adc1, 10) #Pin, calibration
while True:
irms = await ct.calc_current_rms(1000) # Sample
print(irms)
asyncio.sleep(2)
loop = asyncio.get_event_loop()
loop.run_until_complete()
the code works but there is no output, is like the code get stucked at _irms = await ct.calc_currentrms(1000)** , want to get some values printed out.. where i am doing wrong?**
kind regards
Hi,
Thank you for this great library.
I finally managed to make it work with a few changes in emonlib.py :
@@ -10,6 +10,7 @@
import machine
import time
+import uasyncio
from math import pow, sqrt
@@ -161,7 +162,7 @@
:param _number_samples:
:return:
"""
- adc_current = machine.ADC(self._inPinI)
+ adc_current = machine.ADC(self._inPinI, atten = machine.ADC.ATTN_11DB)
for sample in range(0, _number_samples):
sample_current = adc_current.read()
In summary :
Add atten = machine.ADC.ATTN_11DB to instance of ADC to raise voltage (according to the doc https://docs.micropython.org/en/latest/esp32/quickref.html#adc-analog-to-digital-conversion)
Here is also a sample main.py which works :
import emonlib
from machine import ADC, Pin
import uasyncio
async def main():
ct = emonlib.Emonlib() # Initialization Library
await ct.current(Pin(32), 29) #Pin, calibration
while True:
irms = await ct.calc_current_rms(1480) # Sample
print(irms, ' => ', irms * 230)
uasyncio.sleep(1)
loop = asyncio.get_event_loop()
loop.run_until_complete()
uasyncio.run(main())
My setup is Micropython 1.19.1 on Wemos Lolin32 Lite
Hope this will help you,
Fred
didnt understand how do u calculate voltage?
Hi @alisonsalmeida , I'm insterested on your library, but i cannot find some example or tutorial, could you provide any information? Thanks!