Problem with subscribing to thingspeak topic with SIM7600 hat on raspberry pi
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Milan Tomin
el 6 de Abr. de 2024
Comentada: Christopher Stapels
el 8 de Abr. de 2024
Having a bizarre problem subscribing to topic with sim7600 hat on raspberry pi. When i try subscribing i get: the following: OK CMQTTSUB: 0,6 CMQTTCONNLOST: 0,1.
Code 6 in the CMQTTSUB means failed to receive message. Code 0 in CMQTTCONNLOST means socket closed passively. Why tho?
Mqtt actions with sim7600 such as Acquire client, connect, and even publish works without any difficulties in the same project, sub however does not. I'm using thingspeak as a broker, and have tried broker.emqx.io to subscribe to and... it worked without any problem. Has anyone had any similar problem? I've tried subscribing to one or multiple fields, still no dice. Topic strings i've tried:
channels/2429193/subscribe/fields/field1
channels/2429193/subscribe/fields/+
channels/2429193/subscribe
I provide the code:
def setup(self):
isSerial2Available = True
self.SentMessage('AT+CMQTTDISC=0,60\r\n')
time.sleep(0.5)
self.SentMessage('AT+CMQTTREL=0\r\n')
time.sleep(0.5)
self.SentMessage('AT+CMQTTSTART\r\n') # Enable MQTT service.
time.sleep(1)
connect_cmd = 'AT+CMQTTACCQ=0,"'+ self.ID +'",0,4' # Apply for MQTT client with client ID.
self.SentMessage(connect_cmd + '\r\n')
time.sleep(1)
connect_cmd = 'AT+CMQTTCONNECT=0,"tcp://mqtt3.thingspeak.com",60,1,"'+ self.username +'","'+ self.password +'"' # Send MQTT connection request to the server.
self.SentMessage(connect_cmd + '\r\n')
time.sleep(2)
dataLength = str(len(self.pub))
connect_cmd = "AT+CMQTTTOPIC=0,{}".format(dataLength) # Publish to the inputed topic.
self.input_message(connect_cmd, self.pub)
time.sleep(1.5)
dataLength = str(len('channels/2429193/subscribe'))
connect_cmd = "AT+CMQTTSUBTOPIC=0,{},0".format(dataLength) # Subscribe to the inputed topic.
self.input_message(connect_cmd, "channels/2429193/subscribe")
time.sleep(0.5)
self.SentMessage('AT+CMQTTSUB=0\r\n')
def SentMessage(self, p_char):
global isSerial2Available
towrite = p_char.encode()
self.ser.write(towrite)
time.sleep(1)
response = self.ser.read_all().decode()
print(response)
responses = response.split('\r\n')
for resp in responses:
if "+CREG: 0," in resp:
status = int(resp.split("+CREG: 0,")[1]) # Check if connected to the network.
if status == 6:
isSerial2Available = False
print("\nNetWork Connected")
elif "+CMQTTCONNECT: 0," in resp:
status = int(resp.split("+CMQTTCONNECT: 0,")[1]) # Check if the client is connected.
if status == 0:
isSerial2Available = False
print("\nMqtt Connected")
elif resp == "+CMQTTSTART: 23":
isSerial2Available = False
print("\nMqtt is already Connected")
def input_message(self, p_char, p_data):
global startSent
self.ser.write(p_char.encode() + b'\r\n')
time.sleep(0.2)
encodedstr = p_data.encode() + b'\r\n'
self.ser.write(p_data.encode() + b'\r\n')
time.sleep(1)
response = self.ser.read_all().decode()
responses = response.split('\r\n')
for resp in responses:
if "+CMQTTSUB: 0," in resp:
status = int(resp.split("+CMQTTSUB: 0,")[1])
if status == 0:
print("\nSubTopic Sub")
startSent = True
self.subThreadStart()
def publishData(self, updateMsn):
dataLength = str(len(self.pub))
connect_cmd = "AT+CMQTTTOPIC=0,{}".format(dataLength) # Publish to the inputed topic.
self.input_message(connect_cmd, self.pub)
dataLength = str(len(str(updateMsn)))
connect_cmd = "AT+CMQTTPAYLOAD=0,{}".format(dataLength) # Input the publish message
self.input_message(connect_cmd, str(updateMsn))
time.sleep(0.5)
self.ser.write(b'AT+CMQTTPUB=0,0,120\r\n')
1 comentario
Christopher Stapels
el 7 de Abr. de 2024
Is your channel set to allow the device to subscribe? I dont see the credentials part of your code to be sure you are sending the clientID and the like correctly. Does publish work?
Respuesta aceptada
Milan Tomin
el 7 de Abr. de 2024
Movida: Christopher Stapels
el 8 de Abr. de 2024
1 comentario
Christopher Stapels
el 8 de Abr. de 2024
Thaks for sharing the details, glad to hear you got it working.
Más respuestas (0)
Comunidades de usuarios
Más respuestas en ThingSpeak Community
Ver también
Categorías
Más información sobre Write Data to Channel en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!