CSV output of pulse data with Raspberry Pi (confirm analog input with python)

Procedures and links

  1. Preparation and basics http://qiita.com/tkyko13/items/62ff91bce7d9e555c434
  2. Analog input confirmation http://qiita.com/tkyko13/items/981989a006a95821ccde
  3. Check with python here
  4. Pulse sensor http://qiita.com/tkyko13/items/e4afc73add81d7bbb426
  5. csv output http://qiita.com/tkyko13/items/9c0eb46c1b65129d2556

environment

A note on how to write python code on a Raspberry Pi ・ Created with IDLE on the desktop of Raspberry Pi (Requires display and keyboard) ・ Vi command with Raspberry Pi (Requires display and keyboard, no mouse required) ・ Created with Remote Desktop and IDLE on Raspberry Pi (You need a way to check the Raspberry Pi's internet connection and IP) ・ Vi from ssh connection to Raspberry Pi (If you can confirm the internet connection and IP) ・ Send python file to Raspberry Pi by ftp (If you can confirm the internet connection and IP Also, you can write in the development environment of your own PC, sublime text, etc.)

I use it flexibly Basically remote connection There is an "arp -a" command from within the same network as a means to check the IP. I don't use mac ftp software so much, but I don't think it's good. At the time of win, winscp + putty was very nice, and I also liked teraterm. I like the terminal

Sample code part 1

I'll put one of the simplest chords for now I am using the I2C address from the previous "Analog Input Confirmation" First of all, I see the value every time I execute it

sample1.py


import smbus

I2C_ADDRESS = 0x48
bus = smbus.SMBus(1)

bus.write_byte(I2C_ADDRESS, 0xFF)
value=bus.read_byte(I2C_ADDRESS)
print value

Sample code part 2

Make it always visible in repetitive statements

sample2.py


import smbus
import time

I2C_ADDRESS = 0x48

bus = smbus.SMBus(1)

while True:
  bus.write_byte(I2C_ADDRESS, 0xFF)
  value=bus.read_byte(I2C_ADDRESS)
  print value
  time.sleep(0.1)

Sample code part 3

Since the purpose is to finally take the pulse, we will use threads to process every millisecond.

sample3.py


import threading
import smbus
import time

I2C_ADDRESS = 0x48

bus = smbus.SMBus(1)

def loop():
	bus.write_byte(I2C_ADDRESS, 0xFF)
	value=bus.read_byte(I2C_ADDRESS)
	print value
	t=threading.Timer(0.1, loop)
	t.start()

t=threading.Thread(target=loop)
t.start()

Sample code 4 (additional note)

This time, I want to measure only 10 seconds and end it, so I will write a process that ends when the thread is executed 100 times. The value given to the argument of the thread is increased steadily. I thought of a simpler way, but I couldn't think of it. If you know anything, please comment. (Since the file operation will be performed later, it is necessary to close the file at the end of the process, so I added it.)

sample4.py


import smbus
import time
import threading
import csv

I2C_ADDRESS = 0x48

bus = smbus.SMBus(1)
f = open('data.csv', 'w')

def loop(count):

    # count = count+1
    bus.write_byte(I2C_ADDRESS, 0xFF)
    value = bus.read_byte(I2C_ADDRESS)
    print value

    writer = csv.writer(f, lineterminator='\n')
    writer.writerow([value])

    if count < 100 :
        t = threading.Timer(0.1, loop, [count])
        t.start()
    else :
        f.close()
        print 'finish'

t = threading.Thread(target=loop, args=(0,))
t.start()

Next time I will use the last sample code

Recommended Posts

CSV output of pulse data with Raspberry Pi (confirm analog input with python)
CSV output of pulse data with Raspberry Pi (CSV output)
Data input / output in Python (CSV, JSON)
Get CPU information of Raspberry Pi with Python
Measure CPU temperature of Raspberry Pi with Python
Let's operate GPIO of Raspberry Pi with Python CGI
I tried running Movidius NCS with python of Raspberry Pi3
Use vl53l0x with Raspberry Pi (python)
Output to csv file with Python
Input / output with Python (Python learning memo ⑤)
UnicodeEncodeError struggle with standard output of python3
Recommendation of Altair! Data visualization with Python
[Python] Chapter 02-03 Basics of Python programs (input / output)
Working with GPS on Raspberry Pi 3 Python
Detect analog signals with A / D converter using python on Raspberry Pi 3!
Plot CSV of time series data with unixtime value in Python (matplotlib)
Read the data of the NFC reader connected to Raspberry Pi 3 with Python and send it to openFrameworks with OSC
[Python] From morphological analysis of CSV data to CSV output and graph display [GiNZA]
Csv output from Google search with [Python]! 【Easy】
Discord bot with python raspberry pi zero with [Notes]
Summary of how to read numerical data with python [CSV, NetCDF, Fortran binary]
Read Python csv data with Pandas ⇒ Graph with Matplotlib
Read JSON with Python and output as CSV
I tried L-Chika with Raspberry Pi 4 (Python edition)
Periodically notify the processing status of Raspberry Pi with python → Google Spreadsheet → LINE
Write CSV data to AWS-S3 with AWS-Lambda + Python
One-liner that outputs 10000 digits of pi with Python
Connect to MySQL with Python on Raspberry Pi
Speed evaluation of CSV file output in Python
Example of reading and writing CSV with Python
GPS tracking with Raspberry Pi 4B + BU-353S4 (Python)
Consolidate a large number of CSV files in folders with python (data without header)
Full-width and half-width processing of CSV data in Python
Try debugging Python on Raspberry Pi with Visual Studio.
Take the value of SwitchBot thermo-hygrometer with Raspberry Pi
Challenge principal component analysis of text data with Python
Log the value of SwitchBot thermo-hygrometer with Raspberry Pi
How to output CSV of multi-line header with pandas
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
Process csv data with python (count processing using pandas)
[Python-pptx] Output PowerPoint font information to csv with python
Output to "7-segment LED" using python on Raspberry Pi 3!
[Basics of data science] Collecting data from RSS with python
Extract the band information of raster data with python
Display USB camera video with Python OpenCV with Raspberry Pi
Raspberry Pi with Elixir, which is cooler than Python
Data analysis with python 2
GPGPU with Raspberry Pi
python input and output
Python audio input / output
Csv tinkering with python
DigitalSignage with Raspberry Pi
Basics of python: Output
Data analysis with Python
[Raspberry Pi] Scraping of web pages that cannot be obtained with python requests + Beautiful Soup
Try scraping the data of COVID-19 in Tokyo with Python
Update Python for Raspberry Pi to 3.7 or later with pyenv
Python beginner opens and closes interlocking camera with Raspberry Pi
Notes on importing data from MySQL or CSV with Python
Notes on handling large amounts of data with python + pandas
Check! Get sensor data via Bluetooth with Raspberry Pi ~ Preparation