TXT4.0 MQTT broker and its configuration

Alles rund um TX(T) und RoboPro, mit ft-Hard- und Software
Computing using original ft hard- and software
Forumsregeln
Bitte beachte die Forumsregeln!
Antworten
vleeuwen
Beiträge: 1557
Registriert: 31 Okt 2010, 22:23
Wohnort: Enschede (NL)
Kontaktdaten:

TXT4.0 MQTT broker and its configuration

Beitrag von vleeuwen » 02 Dez 2022, 20:01

On the TXT is a Mosquitto MQTT broker available and it is possible to change its configuration file.
Is this also possible on the TXT4.0?
How to stop and start the MQTT Mosquitto broker on the TXT4.0?
How to change the mosquitto.conf in /etc/mosquitto?
software enigineer/teacher/advisor
Google translate
http://tescaweb.nl/Carel/?p=713

rugee-f
Beiträge: 63
Registriert: 28 Jan 2022, 13:46

Re: TXT4.0 MQTT broker and its configuration

Beitrag von rugee-f » 02 Dez 2022, 22:06

I built my own MQTT Broker on TXT4 longer time ago in plain python code:

# MQTT ft-TXT IO Bridge
# Program to expose ft-TXT IO to MQTT for Node-Red use

import paho.mqtt.client as mqtt
BROKER_ADDRESS = "127.0.0.1"
PORT = 1883
QOS = 0

import ftlock
import sys

if ftlock.try_lock(sys.argv[0]) < 0 :
state = ftlock.state()
print("Resources locked by " + state.name)
sys.exit(1)

import time
import datetime
import json

import cv2 as cv
import base64

import ft
import bme680

from smbus import SMBus

#from sft import ft, ftlock

# Object to capture the frames
cap = cv.VideoCapture(0)

#setting of fps rate not working on TXT4!
#in camera - config via MQTT 'fps setting' is not implemented.
#set 10 fps (not working!)
cap.set(5,10)

#HD 720p Webcam
#Sonix Technology USB 2.0 camera
cap.set(3,640)
cap.set(4,360)

MESSAGE = ""

T_LAST = datetime.datetime.utcnow()
T_START = T_LAST

#init cam settings
CAM_PERIOD = 3
CAM_ON = False

#init ldr settings
LDR_PERIOD = 3
t_ldr_last = datetime.datetime.utcnow()


BME_PERIOD = 3
t_bme_last = datetime.datetime.utcnow()

#Declarations
DATA = 0

MEM_DI_1 = 0
MEM_DI_2 = 0
MEM_DI_3 = 0
MEM_DI_4 = 0

MEM_AI_5 = 0
MEM_AI_6 = 0
MEM_AI_7 = 0
MEM_AI_8 = 0

TXT4=ft.fttxt2()

TXT4_VAL = str(TXT4.get_values)
MEM_raw_io = str(TXT4.get_values)

#I1-I4 used as Digital Input (S1-S4)
TXT_DI_1 = TXT4.switch(1)
TXT_DI_2 = TXT4.switch(2)
TXT_DI_3 = TXT4.switch(3)
TXT_DI_4 = TXT4.switch(4)

#I5-I6 used as Analog Input (0-15kOhm)
TXT_AI_5 = TXT4.resistor(5)
TXT_AI_6 = TXT4.resistor(6)
#I7-I8 used as Analog Input (0-9V)
TXT_AI_7 = {}
TXT_AI_8 = {}

#O1-O4 used as Digital Output (L1-L4)
TXT_OUT_L1=TXT4.lamp(1)
TXT_OUT_L2=TXT4.lamp(2)
TXT_OUT_L3=TXT4.lamp(3)
TXT_OUT_L4=TXT4.lamp(4)

#O5-O6 used as Motor Output (M3)
TXT_OUT_M3=TXT4.motor(3)
#O7-O8 used as Motor Output (M4)
TXT_OUT_M4=TXT4.motor(4)

ftlock.notify("R")

# callback when a PUBLISH message is received from server
def on_message(client, userdata, message):

global CAM_ON
global CAM_PERIOD
global LDR_PERIOD

msg = str(message.payload.decode("utf-8","ignore"))
print("message received: ", msg)
print("message topic: ", message.topic)

if message.topic == "TXT/outputs/O1_lamp/PWM":
write_setpoints(TXT_OUT_L1, int(msg))

if message.topic == "TXT/outputs/O2_lamp/PWM":
write_setpoints(TXT_OUT_L2, int(msg))

if message.topic == "TXT/outputs/O3_lamp/PWM":
write_setpoints(TXT_OUT_L3, int(msg))

if message.topic == "TXT/outputs/O4_lamp/PWM":
write_setpoints(TXT_OUT_L4, int(msg))

if message.topic == "TXT/outputs/O5_motor/SPEED":
motor_control(TXT_OUT_M3, int(msg))

if message.topic == "TXT/outputs/O7_motor/SPEED":
motor_control(TXT_OUT_M4, int(msg))

#new camera config message arrived
if message.topic == "TXT/c/cam":
#print("data Received type",type(msg))

MESSAGE = json.loads(msg)
#extract PERIOD
CAM_PERIOD = int(MESSAGE['period'])
#extract camera state
CAM_ON = bool(MESSAGE['on'])

client.publish("TXT/c/cam/config", "on: " + str(CAM_ON) + " per.: " + str(CAM_PERIOD))

#new ldr config message arrived
if message.topic == "TXT/c/ldr":
MESSAGE = json.loads(msg)
#extract PERIOD
LDR_PERIOD = int(MESSAGE['period'])

# callback when client receives CONNACK from server
def on_connect(client, userdata, flags, rc):
print("Connected to MQTT Broker: " + BROKER_ADDRESS)
#subscribe for setpoints for outputs
client.subscribe("TXT/outputs/O1_lamp/PWM")
client.subscribe("TXT/outputs/O2_lamp/PWM")
client.subscribe("TXT/outputs/O3_lamp/PWM")
client.subscribe("TXT/outputs/O4_lamp/PWM")
client.subscribe("TXT/c/cam")
client.subscribe("TXT/c/ldr")
client.subscribe("TXT/outputs/O5_motor/SPEED")
client.subscribe("TXT/outputs/O7_motor/SPEED")
client.publish("TXT/io_raw/io", TXT4_VAL)

#define IO Usage of TXT for software application
#global var usage not checked!
def init_TXT_IO():
# TXT4 IO config
#INPUTS
TXT_DI_1=TXT4.switch(1)
TXT_DI_2=TXT4.switch(2)
TXT_DI_3=TXT4.switch(3)
TXT_DI_4=TXT4.switch(4)

#OUTPUTS
#O1-O4 used as Digital Output (L1-L4)
TXT_OUT_L1=TXT4.lamp(1)
TXT_OUT_L2=TXT4.lamp(2)
TXT_OUT_L3=TXT4.lamp(3)
TXT_OUT_L4=TXT4.lamp(4)
#create MQTT setpoints for outputs
client.publish("TXT/outputs/O1_lamp/PWM", 0)
client.publish("TXT/outputs/O2_lamp/PWM", 0)
client.publish("TXT/outputs/O3_lamp/PWM", 0)
client.publish("TXT/outputs/O4_lamp/PWM", 0)

#O5-O6 used as Motor Output (M3)
TXT_OUT_M3=TXT4.motor(3)
#O7-O8 used as Motor Output (M4)
TXT_OUT_M4=TXT4.motor(4)
client.publish("TXT/outputs/O5_motor/SPEED", 0)
client.publish("TXT/outputs/O7_motor/SPEED", 0)

#set config
TXT4.update_config()

def write_setpoints(out_no, out_val):
out_no.pwm_set(out_val)
client.publish("TXT/io_raw/message", (str(out_no)) + " setpoint written " + (str(out_val)) )

def motor_control(out_no, out_val):
out_no.start_speed(out_val)
client.publish("TXT/io_raw/message", (str(out_no)) + " setpoint written " + (str(out_val)) )

#End of subroutines

#Start of Main Program

#connect MQTT
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(BROKER_ADDRESS, PORT)

#global var usage not checked!
init_TXT_IO()
... and so on...

I started and stopped the broker with an EXEC node from Node-RED

vleeuwen
Beiträge: 1557
Registriert: 31 Okt 2010, 22:23
Wohnort: Enschede (NL)
Kontaktdaten:

Re: TXT4.0 MQTT broker and its configuration

Beitrag von vleeuwen » 02 Dez 2022, 22:57

MQTT broker === services, it takes care of the connection with the WLAN
It is managing subscriptions and publications in general, including QOS.
You have a MQTT client that is using the MQTT broker services.
See: http://www.steves-internet-guide.com/mosquitto-broker/
software enigineer/teacher/advisor
Google translate
http://tescaweb.nl/Carel/?p=713

rugee-f
Beiträge: 63
Registriert: 28 Jan 2022, 13:46

Re: TXT4.0 MQTT broker and its configuration

Beitrag von rugee-f » 03 Dez 2022, 20:58

Thank you for pointing this out - you are right, I only used client functionality. I got a bit confused about the possibility of declaring own topics and publish them for use in other clients. In client server based environments this is normally server functionality - not so in MQTT.
By the way - today I was in Salzstetten at the ft home for visiting their open day. I picked up the Information that the broker on Port 1883 is only operating in the new TXT4 firmware, when it is paired to the ft-cloud - so I suppose my old python code would not work correct with the new firmware. I have to test this...

vleeuwen
Beiträge: 1557
Registriert: 31 Okt 2010, 22:23
Wohnort: Enschede (NL)
Kontaktdaten:

Re: TXT4.0 MQTT broker and its configuration

Beitrag von vleeuwen » 03 Dez 2022, 21:48

The new MQTT blockleys in firmware 3.1.4 are unfortunately not documented. There are even more extensions.
Since Nov 2021 I haven't tested much; this because I had already reported some minor problems that prevented me from getting any further.
Next week I will restart testing and also the MQTT extension. I have a Mosquitto MQTT broker on my laptop, complete with logging as well as a recent NodeRed environment with a test application. There is no reason from MQTT why the MQTT broker should run on a TXT (4.0), that is precisely the essence of MQTT.
In the same local network, MQTT can be used to collaborate with various IoT devices (for example: RaspPy Arduino, Laptop).
The bridge in the MQTT broker makes it possible to reach other MQTT brokers in WLAN as well. However, for this the MQTT broker must be configured via the Morsquitto .conf.
What is worth investigating is to stop the MQTT broker on the TXT4.0 via Putty and start it again with another conf file.
Currently, the older TXT offers more options and is more convenient to use with RoboPro (classic). Programming 4G versus your limited 3G.
What I will also test is developing remote Python with MS-Visual Studio code (free). With the previous firmware, the handling of IP traffic was unfortunately too unstable for MS-Visual Studio Code to work stably.
software enigineer/teacher/advisor
Google translate
http://tescaweb.nl/Carel/?p=713

Techum
Beiträge: 84
Registriert: 25 Dez 2014, 20:50
Kontaktdaten:

Re: TXT4.0 MQTT broker and its configuration

Beitrag von Techum » 23 Sep 2023, 23:27

Hello Carel, I just use this old thread of You to sum up and ask for additional comments.
Probably You have found out much more in the meantime.
I just started looking into MQTT and am fascinated, though I probably do not have the full picture.
vleeuwen hat geschrieben:
03 Dez 2022, 21:48
The new MQTT blockleys in firmware 3.1.4 are unfortunately not documented. There are even more extensions.
Since Nov 2021 I haven't tested much; this because I had already reported some minor problems that prevented me from getting any further.
Next week I will restart testing and also the MQTT extension. I have a Mosquitto MQTT broker on my laptop, complete with logging as well as a recent NodeRed environment with a test application. There is no reason from MQTT why the MQTT broker should run on a TXT (4.0), that is precisely the essence of MQTT.
In the same local network, MQTT can be used to collaborate with various IoT devices (for example: RaspPy Arduino, Laptop).
The bridge in the MQTT broker makes it possible to reach other MQTT brokers in WLAN as well. However, for this the MQTT broker must be configured via the Morsquitto .conf.
What is worth investigating is to stop the MQTT broker on the TXT4.0 via Putty and start it again with another conf file.
Currently, the older TXT offers more options and is more convenient to use with RoboPro (classic).
Indeed the MQTT on TXT4.0 is set to port 2883 and bind_address 127.0.0.1, so a connection from another device (without root) is impossible.
However, with the new Blockly elements one can easily connect to a MQTT server in the network like this (here with anonymous login)
blockly_mqtt.JPG
blockly_mqtt.JPG (63.36 KiB) 2806 mal betrachtet
The TXT in "MQTT Broker mode" is open and can be used as MQTT server from any device in the network without any problem as You described in another thread. So rather than setting up a Mosquitto MQTT server on a PC or a Raspberry PI - if one has a TXT - one can simply put that in MQTT Broker mode and have fun with communicating between them or any other device in the network.

What I personally found difficult was the definition of the JSON message structure just using RoboPro Coding without Python commands. The only way I found is through file. A very simple one e.g. like this:
json.JPG
json.JPG (28.4 KiB) 2806 mal betrachtet
For receiving information, one can work with JSON messages like this in a call-back way:
callback.JPG
callback.JPG (42.48 KiB) 2806 mal betrachtet
As long as working without UTF coding, one can do message sending and receiving without a single line of Python.

From my point of view, even after almost two years, the fischertechnik documentation of RoboPro Coding is far from the level that had been reached with RoboPro. I have read about the idea to create a WIKI and I consider it a great idea...

Have a nice evening,
Techum

Antworten