• 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

How to measure only temperture in the micropython esp32 course – chapter Node_RED ?

Q&A Forum › Category: ESP8266 › How to measure only temperture in the micropython esp32 course – chapter Node_RED ?
0 Vote Up Vote Down
gj.linthorst asked 6 years ago

In the boot.py file I removed:  topic_sub = b’output’ and led = machine.Pin(2, machine.Pin.OUT. value=0)
In the main.py file I removed: client.subscribe(topic_sub).
The defenition -def connect_and_subscribe(0: I don,t know how to configure because there is no topic_sub and I get the error no sub_cb is defined.

5 Answers
0 Vote Up Vote Down
Rui Santos Staff answered 6 years ago

Hello, here’s what you need to remove. In the boot.py: https://github.com/RuiSantosdotme/ESP-MicroPython/blob/master/code/MQTT/Node_RED_Client/boot.py
Line 22 and line 42:

topic_sub = b'output'

led = machine.Pin(2, machine.Pin.OUT, value=0)

In the main.py: https://github.com/RuiSantosdotme/ESP-MicroPython/blob/master/code/MQTT/Node_RED_Client/main.py
You need to remove lines 20-25:

def sub_cb(topic, msg):
  print((topic, msg))
  if msg == b'on':
    led.value(1)
  elif msg == b'off':
    led.value(0)

Modify line 28 to:

global client_id, mqtt_server

And line 30:

client.set_callback(sub_cb)

You also need to remove line 32 and 33:

client.subscribe(topic_sub)
print('Connected to %s MQTT broker, subscribed to %s topic' % (mqtt_server, topic_sub))

You forgot that line 30, 32, 33 and that’s why you got that error message. Let me know if that solves your problem.

0 Vote Up Vote Down
gj.linthorst answered 6 years ago

Hello,

The above correction do not solve the problem and also I don’t understand.

Start your code here
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

In the above “def connnect_and_subscribe():” The client.subscribe(topic_sub) is removed. In the following line is connected to the broker and subscribed to mqtt_server, Topic_sub).

But there is no Topic_sub I hope you can give me more info.

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

You’re right! Sorry about that. I’ve updated my previous answer… Can you double-check what I’ve said?

In summary, your connect_and_subscribe() function should look like this:

def connect_and_subscribe():
  global client_id, mqtt_server
  client = MQTTClient(client_id, mqtt_server)
  client.connect()
  return client

I hope that works for you!

0 Vote Up Vote Down
gj.linthorst answered 6 years ago

It is working now 

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

I’m glad it’s working! Thanks for letting me know. I’ll mark this thread as “Resolved”.

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

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

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

Copyright © 2013-2025 · 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.