Pages: 1 [2]   Go Down
Author Topic: Complier window saying "expected primary-expression before ')' token"  (Read 2088 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 49
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi PaulS

This how I added the code

regards

Gary

Code:
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>

// Update these with values suitable for your network.
byte mac[]    = {  0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
byte server[] = { 172, 16, 0, 2 };
byte ip[]     = { 172, 16, 0, 100 };

// Callback function header
void callback(char* topic, byte* payload, unsigned int length);

EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);

// Callback function = only received at random intervals could be seconds hours or days.
void callback(char* topic, byte* payload, unsigned int length) {
  client.publish("outTopic", payload, length);
}

void setup()
{
 
  Ethernet.begin(mac, ip);
  if (client.connect("arduinoClient")) {
    client.publish("outTopic","hello world");
    client.subscribe("inTopic");
  }
}


void update()
{
  byte *payload = "my payload";
  int length = strlen(payload);
  client.publish("outTopic", payload , length);
}


void loop()
{
  client.loop();
  delay (15000);
  update();       // Update after 15 seconds
 
}


Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 610
Posts: 49083
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
This how I added the code
Code:
void update()
{
  byte *payload = "my payload";
  int length = strlen(payload);
  client.publish("outTopic", payload , length);
}
"my payload" is a string - a null terminated array of chars. That's what strlen() expects. If you are going to point to that string, you need a char pointer.

Then, if client.publish() expects the second argument to be a pointer to byte, lie to it and tell it that that is what payload is:
Code:
void update()
{
  char *payload = "my payload";
  int length = strlen(payload);
  client.publish("outTopic", (byte *)payload , length);
}

Although, why you would publish the constant string "my payload" over and over is a mystery. That is not what PeterH was suggesting.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 49
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi PaulS,

Thanks that compiles and now works sending the string to my MQTT server.

Now all i need is to get the variable from:
void callback() 'byte* payload'

in place of the dummy "my payload"
Please find highlighted with *** below in routine 'update()'

Any ideas ?

regards

Gary



Code:
//My addition to original code:


#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>

// Update these with values suitable for your network.
byte mac[]    = {  0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xEF };
byte server[] = {192,168,1,64};     
byte ip[]     = {192,168,1,1};

// Callback function header
void callback(char* topic, byte* payload, unsigned int length);

EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);


// Callback function = only received at random intervals could be seconds hours or days.
void callback(char* topic, byte* payload, unsigned int length)
{
  client.publish("outTopic", payload, length);
}

void setup()
{
  Ethernet.begin(mac, ip);
 
  if (client.connect("arduinoClient")) {
     client.publish("outTopic","hello world");
     client.subscribe("inTopic");
  }
}


void update()
{
  char *payload = "my payload"; // *** Need this to get variable from byte* payload in 'void callback' code above ***
  int length = strlen(payload);
  client.publish("outTopic", (byte *)payload , length);
}

void loop()
{
  client.loop();
  delay (1000);
  update();       // Update after 1 second
}


Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 610
Posts: 49083
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

callback describes the purpose of the function, but is a really crappy name for the function.

When the PubSubClient instance has something to report, it does so be calling that function.

You'll need to copy the data from the payload argument to some global variable (that is an array), and use the data in the global array in the update() function.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 49
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I have tried various ways of trying to do what PaulS said, but cannot figure out how to do it.
I have googled arrays, pointers, chars and Strings, I do understand more but not yet enough to make the code work.
And am now a bit overwhelmed by it all  smiley-confuse

If someone could help that would be appreciated, then when I have the answer I will google again to find out how it works.

My code is still that same as my last reponse.

Thanks

regards

Gary
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 610
Posts: 49083
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In callback(), payload is an argument, of type pointer to byte. You need to copy this data to a global variable, so that it can be used in another function.

What have you tried? There are only 7 lines of code needed - two to declare the variables and 5 to copy the data, using a for loop. Two of those lines of code are the open and close curly braces.

Code:
byte copyOfPayload[80];
int payloadSize;

Code:
for(byte i=0; i<length; i++)
{
   copyOfPayload[i] = payload[i];
}
payloadSize = length;

Now that you have copies of the data passed to callback(), you can use them in update().
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 49
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi PaulS,

Thanks for that, I can see how its done now, not as complicated as I thought, Will come in handy for the future now.

Just one problem left I pass "test" into the callback, it sends it back to the MQTT server but it comes back as "estt"

I have added update1 so that I can see the text changing.

Code:
//My addition to original code:

    #include <SPI.h>
    #include <Ethernet.h>
    #include <PubSubClient.h>
     
    byte copyOfPayload[80];
    int payloadSize;

    byte server[] = {192,168,1,64};                                                       
    byte ip[]     = {192,168,1,1};                                                         
    byte mac[]    = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEF };                                 

    // Callback function header
    void callback(char* topic, byte* payload, unsigned int length);

    EthernetClient ethClient;
    PubSubClient client(server, 1883, callback, ethClient);


    // Callback function = only received at random intervals could be seconds hours or days.
    void callback(char* topic, byte* payload, unsigned int length) {
    client.publish("outTopic", payload, length);

      for(byte i=0; i<length; i++)
        {
           copyOfPayload[i] = payload[i];
        }
      payloadSize = length;
    }


void setup()
{
     Serial.begin(9600);   
     Ethernet.begin(mac, ip);
     
  if (client.connect("arduinoClient"))
    {
      client.publish("outTopic","hello world");
      client.subscribe("inTopic");
    }
}


void update()
{
  client.publish("outTopic", copyOfPayload , payloadSize);
}


void update1()
{
  char *payload = "blank";
  int length = strlen(payload);
  client.publish("outTopic", (byte *)payload , length);
  Serial.println (payload);
}


void loop()
{
  update();       // Update after 15 seconds
  delay (1000);
   
  update1();       // Update after 15 seconds
  delay (1000);
   
  client.loop();
}

Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 49
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi PaulS,

I have solved that last problem, I remeber I had it before, if the following line was not at the end of the routine it would become corrupt,
but have no idea why, but putting it at the end solves the text error.

 client.publish("outTopic", payload, length);
See the code below as it has a note about this.

Thanks for your time PaulS, it was most helpful.

Regards
Gary

Added the working code here, for anyone that may read this in the future.

Code:
//My addition to original code:

    #include <SPI.h>
    #include <Ethernet.h>
    #include <PubSubClient.h>
     
    byte copyOfPayload[80];
    int payloadSize;

    byte server[] = {192,168,1,64};                                                       
    byte ip[]     = {192,168,1,1};                                                         
    byte mac[]    = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEF };                                 

    // Callback function header
    void callback(char* topic, byte* payload, unsigned int length);

    EthernetClient ethClient;
    PubSubClient client(server, 1883, callback, ethClient);


    // Callback function = only received at random intervals could be seconds hours or days.
    void callback(char* topic, byte* payload, unsigned int length) {
    //

      for(byte i=0; i<length; i++)
        {
           copyOfPayload[i] = payload[i];
        }
     
       payloadSize = length;
           
       client.publish("outTopic", payload, length); // NOTE must be placed at end of routine or else it gets corrupted
    }


void setup()
{
     Serial.begin(9600);   
     Ethernet.begin(mac, ip);
     
  if (client.connect("arduinoClient"))
    {
      client.publish("outTopic","hello world");
      delay (1000);
      client.subscribe("inTopic");
    }
}


void update()
{
  client.publish("outTopic", copyOfPayload , payloadSize);
}


void update1()
{
  char *payload = "blank";
  int length = strlen(payload);
  client.publish("outTopic", (byte *)payload , length);
}


void loop()
{
  update();       // Update after 1 seconds
  delay (1000);
   
  update1();       // Update after 1 seconds
  delay (1000);
   
  client.loop();
}


Logged

Pages: 1 [2]   Go Up
Jump to: