Try using an inexpensive and easy-to-connect temperature sensor (LM75B) with a raspberry pi (hereinafter Raspberry Pi) Upload the temperature sensor data to Nifty Cloud mobile backemobile backend
--NXP temperature sensor (LM75B) --Raspberry Pi --Python language
Connection is I2C method
Therefore, enable I2C in advance.
Execute the following command. sudo apt-get update sudo apt-get upgrade sudo apt-get install -y i2c-tools
sudo raspi-config 「Advanced Options」->「A7」I2C」 Enable I2C in
Turn off the power and connect VCC, SCL, SDA, GND to Raspberry Pi,
LM75B --Raspberry Pi VCC <->3.3V(1) SCL<->SDA(3) SDA<->SCL(5) GND<->GND(6)
Reboot and check
Confirm with ** i2cdetect ** command
Execution example pi@raspberrypi:~$ i2cdetect -y 1 0 1 2 3 4 5 6 7 8 9 a b c d e f 00: -- -- -- -- -- -- -- -- -- -- -- -- -- 10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 30: -- -- -- -- -- -- -- -- -- -- -- -- 3c -- -- -- 40: -- -- -- -- -- -- -- -- 48 -- -- -- -- -- -- -- 50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 70: -- -- -- -- -- -- -- --
It is a binary number that is organized by two digits and is arranged in ascending order. The lower 5 bits are invalid and are in 0.125 degree steps. The number of effective digits is 11 bits A minus Celsius is a two's complement when the most significant bit is 1.
================================
get_temp.py
#!/usr/bin/python
#coding: utf-8
import smbus
import time
i2c = smbus.SMBus(1)
lm75b_address = 0x48
def get_Temperature():
block = i2c.read_i2c_block_data(lm75b_address, 0x00, 2)
val = block[0] << 8 #Shift and set the high-order bits
val = val | block[1] #Set the lower bits with OR
# val = 0xc920 #Negative temperature test
#Calculate the temperature by making positive and negative judgments below.
if(val >= 0x7fff):
val = val - 0xffff
result = ((val >>5) * 0.125)
return result
while True:
print("Temperature:%6.2f" % get_Temperature())
time.sleep(5)
===============================
Execution example
pi@raspberrypi:~$ ./get_temp.py Temperature: 32.50 Temperature: 32.50 Temperature: 32.62
Installation of ncmb python module sudo pip install py_nifty_cloud
Set the file application key and client key in nifty_cloud.yml nifty_cloud.yml APPLICATION_KEY: 'your application key' CLIENT_KEY: 'your client key'
===============================
ncmbsample.py
#!/usr/bin/python
#coding: utf-8
import smbus
import datetime
import locale
from py_nifty_cloud.nifty_cloud_request import NiftyCloudRequest
i2c = smbus.SMBus(1)
lm75b_address = 0x48
def get_Temperature():
block = i2c.read_i2c_block_data(lm75b_address, 0x00, 2)
val = block[0] << 8 #Shift and set the high-order bits
val = val | block[1] #Set the lower bits with OR
# val = 0xc920 #Negative temperature test
#Calculate the temperature by making positive and negative judgments below.
if(val >= 0x7fff):
val = val - 0xffff
result = ((val >>5) * 0.125)
return result
# instanciate with yaml file contains APPLICATION KEY and CLIENT KEY
ncr = NiftyCloudRequest('.nifty_cloud.yml')
path = '/classes/Temperature'
method = 'POST'
# today()Get the variable of datetime type data of the current date / time with the method
d = datetime.datetime.today()
date_str = d.strftime("%Y-%m-%d %H:%M:%S")
value = "%6.2f" % get_Temperature()
# post a new recode
values = {'date':date_str,'temperature': value}
response = ncr.post(path=path, query=values)
print(response.status_code)
# show response as json format
print(response.json())
===============================
Recommended Posts