how to set up an escape?

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Rcayot
 
Posts: 321
Joined: Sat Feb 08, 2020 6:48 pm

how to set up an escape?

Post by Rcayot »

I have a program I am trying to get polished up. The issue is that using Thonny, hitting "stop" or control C dors not immediately stop the program, it may take several minutes to come back to the REPL prompt.

What is the syntax for adding an 'exception' to a ^C or some such so that the program will react more quickly?

Roger

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: how to set up an escape?

Post by mikeysklar »

ctrl-C is the correct command to stop the running program from the REPL.

ctrl-D will reload the code.

https://learn.adafruit.com/welcome-to-c ... n/the-repl

You could also stop the running program from within the code with quit(), exit() and sys.exit().

User avatar
Rcayot
 
Posts: 321
Joined: Sat Feb 08, 2020 6:48 pm

Re: how to set up an escape?

Post by Rcayot »

Thanks Mike.

My problem is that repeated control-C presses take up o two minutes to respond. I thought that adding an exception for keyboard interrupt would help.

Roger

User avatar
tannewt
 
Posts: 3304
Joined: Thu Oct 06, 2016 8:48 pm

Re: how to set up an escape?

Post by tannewt »

Please post the code you are running. We have had bugs where native code includes loops that ignore CTRL-C. Sharing the code can help us determine if that is the case here.

User avatar
Rcayot
 
Posts: 321
Joined: Sat Feb 08, 2020 6:48 pm

Re: how to set up an escape?

Post by Rcayot »

here is the problem code.

Code: Select all

# SPDX-FileCopyrightText: 2020 Jerry Needell for Adafruit Industries
# SPDX-License-Identifier: MIT
#modified by Roger Ayotte 11/12/2021
# Example to send a packet periodically between addressed nodes with ACK

import time
import board
import busio
import digitalio
import analogio
import asyncio
#import adafruit_bme280
import adafruit_rfm9x
#import adafruit_ahtx0

adcV= analogio.AnalogIn(board.VOLTAGE_MONITOR)
voltage = adcV.value

adcW=analogio.AnalogIn(board.GP26)
wind = adcW.value

# Create library object using our Bus I2C port
from adafruit_bme280 import basic as adafruit_bme280
# i2c = board.I2C()  # uses board.SCL and board.SDA
i2c = busio.I2C(scl=board.GP17, sda=board.GP16)
bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c)
bme280.sea_level_pressure = 1013.25

i2c1=busio.I2C(scl= board.GP15, sda = board.GP14)
newsensor = adafruit_ahtx0.AHTx0(i2c1)
# set the time interval (seconds) for sending packets
delay_interval = 600
Wdelay_interval = 30
Wcount = 0
# Define radio parameters.
RADIO_FREQ_MHZ = 915.0  # Frequency of the radio in Mhz. Must match your
# module! Can be a value like 915.0, 433.0, etc.

# Define pins connected to the chip.
# set GPIO pins as necessary -- this example is for Raspberry Pi
CS = digitalio.DigitalInOut(board.GP5)
RESET = digitalio.DigitalInOut(board.GP6)

# Initialize SPI bus.
# for Adafruit boards
#spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)

# for PICO
spi = busio.SPI(clock=board.GP2, MOSI=board.GP3, MISO=board.GP4)

# Initialze RFM radio
rfm9x = adafruit_rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ)

# Optionally set an encryption key (16 byte AES key). MUST match both
# on the transmitter and receiver (or be set to None to disable/the default).
#rfm69.encryption_key = (
 #   b"\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
#)

# set delay before sending ACK
rfm9x.ack_delay = 0.1
# set node addresses
rfm9x.node = 3
rfm9x.destination = 2
# initialize counter
counter = 0
ack_failed_counter = 0
# send startup message from my_node
rfm9x.send_with_ack(bytes("startup message from node {}".format(rfm9x.node), "UTF-8"))

# Wait to receive packets.
# print("Waiting for packets...")
# initialize flag and timer
weather_time_now = time.monotonic()
wind_time_now = time.monotonic()

async def get_temperature2(interval):
    while True:
        temperature = str(round((newsensor.temperature), 2))
        #temperature = round(temperature, 2)
        #temperature=str(temperature)
        rfm9x.flags=1
        print(rfm9x.flags, temperature)
        if not rfm9x.send_with_ack(temperature.encode("utf-8")):
            print("No Ack T") 
        await asyncio.sleep(interval)

async def get_humidity(interval):
    while True:
        humidity = (newsensor.relative_humidity)
        humidity = round(humidity, 2)
        humidity = str(humidity)
        rfm9x.flags=3
        print(rfm9x.flags, humidity)
        if not rfm9x.send_with_ack(humidity.encode("utf-8")):
            print("No Ack H") 
        await asyncio.sleep(interval)
    
async def get_pressure(interval):
    while True:
        pressure = (bme280.pressure)
        pressure = round(pressure, 1)
        pressure = str(pressure)
        rfm9x.flags=2
        print(rfm9x.flags, pressure)
        if not rfm9x.send_with_ack(pressure.encode("utf-8")):
            #ack_failed_counter += 1
            print(" No Ack P") 
        await asyncio.sleep(interval)

async def get_voltage(interval):
    while True:
        #voltage = (adcV.value)
        voltage = str(round((adcV.value*3.3*3/65535), 2))
        #voltage=str(voltage)
        rfm9x.flags=4
        print(rfm9x.flags, voltage)
        if not rfm9x.send_with_ack(voltage.encode("utf-8")):
            ack_failed_counter += 1
            print(" No Ack H")    
        await asyncio.sleep(interval)

async def get_wind(interval):
    wsample = 0
    wdata = []
    WindAV = []
    Wcount = 0
    Awind = 0
    while Wcount <=15:
        for i in range(200):
            wsample =(((adcW.value*0.00005035)-0.42)*32.4*(2.0-0.42))
            time.sleep(.01)
            if wsample < 0:
                wsample = 0
            wdata.append(wsample)
        waverage = sum(wdata)/len(wdata)
        wdata.clear()
        WindAV.append(waverage)
        Wcount = Wcount + 1
        if Wcount == 15:
            Awind = sum(WindAV)/len(WindAV)
            Awind = round(Awind, 2)
            Awind = str(Awind)
            rfm9x.flags = 5
            print(rfm9x.flags , Awind)
            if not rfm9x.send_with_ack(Awind.encode("utf-8")):
                print("No Ack W")
            rfm9x.flags = 6
            Gust = max(WindAV)
            Gust=round(Gust, 2)
            Gust = str(Gust)
            print(rfm9x.flags, Gust)
            if not rfm9x.send_with_ack(Gust.encode("utf-8")):
                print("No Ack W")
            WindAV.clear()
            Wcount = 0
        await asyncio.sleep(interval)
        
async def main():
    temperature2_task = asyncio.create_task(get_temperature2(delay_interval))
    pressure_task = asyncio.create_task(get_pressure(delay_interval))                                    
    humidity_task = asyncio.create_task(get_humidity(delay_interval))
    voltage_task = asyncio.create_task(get_voltage(delay_interval))
    wind_task = asyncio.create_task(get_wind(Wdelay_interval))
    await asyncio.gather(voltage_task, pressure_task, humidity_task, wind_task, temperature2_task)  # Don't forget "await"!
    await(voltage_task)                     
asyncio.run(main())



User avatar
tannewt
 
Posts: 3304
Joined: Thu Oct 06, 2016 8:48 pm

Re: how to set up an escape?

Post by tannewt »

What is the backtrace you get when it finally does get interrupted? That should tell us where it gets stuck.

User avatar
Rcayot
 
Posts: 321
Joined: Sat Feb 08, 2020 6:48 pm

Re: how to set up an escape?

Post by Rcayot »

Not sure there is one. I will try a few more times and captue the info.

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”