alisonsalmeida / emonlib-micropython

An asynchronous version of the emonlib library for the micropython firmware
7 stars 1 forks source link

Example of use please... #3

Open nkgiovannivl opened 2 years ago

nkgiovannivl commented 2 years ago

Hi @alisonsalmeida , I'm insterested on your library, but i cannot find some example or tutorial, could you provide any information? Thanks!

rokkino commented 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?

alisonsalmeida commented 2 years ago

@rokkino only async mode is allowed to use, for more information see asyncio documentation.

alisonsalmeida commented 2 years ago

`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() `

rokkino commented 2 years ago

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

bereldhuin commented 2 years ago

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 :

import uasyncio

async def main():

adc = ADC(Pin(2)) # Initialization ADC

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
neonarc4 commented 6 months ago

didnt understand how do u calculate voltage?