Milan Tomin in MATLAB Answers
最後のアクティビティ: 2024 年 4 月 8 日

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')
Johan Vervaet in MATLAB Answers
最後のアクティビティ: 2022 年 2 月 6 日

I am using a free license for publishing data with MQTT on ThingSpeak which works very good, according to the format: channels/<channelID>/publish/fields/field<fieldnumber>. Can I subscribe my channel(s) with MQTT with a free license with the format: channels/<channelID>/subscribe/fields/field<fieldnumber>. Or is there any restriction? Thanks for your answer and best regards.
Jason Wu in MATLAB Answers
最後のアクティビティ: 2021 年 5 月 29 日

Dear Sirs, I am student in university, new in MQTT, used MQTTlens and MQTT.fx as the client. Used them to subscribe the public channel "1293177", the channel publish weather data frequently. But, only got the first record immediately after subscribe, and no more. If subscribe again, will got the same record again. I follow the article at https://www.mathworks.com/help/thingspeak/troubleshoot-MQTT-subscribe.html and without mismatch. Would you kindly help to figure out the problem is?
Mar Ron in MATLAB Answers
最後のアクティビティ: 2020 年 5 月 8 日

I have a channel that has several feeds with live data. I am trying to use a particle Argon and thingspeak MQTT broker to subscribe to this data. I used the example provided and got nothing back from the subscription. I am pasting my code below. If anyone has a working example, that would be great! #include "MQTT.h" const long channelID = 10X4XXX; String ReadAPIKey = "XXXXXXXXX"; String subscribeTopic = "channels/" + String( channelID ) + "/subscribe/fields/field1/"+String(ReadAPIKey); String MQTTAPIKey = "XXXXXXXXXX"; void callback( char* topic, byte* payload, unsigned int length ); MQTT client( "mqtt.thingspeak.com" , 1883 , callback ); // This function processes the messages relayed by the MQTT broker. void callback( char* topic, byte* payload, unsigned int length ) { Serial.println("in callback"); char p[ length + 1 ]; // Leave an extra space to null terminate the string. memcpy( p, payload, length ); p[ length ] = NULL; // Terminate the string. } void setup() { Serial.println("in void setup"); // Connect to the server. subscribeMQTT(); } void loop() { Serial.println("in void loop"); int timeHour = Time.hour(); if (client.isConnected()){ client.loop(); } else{ subscribeMQTT(); } if ( ( timeHour > 23 ) or ( timeHour < 4 ) ){ Particle.publish( "Sleep" ); System.sleep( SLEEP_MODE_DEEP , 7200 ); } delay(20000); } void subscribeMQTT(){ Serial.println("in subscribe MQTT"); if (!client.isConnected()) { client.connect( " MGSubscribeArgon" , "MGUsername" , MQTTAPIKey , NULL , MQTT::QOS0 , 0 , NULL , true ); Particle.publish( " Connect " ); Serial.println("Connected to broker"); delay( 1000 ); if ( client.isConnected()) { Serial.println("MQTT connected"); client.subscribe( subscribeTopic ); Particle.publish( "subs" ); } } }
Gcobani Mkontwana in MATLAB Answers
最後のアクティビティ: 2019 年 11 月 5 日

Hi Is there anyone who can me help with work around example, as to how to publish message using talkback or plugin on client side? I am using native javascript with jquery and have API key sending GET protocol. I want my button to subscribe to that channel i created, please help me by showing an example i am sure can able to do this. My channel have two fields temp and illuminance, both been programmed on IDE Arduno and have button_state that must read from my talkback functionality; Plugin functionality for my button to read or subscribing my channel; <html> <head> <!-- NOTE: This plugin will not be visible on public views of a channel. If you intend to make your channel public, consider using the MATLAB Visualization App to create your visualizations. --> %%PLUGIN_CSS%% %%PLUGIN_JAVASCRIPT%% </head> <body> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"><br/> <div class="col-md-4 text-center"> <button id="singlebutton" name="singlebutton" class="btn btn-primary">IOT-Talk</button> </div> </body> </html> <script src="https://code.jquery.com/jquery-3.3.1.js"integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="crossorigin="anonymous"></script> <script type="text/javascript"> function OPENDOOR() { $.ajax({ url:"https://api.thingspeak.com/talkbacks/35734/commands?key=MF710NTKMG0E44UG&command_string=OPENDOOR&position=1", type: 'GET', success: function(data) { //called when successful //console.log(data); $('#singlebutton').on('click', function (e) { $('#singlebutton').append(data); }) } }); } </script>

ThingSpeak について

The community for students, researchers, and engineers looking to use MATLAB, Simulink, and ThingSpeak for Internet of Things applications. You can find the latest ThingSpeak news, tutorials to jump-start your next IoT project, and a forum to engage in a discussion on your latest cloud-based project. You can see answers to problems other users have solved and share how you solved a problem.

モデレーター