I'm sorry. I happened to browse this article related to massives I couldn't do it, so I will stop browsing once.
This time, I combined the temperature sensor of rasberry pi 3 and 20 kinds of sensors, Challenge real-time graphs with python.
I bought a raspberry pi 3 and 20 types of sensors
To get the wiring procedure and data procedure, go to the URL below.
https://colo-ri.jp/develop/2016/05/raspberry-pi-ds18b20.html
Next is the wiring method: Here, the DQ pin is connected to GPIO4, the VDD pin is connected to 3.3V, and the GND pin is connected to GND. Then, pull up between the DQ pin and the VDD pin with a 10KΩ resistor.
Later, personally, I wanted to make a real-time graph of the values, so I created the following code. When executed, it is as shown in the image below (* It is a little difficult to see, but the real-time graph is displayed in the upper left and the real-time sensor etc. is displayed in the lower left. Ignore the prompt for the capture shot in the upper right.)
================================= ↓ Copy from here
#-*- coding: utf-8 -*-
#http://denshi.blog.jp/arduino/temperature-graph
import numpy as np
import matplotlib.pyplot as plt
import pygame
from pygame.locals import *
import serial
import sys
import os
import glob
from time import sleep
os.system('modprobe w1-gpio')
os.system('modprobe w1-gpio')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos + 2:]
temp_c = float(temp_string) / 1000.0
return temp_c
def main():
temps = [0]*100 #Temperature storage
t = np.arange(0,100,1)
plt.ion()
pygame.init() #Initialize Pygame
screen = pygame.display.set_mode((200, 200)) #Screen creation(100×100)
pygame.display.set_caption("Tilt angle") #Title bar
font = pygame.font.Font(None, 50) #Character setting
while True:
screen.fill((0,0,0)) #Clear screen
deviceTemp = read_temp()
print(read_temp())
temp = str(deviceTemp) # \Read up to n(\n is deleted)
text = font.render(temp + "[C]", False, (255,255,255)) #Setting of characters to be displayed
screen.blit(text, (10, 10)) #Render, display position
pygame.display.flip() #Refresh the screen to reflect the changes
#Updated list of temperature data
temps.pop(99)
temps.insert(0,float(temp))
#Graph display settings
line, = plt.plot(t, temps, 'r-',label="Temperature[C]") #Y-axis update
line.set_ydata(temps)
plt.title("Real-time temperature")
plt.xlabel("Time [s]")
plt.ylabel("Temperature [Celsius]")
plt.legend()
plt.grid()
plt.xlim([100,1])
plt.ylim([0,40])
plt.draw()
plt.clf()
#print(read_temp())
for event in pygame.event.get():
#End processing when the end button is pressed
if event.type == QUIT:
pygame.quit()
ser.close()
plt.close()
sys.exit()
if __name__ == '__main__':
main()
Recommended Posts