Articles Hierarchy

Articles Home » RPI » RASPBERRY PI and ARDUINO

RASPBERRY PI and ARDUINO

this part is about the installation and connection of ARDUINO on a RPI
- the IDE
- usb serial stream read and file write by PYTHON
- show the file data ( 1 min snaps ) in webserver

7.7 - connect a ARDUINO
working on connections between arduino and raspberry
- the easy way is by USB ( and also can use arduino software for linux )
- connect on GPIO level with the 3V3 / 5V problem


sudo apt-get update
sudo apt-get install arduino

and start ( in VNC window )


next: test to connect arduinos to USB
start arduino IDE on RPI (via VNC )
check serial port
its on COM1 and grey
( after test a device under win7 arduino IDE )
plug in a 9VDC powersupply and then
the usb cable to the RPI ( upper USB port )

1. arduino mega 2560 R3 china copy
now see SERIAL Port /dev/ttyACM0 select

start the arduino serial monitor,
change to my usual speed 115200 and see the arduino (soft) boot msg.


2. arduino mega 2010 DFRobot
now see SERIAL Port /dev/ttyUSB0 select

start the arduino serial monitor and see the arduino (soft) boot msg.


3. arduino UNO original
now see SERIAL Port /dev/ttyACM0 select
start the arduino serial monitor,

but later it worked also


4. arduino 2009 (MLT) thailandcopy
now see SERIAL Port /dev/ttyUSB0 select


5. linksprite diamond back WIFI arduino ( 2009 clone with WIFI )
has a 2-pin JST connector for DC power, connect 9VDC but need a adapter


so despite the many hacks about the ports you find in the web,
it looks like the newest version of arduino IDE for RPI
links with all devices.

but while upload sketch the port makes often problems!
also due to the usual mistake to make loops with print commands to USB without delay,
no problem for arduino, but host computer with arduino IDE, and terminal window and highspeed USB data input easy reach their limit, so RPI can not be blamed.
In the desktop you can see how bussy the cpu is.
so sometimes its freezing and needed then minutes
to shutdown terminal and arduino IDE,
not so clear if removing the usb link helps or makes it worse,
same about pressing the arduino reset button.
But all this i experienced also with bigger computer and windows os.



and i had a questions how to deal with the source code dir path? "sketch dir"
and how to exchange files ( this VNC not does allow file transfer??? )

But as the FTP is running now, file exchange no problem any more.


7.8 - make some baby steps with PYTHON
install a serial library for PYTHON:
sudo apt-get install python-serial

testing with IDLE3 is a problem that serial lib is not found

sudo apt-get install python-serial again!
Reading package lists... Done
Building dependency tree
Reading state information... Done
python-serial is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 3 not upgraded.


testing with IDLE ( Python 2.7.3rc2 ) its ok
( i copy some code from web to files, and copy the directory with FTP to
the RPI,

file serial1.py: ( arduino UNO on upper USB port RPI )

import serial
ser = serial.Serial('/dev/ttyACM0', 115200)
ser.open()

try:
while 1 :
result = ser.readline()
print result

except KeyboardInterrupt:
ser.close()

how to do:
start IDLE
[file] [open] [home/pi/Documents/terminalcode/serial1.py]
in the file window
[RUN][RUN MODULE]
and i see the analogue measuring values from my arduino UNO!!!
( there was a jumper cable from A0 to 3v3 )





here now try to find a easy way to bring the arduino measuring values into
the web page.
later, the concept could be to read the arduino USB datastream
by a python service, and write them to a "historic datacollection" in mysql
( where the web site using php extracts it and visualize it).
But i want start easy,
anyhow looks like i have to learn that language PYTHON.

Thats the approach:
-- python reads the USB, writes the line to a file. ( continuous service )
-- in websever php reads the file and writes it to HTML ( at call of the websie )

but the file I/O seems to be little bit different in PYTHON and PHP
if i write to the file with append, i need in php ( file open by (read))
to read each line of the file, until detect EOF
with remembering the last but one line ?the last seems to be empty?

In case i read the datastream of arduino ( one record timed 2 sec )
but write only to file every minute by python,
still the appended file gets very long and the php reading ( until end ) slow.

so i now use more easy way,
write every minute a record to
-- a file with "w" ( it will be long only one line ) to be read by php
-- and a other file with "a" ( what could be called a historic database )
so that one minute file could be to worry about SD card wearing out by block writes after 10000 minutes / 7 days ?
the webpage:

and the code:


but currently i dont see a other way, a ramdisk file would be good ? how to do ?. HD is a option, usb data STICK could reduce risk for SD system disk.
for the data base file in the next step i want to rename it to hourly files,
so a block is only written to max 1440 times.
THAT WAS EASY
looks like there is a ramdisk started on RPI, check at $ df -h
so all i had to do was to change the python program and the index.php
to look for the datafile:
/run/lock/arduinodata.csv ( max one minute old, one line long )
look with
ls -l /run/lock/

other question i need to learn with python:
-- make it a executable
chmod +x /home/pi/Documents/terminalcode/arduino_stream.py
-- auto start it at boot
here again we do a startfile:
sudo nano /etc/init.d/arduino_stream
same file like we do for the VNC
sudo chmod 755 /etc/init.d/arduino_stream make it public
sudo /etc/init.d/arduino_stream start for start
sudo /etc/init.d/arduino_stream stop for stop ! that does not work PID/filename...
sudo update-rc.d arduino_stream defaults register/ update after edit...
sudo update-rc.d -f arduino_stream remove remove

after reboot my arduino reading sevice autostart and after one minute webserver show good data!

-- check on the USB without stopping, if arduino is off

-- and, like with the monitor in arduino IDE, also python serial open resets the arduino,
if i would not know that the hterm software can connect without reset, i would think it must be this way.

-- check on the serial interfaces at RPI boot

we see in /etc/inittab ( last line ) that a terminal server login window (getty) is started
at ttyAMA0, what is the serial interface on the GPIO.
if you use that for communication with arduino... need to uncomment that line.
but USB no problem.

also can now show the ARDUINO measurements
at a php-fusion panel ( right down )


even the system works, with a connected arduino UNO at boot
i am unable to fix the problem with the
ser.open() and exception
to catch if no, or other , arduino is connected at start.
but now we are able to catch the exception while disconnecting a arduino
from running condition


here the worst example on exception checking
WHAT STILL DON'T WORK

for play, use this example code