Page 1 of 2

uPython syntax errors

Posted: Mon Aug 26, 2019 3:47 pm
by Netrosec
I use uPyCraft on a esp8266 board. I am working with an mqtt example for uPython i adapted to publish a simple message when light levels drops below a given threshold but i get this error:

exec(open(‘main.py’).read(),globals())
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
File “<string>”, line 43
SyntaxError: invalid syntax

Below is my main code.

Code: Select all

from machine import Pin, ADC
from time import sleep

ldr = ADC(0)


def sub_cb(topic, msg):
  print((topic, msg))
if topic == b'notification' and msg == b'received':
  print('ESP received hello message')


def connect_and_subscribe():
  global client_id, mqtt_server, topic_sub
  client = MQTTClient(client_id, mqtt_server)
  client.set_callback(sub_cb)
  client.connect()
  client.subscribe(topic_sub)
  print('Connected to %s MQTT broker, subscribed to %s topic' % (mqtt_server, topic_sub))
  return client
  
  
def restart_and_reconnect():
  print('Failed to connect to MQTT broker. Reconnecting...')
  time.sleep(10)
  machine.reset()
  
  
try:
  client = connect_and_subscribe()
except OSError as e:
  restart_and_reconnect()
  
  
while True:
  ldrValue = ldr.read()
  print(ldrValue)
  sleep(0.1)
  
if ldrValue >= 350
  msg = b'laser0 #%d' % counter
  client.publish(topic_pub, msg)
  last_message = time.time()
  counter += 1
except OSError as e:
  restart_and_reconnect()

Re: uPython syntax errors

Posted: Mon Aug 26, 2019 4:04 pm
by Roberthh
Please check what you posted. There is an except clause at the end without an matching try, and the indentation also seems wrong.

Re: uPython syntax errors

Posted: Mon Aug 26, 2019 4:13 pm
by Netrosec
Roberthh wrote:
Mon Aug 26, 2019 4:04 pm
Please check what you posted. There is an except clause at the end without an matching try, and the indentation also seems wrong.
OK, so i checked and pasted your edited code and the error stopped. But ldr does not give me a reading..

Re: uPython syntax errors

Posted: Mon Aug 26, 2019 4:18 pm
by Netrosec
Netrosec wrote:
Mon Aug 26, 2019 4:13 pm
Roberthh wrote:
Mon Aug 26, 2019 4:04 pm
Please check what you posted. There is an except clause at the end without an matching try, and the indentation also seems wrong.
OK, so i checked and pasted your edited code and the error stopped. But ldr does not give me a reading..
Ooops. the error still persists.

Re: uPython syntax errors

Posted: Mon Aug 26, 2019 5:09 pm
by Roberthh
Please re-post your code.
Edit: it is usually better not to put your code in "main.py", but in a file with a different name which you then can import from REPL or main.py

Re: uPython syntax errors

Posted: Mon Aug 26, 2019 5:13 pm
by Netrosec
Advice well taken. But the problem isn't yet resolved.

Re: uPython syntax errors

Posted: Mon Aug 26, 2019 5:28 pm
by Roberthh
I still have the problem that what you posted is not well formatted python code, but that should give different error messages. Therefore i asked to re-post the code. The posting may have corrupted it.

Re: uPython syntax errors

Posted: Mon Aug 26, 2019 5:55 pm
by Netrosec
I understand that uPython uses 2 spaces indentation as opposed to regular python which uses 4 spaces indentation. I have fixed the formatting problem.

Re: uPython syntax errors

Posted: Mon Aug 26, 2019 6:22 pm
by Roberthh
UPython works like cPython with respect to indentation. You can use any number of spaces, as long as it is consistent in a level per block.

Re: uPython syntax errors

Posted: Tue Aug 27, 2019 7:15 am
by pythoncoder
It is also vital (in Python or MicroPython code) not to mix tabs and spaces. That leads to code which looks fine but won't work. Ensure your editor converts tabs to spaces.