Pages: [1]   Go Down
Author Topic: recommend book for python interfacing with Arduino  (Read 421 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am not a programmer but would like to use python to capture and manipulate serial data from Arduino.  I modified code from the web to read dht11 sensor data and write it to rrdtool but I would like to improve it.  For example, the script crashed when there was a hiccup in the serial data sent.  From other forum postings I see that there could be more efficient ways to capture the data but I don't know how to implement the code snippets.

http://stackoverflow.com/questions/1093598/pyserial-how-to-read-the-last-line-sent-from-a-serial-device

I have looked at some of these https://wiki.python.org/moin/BeginnersGuide/NonProgrammers and at Amazon's books but nothing seems like it is appropriate.  Either very basic, very advanced or focused on writing games.

Can anyone suggest a guide to help me learn to fix my code below?


Code:
#!/usr/bin/env python
#

import serial
import time
import rrdtool

########################################################################
# Global Variables   -  Yes I know these are bad...

ready=0                        # ready to send command to arduino
timeout = 0                    # To avoid infinite wait state, we will count 100 loops and assume failure
line = None                     # Holds current response from Arduino Serial
line_len = 0                    # number of items in "line"


##########################################################################
# Declare functions

def get_arduino_response():
   global line
   global line_len

   if (arduino.inWaiting() > 0):              # Number of characters in arduino buffer
       line = arduino.readline()
       humidity =  line [0:5]
       temperature = line [9:14]
       print line, temperature, humidity
       rrdtool.update('dht11.rrd', 'N:%s:%s' % (temperature, humidity)) # Write values to RRD
       time.sleep(300)    # Time for next read


##########################################################################
# Set up serial port to Arduino
arduino = serial.Serial('/dev/ttyACM0', 9600, timeout = 10)
arduino.open()


time.sleep(10)    # Time for Arduino to reset and settle
arduino.flushInput()
##########################################################################
# Main Loop

running = True

while 1:                    # Continuous loop
       # os.system('clear')
       stamp = time.time()

       get_arduino_response()                   # If data available from arduino, retrieve into "line"

#       if line_len == 17:    # Ensure entire line came through
#       print "temperature is ", temperature, "and humidity is ", humidity

Logged

UK
Offline Offline
Tesla Member
***
Karma: 104
Posts: 6360
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I wrote a demo Python-Arduino program in this Thread - it may be useful.

Also - don't apologize for global variables. I like them in the limited memory of the Arduino.

...R
Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 604
Posts: 33430
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Global variables are bad but you need them in python because you can't have static local variables in the language.
Logged

Pages: [1]   Go Up
Jump to: