-1

Okay so I have a AMG8833 hooked up to my Raspberry Pi 4. I created a python script that outputs the temperature readings from the sensor along with creating a graphical interface.

My issue is understanding how I could get this output from the shell and create a csv file that I could then move to a different computer and analyze it in Excel. Below is the code I have.

import os
import math
import time

import busio
import board

import numpy as np
import pygame
from scipy.interpolate import griddata

from colour import Color

import adafruit_amg88xx

    

i2c_bus = busio.I2C(board.SCL, board.SDA)

#low range of the sensor (this will be blue on the screen)
MINTEMP = 17.

#high range of the sensor (this will be red on the screen)
MAXTEMP = 38.

#how many color values we can have
COLORDEPTH = 1024

os.putenv('SDL_FBDEV', '/dev/fb1')
pygame.init()

#initialize the sensor
sensor = adafruit_amg88xx.AMG88XX(i2c_bus)

# pylint: disable=invalid-slice-index
points = [(math.floor(ix / 8), (ix % 8)) for ix in range(0, 64)]
grid_x, grid_y = np.mgrid[0:7:32j, 0:7:32j]
# pylint: enable=invalid-slice-index

#sensor is an 8x8 grid so lets do a square
height = 320
width = 320

#the list of colors we can choose from
blue = Color("indigo")
colors = list(blue.range_to(Color("red"), COLORDEPTH))

#create the array of colors
colors = [(int(c.red * 255), int(c.green * 255), int(c.blue * 255)) for c in colors]

displayPixelWidth = width / 30
displayPixelHeight = height / 30

lcd = pygame.display.set_mode((width, height))

lcd.fill((255, 0, 0))

pygame.display.update()
pygame.mouse.set_visible(False)

lcd.fill((0, 0, 0))
pygame.display.update()

#some utility functions
def constrain(val, min_val, max_val):
    return min(max_val, max(min_val, val))

def map_value(x, in_min, in_max, out_min, out_max):
    return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min

#let the sensor initialize
time.sleep(.9)

while True:

    #read the pixels
    pixels = []
    for row in sensor.pixels:
        pixels = pixels + row
        print(max(pixels))
    pixels = [map_value(p, MINTEMP, MAXTEMP, 0, COLORDEPTH - 1) for p in pixels]

    #perform interpolation
    bicubic = griddata(points, pixels, (grid_x, grid_y), method='cubic')

    #draw everything
    for ix, row in enumerate(bicubic):
        for jx, pixel in enumerate(row):
            pygame.draw.rect(lcd, colors[constrain(int(pixel), 0, COLORDEPTH- 1)],
                             (displayPixelHeight * ix, displayPixelWidth * jx,
                              displayPixelHeight, displayPixelWidth))

    pygame.display.update()

1 Answers1

0

You might try this similar question. In short: build your desired output as a string as you go. Once you're done, write the output string to a CSV, which the link above shows how to do.

EDIT: The long and short of it is you need a single string to write to a CSV, the string needs separators (hence "comma separated values") to distinguish distinct elements of your CSV, and the string needs newline characters (\n) to distinguish distinct rows of your CSV. A simple program that will make a CSV with 5 columns and 10 rows is:

ncols = 5
nrows = 10

output_to_csv = ''

for i in range(nrows):
    for j in range(ncols):
        if j == 0:
            # we don't need commas on the first entry of a row
            output_to_csv += str(i * j)
        else:
            # we do want commas everywhere else
            output_to_csv += ', ' + str(i * j)
    # when we get to the end of a row, start a new line
    output_to_csv += '\n'

print(output_to_csv)
f = open('test.csv', 'w')
f.write(output_to_csv)

Hope that helps!

Jace
  • 177
  • 6