Serverless Monitoring heat remotely

So I attend a small church and I am responsible for the heating system.

That should be a simple task, but sometimes people fiddle with the heating system and sometimes the furnace simply stops.

And even though I live pretty close to the church I don't want to go there all the time to check the heat.

So I decided to setup remote heat monitoring in our church.

The way I have done it is I have setup a raspberry pi with a spark fun tmp102 temperature sensor and every 5 minutes, through a cron job, I send the data to AWS.

I decided to do it serverless since it is a way to small task to have a ec2 server running all the time.

I setup a lambda, where I can send a curl message to with the room and temperature.

I used the default template for a lambda and an API gateway to setup a Python lambda.

The following code is the lambda:

import boto3
import json


def respond(err, res=None):
    return {
        'statusCode': '400' if err else '200',
        'body': err.message if err else json.dumps(res),
        'headers': {
            'Content-Type': 'application/json',
        },
    }


def lambda_handler(event, context):
    operation = event['httpMethod']

    if operation != "POST":
        respond(ValueError('Unsupported method "{}"'.format(operation)))

    js = json.loads(event['body'])
    print(js["room"])
    print(js["temperature"])
    cw = boto3.client('cloudwatch')
    cw.put_metric_data(
    Namespace='temperature',
    MetricData=[
        {
            'MetricName': js["room"],
            'Value': js["temperature"]

        }"{}"'.
    ]
)

return respond(None, js)

This code is extermely naive and simply fails if not called correctly, for this use case that is just fine.

So if it is not called with room and temperature in a json it will return an internal server error. But I figure the only client is the bash script below and if it fails I will know through the fact that the metrics are missing.

As you can see this is really just the default template with me sending the room and temperature to CloudWatch.

I decided that if I wanted to go serverless Cloudwatch metrics would be the way to go.

So what is now left is to send the temperature from the tmp102 and send it to the lambda via a curl.

The following code is what I use to get the temperature from the tmp102 and send the curl

#!/bin/bash
hex_temp=$(i2cget -y 1 0x48)
dec_temp=$(printf "%d" $hex_temp)
echo $dec_temp
curl -X POST --data '{"temperature": '$dec_temp', "room": "kids"}' https://lambda-stuff.execute-api.us-east-1.amazonaws.com/prod/temperature

So the tmp102 sends the temperature in Celsius through hex. I convert it to decimal and send it in json to the lambda.

I set it up in a cron to send every 5th minute, and .... it didn't work.

After some googling I found this:

https://www.raspberrypi.org/forums/viewtopic.php?t=60702

What made it work was adding to the path of the crontab:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games

After that I simply called the above bash script every 5th minute.

And I now have a cloudwatch dashboard in AWS where I can see the temperature in the church.

And I can even set alarms if the temperature goes below or above a preset.