Week 3: Raspberry Room Temperatures - mparra-mpz/week-of-code GitHub Wiki
Hardware Components
Software Components
Hardware Assembly
As we can see in the previous image the connection is very simple, in the tmp36gz sensor the left leg should be connected to the power (5 [V]), the right leg to the ground and the middle leg to the analog port 0. The raspberry and the arduino will be connected with a USB cable.
Software Development
The connection with the google docs was studied in week 2 project, so read this link first.
To stablish a serial connection with python you need install the pySerial module, I advice to use pip for the installation.
pip install pyserial
For the serial connection, I need the serial port name in my case is /dev/ttyACM0
, the baud rate in my case is 9600 and the timeout in my case is 3 seconds. While the serial port is delivering data (not empty) I storage the data in a string, after that I calculate the average temperature.
# Open Serial connection.
port = serial.Serial("/dev/ttyACM0", baudrate=9600, timeout=3.0)
# Read all the data in the serial port and store in a string.
data = ""
rcv = port.readline()
while rcv != "":
data = data + rcv
rcv = port.readline()
# Calculate the average temperature read from the serial port.
temperature = 0.0
sample = 0
data_list = data.split("\n")
for element in data_list:
if element != "":
sample = sample + 1
temperature = temperature + float(element)
temperature = temperature / sample
You can run the application in different ways, for example: python ambient-temperature.py -f ~/CODE/raspberry-pi-cd4c80393fcc.json
.
My final result looks:
You can check the current status in this link.