Serial example: Python : Processing data

656 views
Skip to first unread message

Maciek Wąchalski

unread,
Jan 25, 2014, 3:02:57 AM1/25/14
to web...@googlegroups.com
Hi,
I'm doing some tests with arduino connected to raspberry and using serial examples.
One of them is Python -Porocessing data.
I uploaded arduino sketch and did config on raspberry side.

When it runs for some time i get this error from time to time:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python3.2/threading.py", line 740, in _bootstrap_inner
    self.run()
  File "/usr/local/lib/python3.2/dist-packages/WebIOPi-0.6.2-py3.2-linux-armv6l.                 egg/webiopi/utils/thread.py", line 23, in run
    self.func()
  File "/home/pi/Domeq/python/script3.py", line 27, in loop
    value = int(cv[1])
IndexError: list index out of range

then on the serial monitor i get:
2=267
3=263
4=252
5=246
46
1=252
2=263
3=258
4=246
5=240
2
4=241
5=236

Looks like some times data from arduino is not as expected (X=Y).
I'm not sure if it's arduino or webiopi side?

Eric PTAK

unread,
Jan 25, 2014, 5:06:27 AM1/25/14
to web...@googlegroups.com
Is your serial monitor running when you got the error ?
Or do you launch the serial monitor after the error ?


--
You received this message because you are subscribed to the Google Groups "WebIOPi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webiopi+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Maciek Wąchalski

unread,
Jan 25, 2014, 5:08:42 AM1/25/14
to web...@googlegroups.com
It is running in browser window.

Eric PTAK

unread,
Jan 25, 2014, 5:15:15 AM1/25/14
to web...@googlegroups.com
From https://code.google.com/p/webiopi/wiki/Tutorial_Serial#With_REST_mapping
"Be careful when using the REST API (with the Serial monitor) as well as using it from a custom script, you may notice strange behavior if you read data in your script. When data is received, it can be read only once."

When you make a script that poll Serial, you CAN'T use Serial Monitor.

Eric PTAK

unread,
Jan 25, 2014, 5:20:15 AM1/25/14
to web...@googlegroups.com
There is also another note 
"Instead of using Javascript Serial library, I prefer consume Arduino data directly on the server side, within a custom script, and provide a macro to ease UI interfacing. Disable Serial REST mapping by commenting or removing Serial setup from /etc/webiopi/config"

Maciek Wąchalski

unread,
Jan 25, 2014, 5:34:38 AM1/25/14
to web...@googlegroups.com
Thanks, i'll 
Disable Serial REST mapping by commenting or removing Serial setup from /etc/webiopi/config"
and give it some more testing.

Maciek Wąchalski

unread,
Jan 25, 2014, 5:41:19 AM1/25/14
to web...@googlegroups.com
Sometimes after first run:  webiopi -d -c /etc/webiopi/config
I get:
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python3.2/threading.py", line 740, in _bootstrap_inner
    self.run()
  File "/usr/local/lib/python3.2/dist-packages/WebIOPi-0.6.2-py3.2-linux-armv6l.egg/         webiopi/utils/thread.py", line 23, in run
    self.func()
  File "/home/pi/Domeq/python/script3.py", line 26, in loop
    channel = int(cv[0])
ValueError: invalid literal for int() with base 10: ''

does it have something to do with #empty input buffer before starting processing
in serial py examle?

Maciek Wąchalski

unread,
Jan 25, 2014, 12:17:41 PM1/25/14
to web...@googlegroups.com
Looks like adding delay resolves this problem:

def loop():
    if (serial.available() > 0):
        webiopi.sleep(0.1)
        data = serial.readString()     # read available data

Eric PTAK

unread,
Jan 25, 2014, 1:18:59 PM1/25/14
to web...@googlegroups.com
hum, ok, thanks !

praveen kumar

unread,
Feb 22, 2014, 4:24:30 AM2/22/14
to web...@googlegroups.com
Hi Trouch,
                  i am running the sample processing that u have given for four sensors.i  made a custom script,when i compile it i got the following error..
pi@raspberrypi ~ $ sudo webiopi -d -c /etc/webiopi/config
2014-02-22 09:02:20 - WebIOPi - INFO - Starting WebIOPi/0.6.0/Python3.2
2014-02-22 09:02:20 - WebIOPi - DEBUG - Mapping GPIO.digitalCount to REST GET /GPIO/count
2014-02-22 09:02:20 - WebIOPi - DEBUG - Mapping GPIO.digitalRead to REST GET /GPIO/%(channel)d/value
2014-02-22 09:02:20 - WebIOPi - DEBUG - Mapping GPIO.digitalWrite to REST POST /GPIO/%(channel)d/value/%(value)d
2014-02-22 09:02:20 - WebIOPi - DEBUG - Mapping GPIO.getFunctionString to REST GET /GPIO/%(channel)d/function
2014-02-22 09:02:20 - WebIOPi - DEBUG - Mapping GPIO.getPulse to REST GET /GPIO/%(channel)d/pulse
2014-02-22 09:02:20 - WebIOPi - DEBUG - Mapping GPIO.outputSequence to REST POST /GPIO/%(channel)d/sequence/%(args)s
2014-02-22 09:02:20 - WebIOPi - DEBUG - Mapping GPIO.portRead to REST GET /GPIO/*/integer
2014-02-22 09:02:20 - WebIOPi - DEBUG - Mapping GPIO.portWrite to REST POST /GPIO/*/integer/%(value)d
2014-02-22 09:02:20 - WebIOPi - DEBUG - Mapping GPIO.pulse to REST POST /GPIO/%(channel)d/pulse/
2014-02-22 09:02:20 - WebIOPi - DEBUG - Mapping GPIO.pulseAngle to REST POST /GPIO/%(channel)d/pulseAngle/%(value)f
2014-02-22 09:02:20 - WebIOPi - DEBUG - Mapping GPIO.pulseRatio to REST POST /GPIO/%(channel)d/pulseRatio/%(value)f
2014-02-22 09:02:20 - WebIOPi - DEBUG - Mapping GPIO.setFunctionString to REST POST /GPIO/%(channel)d/function/%(value)s
2014-02-22 09:02:20 - WebIOPi - DEBUG - Mapping GPIO.wildcard to REST GET /GPIO/*
2014-02-22 09:02:20 - WebIOPi - INFO - GPIO - Native mapped to REST API /GPIO
2014-02-22 09:02:20 - WebIOPi - INFO - Loading configuration from /etc/webiopi/config
2014-02-22 09:02:20 - WebIOPi - INFO - Loading myscript from /home/pi/customscript.py
2014-02-22 09:02:20 - WebIOPi - ERROR - 'Serial' object has no attribute 'readString'
Traceback (most recent call last):
  File "/usr/local/lib/python3.2/dist-packages/WebIOPi-0.6.0-py3.2-linux-armv6l.egg/webiopi/__main__.py", line 73, in <module>
    main(sys.argv)
  File "/usr/local/lib/python3.2/dist-packages/WebIOPi-0.6.0-py3.2-linux-armv6l.egg/webiopi/__main__.py", line 67, in main
    server = Server(port=port, configfile=configfile)
  File "/usr/local/lib/python3.2/dist-packages/WebIOPi-0.6.0-py3.2-linux-armv6l.egg/webiopi/server.py", line 86, in __init__
    loadScript(name, source, self.restHandler)
  File "/usr/local/lib/python3.2/dist-packages/WebIOPi-0.6.0-py3.2-linux-armv6l.egg/webiopi/utils.py", line 168, in loadScript
    script.setup()
  File "/home/pi/customscript.py", line 13, in setup
    serial.readString()
AttributeError: 'Serial' object has no attribute 'readString'
pi@raspberrypi ~ $


Note:I had serial library for raspberry pi.


Awaiting for your valuable reply

regards,
Praveen Kumar

Eric PTAK

unread,
Feb 22, 2014, 7:35:31 AM2/22/14
to web...@googlegroups.com
On the wiki, serial tutorial, prerequisites :
You will need to use the latest trunk from the SVN repository. There is a bug in the 0.6 release avoiding low-level binary interfacing. It has been fixed by r1368.

pabs...@gmail.com

unread,
Sep 15, 2015, 10:21:23 AM9/15/15
to WebIOPi


i have same problem with webiopi serial streaming of arduino to raspberry pi,,it give me a error : File "/home/pi/serial/python/script3.py", line 26, in loop
    channel = int(cv[0])
ValueError: invalid literal for int() with base 10: ''

please help me,

pabs...@gmail.com

unread,
Sep 15, 2015, 10:27:19 AM9/15/15
to WebIOPi
i already add webiopi.sleep(0.1) in python script but gives me same error


Pete Dudash

unread,
Sep 15, 2015, 10:53:19 AM9/15/15
to WebIOPi

This error message tells you what is wrong. Changing the sleep time won’t help, but you already know that.

ValueError: invalid literal for int() with base 10: ''

What data type is stored in cv[0]? If it is a float or an empty string, then int() won’t work and you get the same error. If it is a string representing a decimal, try converting that first:

channel = int(float(cv[0]))

On Tue, Sep 15, 2015 at 9:27 AM, <pabs...@gmail.com> wrote:
i already add webiopi.sleep(0.1) in python script but gives me same error


--
You received this message because you are subscribed to the Google Groups "WebIOPi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webiopi+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

pabs...@gmail.com

unread,
Sep 16, 2015, 10:23:03 PM9/16/15
to WebIOPi
thank you sir Pete Dudash,,

pabs...@gmail.com

unread,
Sep 16, 2015, 11:07:51 PM9/16/15
to WebIOPi
error again, help please this is my code: 
 import webiopi

# import Serial driver
from webiopi.devices.serial import Serial

# initialize Serial driver
serial = Serial('/dev/ttyUSB0', 9600)
sensors = [0 for a in range(6)]

def setup():
    # empty input buffer before starting processing
    while (serial.available() > 0):
        serial.readString()

def loop():
    if (serial.available() > 0):

        data = serial.readString()     # read available data
        lines = data.split("\r\n")     # split lines
        count = len(lines)             # count lines

        lines = lines[0:count-1]       # remove last item from split which is empty

        # process each complete line
        for pair in lines:
            cv = pair.split("=")       # split channel/value
            channel = int(float(cv[0]))
            value = int(cv[1])
            sensors[int(channel)] = value   # store value

    webiopi.sleep(1)


# this macro scales sensor value and returns it as percent string
@webiopi.macro
def getSensor(channel):
    global result
    percent = sensors[int(channel)]
    return "%.1f" % percent

pabs...@gmail.com

unread,
Sep 16, 2015, 11:12:05 PM9/16/15
to WebIOPi
line 28, in loop
   value = int(cv[1])
Reply all
Reply to author
Forward
0 new messages