1

I tried to send random data to MQTT but it's not working and I get:

{
   "m2m:rsp" : {
      "rsc" : 4004,
      "rqi" : "123456",
      "pc" : "Resource not found",
      "to" : "5a185cedc6ad749b:edb0b82be780ba6d",
      "fr" : "/antares-cse"
   }
}

This is from mqtt.fx

This is my payload what should I do

    #include <SoftwareSerial.h> //koneksi ke modem
    
    #define TINY_GSM_MODEM_SIM7000
    #define TINY_GSM_DEBUG SerialMon
    #define BAUD 9600
    #define SerialMon Serial
    
    SoftwareSerial SerialAT(10, 8); // RX, TX ke modem
    
    const char apn[]  = "nb1internet"; //untuk gprs gunakan "internet"
    const char user[] = "";
    const char pass[] = "";
    byte randomValue = random(0, 37);
    const char *mqtt_server = "mqtt.antares.id"; //server antares
    const char *mqtt_user = "";
    const char *mqtt_pass = "";
    const int mqtt_port = 1883;
    const char *mqtt_client_name = "KEKW" + randomValue; // random
    
    //dibawah ini sesuaikan dengan akun atares kamu
    #define TOPIKPUB "/oneM2M/req/5a185cedc6ad749b:edb0b82be780ba6d/antares-cse/json"
    #define accessKey "5a185cedc6ad749b:edb0b82be780ba6d"
    #define projDev "ECSENSOR/pHnEC" 
    
    // library modem dan mqtt 
    #include <TinyGsmClient.h>
    #include <PubSubClient.h>
    
    #ifdef DUMP_AT_COMMANDS
      #include <StreamDebugger.h>
      StreamDebugger debugger(SerialAT, SerialMon);
      TinyGsm modem(debugger);
    #else
    
    TinyGsm modem(SerialAT);
    #endif
    TinyGsmClient client(modem);
    PubSubClient mqtt(client);
    
    long lastReconnectAttempt = 0;
    int val;
    
    
    void setup()
    {
      // Set debug console dan modem
      SerialMon.begin(9600);
      delay(10);
      SerialAT.begin(9600);
      delay(3000);
    
      String modemInfo = modem.getModemInfo();
      SerialMon.print("Modem: ");
      SerialMon.println(modemInfo);
    
      SerialMon.print("Waiting for network...");
      if (!modem.waitForNetwork(240000L)) {
        SerialMon.println(" fail");
        delay(10000);
        return;
      }
      SerialMon.println(" OK");
    
      if (modem.isNetworkConnected()) {
        SerialMon.println("Network connected");
      }
    
      SerialMon.print(F("Connecting to "));
      SerialMon.print(apn);
        if (!modem.gprsConnect(apn, user, pass)) {
        SerialMon.println(" fail");
          delay(10000);
          return;
      }
      
        SerialMon.println(" OK");
    
    
       // MQTT Broker setup
      mqtt.setBufferSize(512);
      mqtt.setServer(mqtt_server, mqtt_port);
    }
    
    
    boolean mqttConnect() {
      SerialMon.print("Connecting to ");
      SerialMon.print(mqtt_server);
    
      // koneksi ke MQTT Broker
      boolean status = mqtt.connect(mqtt_client_name,mqtt_user, mqtt_pass);
    
      if (status == false) {
        SerialMon.println(" fail");
        return false;
      }
      SerialMon.println(" OK");
    
      return mqtt.connected();
    }
    
    
    void loop()
    {
    
      //cek jika koneksi mqtt gagal maka ulang lagi
       if (!mqtt.connected()) {
        SerialMon.println("=== MQTT NOT CONNECTED ===");
        // Reconnect every 10 seconds
        unsigned long t = millis();
        if (t - lastReconnectAttempt > 10000L) {
          lastReconnectAttempt = t;
          if (mqttConnect()) {
            lastReconnectAttempt = 0;
          }
        }
        delay(100);
        return;
      }
    
      int val = random(0-450);  //TEST RANDOM DATA
                    
      String pubString ;  
     //JSON yang panjang untuk kirim data ADC ke antares
        pubString +=  F("{");
        pubString += F("\"m2m:rqp\": {");
        pubString += F("\"fr\": \"");
        pubString += String(accessKey) ;
        pubString += F("\",");
        pubString += F("\"to\": \"/antares-cse/antares-id/");
        pubString += String(projDev);
        pubString += F("\",");
        pubString += F("\"op\": 1,");
        pubString += F("\"rqi\": 123456,");
        pubString += F("\"pc\": {");
        pubString += F("\"m2m:cin\": {");
        pubString += F("\"cnf\": \"message\",");
        pubString += F("\"con\": \"{\\\"level\\\":");
        pubString += String(val);
        pubString += F("}\"");
        pubString += F("}");
        pubString += F("},");
        pubString += F("\"ty\": 4");
        pubString += F("}");
        pubString += F("}");
      
      // kirim atau publish ke antares
      SerialMon.println(pubString);
      char message_buff[pubString.length() + 1];
      pubString.toCharArray(message_buff, pubString.length() + 1);
      mqtt.publish(TOPIKPUB, message_buff);
    
    delay(5000);
    }
1
  • 1
    what you should do is to include the whole sketch
    – jsotola
    Dec 28 '20 at 8:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.