[PYTHON] Try using the temperature sensor (LM75B) on the Raspberry Pi.

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

Prerequisites

--NXP temperature sensor (LM75B) --Raspberry Pi --Python language

How to use the temperature sensor.

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

Connection between temperature sensor and Raspberry Pi

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: -- -- -- -- -- -- -- --

Temperature sensor data format

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

Upload to Nifty Cloud mobile backemobile backend

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

Try using the temperature sensor (LM75B) on the Raspberry Pi.
Try using ArUco on Raspberry Pi
Using the 1-Wire Digital Temperature Sensor DS18B20 from Python on a Raspberry Pi
I tried using the DS18B20 temperature sensor with Raspberry Pi
Use the Grove sensor on the Raspberry Pi
Detect temperature using python on Raspberry Pi 3!
Try using a QR code on a Raspberry Pi
Sound the buzzer using python on Raspberry Pi 3!
Using the digital illuminance sensor TSL2561 with Raspberry Pi
Detect "temperature (using A / D converter)" using python on Raspberry Pi 3!
Detect "brightness" using python on Raspberry Pi 3!
Run servomotor on Raspberry Pi 3 using python
Raspberry Pi --1 --First time (Connect a temperature sensor to display the temperature)
Try the Grove Pi + Starter Kit Sensor
Use BME280 temperature / humidity / barometric pressure sensor from Python on Raspberry Pi 2
I connected the thermo sensor to the Raspberry Pi and measured the temperature (Python)
Detect slide switches using python on Raspberry Pi 3!
Python: Try using the UI on Pythonista 3 on iPad
Detect magnet switches using python on Raspberry Pi 3!
Use Grove-Temperature & Humidity Sensor (DHT11) on Raspberry Pi
Display CPU temperature every 5 seconds on Raspberry Pi 4
pigpio on Raspberry pi
Cython on Raspberry Pi
Build an OpenCV4 environment on Raspberry Pi using Poetry
Create a color sensor using a Raspberry Pi and a camera
Try to visualize the room with Raspberry Pi, part 1
Try debugging Python on Raspberry Pi with Visual Studio.
Output to "7-segment LED" using python on Raspberry Pi 3!
[Don't refer to 04.02.17] Display the temperature sensor on a real-time graph with rasberry pi 3.
Install PyCall on Raspberry PI and try using GPIO's library for Python from Ruby
Indoor monitoring using Raspberry Pi
Try using the Twitter API
Try using the Twitter API
Try using the PeeringDB 2.0 API
Introduced pyenv on Raspberry Pi
Use NeoPixel on Raspberry Pi
Install OpenCV4 on Raspberry Pi 3
Install TensorFlow 1.15.0 on Raspberry Pi
Cross-compiling for Raspberry Pi Zero on Debian-Try using shared libraries
Access google spreadsheet using python on raspberry pi (for myself)
How to get temperature from switchBot thermo-hygrometer using raspberry Pi
Get the weather using the API and let the Raspberry Pi speak!
Input and output display images directly using the frame buffer (/ dev / fb0) on Raspberry Pi
Create your own IoT platform using raspberry pi and ESP32 (Part 3) ~ ESP32 settings Analog temperature sensor
Testing uart communication on Raspberry Pi
USB over ethernet using Raspberry pi
MQTT on Raspberry Pi and Mac
raspberry pi 4 centos7 install on docker
Install ghoto2 on Raspberry Pi (memo)
Try using Pillow on iPython (Part 2)
Make a simple CO2 incubator using Raspberry PI and CO2 sensor (MH-Z14A)
Use python on Raspberry Pi 3 to illuminate the LED (Hello World)
Try Ajax on the Django page
Try L Chika with raspberry pi
OpenCV installation procedure on Raspberry Pi
Weighing instrument using raspberry pi and hx711 (GUI display on Tkinter)
Try using Pillow on iPython (Part 3)
Try moving 3 servos with Raspberry Pi
Try using the Python Cmd module
Power on / off Raspberry pi on Arduino
Detect switch status on Raspberry Pi 3