Electron Publish code

I am looking for the particle electron code to send data to the back-end of my company.I am using libraries like PubSubclient.h client and MQTT.h. Can anyone help me with the code?

What have you tried so far?
You may start with the usage examples that come with the respective libraries or browse the forum for examples provided by members.
Once you got something to discuss we can build on, people might chime in but if you want someone to do the job for you that’d be contracting work.

2 Likes
// This #include statement was automatically added by the Particle IDE.
#include <MQTT.h>

// This #include statement was automatically added by the Particle IDE.
#include <PubSubClient.h>


#define TOKEN "7LpD6EGJGtoHXc6QNlJu"
//#define THINGSBOARD_HOST "host name"
byte server_name[] = { IP address };
//EthernetClient ethClient;
//PubSubClient client;
//client MQTT.client();
void callback(char* topic, byte* payload, unsigned int length);
MQTT client(server_name, 1883, callback);
//char thingsboardServer[] = "host name";
//char* thingsboardServer = "host name";

//PubSubClient client("host name",1883,60);




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

//void callback(const char[] topic, byte* payload, unsigned int length)

void setup() 
{
    Serial.begin(9600);
   // byte server_name[] = {18.220.94.193};
    client.connect("host name","username","password");
    client.publish("outTopic","hello world");
  //  client.setClient(ethClient);
 //   client.setServer("host name",1883);
    //client.setServer(thingsboardServer , 1883 );

}

void loop() 
{
   delay(1000);
    //client.connect();
   // Serial.println("Hello");
   // client.sendValues();
   client.publish("outTopic","hello world");
   client.loop();
}

I have written this code,this code compiles but i am not getting any output, Please help.

You have made sure your server is accessible from outside and that port is open for inbound trafic?

BTW, the line String message(p) makes no sense in this code. It does nothing really since message will vanish and only leave a heap fragment behind once the callback() returns.

And about this statement

    client.connect("host name","username","password");

The first parameter isn’t really the host name but the ID of the “channel” on that host/server - although you could use the host name as ID too, but to avoid confusion I’d rather not reuse that.
Additionally for testing purposes I’d omit username/password at first.

For instance with test.mosquitto.org as broker I can establish a connection with client.connect("channelID") but not with client.connect("channelID", "username", "password").

2 Likes