0

PyPortal hangs on temperature sensor read
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

PyPortal hangs on temperature sensor read

by bretthoward on Mon Feb 10, 2020 3:35 am

I can't for the life of me figure this out but even if I just upload the sample code whenever I try to read from the adt7410 sensor the whole thing packs it in and dies.

I have all the setup code in place and when I run this I only see the first got here and the whole board stops running and the status light dies and everything.

print("got here")
degrees_celsius = tempSensor.temperature
print("got here2"

~Brett

bretthoward
 
Posts: 37
Joined: Thu Dec 21, 2017 8:47 pm

Re: PyPortal hangs on temperature sensor read

by dastels on Mon Feb 10, 2020 10:46 am

I'm assuming the missing parenthesis at the end was a copy/paste mistake?

Dave

dastels
 
Posts: 1643
Joined: Tue Oct 20, 2015 3:22 pm

Re: PyPortal hangs on temperature sensor read

by bretthoward on Mon Feb 10, 2020 1:44 pm

Is that supposed to be a function call? I thought it was just a member as they didn't use it in this example here (see line 83):
https://learn.adafruit.com/pyportal-sma ... cuitpython

Oh that parenthesis at the end. Yes... That was a copy paste error.

bretthoward
 
Posts: 37
Joined: Thu Dec 21, 2017 8:47 pm

Re: PyPortal hangs on temperature sensor read

by dastels on Mon Feb 10, 2020 1:53 pm

temperature is implemented as a property, allowing it to be used without parens. It's an implicit function call.

Can you post all your code, please?

A good approach is to strip away everything but the adt setup and use. I.e. isolate the symptom, then find & fix the cause.

Dave

dastels
 
Posts: 1643
Joined: Tue Oct 20, 2015 3:22 pm

Re: PyPortal hangs on temperature sensor read

by bretthoward on Mon Feb 10, 2020 10:26 pm

Code: Select all | TOGGLE FULL SIZE
import time
import rtc
import board
import busio
from digitalio import DigitalInOut
from analogio import AnalogIn
import neopixel
from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager
import microcontroller

from adafruit_pyportal import PyPortal

import adafruit_adt7410 #temperature sensor

import random

import audioio

from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError

print("BTHBilge Starting...")

# Get wifi details and more from a secrets.py file
try:
    from secrets import secrets
except ImportError:
    print("WiFi secrets are kept in secrets.py, please add them there!")
    raise

#Setup ESP32 pins
esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)
#get ESP32 handles
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)

#instantiate neopixel
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=1)

#Start Wifi Manager
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)

#Init Temp Sensor
i2c_bus = busio.I2C(board.SCL, board.SDA)
tempSensor = adafruit_adt7410.ADT7410(i2c_bus, address=0x48)
tempSensor.high_resolution = True

# init. the light sensor
light_sensor = AnalogIn(board.LIGHT)

# Create an instance of the IO_HTTP client
io = IO_HTTP(secrets['aio_username'], secrets['aio_key'], wifi)

# Get the feeds from Adafruit IO
try:
    # Get the 'temperature' feed from Adafruit IO
    test_feed = io.get_feed('test')
except AdafruitIO_RequestError:
    # If no 'temperature' feed exists, create one
    test_feed = io.create_new_feed('test')

counter = random.randint(0,20)

def play_sound_file(file_path):
    try:
        with open(file_path, "rb") as f:
            wave = audioio.WaveFile(f)
            audio.play(wave)
            while audio.playing:
                time.sleep(0.005)
    except OSError as e:
        print('Error opening file: %s' % e)
       
def disconnected(client):
    # Disconnected function will be called when the client disconnects.
    print('Disconnected from Adafruit IO!')
    sys.exit(1)

def message(client, feed_id, payload):
    # Message function will be called when a subscribed feed has a new value.
    # The feed_id parameter identifies the feed, and the payload parameter has
    # the new value.
    print('\t Feed {0} received new value: {1}'.format(feed_id, payload))
   
def set_time(con,tz):
  while True:
    try:
      response = con.get("http://worldtimeapi.org/api/timezone/"+tz)
      tinfo    = response.json()
      print("setting current time to: \n" + tinfo['datetime'])
      rtc.RTC().datetime = time.localtime(tinfo['unixtime']+tinfo['raw_offset'])
      response.close()
      return
    except Exception as ex:
      print("exception in set_time:")
      print(ex)

set_time(wifi, secrets['timezone'])

while False:
    #degrees_celsius = tempSensor.temperature
    degrees_celsius = light_sensor.value
    #degrees_celsius = microcontroller.cpu.temperature
    print("Temperature: {} C".format(degrees_celsius))
    time.sleep(1)

while False:
    with audioio.AudioOut(board.AUDIO_OUT) as audio:  # or board.A0
        play_sound_file("alarm.wav")

while True:
    try:
        print("Posting data...", end='')
        io.send_data(test_feed['key'], counter)
        print("posted: " + str(counter) + " at " + str(time.localtime(time.time()).tm_hour) + ":" + str(time.localtime(time.time()).tm_min) )
        counter = light_sensor.value
    except (ValueError, RuntimeError) as e:
        print("Failed to get data, retrying\n", e)
        wifi.reset()
        continue
    time.sleep(1)

bretthoward
 
Posts: 37
Joined: Thu Dec 21, 2017 8:47 pm

Re: PyPortal hangs on temperature sensor read

by bretthoward on Mon Feb 10, 2020 10:29 pm

The first while False block under the set_time call is the block of code that I was running into issues with. You'll notice that I had a couple other implementations in there. The light sensor value and the microcontroller temp lines of code work but the one that reads the temperature from the ADI part pukes and it dies on the vine... Basically this is me playing with hunks of code from examples and trying to get a basic plan in place before I start a real design.... I've even pasted in examples directly that fail to work....

Thanks for any help!!!

bretthoward
 
Posts: 37
Joined: Thu Dec 21, 2017 8:47 pm

Re: PyPortal hangs on temperature sensor read

by dastels on Tue Feb 11, 2020 11:30 am

Do you get any output in the REPL?

dastels
 
Posts: 1643
Joined: Tue Oct 20, 2015 3:22 pm

Re: PyPortal hangs on temperature sensor read

by bretthoward on Tue Feb 11, 2020 1:11 pm

If I use the other method yes.... But if I use the temperature method (the first of the three) it sets the time and then dies completely.

I also just tried typing the commands manually into REPL and the same thing happens. Everything works until the read of the temp and then I simply don't get a prompt in return.

bretthoward
 
Posts: 37
Joined: Thu Dec 21, 2017 8:47 pm

Re: PyPortal hangs on temperature sensor read

by tannewt on Tue Feb 11, 2020 2:33 pm

What happens if you press CTRL-C while it is hung? Does it produce a backtrace?

tannewt
 
Posts: 1490
Joined: Thu Oct 06, 2016 8:48 pm

Re: PyPortal hangs on temperature sensor read

by adafruit_support_carter on Tue Feb 11, 2020 2:47 pm

You've got a lot going on. Not sure if just a basic check of reading the ADT7410 has been done? This:
I also just tried typing the commands manually into REPL and the same thing happens. Everything works until the read of the temp and then I simply don't get a prompt in return.

sounds like an attempt? Trying things via REPL is a good idea. Can you copy/paste what you actually tried here.

Or, try this and paste what happens in your case:
Code: Select all | TOGGLE FULL SIZE
Adafruit CircuitPython 4.1.2 on 2019-12-18; Adafruit PyPortal with samd51j20
>>> import board
>>> import adafruit_adt7410
>>> tempSensor = adafruit_adt7410.ADT7410(board.I2C(), address=0x48)
>>> tempSensor.high_resolution = True
>>> tempSensor.temperature
28.7188
>>>

adafruit_support_carter
 
Posts: 14475
Joined: Tue Nov 29, 2016 2:45 pm

Re: PyPortal hangs on temperature sensor read

by bretthoward on Tue Feb 11, 2020 4:26 pm

Just tried things via the REPL and then hit CTRL+C and got the following:

Code: Select all | TOGGLE FULL SIZE
Adafruit CircuitPython 4.1.2 on 2019-12-18; Adafruit PyPortal with samd51j20
>>>
>>> import busio
>>> import board
>>> import adafruit_adt7410
>>> i2c_bus = busio.I2C(board.SCL, board.SDA)
>>> tempSensor = adafruit_adt7410.ADT7410(i2c_bus, address=0x48)
>>> tempSensor.high_resolution = True
>>> a = tempSensor.temperature
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "adafruit_adt7410.py", line 91, in temperature
  File "adafruit_register/i2c_bit.py", line 58, in __get__
  File "adafruit_register/i2c_bit.py", line 58, in __get__
  File "adafruit_bus_device/i2c_device.py", line 146, in write_then_readinto
KeyboardInterrupt:
>>>

bretthoward
 
Posts: 37
Joined: Thu Dec 21, 2017 8:47 pm

Re: PyPortal hangs on temperature sensor read

by adafruit_support_carter on Tue Feb 11, 2020 5:22 pm

Thanks for trying that. It's interesting - the basic I2C comms are working, since you were able to init the sensor and even set the resolution. That all required talking to the sensor over I2C. It seems to be getting stuck in this loop:

https://github.com/adafruit/Adafruit_Ci ... 410.py#L90

Not sure what could cause the sensor to get in that state. Can you try the following, which will dump all of the register values and we can maybe see something about the sensor configuration that will give a clue.
Code: Select all | TOGGLE FULL SIZE
Adafruit CircuitPython 4.1.2 on 2019-12-18; Adafruit PyPortal with samd51j20
>>> import board
>>> import adafruit_adt7410
>>> tempSensor = adafruit_adt7410.ADT7410(board.I2C(), address=0x48)
>>> tempSensor.high_resolution = True
>>> for reg in range(0xC):
...     print("{}:0b{:08b}".format(hex(reg),tempSensor._read_register(reg)[0]))
...
0x0:0b00001111
0x1:0b10100110
0x2:0b10000000
0x3:0b10000000
0x4:0b00100000
0x5:0b00000000
0x6:0b00000101
0x7:0b00000000
0x8:0b01001001
0x9:0b10000000
0xa:0b00000101
0xb:0b11001011
>>>

adafruit_support_carter
 
Posts: 14475
Joined: Tue Nov 29, 2016 2:45 pm

Re: PyPortal hangs on temperature sensor read

by bretthoward on Tue Feb 11, 2020 8:18 pm

There are some differences...

Code: Select all | TOGGLE FULL SIZE
Adafruit CircuitPython 4.1.2 on 2019-12-18; Adafruit PyPortal with samd51j20
>>> import board
>>> import adafruit_adt4710
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: no module named 'adafruit_adt4710'
>>> import adafruit_adt7410
>>> tempSensor = adafruit_adt7410.ADT7410(board.I2C(), address=0x48)
>>> tempSensor.high_resolution = True
>>> for reg in range(0xC):
...     print("{}:0b{:08b}".format(hex(reg),tempSensor._read_register(reg)[0]))
...
0x0:0b00001100
0x1:0b10011100
0x2:0b10000000
0x3:0b10001100
0x4:0b00100000
0x5:0b00000000
0x6:0b00000101
0x7:0b00000000
0x8:0b01001001
0x9:0b10000000
0xa:0b00000101
0xb:0b11001011
>>>

bretthoward
 
Posts: 37
Joined: Thu Dec 21, 2017 8:47 pm

Re: PyPortal hangs on temperature sensor read

by adafruit_support_carter on Tue Feb 11, 2020 8:44 pm

Thanks again for trying these tests. The hope here is that this is something we can recover from since we appear to still be able to talk to the sensor. But we need to figure out what's going on first.

From that output, the two interesting ones are these:
Code: Select all | TOGGLE FULL SIZE
0x2:0b10000000
0x3:0b10001100


The 0x2 one is the status register. The 7th bit is the data ready bit. That needs to go to 0 at some point to indicate a data value is ready. That is what that loop is checking for.

The 0x3 one is the configuration register. The7th bit being 1 is expected - that sets the resolution. But it's odd because it has the 2nd and 3rd bits set. Those bits just change the polarity of some output pins that aren't really being used. So shouldn't matter. But how/why did they become set? When you setup the sensor, a software reset is done, so they should go back to their default values of 0. Is the reset not working?

Let's check that. Try these sets of commands:
Code: Select all | TOGGLE FULL SIZE
Adafruit CircuitPython 4.1.2 on 2019-12-18; Adafruit PyPortal with samd51j20
>>> import board
>>> import adafruit_adt7410
>>> tempSensor = adafruit_adt7410.ADT7410(board.I2C(), address=0x48)
>>> tempSensor.high_resolution = True
>>> tempSensor._read_register(0x03)
bytearray(b'\x80')
>>> tempSensor._write_register(0x2f)
>>> tempSensor._read_register(0x03)
bytearray(b'\x00')
>>> 


The write to 0x2f is the reset. You can see how before the reset, the 0x03 register was 0x80 because we set the resolution, but after the rest it went back to 0x00. Wondering how yours behaves?

adafruit_support_carter
 
Posts: 14475
Joined: Tue Nov 29, 2016 2:45 pm

Re: PyPortal hangs on temperature sensor read

by bretthoward on Tue Feb 11, 2020 9:34 pm

Seems to work (ignore my spelling error in there):

Code: Select all | TOGGLE FULL SIZE
Adafruit CircuitPython 4.1.2 on 2019-12-18; Adafruit PyPortal with samd51j20
>>>
>>>
>>> import board
>>> import adafruit_adt7410
>>> tempSensor = adafruit_adt7410.ADT7410(board.I2C(), address=0x48)
>>> tempSensor.high_resolution = True
>>> tempSensor._read_register(0x03)
bytearray(b'\x8a')
>>> tempsensor._write_register(0x2f)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'tempsensor' is not defined
>>> tempSensor._write_register(0x2f)
>>> tempSensor._read_register(0x03)
bytearray(b'\x00')
>>>

bretthoward
 
Posts: 37
Joined: Thu Dec 21, 2017 8:47 pm

Please be positive and constructive with your questions and comments.