• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

mqtt syntax problem

Q&A Forum › Category: ESP8266 › mqtt syntax problem
0 Vote Up Vote Down
Joseph Jakpa asked 4 years ago

I am working with the mqtt examples for uPython, i adapted the example given to do something different. My code is suppose to publish a simple message when light levels drops below a given threshold. but i get this error:

download ok
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.py

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'
client.publish(topic_pub, msg)
last_message = time.time()
counter += 1
except OSError as e:
restart_and_reconnect()
Question Tags: uPython
9 Answers
0 Vote Up Vote Down
Rui Santos Staff answered 4 years ago

Hello Joseph, what’s your line 43 in that script? Can you tell me the line and double-check the indentation. According to that error, it’s usually missing some spaces.

0 Vote Up Vote Down
Joseph Jakpa answered 4 years ago

Line 43 is

client.publish(topic_pub, msg)
0 Vote Up Vote Down
Rui Santos Staff answered 4 years ago

Did you double-check the spacing?
Unfortunately the forum doesn’t support MicroPython spacing, you should use pastebin.com  to share properly formatted MicroPython scripts

0 Vote Up Vote Down
Joseph Jakpa answered 4 years ago

Yes. I double checked that spacing.  I made sure they’re all tabbed in two spaces. Nutbfor clarity purpose here’s the code on paste bin. https://pastebin.com/kcuRfR2A

0 Vote Up Vote Down
Rui Santos Staff answered 4 years ago

The spacing does look right. However, line 43 is:

last_message = time.time()

What’s that line doing? I think you can delete it

0 Vote Up Vote Down
Joseph Jakpa answered 4 years ago

Deleted that line. Didn’t fix the problem, and oh, the error is now line 41 which is

 msg = b'laser

‘

0 Vote Up Vote Down
Rui Santos Staff answered 4 years ago

Hello Joseph,

This is your problem at line 40, there’s a : missing after the if statement. it needs to be something like this:

  • https://pastebin.com/dukYVTdV

You also had your indentation wrong (specially in the while true). It should now be fixed the indentation, but I’m not sure if it will work as expected, because I couldn’t try it.

I apologize for taking so long to get back to you, but I’ve been with an eye problem and I had to limit my access to screens (computer/smartphones).

0 Vote Up Vote Down
Joseph Jakpa answered 4 years ago

Thanks for the reply Rui. I’ll test it and give feed back. btw, there’s s protective eye glass you can buy (if you do no already own one) that blocks the harmful uv light from your eyes.

0 Vote Up Vote Down
Rui Santos Staff answered 4 years ago

Thanks for the tip, I had an allergic infection, so it’s nothing to do with the screens. However avoid screens helped getting better faster. Let me know your results. 

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] SMART HOME with RPi, ESP32, and ESP8266 (version 1.2) March 8, 2023
  • [eBook Updated] ESP32-CAM Projects (version 1.3) January 7, 2023

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2023 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.