Don't know why is giving an error



  • Hi all

    I have this code to read some sensors (could be a goor guide for newbies like me)
    Now is working perfecte, but when I triying to add Sigfox the code returns an error:

    Code without Sigfox:

    import machine
    import bme280_float as bme280
    import time
    from machine import Pin
    from machine import ADC
    from ds18x20 import DS18X20
    from onewire import OneWire
    import pycom
    # from network import Sigfox
    # import socket
    
    def read_bme280():
        # this script assumes the default connection of the I2C bus
        # On pycom devuces that is P9 = SDA, P10 = scl
        #
        print('**************Reading BME280**************')
        # i2c = machine.I2C()
        # bme = bme280.BME280(i2c=i2c)
        # print(bme.read_compensated_data())
        # print(bme.dew_point)
        i2c = machine.I2C()
        bme = bme280.BME280(i2c=i2c)
        Values= bme.values
        tempbme=res = ''.join(filter(lambda i: i.isdigit(), Values[0]))
        tempbme280=int(tempbme)/100
        print(tempbme280)
    
        pressbme=res = ''.join(filter(lambda i: i.isdigit(), Values[1]))
        pressbme280=int(pressbme)/100
        print(pressbme280)
    
        humbme=res = ''.join(filter(lambda i: i.isdigit(), Values[2]))
        humbme280=int(humbme)/100
        print(humbme280)
        print('**************End of Reading BME280**************')
    
    def read_analogs():
        print('**************Reading moisture **************')
    
        adc = machine.ADC()             # create an ADC object
        adc.init(bits=12)
        soilmoisture = adc.channel(pin='P13', attn = machine.ADC.ATTN_11DB)   # create an analog pin on P13, range 0..3.3V
        print('moisture raw: ')
        print (soilmoisture())
        print('**************End of Reading moisture **************')
    
        print('**************Reading Battery **************')
        adc = ADC()
        # Set calibration - see note above
        adc.vref(1100)
        # Check calibration by reading a known voltage
        adc_c = adc.channel(pin='P16', attn=ADC.ATTN_11DB)
        print('Battery raw: ')
        print(adc_c.voltage())
        print('**************End of Reading Battery **************')
    
    
    def read_ds18X20():
        print('**************Reading DS18X20 **************')
        #DS18B20 data line connected to pin P10
        ow = OneWire(Pin('P10'))
        temp = DS18X20(ow)
        print("Powermode = ", temp.powermode(Pin('P11')))
        roms = temp.scan()
    
        temp.resolution(roms[0], 9)
        print("Resolution", temp.resolution(roms[0]))
    
        temp.convert_temp()
        contador_DS18X20 = 0
        temp_Acc=0
        temp_moisture=0
        while contador_DS18X20 <5:
            time.sleep(1.5)
            for rom in roms:
                print(temp.read_temp(rom), end=" ")
            print()
            temp.convert_temp()
            temp_Acc =(temp.read_temp(rom) + temp_Acc)
            temp_moisture=temp_Acc/5
            contador_DS18X20 +=1
            print (contador_DS18X20)
            print (temp_Acc)
            print (temp_moisture)
            print ('---------')
            print('**************End of Reading DS18X20 **************')
    
    time.sleep(1)
    
    
    read_bme280()
    
    time.sleep(1)
    
    read_analogs()
    
    time.sleep(1)
    
    read_ds18X20()
    

    And now when I want to import the libraries to activate Sigfox returns an error:

    from network import Sigfox
    import socket
    import machine
    import bme280_float as bme280
    import time
    from machine import Pin
    from machine import ADC
    from ds18x20 import DS18X20
    from onewire import OneWire
    # import pycom
    # from network import Sigfox
    # import socket
    
    def read_bme280():
        # this script assumes the default connection of the I2C bus
        # On pycom devuces that is P9 = SDA, P10 = scl
        #
        print('**************Reading BME280**************')
        # i2c = machine.I2C()
        # bme = bme280.BME280(i2c=i2c)
        # print(bme.read_compensated_data())
        # print(bme.dew_point)
        i2c = machine.I2C()
        bme = bme280.BME280(i2c=i2c)
        Values= bme.values
        tempbme=res = ''.join(filter(lambda i: i.isdigit(), Values[0]))
        tempbme280=int(tempbme)/100
        print(tempbme280)
    
        pressbme=res = ''.join(filter(lambda i: i.isdigit(), Values[1]))
        pressbme280=int(pressbme)/100
        print(pressbme280)
    
        humbme=res = ''.join(filter(lambda i: i.isdigit(), Values[2]))
        humbme280=int(humbme)/100
        print(humbme280)
        print('**************End of Reading BME280**************')
    
    def read_analogs():
        print('**************Reading moisture **************')
    
        adc = machine.ADC()             # create an ADC object
        adc.init(bits=12)
        soilmoisture = adc.channel(pin='P13', attn = machine.ADC.ATTN_11DB)   # create an analog pin on P13, range 0..3.3V
        print('moisture raw: ')
        print (soilmoisture())
        print('**************End of Reading moisture **************')
    
        print('**************Reading Battery **************')
        adc = ADC()
        # Set calibration - see note above
        adc.vref(1100)
        # Check calibration by reading a known voltage
        adc_c = adc.channel(pin='P16', attn=ADC.ATTN_11DB)
        print('Battery raw: ')
        print(adc_c.voltage())
        print('**************End of Reading Battery **************')
    
    
    def read_ds18X20():
        print('**************Reading DS18X20 **************')
        #DS18B20 data line connected to pin P10
        ow = OneWire(Pin('P10'))
        temp = DS18X20(ow)
        print("Powermode = ", temp.powermode(Pin('P11')))
        roms = temp.scan()
    
        temp.resolution(roms[0], 9)
        print("Resolution", temp.resolution(roms[0]))
    
        temp.convert_temp()
        contador_DS18X20 = 0
        temp_Acc=0
        temp_moisture=0
        while contador_DS18X20 <5:
            time.sleep(1.5)
            for rom in roms:
                print(temp.read_temp(rom), end=" ")
            print()
            temp.convert_temp()
            temp_Acc =(temp.read_temp(rom) + temp_Acc)
            temp_moisture=temp_Acc/5
            contador_DS18X20 +=1
            print (contador_DS18X20)
            print (temp_Acc)
            print (temp_moisture)
            print ('---------')
            print('**************End of Reading DS18X20 **************')
    
    time.sleep(1)
    
    
    read_bme280()
    
    time.sleep(1)
    
    read_analogs()
    
    time.sleep(1)
    
    read_ds18X20()
    
    
    # time.sleep(1)
    
    

    It crashes, any idea why? thank very very much

    **************End of Reading Battery **************
    **************Reading DS18X20 **************
    Powermode =  0
    Traceback (most recent call last):
      File "main.py", line 101, in <module>
      File "main.py", line 68, in read_ds18X20
    IndexError: list index out of range
    Pycom MicroPython 1.20.2.r1 [v1.11-a5aa0b8] on 2020-09-09; SiPy with ESP32
    Pybytes Version: 1.6.0
    Type "help()" for more information.
    >>> 
    

    `



  • @ecabanas Python almost tells you where the problem is. Check line 68, looks like your array "roms" is empty. Go back from there. Also, always try to reduce your example to a minimum example that fails (just a few lines of code), this increases the probability for someone to help you.


Log in to reply
 

Pycom on Twitter