Raspberry Pi Temperature / Humidity Sensor Setup for PAPER Polarimetry

Supplies list

Here is a list of materials needed for the Raspberry Pi, sensors, etc. ...

RPi Configuration Steps

  1. First, format the micro SD card using SD Card Formatter (available here: sdcard.org) DONE
  2. Next, download NOOBS Lite from raspberrypi.org DONE
  3. Extract the files from the NOOBS Lite zip file into a folder on your computer, then copy the files to the formatted SD card DONE
  4. Plug in the micro-SD card, the Ethernet cable, the keyboard, mouse, video cable, and power supply (in that order).
  5. Select Raspbian as the operating system in the pop-up, and select English as the language and a U.S. keyboard layout. Then click Install, and wait for the installation process to complete.
  6. After the RPI reboots, follow prompts to complete the installation process.
  7. Go to Preferences -> Raspberry Pi Configuration -> Interfaces and enable VNC connection. Then note down the IP address and connect to the Pi through VNC.

Reading sensor data using an RPi, an MCP3208 ADC hat, and a GPIO breakout cable

Here are the docs for adc interfaces through gpiozero: gpiozero docs

  1. With the power disconnected, connect the ADC hat to the RPi, then connect the GPIO breakout cable.
  2. Use jumper wires and a breadboard to connect up the sensors to power, ground, and the ADC ports as shown in the image below:
    raspberry-pi-final-setup.jpg
  3. Reconnect power to the RPi.
  4. Once you have logged in to the RPi, make sure to enable SPI interface in Preferences.
  5. To record sensor readings in a text file on your host computer (i.e., not the RPi but the computer that is attached to the PAPER antenna back end), you must run two programs simultaneously on the RPi and your host computer to set up a data socket. First, run the program below on the RPi:

#rpi-sensor-data.py

import gpiozero
import socket
import time

def main():
    INT_TIME = 10
    HEADERSIZE = 10

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.bind(('ENTER RPI'S IP ADDRESS HERE', 1234))
    s.listen(5)
    
    temp_0 = gpiozero.MCP3208(channel=0, differential=True) #read in voltages from the appropriate channels
    hum_v = gpiozero.MCP3208(channel=2, differential=True, max_voltage=5)

    while True:
        # now our endpoint knows about the OTHER endpoint.
        clientsocket, address = s.accept()
        print("Connection from {0} has been established.".format(address))
        print("\nSending Raspberry Pi sensor data to client...")

        while True:

            initial_time = time.time()
            tot_time = initial_time
            total_temp_voltage = 0
            total_humidity_voltage = 0
            i = 1

            while time.time() - initial_time <= INT_TIME:
                tot_time += time.time()

                diff_v = temp_0.voltage #get temp sensor voltage
                total_temp_voltage += diff_v

                curr_hum_v = hum_v.voltage #get humidity voltage
                total_humidity_voltage += curr_hum_v
                
                i = i + 1

            #average sensor voltages over the amount of time specified
            temp_voltage = round((total_temp_voltage / i), 5)

            #convert temperature voltage to actual temperature
            #using crude calibration
            actual_temp = round((temp_voltage / 0.01)+14.8, 5)
            hum_voltage = round((total_humidity_voltage / i), 5)
    
            time_stamp = time.ctime(tot_time / i)

            msg = "{0:<15}{1:<20}{2:<20}".format(actual_temp, hum_voltage, time_stamp)
            msg = "{0:<{1}}".format(len(msg), HEADERSIZE)+msg

            clientsocket.send(bytes(msg,"utf-8"))


if __name__ == "__main__":
  main()

Then run the following program on the host computer:

#rpi-client.py
import socket
import datetime
import time

HEADERSIZE = 10

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('ENTER RPI'S IP ADDRESS HERE', 1234))

#today = time.gmtime(time.clock())
fname_date = time.strftime("%m_%d_%Y_%H-%M-%S", time.gmtime())

ofName = "RPI_DATA_"+fname_date+".txt"
outFile = open(ofName, 'w')

print("\nAccepting data from the RPI, saving to file {}...\n".format(ofName))
print("\n{0:<15}{1:<20}{2:<20}".format("Temp. (*C)","Humidity Voltage","Time Stamp"))

while True:
    full_msg = ''
    new_msg = True
    while True:
        msg = s.recv(128)
        if new_msg:
            #print("new msg len:",msg[:HEADERSIZE])
            msglen = int(msg[:HEADERSIZE])
            new_msg = False

        #print("full message length: {0}".format(msglen))

        full_msg += msg.decode("utf-8")

        #print(len(full_msg))


        if len(full_msg)-HEADERSIZE == msglen:
            #print("full msg recvd")
            print(full_msg[HEADERSIZE:])
            outFile.write(full_msg[HEADERSIZE:]+"\n")
            new_msg = True
            full_msg = ""

You should see a stream of sensor readings populate the terminal window; this will also create a text file with sensor readings. The program will continue to run until you do a keyboard interrupt.

You can find a file containing sample data with calibrated sensor data and time stamps at this link -- note that the integration time in this file is 10 s: RPI_DATA_06_06_2019_20-47-53.txt

*Special thanks to Python Programming Tutorials for providing open-source code which was very useful in establishing the data socket.

-- EllieWhite - 2019-05-28
Topic attachments
I Attachment Action Size Date Who Comment
RPI_DATA_06_06_2019_20-47-53.txttxt RPI_DATA_06_06_2019_20-47-53.txt manage 732 bytes 2019-06-06 - 16:50 UnknownUser  
outfile-new.txttxt outfile-new.txt manage 3 K 2019-06-05 - 13:04 UnknownUser  
outfile.txttxt outfile.txt manage 1 K 2019-06-05 - 11:28 UnknownUser  
raspberry-pi-final-setup.jpgjpg raspberry-pi-final-setup.jpg manage 331 K 2019-06-06 - 16:35 UnknownUser  
Topic revision: r8 - 2019-06-06, EllieWhite
This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding NRAO Public Wiki? Send feedback