Help with IBM cloud IoT using MQTT

Hello, can anyone help me with connecting to IBM cloud using MQTT. I’ve been trying for days any I just can’t get my Particle Photon to connect.

I followed the steps to create a Internet of Things service on IBM cloud. I created a device type and device for my Photon. I have my organization ID, and authentication token. I opened the MQTT port (1883) on my windows machine. And this is the code I am trying to use, given to me by my professor:

#include <MQTT.h>

#define HOST_PORT  1883
#define MQTT_QoS   0

char *IOT_HOST = "euzj21.messaging.internetofthings.ibmcloud.com"; 
char *IOT_CLIENT = "d:euzj21:Photon:WarrensPhoton";
char *IOT_USERNAME = "use-token-auth"; 
char *IOT_PASSWORD = "_I put my token here_";
char *IOT_TOPIC = "iot-2/evt/status/fmt/json";

MQTT client( IOT_HOST, HOST_PORT, callback );

bool flag = 1;
int IndicatorLed = D7;
char payload[80];

void setup() {
  pinMode(IndicatorLed, OUTPUT);
  Serial.begin( 9600 );
  Serial.println( "Connecting Photon to IBM Watson IoT Platform ...... " );

  while( !Serial.available() ) {
    Particle.process();
  }  

  client.connect( IOT_CLIENT, IOT_USERNAME, IOT_PASSWORD  );

  if( client.isConnected() ) {
    Serial.println( "Now connected!" );
    // client.subscribe( IOT_SUBSCRIBE );
  }
}

void loop() {
  sprintf(payload, "{ \"binary\": \"%d\" }", flag);
 
  client.publish(IOT_TOPIC, const_cast<char*> (payload) ); 
 
  digitalWrite(IndicatorLed, HIGH);
  delay(100);
  digitalWrite(IndicatorLed, LOW);
  delay(100);
   
  client.loop();

  Serial.print( "Data being sent to Cloud: " );
  Serial.println( flag );
  delay( 3000 );
 
  flag = (!flag);
}

void callback( char* topic, byte* payload, unsigned int length ) {
  char p[length + 1];
   
  memcpy( p, payload, length );
  p[length] = NULL;
   
  String message( p );
}

Using the particle serial monitor, the program gets as far as “Connecting Photon to IBM Watson IoT Platform…” but then it never gets to the “Now connected”. And it still shows as disconnected in the IBM cloud dashboard.

Any help would be appreciated.

Edit: Just as an update, I have since connected both a Raspberry Pi and an ESP32 and been able to send simple data to the same IBM Watson IoT platform, so I know the IBM side is correct and working.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.