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

How can I read multiple sensor data with blynk app and python? #45

Open
GabrieleAnsaldo opened this issue Mar 28, 2021 · 2 comments
Open

Comments

@GabrieleAnsaldo
Copy link

Hello,

I am trying to connect a bunch of sensors to my ESP32. I want to connect it to the Blynk app in order to get some data on my phone.

I am currently coding on python, however, I have an issue.

The code seems to always stop after it reads the light sensor. Basically, I only get the data from the light sensor and it loops from there.

Any clue what is wrong with the code?

import network
from time import sleep
import machine
import onewire, ds18x20
import dht
from machine import Pin
import blynklib_mp as blynklib

"""--- Connect to WiFi ---"""
WIFI_SSID = '******'
WIFI_PASS = '******'
BLYNK_AUTH = '*********'

print("Connecting to WiFi network '{}'".format(WIFI_SSID))
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(WIFI_SSID, WIFI_PASS)
while not wifi.isconnected():
    sleep(0.1)
    print('.', end="")

print('\nWiFi IP:', wifi.ifconfig()[0])

"""--- Connect to Blynk Server ---"""
print("Connecting to Blynk server...")
blynk = blynklib.Blynk(BLYNK_AUTH)


"""--- Settings ---""" 

#LIGHT
light_PIN = 35
blynk_VPIN_Light = 0
light_RAW = machine.ADC(machine.Pin(light_PIN))
light_RAW.atten(machine.ADC.ATTN_11DB)

#MOISTURE
moisture_PIN = 34
blynk_VPIN_Moisture = 1
moisture_RAW = machine.ADC(machine.Pin(moisture_PIN))               
moisture_RAW.atten(machine.ADC.ATTN_11DB)
moisture_PWM_PIN = 25
moisture_RAW_MIN = 20
moisture_RAW_MAX = 3600

#SOIL TEMPERATURE
soil_temperature_PIN = 4
blynk_VPIN_Soil_Temp = 2
ds_pin = machine.Pin(soil_temperature_PIN)
soil_temperature_RAW = ds18x20.DS18X20(onewire.OneWire(ds_pin))
roms = soil_temperature_RAW.scan()

#AIR TEMPERATURE HUMIDITY
blynk_VPIN_Air_Temp = 3
blynk_VPIN_Air_Hum = 4
sensor = dht.DHT22(Pin(33))



@blynk.handle_event('read V{}'.format(blynk_VPIN_Light))
def read_handler(vpin):

    """--- Light Sensor ---""" 
    #WHILE LOOP
    print('Light RAW: ',light_RAW.read())
    sleep(1)
    blynk.virtual_write(blynk_VPIN_Light, light_RAW.read())

    """--- Moisture Sensor ---""" 
    #WHILE LOOP
    moisture_PWM = machine.PWM(machine.Pin(moisture_PWM_PIN), freq=600, duty=512)       #Send a PWM signal of 600 kHz to pin 25 with a dutycycle of 50%
    sleep(0.2)                                                                          #Allow the circuit to stabilize 
    moisture_Cumulative_RAW = 0
    for _ in range(20):
        moisture_Cumulative_RAW = moisture_Cumulative_RAW + moisture_RAW.read()         #Read data from analog pin 34 and add it to MoistLevel variable
        sleep(0.05)
        if moisture_RAW.read() < moisture_RAW_MIN:
            moisture_RAW_MIN = moisture_RAW.read()
        if moisture_RAW.read() > moisture_RAW_MAX:
            moisture_RAW_MAX = moisture_RAW.read()

    moisture_Average_RAW = moisture_Cumulative_RAW / 20

    moisture_PERC = (1 - (moisture_Average_RAW - moisture_RAW_MIN) / (moisture_RAW_MAX - moisture_RAW_MIN))*100
    print('Soil Moisture: ' + str(moisture_PERC) + '%')

    moisture_PWM.deinit()
    blynk.virtual_write(blynk_VPIN_Moisture, moisture_PERC)
    


    """--- Soil Temperature Sensor ---""" 
    #WHILE LOOP
    soil_temperature_RAW.convert_temp()
    sleep(0.750)
    for rom in roms:
        #print(rom)
        print("Soil Temperarture: ", soil_temperature_RAW.read_temp(rom))
    sleep(1)
    blynk.virtual_write(blynk_VPIN_Soil_Temp, soil_temperature_RAW.read_temp(rom))


    """--- Air Temperature/Humidity Sensor ---""" 
    try:
        sleep(2)
        sensor.measure()
        temp = sensor.temperature()
        hum = sensor.humidity()
        print('Air Temperature: %3.1f C' %temp)
        print('Air Humidity: %3.1f %%' %hum)
    except OSError as e:
        print('Failed to read sensor.')

    print(" ")
    blynk.virtual_write(blynk_VPIN_Air_Temp, temp)
    blynk.virtual_write(blynk_VPIN_Air_Hum, hum)

while True:
    blynk.run()
    sleep(5)
@MarkSDS
Copy link

MarkSDS commented Mar 28, 2021

Have you tried to swap """--- Moisture Sensor ---""" with any other part such as """--- Soil Temperature Sensor ---""" ?

If code gets stuck again at the """--- Moisture Sensor ---""" then something is wrong there.
Otherwise obviously the program assumes it's done after the """--- Light Sensor ---"""

In that case try to send to Blynk all data at the end of the program and first finish reading the sensors?

@antohaUa
Copy link
Collaborator

Gabriele, I can propose several ways to debug the issue

  1. If you not familiar with blynklib an this you first project - you can try to use as base and adopt to your sensor this esp32 example
    https://github.com/blynkkk/lib-python/blob/master/examples/esp32/03_temperature_humidity_dht22.py
  2. In your example try to remove last sleep(5) . Also for future I recommend to use not time sleep but machine.sleep
    https://docs.micropython.org/en/v1.10/library/machine.html?highlight=sleep#machine.sleep
    Additionally you can add more print messages to your code and just share with me log.
    This will help us to identify exact place of failure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants