Ringo who joined a web company for some reason from April I ended up touching Laravel, MySQL, and Ansible, which I had never touched in training. So, after studying, I will visualize the room using the Raspberry Pi and the sensor that were in the room. It seems that you did something similar with the title Making a temperature monitoring bot with ESP8266 and Raspberry Pi.
Roughly, I will try to meet the following conditions.
--Monitor temperature, humidity, and atmospheric pressure ――I want to be able to see it not only from inside the house but also from the outside ――I want to graphically display not only the current data but also changes in the day.
--Raspberry Pi, I used 3b + in this article --Temperature / humidity sensor BME280 --0.96 inch OLED
Consider the configuration based on the specifications. The sensor and OLED are connected by I2C communication by piercing the breadboard. The acquired data will be saved in Raspberry Pi and thrown to the instance set up on CONOHA. The processing procedure is as follows.
I will make a process according to the procedure of. In this article, I will explain 1-3.
Since it is I2C communication, just connect SDI and SCK, and the module is 3.3V, so be careful. ʻI2cdetect -y 1` If it is confirmed and recognized, it's okay
In the image, there are other connections, but 0x3C is the OLED address and 0x76 is the BME280 address.
In I2C, there are various things such as pull-up is necessary, but I will proceed assuming that it is on the board on the sensor side. However, I2C communication itself is not resistant to noise, so it may be better not to stretch it too much.
The library used the SWITCH CIENCE sample. SWITCHSCIENCE/BME280 It's written for Python2, so I'll edit it a bit. As a change
--Rewrite the writing method of the print part for Python3 --Changed so that it can be acquired as a return value so that it is easy to use what was printed in the function that corrects the acquired raw data.
The edited code will be here. You will also need smbus to run it.
I was able to confirm the acquisition of the value on the terminal.
The liquid crystal used is an OLED module equipped with SSD1306. I can't write because I don't remember where I bought it, but I think it's almost the same no matter where I bought it. I used the library published by Adafruit. adafruit/Adafruit_Python_SSD1306
I need some tips to move it. Move the sample code of the organic EL display on Raspberry Pi Refer to the article above and install the necessary libraries on pyenv so that you can use OLED. Add the processing of bme280 by referring to Adafruit_Python_SSD1306 / examples / stats.py.
#Clear screen
draw.rectangle((0,0,width,height), outline=0, fill=0)
sensor_data = bme280.readData() #Acquisition of sensor data
temp = "%6.2f" % (sensor_data[0])
draw.text((x, top), "Temp =" + temp + " °C", font = font, fill = 255)
press = "%7.2f" % (sensor_data[1])
draw.text((x, top + 8), "Press = " + press + " hPa", font = font, fill = 255)
hum = "%6.2f" % (sensor_data[2])
draw.text((x, top + 16), "Hum =" + hum + " %", font = font, fill = 255)
#Screen display
disp.image(image)
disp.display()
Extracted the processing in the while loop. As for the content of the process, the data is acquired from the sensor and displayed line by line.
I was able to display the acquired data on the OLED.
I said save to the cloud, but I will save it to csv for the time being.
#File writing process
date = str(datetime.date.today())
nowtime = datetime.datetime.now()
#Overwrite the file if it exists
if(os.path.exists(path + date +".csv")):
f = open(path + date +".csv", 'a')
f.write(str(nowtime.hour) + ':' + str(nowtime.minute) + ':' + str(nowtime.second) + ',')
f.write(temp + ',' + press + ',' + hum + ' \n')
f.close()
#Generate if not
else:
f = open(path + date +".csv", 'w')
f.write("date,Temp,Press,Hum\n")
f.write(str(nowtime.hour) + ':' + str(nowtime.minute) + ':' + str(nowtime.second) + ',')
f.write(temp + ',' + press + ',' + hum + ' \n')
f.close()
I wrote the process so that it is separated by date and saved in csv. When the output graph is displayed in Excel, it looks like this.
The final program is posted on gist. raspi_logger.py Next time, I will try to create an API using Laravel and the cloud and display the graph on the browser.
Recommended Posts