uPython syntax errors

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Netrosec
Posts: 7
Joined: Wed Jan 17, 2018 7:07 pm

uPython syntax errors

Post by Netrosec » Mon Aug 26, 2019 3:47 pm

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()
Last edited by Netrosec on Mon Aug 26, 2019 5:53 pm, edited 1 time in total.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: uPython syntax errors

Post by Roberthh » 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.

Netrosec
Posts: 7
Joined: Wed Jan 17, 2018 7:07 pm

Re: uPython syntax errors

Post by Netrosec » 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..

Netrosec
Posts: 7
Joined: Wed Jan 17, 2018 7:07 pm

Re: uPython syntax errors

Post by Netrosec » Mon Aug 26, 2019 4:18 pm

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.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: uPython syntax errors

Post by Roberthh » Mon Aug 26, 2019 5:09 pm

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

Netrosec
Posts: 7
Joined: Wed Jan 17, 2018 7:07 pm

Re: uPython syntax errors

Post by Netrosec » Mon Aug 26, 2019 5:13 pm

Advice well taken. But the problem isn't yet resolved.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: uPython syntax errors

Post by Roberthh » Mon Aug 26, 2019 5:28 pm

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.

Netrosec
Posts: 7
Joined: Wed Jan 17, 2018 7:07 pm

Re: uPython syntax errors

Post by Netrosec » Mon Aug 26, 2019 5:55 pm

I understand that uPython uses 2 spaces indentation as opposed to regular python which uses 4 spaces indentation. I have fixed the formatting problem.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: uPython syntax errors

Post by Roberthh » Mon Aug 26, 2019 6:22 pm

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.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: uPython syntax errors

Post by pythoncoder » Tue Aug 27, 2019 7:15 am

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.
Peter Hinch
Index to my micropython libraries.

Post Reply