Problem with subscribing to thingspeak topic with SIM7600 hat on raspberry pi

5 ビュー (過去 30 日間)
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 件のコメント
Christopher Stapels
Christopher Stapels 2024 年 4 月 7 日
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?

サインインしてコメントする。

採用された回答

Milan Tomin
Milan Tomin 2024 年 4 月 7 日
移動済み: Christopher Stapels 2024 年 4 月 8 日
Yep publish works. I managed to solve the problem. It was actually the sim7600 hat's that was causing it. My credentials and subscribe message were correct.
The thing is, in setup funciton after the connect command (AT+CMQTTCONNECT) i inputed the publish topic for publishing with AT+CMQTTTOPIC. This is what caused the problem. I removed this command, and it worked out. It seems that when you do the input publish topic command and then inp ut subscribe topic command one after the other it causes problems.
So now i first input the subscribe topic and then subscribe (AT+CMQTTSUB) to the thingspeak channel, and then do the publishing commands.
  1 件のコメント
Christopher Stapels
Christopher Stapels 2024 年 4 月 8 日
Thaks for sharing the details, glad to hear you got it working.

サインインしてコメントする。

その他の回答 (0 件)

コミュニティ

その他の回答  ThingSpeak コミュニティ

カテゴリ

Help Center および File ExchangeWrite Data to Channel についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by