SyntaxError: invalid syntax

348 views
Skip to first unread message

Marco Craamer

unread,
Feb 26, 2015, 4:00:52 PM2/26/15
to web...@googlegroups.com
Hi All!

I am a bit afraid of asking a question which has been answered before, but I can't find anything like my question on this forum.

I keep getting a very strange error message in the python part, when I start the script without webiopi, it just works.

What am I doing wrong?

Thank you very much!

Overhere is my setup:


/etc/webiopi/config
**********************************************************************************************************************************
[SCRIPTS]

DAC = /home/pi/terrariumController/fan/python/fan.py

#------------------------------------------------------------------------#

[HTTP]
# HTTP Server configuration

# Use doc-root to change default HTML and resource files location
doc-root = /home/pi/terrariumController/fan/html
**********************************************************************************************************************************



fan.py
**********************************************************************************************************************************
#!/usr/bin/python

from Adafruit_MCP4725 import MCP4725
import time
import webiopi
import sys
sys.path.append("/home/pi/terrariumController/fan/html")
sys.path.append("/home/pi/terrariumController/fan/python")
sys.path.append("/home/pi/mcp4725/Adafruit-Raspberry-Pi-Python-Code/Adafruit_MCP4725")
sys.path.append("/home/pi/mcp4725/Adafruit-Raspberry-Pi-Python-Code/Adafruit_I2C")

@webiopi.macro
def setDAC(value):
dac.setVoltage(value)
return

# Initialise the DAC using the default address
dac = MCP4725(0x62)
dac.setVoltage(0)
setDAC(00)
**********************************************************************************************************************************





And this is the error message which I get:
**********************************************************************************************************************************
pi@raspberrypi ~/terrariumController/fan/python $ webiopi -d -c /etc/webiopi/config
2015-02-26 20:26:40 - WebIOPi - INFO - Starting WebIOPi/0.7.0/Python3.2
2015-02-26 20:26:40 - WebIOPi - DEBUG - Mapping GPIO.digitalCount to REST GET /GPIO/count
2015-02-26 20:26:40 - WebIOPi - DEBUG - Mapping GPIO.digitalRead to REST GET /GPIO/%(channel)d/value
2015-02-26 20:26:40 - WebIOPi - DEBUG - Mapping GPIO.digitalWrite to REST POST /GPIO/%(channel)d/value/%(value)d
2015-02-26 20:26:40 - WebIOPi - DEBUG - Mapping GPIO.getFunctionString to REST GET /GPIO/%(channel)d/function
2015-02-26 20:26:40 - WebIOPi - DEBUG - Mapping GPIO.getPulse to REST GET /GPIO/%(channel)d/pulse
2015-02-26 20:26:40 - WebIOPi - DEBUG - Mapping GPIO.outputSequence to REST POST /GPIO/%(channel)d/sequence/%(args)s
2015-02-26 20:26:40 - WebIOPi - DEBUG - Mapping GPIO.portRead to REST GET /GPIO/*/integer
2015-02-26 20:26:40 - WebIOPi - DEBUG - Mapping GPIO.portWrite to REST POST /GPIO/*/integer/%(value)d
2015-02-26 20:26:40 - WebIOPi - DEBUG - Mapping GPIO.pulse to REST POST /GPIO/%(channel)d/pulse/
2015-02-26 20:26:40 - WebIOPi - DEBUG - Mapping GPIO.pulseAngle to REST POST /GPIO/%(channel)d/pulseAngle/%(value)f
2015-02-26 20:26:40 - WebIOPi - DEBUG - Mapping GPIO.pulseRatio to REST POST /GPIO/%(channel)d/pulseRatio/%(value)f
2015-02-26 20:26:40 - WebIOPi - DEBUG - Mapping GPIO.setFunctionString to REST POST /GPIO/%(channel)d/function/%(value)s
2015-02-26 20:26:40 - WebIOPi - DEBUG - Mapping GPIO.wildcard to REST GET /GPIO/*
2015-02-26 20:26:40 - WebIOPi - INFO - GPIO - Native mapped to REST API /GPIO
2015-02-26 20:26:40 - WebIOPi - INFO - Loading configuration from /etc/webiopi/config
2015-02-26 20:26:40 - WebIOPi - INFO - Loading DAC from /home/pi/terrariumController/fan/python/fan.py
2015-02-26 20:26:40 - WebIOPi - ERROR - invalid syntax (Adafruit_MCP4725.py, line 29)
Traceback (most recent call last):
  File "/usr/local/lib/python3.2/dist-packages/WebIOPi-0.7.0-py3.2-linux-armv6l.egg/webiopi/__main__.py", line 75, in <module>
    main(sys.argv)
  File "/usr/local/lib/python3.2/dist-packages/WebIOPi-0.7.0-py3.2-linux-armv6l.egg/webiopi/__main__.py", line 69, in main
    server = Server(port=port, configfile=configfile, scriptfile=scriptfile)
  File "/usr/local/lib/python3.2/dist-packages/WebIOPi-0.7.0-py3.2-linux-armv6l.egg/webiopi/server/__init__.py", line 75, in __init__
    loader.loadScript(name, source, self.restHandler)
  File "/usr/local/lib/python3.2/dist-packages/WebIOPi-0.7.0-py3.2-linux-armv6l.egg/webiopi/utils/loader.py", line 8, in loadScript
    script = imp.load_source(name, source)
  File "/home/pi/terrariumController/fan/python/fan.py", line 3, in <module>
    from Adafruit_MCP4725 import MCP4725
  File "Adafruit_MCP4725.py", line 29
    print "Setting voltage to %04d" % voltage
                                  ^
SyntaxError: invalid syntax

Robert More

unread,
Feb 26, 2015, 4:43:19 PM2/26/15
to web...@googlegroups.com
2015-02-26 20:26:40 - WebIOPi - ERROR - invalid syntax (Adafruit_MCP4725.py, line 29)

Try changing line 29 in Adafruit_MCP4725.py, 
print "Setting voltage to %04d" % voltage

to:

print ("Setting voltage to %04d" % voltage)

Or try starting Webiopi with python 2 instead of python 3:

See here:

Marco Craamer

unread,
Feb 27, 2015, 5:29:26 AM2/27/15
to web...@googlegroups.com
Hello Robert,

Thank you very much for your answer...

I tried both of the possible solutions you gave me...

1) Porting the code from Python3 to 2... After several problems (print lines and exception handling problems) I got stuck with the error:   

File "/home/pi/terrariumController/fan/python/fan.py", line 3, in <module>
    from Adafruit_MCP4725 import MCP4725
ImportError: No module named Adafruit_MCP4725


2) Changing the python interpreter gave me the error:

    from Adafruit_I2C import Adafruit_I2C
  File "/home/pi/terrariumController/fan/python/Adafruit_I2C.py", line 3, in <module>
    import smbus
ImportError: No module named smbus

Do you or someone else have any ideas what to do next?

Thank you very much!

Greetings,

Marco


Op donderdag 26 februari 2015 22:43:19 UTC+1 schreef Robert More:

Toshi Bass

unread,
Feb 27, 2015, 9:23:18 AM2/27/15
to web...@googlegroups.com
Hi Marco

Your problem is this statement      from Adafruit_MCP4725 import MCP4725    thats the wrong syntax.

You need this to import the driver  from webiopi.devices.analog.mcp47252 import MCP4725    

Its telling webiopi to look in /home/pi/webiopi/python/webiopi/devices/analog for a driver called mcp4725 and import it as MCP4725 

Toshi

Toshi Bass

unread,
Feb 27, 2015, 10:04:45 AM2/27/15
to web...@googlegroups.com

Oh sorry hope you spotted extra 2 in  mcp47252 import MCP4725 I am sure these silly mistakes write themselves after I have pressed post !!

also notice that your first post you were using webiopi with python 3.2 in this case :

print "Setting voltage to %04d" % voltage  will also produce an syntax error :

print("Statements have to be inside brackets")

Also this may be usefull

#!/usr/bin/python

import time
import webiopi
import sys

from webiopi.devices.analog.mcp4725 import MCP4725

mcp = MCP4725                      # setup a MCP4725 I2C DAC

mcp.analogCount()                   # returns MCP4725 analog channel count
max = mcp.analogMaximum()   # returns MCP4725 maximum integer value
mcp.analogWrite(0, max)          # set 100% on MCP4725 analog channel 0
mcp.analogWriteFloat(0, 0.5)     # set 50% on MCP4725 analog channel 0
mcp.analogWriteVolt(0, 0.0)      # set 0V on MCP4725 analog channel 0
mcp.analogRead(0)                  # returns MCP4725 analog channel 0 as integer
mcp.analogReadFloat(0)          # returns MCP4725 analog channel 0 as float
mcp.analogReadVolt(0)            # returns MCP4725 analog channel 0 as volt

Toshi








Marco Craamer

unread,
Feb 27, 2015, 4:59:45 PM2/27/15
to web...@googlegroups.com
Hello Toshi,

Thank you very much, that is the solution to my problem!

Right now the communication between server and client already works, so I can use macro's to call python functions...

Great!

Thanks again!

Marco

Op vrijdag 27 februari 2015 16:04:45 UTC+1 schreef Toshi Bass:
Reply all
Reply to author
Forward
0 new messages