Raspberry Pi (hereinafter referred to as Raspberry Pi) can be used free of charge by Mathematica. With the Raspberry Pi 4, the processing power of the CPU has increased, so you don't have to wait endlessly for 3D drawing. Raspberry Pi is a Linux machine that can access hardware such as GPIO, I2C, and SPI. Raspbian Buster is now available from June 2019.
Run sudo Mathematica to launch Mathematica with root privileges. Normally, pi users can access GPIO, I2C, and SPI without root privileges. Here, Mathematica reads the temperature from the MEMS sensor BME280 and graphs it.
Raspberry Pi comes with a number of pre-compiled sensor device drivers. Details can be found in / boot / overlays / README. It's just a brief explanation. Most of the drivers are iio. First, enable the I2C interface by clicking Raspberry Pi Configuration from Preferences in the main menu, selecting the Interface tab, checking I2C and pressing OK. From Buster, no reboot is needed.
sudo nano /boot/config.txt
Then open the config file config.txt and on the last line,
dtoverlay = i2c-sensor,bme280,param=0x76
Add, save by overwriting, exit, reboot, and the BME280 will be enabled on the I2C interface. Confirmation is OK when i2cdetect -y 1 is executed and UU is displayed at 0x76. Before that, connect the sensor.
It was 820 yen and it was delivered the next day.
"HiLetgo BME280 temperature sensor humidity sensor pressure sensor Arduino sensor atmospheric pressure sensor temperature and humidity sensor breakout Arduino compatible [parallel import goods]"
Connect with I2C as follows. The BME280 itself supports both SPI and I2C, but this board is dedicated to I2C and the device driver is also for I2C.
When the device driver is installed, the data read under / sys is a text file and is updated from time to time.
Read as follows: It is an integer and has a value of 1000 times.
cat /sys/bus/i2c/devices/1-0076/iio:device0/in_temp_input
In python,
f = open('/sys/bus/i2c/devices/1-0076/iio:device0/in_temp_input')
Temp = round(int(f.read()) / 1000.0, 1)
f.close
print(Temp)
You can read it with.
To use Python programs from Mathematica, use the External Sessions feature. Python programs are functions. Call that function with ExternalValue and receive the value.
dataList = {};
Do[
session = StartExternalSession["Python"];
ExternalEvaluate[session, "def temp():
f = open('/sys/bus/i2c/devices/1-0076/iio:device0/in_temp_input');
Temp = round(int(f.read()) / 1000.0, 1);
f.close;
return Temp
"];
v = ExternalValue[session, "temp()"];
DeleteObject[session];
AppendTo[dataList, v]
, {v, 1, 10}
]
dataList
ListLinePlot[dataList]
It looks like it is running. I read it 10 times and drew a graph.
Please refer to https://www.denshi.club/pc/raspi/linux-3-mathematicabme280.html for the program that updates the graph in real time.
Recommended Posts