CostasVav
Posts: 28
Joined: Fri Jan 30, 2015 10:49 pm

AWS Greengrass lambda keeps failing after a while - RPi or GG issue?

Sat Mar 21, 2020 8:54 pm

I am running AWS Greengrass on my RPi to monitor some sensors and send the data back to AWS. I am reading measurements from an MCP3008 chip using the I2C interface.

When I run a Python script natively on the RPi, it can run indefinitely. When I have Greengass run a Python Lambda on the same device (very similar script, since I copy/pasted most of the code), it tends to fail after an hour or two with the following error in the GG logs:
[2020-03-12T10:28:59.787-05:00][ERROR]-ipc_client.py:67,<urlopen error [Errno 16] Device or resource busy>
[2020-03-12T10:28:59.787-05:00][ERROR]-Traceback (most recent call last):
[2020-03-12T10:28:59.787-05:00][ERROR]- File "/usr/lib/python3.7/urllib/request.py", line 1317, in do_open
[2020-03-12T10:28:59.787-05:00][ERROR]- File "/usr/lib/python3.7/http/client.py", line 1244, in request
[2020-03-12T10:28:59.787-05:00][ERROR]- File "/usr/lib/python3.7/http/client.py", line 1290, in _send_request
[2020-03-12T10:28:59.787-05:00][ERROR]- File "/usr/lib/python3.7/http/client.py", line 1239, in endheaders
[2020-03-12T10:28:59.787-05:00][ERROR]- File "/usr/lib/python3.7/http/client.py", line 1026, in _send_output
[2020-03-12T10:28:59.787-05:00][ERROR]- File "/usr/lib/python3.7/http/client.py", line 966, in send
[2020-03-12T10:28:59.787-05:00][ERROR]- File "/usr/lib/python3.7/http/client.py", line 938, in connect
[2020-03-12T10:28:59.787-05:00][ERROR]- File "/usr/lib/python3.7/socket.py", line 707, in create_connection
[2020-03-12T10:28:59.787-05:00][ERROR]- File "/usr/lib/python3.7/socket.py", line 748, in getaddrinfo
[2020-03-12T10:28:59.787-05:00][ERROR]-OSError: [Errno 16] Device or resource busy
[2020-03-12T10:28:59.787-05:00][ERROR]-During handling of the above exception, another exception occurred:
[2020-03-12T10:28:59.787-05:00][ERROR]-Traceback (most recent call last):
[2020-03-12T10:28:59.787-05:00][ERROR]- File "/runtime/python/greengrass_ipc_python_sdk/ipc_client.py", line 59, in wrapped
[2020-03-12T10:28:59.787-05:00][ERROR]- File "/runtime/python/greengrass_ipc_python_sdk/ipc_client.py", line 174, in post_work
[2020-03-12T10:28:59.787-05:00][ERROR]- File "/usr/lib/python3.7/urllib/request.py", line 222, in urlopen
[2020-03-12T10:28:59.787-05:00][ERROR]- File "/usr/lib/python3.7/urllib/request.py", line 525, in open
[2020-03-12T10:28:59.787-05:00][ERROR]- File "/usr/lib/python3.7/urllib/request.py", line 543, in _open
[2020-03-12T10:28:59.787-05:00][ERROR]- File "/usr/lib/python3.7/urllib/request.py", line 503, in _call_chain
[2020-03-12T10:28:59.787-05:00][ERROR]- File "/usr/lib/python3.7/urllib/request.py", line 1345, in http_open
[2020-03-12T10:28:59.787-05:00][ERROR]- File "/usr/lib/python3.7/urllib/request.py", line 1319, in do_open

Here is the actual Lambda function I am publishing to the RPi-GG core:

Code: Select all

import logging
import platform
from threading import Timer
import sys, time
import greengrasssdk
from spidev import SpiDev

class MCP3008:
    def __init__(self, bus = 0, device = 0):
        self.bus, self.device = bus, device
        self.spi = SpiDev()
        self.open()
        self.spi.max_speed_hz = 1000000 # 1MHz

    def open(self):
        self.spi.open(self.bus, self.device)
        self.spi.max_speed_hz = 1000000 # 1MHz
    
    def read(self, channel = 0):
        cmd1 = 4 | 2 | (( channel & 4) >> 2)
        cmd2 = (channel & 3) << 6

        adc = self.spi.xfer2([cmd1, cmd2, 0])
        data = ((adc[1] & 15) << 8) + adc[2]
        return data
            
    def close(self):
        self.spi.close()


# Setup logging to stdout
logger = logging.getLogger(__name__)
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)

# Creating a greengrass core sdk client
client = greengrasssdk.client("iot-data")

# Retrieving platform information to send from Greengrass Core
my_platform = platform.platform()

def read_MCP3008():

	try:
		mcp = MCP3008()
		raw_ch1 = mcp.read(0)
		raw_ch2 = mcp.read(1)
		raw_ch3 = mcp.read(2)
		raw_ch4 = mcp.read(3)
		raw_ch5 = mcp.read(4)
		raw_ch6 = mcp.read(5)
		raw_ch7 = mcp.read(6)
		raw_ch8 = mcp.read(7)
		
		client.publish(
			topic="iot_sensors/mcp3008/ch1", 
			queueFullPolicy="AllOrException", 
			payload=str(raw_ch1)
		)
		client.publish(
			topic="iot_sensors/mcp3008/ch2", 
			queueFullPolicy="AllOrException", 
			payload=str(raw_ch2)
		)
		client.publish(
			topic="iot_sensors/mcp3008/ch3", 
			queueFullPolicy="AllOrException", 
			payload=str(raw_ch3)
		)
		client.publish(
			topic="iot_sensors/mcp3008/ch4", 
			queueFullPolicy="AllOrException", 
			payload=str(raw_ch4)
		)
		client.publish(
			topic="iot_sensors/mcp3008/ch5", 
			queueFullPolicy="AllOrException", 
			payload=str(raw_ch5)
		)
		client.publish(
			topic="iot_sensors/mcp3008/ch6", 
			queueFullPolicy="AllOrException", 
			payload=str(raw_ch6)
		)
		client.publish(
			topic="iot_sensors/mcp3008/ch7", 
			queueFullPolicy="AllOrException", 
			payload=str(raw_ch7)
		)
		client.publish(
			topic="iot_sensors/mcp3008/ch8", 
			queueFullPolicy="AllOrException", 
			payload=str(raw_ch8)
		)
		
	except Exception as e:
		logger.error("Failed to publish message: " + repr(e))
		client.publish(
			topic="iot_sensors/mcp3008", 
			queueFullPolicy="AllOrException", 
			payload="Try statement failed: " + repr(e)
		)

	# Asynchronously schedule this function to be run again in 5 seconds
	Timer(1, read_MCP3008).start()

read_MCP3008()

def lambda_handler(event, context):
	return
What is causing this error 16?

Thanks!
Current Project: PiMac

Return to “Python”