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
- First, format the micro SD card using SD Card Formatter (available here: sdcard.org)
- Next, download NOOBS Lite from raspberrypi.org
- Extract the files from the NOOBS Lite zip file into a folder on your computer, then copy the files to the formatted SD card
- Plug in the micro-SD card, the Ethernet cable, the keyboard, mouse, video cable, and power supply (in that order).
- 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.
- After the RPI reboots, follow prompts to complete the installation process.
- 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
- With the power disconnected, connect the ADC hat to the RPi, then connect the GPIO breakout cable.
- Use jumper wires and a breadboard to connect up the sensors to power, ground, and the ADC ports as shown in the image below:
- Reconnect power to the RPi.
- Once you have logged in to the RPi, make sure to enable SPI interface in Preferences.
- 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