problem with reading vars on wipy 2 w/ext board and ds18b20



  • import machine
    import sys
    import os
    import time
    import onewire
    import ds18x20
    from machine import Pin
    from onewire import OneWire
    from ds18x20 import DS18X20
    
    while True:
        d3=DS18X20(Pin('P4', mode=Pin.OUT))
        d2=DS18X20(Pin('P8', mode=Pin.OUT))
        d1=DS18X20(Pin('P9', mode=Pin.OUT))
        d=DS18X20(Pin('P10', mode=Pin.OUT))
        result=(d.read_temps())
        result1=(d1.read_temps())
        result2=(d2.read_temps())
        result3=(d3.read_temps())
        if result and result1 and result2 and result3:
            a=str(result[0])
            b=str(result1[0])
            c=str(result2[0])
            d=str(result3[0])
            average=int((result[0]+result1[0]+result2[0]+result3[0])/4)
        else:
            print("Sensor not Found")
        print("Temp1= ", a)
        print( "\nTemp2= ", b)
        print("\ntemp2= ", c)
        print("\ntemp3= ", d )
        print("\nAverage",average ,"\n")
        time.sleep(0)
    

    so code above should work but I'm getting "name is not defined" error and don't know what to do with this code :(
    Any suggestions?
    Error msg is "Variable name not defined" a, b, c, d and average when it comes to print statements.
    Regards, Horacy.
    PS @livius can You show me how to use one pin for 2 or more? I'm just beginning with IoT and electronics. Btw code I gave worked for them at first but after connecting 4th it started all the problems.



  • @kop524

    1. format your post
      http://commonmark.org/help/

    2. and show exact error message with line number
      and why you connect your DS18X20 to different pins? You can support few sensors on the same pin.
      DS18X20 work on onewire protocol.

    3. I do not know about which implementation of DS18X20 lib you have
      but parameter for DS18X20 is onewire bus not pin

    4. Pin can not be OUT if you need to read something from it

    use e.g.:

    oWire=OneWire(Pin('P22', Pin.PULL_UP))
    temp=DS18X20(oWire, True) # you can ommit True if your lib does not have this param
    

 

Pycom on Twitter