Go Down

Topic: recommend book for python interfacing with Arduino (Read 526 times) previous topic - next topic

bad_gui

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: [Select]
#!/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


Robin2

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

Grumpy_Mike

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

Go Up