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
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
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)
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()
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