Alex Merchen
Published

Array Temperature Sensor with Helium

Log Grid-EYE temperatures comparing inside my house and outside to Google Sheets using a Raspberry Pi, Helium and the Google Cloud Platform.

IntermediateFull instructions provided3 hours371
Array Temperature Sensor with Helium

Things used in this project

Story

Read more

Schematics

Schematic

Raspberry pi wiring ikywp6oiam

Code

Original code

Python
from helium_client import Helium

from Adafruit_AMG88xx import Adafruit_AMG88xx

from time import sleep

import numpy as np

sensor = Adafruit_AMG88xx()

from datetime import datetime

helium = Helium("/dev/serial0")
helium.connect()

channel = helium.create_channel("test")
# sensor.readThermistor()
# 'Temp = {0:0.2f} *C'.format(sensor.readThermistor())
while(1):
    tempList = sensor.readPixels()
    channel.send('Start Transmission')
    sleep(5)
    for i in range(0,8):
        channel.send(str(i+1) + " of 8," + ','.join(str(x) for x in tempList[i*8:i*8+8]))
        sleep(5)

    errorCheck = np.zeros(8)
    for i in range (0, len(tempList)):
        for j in range(0,8):
            if i%8 == j:
                errorCheck[j] += tempList[i]
    channel.send('Sum,' + ','.join(str(x/8.0) for x in errorCheck))
    sleep(5)
    channel.send('End Transmission')
    sleep(10)

Final Code

Python
from helium_client import Helium

# Grid-EYE library is Adafruit_AMG88xx
from Adafruit_AMG88xx import Adafruit_AMG88xx

# This is used so that the device doesn't just constantly send
from time import sleep

# numpy is used to do math
import numpy as np

# configure sensor to be the Grid-EYE
sensor = Adafruit_AMG88xx()

# datetime is for logging
from datetime import datetime

# this is in the tutorial
helium = Helium("/dev/serial0")
helium.connect()

# the channel that I used was named test
channel = helium.create_channel("test")

# infinite loop
while(1):
  # tempList reads in the Pixels in a length 64 list
    tempList = sensor.readPixels()
  # convert from C to F
    tempList = np.multiply(tempList,1.8)+32
  # create a resultArray which is an 8x8 array
    resultArray = np.reshape(np.asarray([tempList]),(8,8))
  # create a minArray that takes the minimum value of each column
    minArray = np.amin(resultArray, axis=0)
  # send the current time along with the minArray spaced with commas
    channel.send(datetime.now().strftime('%Y-%m-%d %H:%M:%S') + ',' + ','.join(str(x) for x in minArray))
  # sleep for 20 seconds
    sleep(20)

Cloud Function

JavaScript
const {google} = require('googleapis');

exports.subscribe = (event, callback) => {
  // The Cloud Pub/Sub Message object.
  const pubsubMessage = event.data;

  // We're just going to log the message to prove that
  // it worked.
  console.log(Buffer.from(pubsubMessage.data, 'base64').toString());

  var jwt = getJwt();
  var apiKey = getApiKey();
  var spreadsheetId = 'Your Spreadsheet ID';
  var range = 'A1';
  if (Buffer.from(pubsubMessage.data, 'base64').toString().includes(",")){
    dataArray = Buffer.from(pubsubMessage.data, 'base64').toString().split(",");
    var row = dataArray
  } else {
    var row = [Buffer.from(pubsubMessage.data, 'base64').toString()];
  }
  appendSheetRow(jwt, apiKey, spreadsheetId, range, row);
  
  // Don't forget to call the callback.
  callback();
};

function getJwt() {
  var credentials = require("./credentials.json");
  return new google.auth.JWT(
    credentials.client_email, null, credentials.private_key,
    ['https://www.googleapis.com/auth/spreadsheets']
  );
}

function getApiKey() {
  var apiKeyFile = require("./api_key.json");
  return apiKeyFile.key;
}

function appendSheetRow(jwt, apiKey, spreadsheetId, range, row) {
  const sheets = google.sheets({version: 'v4'});
  sheets.spreadsheets.values.append({
    spreadsheetId: spreadsheetId,
    range: range,
    auth: jwt,
    key: apiKey,
    valueInputOption: 'RAW',
    resource: {values: [row]}
  }, function(err, result) {
    if (err) {
      throw err;
    }
    else {
      console.log('Updated sheet: ' + result.data.updates.updatedRange);
    }
  });
}

Credits

Alex Merchen

Alex Merchen

11 projects • 18 followers
I'm an EE with a Masters in ECE. I like building things.

Comments