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.
//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();
}