• 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

msqqt control multiple pins

Q&A Forum › msqqt control multiple pins
0 Vote Up Vote Down
t.fysh asked 3 years ago

I’m having trouble getting my head around multiple pins on esp8266 with node red and micropython I can’t get broker to recognize more than one subject.

8 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

Hi.
What project are you following? and what errors are you receiving?
Can you provide more information?

0 Vote Up Vote Down
t.fysh answered 3 years ago

Module 5 on micropython for esp8266 and esp32 what iwant to do is equivalent to adding a second led. (I’m going to use it to control two relays) 

0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

Hi.
Can you share your code? Are you publishing on multiple topics, or publishing different messages on the same topic?
You can use pastebin to share your code.
Regards,
Sara

0 Vote Up Vote Down
t.fysh answered 3 years ago

one topic with 2 switches which give 4 unique answers. How do I use pastebin

0 Vote Up Vote Down
t.fysh answered 3 years ago
Start your code here boot.py

import time
from umqttsimple import MQTTClient
import ubinascii
import machine
import micropython
import network
import esp
esp.osdebug(None)
import gc
gc.collect()

ssid = '**********'
password = '********'
mqtt_server = '192.168.1.241'

#EXAMPLE IP ADDRESS
#mqtt_server = '192.168.1.144'
client_id = ubinascii.hexlify(machine.unique_id())
topic_sub = b'relay'




station = network.WLAN(network.STA_IF)

station.active(True)
station.connect(ssid, password)

while station.isconnected() == False:
  pass

print('Connection successful')
print(station.ifconfig())

gPin = machine.Pin(4, machine.Pin.OUT, value=1)

cPin = machine.Pin(5, machine.Pin.OUT, value=1)

main.py
# Complete project details at https://RandomNerdTutorials.com



def sub_cb(topic, msg):
  print((topic, msg))
  if msg == b'gon':
    gPin.value(0)
  elif msg == b'goff':
    gPin.value(1)
  elif msg == b'con':
    cPin.value(0)
  elif msg == 'bcoff':
    cPin.value(1)

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()






0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

Hi.
Can you add the following snippet to the end of your main.py file and see if it works?

while True:
  try:
    client.check_msg()
  except OSError as e:
    restart_and_reconnect()

Basically, this snippet checks whether there’s a pending message to receive and delivers that message to the sub_cb() function.

Regards,
Sara

0 Vote Up Vote Down
t.fysh answered 3 years ago

That’s the answer thanks very much. By the way I have enjoyed the course clear and very helpful.
I think it would help if you could produce a tutorial(s) on more elaborate mqtt examples etc.

0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

Hi.
I’m glad it is working now.
Yes, you’re right. Examples like publishing on multiple topics, publishing different messages and so on, have more than two devices communicating, etc, and other subjects.
Thanks for the suggestion. We’ll may come up with new projects on the next ebook update, but at the moment it’s not planned yet.
Regards,
Sara

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.