UART Creation Problem



  • I have two Wipy 3.0s. The following code works fine on one, but on the other uart1 does not get created. I am putting these boards into the one hardware setup. The only difference is the boards themselves. Is there a hardware failure that would cause this error?

    The REPL error is at the end of this post.

    from machine import UART  #  for serial communication
    from utime import sleep # For delaying in seconds, MicroPython version
    import time
    
    
    
    def sdi_setup():
        # initialize UART port to communicate with SDI-12 translator board
        uart1 = UART(1, baudrate=9600, pins=('P9','P10'))   # init with given UART port & baudrate (tx, rx)
        uart1.init(baudrate=9600, bits=8, parity=None, stop=1, pins=('P9','P10'), timeout_chars=10)  # init with given parameters
        
        print(uart1)
        global sdi_12_address
        # setup SDI-12 addresses.  Valid addresses are single character.
        sdi_12_address=''
        #user_sdi_12_address=input('Enter all SDI-12 sensor addresses, such as 1234:')
        user_sdi_12_address = '01'
        user_sdi_12_address=user_sdi_12_address.strip() # Remove any \r from an input file typed in windows
        for an_address in user_sdi_12_address:
            if ((an_address>='0') and (an_address<='9')) or ((an_address>='A') and (an_address<='Z')) or ((an_address>='a') and (an_address<='z')):
                print("Using address:",an_address);
                sdi_12_address=sdi_12_address+an_address
            else:
                print('Invalid address:',an_address)
    
    
    def sensor_info():
        global sdi_12_address
        sensor_dict = {} # init dictionary to hold sensor info
        for an_address in sdi_12_address:
            # print(an_address+'I!')
            uart1.write(an_address+'I!')
            sleep(1)  #give time for sensor to respond
            sdi_12_line=uart1.readline().decode().strip()
            sensor_txt = 'Sensor address & info,' + an_address + ',' + sdi_12_line + '\n'
            print(sensor_txt)
            sensor_info=sdi_12_line.split(',') #  create list of values
            sensor_dict[an_address] = sensor_info #  store in dict, sensor address is the key
        print(sensor_dict)
        return sensor_dict
    
    def sensor_measurement():
        measurement_dict = {}  # initialize dictionary to hold measurement values
        for j in sdi_12_address:
            str_j = str(j)
            uart1.write(str_j + 'M!')
            sleep(1) # keep at or above 1 second to ensure all respones from sensor are captured
            meas_delay = uart1.readall().decode().strip()  # read sensor response, time until ready
            meas_delay = meas_delay[len(str_j):]
            try:
                meas_delay = float(meas_delay[:3])
            except:
                meas_delay = 3
                print ('Delay exception')
            sleep(meas_delay) # sleep until measurement is ready
            uart1.write(str_j + 'D0!')  # tell sensor to send data
            sleep(.5)  # dealy to allow data to be sent to UART
            sensor_values = uart1.readall().decode().strip().replace('+',',') # read data
            now = time.localtime()
            time_stamp = "%04d/%02d/%02d,%02d:%02d:%02d" % (now[0], now[1], now[2], now[3], now[4], now[5])
            sensor_values=sensor_values.split(',') #  create list of values
            sensor_values.pop(0)  # remove sensor address from list
            sensor_values = [float(i) for i in sensor_values]  # clean data, convert to floats
            sensor_values.insert(0,time_stamp)  # add timestamp as first value of list
            measurement_dict[j] = sensor_values #  store in dict, sensor address is the key
    
        print(measurement_dict)
        return measurement_dict
    
    if __name__ == "__main__":
        sdi_setup()
        sensor_info()
        sensor_measurement()
    
    

    configsip: 0, SPIWP:0xee
    clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
    mode:DIO, clock div:1
    load:0x3fff8028,len:8
    load:0x3fff8030,len:1728
    load:0x4009fa00,len:0
    load:0x4009fa00,len:14584
    entry 0x400a059c
    WLAN connected ip 192.168.1.52 gateway 192.168.1.1
    SD card mounted
    None
    SD card found
    2018-07-22T01:17:48+00:00
    /sd/20180722.csv
    UART(1, baudrate=9600, bits=8, parity=None, stop=1)
    Using address: 0
    Using address: 1
    Traceback (most recent call last):
    File "main.py", line 5, in <module>
    File "/sd/sd_main.py", line 85, in <module>
    File "/sd/measure.py", line 32, in sensor_info
    NameError: name 'uart1' is not defined
    Pycom MicroPython 1.18.0 [v1.8.6-849-046b350] on 2018-06-01; WiPy with ESP32
    Type "help()" for more information.



  • @jmpratt I cannot believe that you have the same code on both WiPy's. Please reload the code to both devices, reset them and try.



  • @robert-hh Thanks. I'll rewrite that part of the code.

    However, why would this code work on one WiPy and not the other? That makes me thing there is another, hardware specific issue.



  • @jmpratt The error tells that uart1 is not defined in sensor_info(), which is obvious, because it is not in scope. You may have to declare uart1 global in sdi_setup(), sensor_info() and sensor_measurement, like you did with sdi_12_address.

    You may consider to put that all into a class, or hand over uart1 and sdi_12_address between the functions by return values and functions arguments. That make information passing more clear.


 

Pycom on Twitter