Hi,
Thanks for your patience, I have thought about how best to demonstrate what I am trying to achieve
and its actually best to show you the original file I downloaded fron the net.
It requires a lib and I have attached this. Also in the second piece of code shows you what I have added.
The purpose of my changes are to update a MQTT server with latest data every 15 seconds
As the original would only update on a received message. which may be seconds or hours away.
Obviously there is a definite fault and its different to the title of this thread.
But it is caused by what I am trying to achieve, it uses pointers and I have
never worked with them. And i do not know how to declare them satifactorly for this to compile.
The errors given are as follows:
Test_MQTT_V1_0.cpp: In function 'void update()':
Test_MQTT_V1_0:35: error: 'payload' was not declared in this scope
Test_MQTT_V1_0:35: error: 'length' was not declared in this scope
Original code:
/*
Publishing in the callback
- connects to an MQTT server
- subscribes to the topic "inTopic"
- when a message is received, republishes it to "outTopic"
This example shows how to publish messages within the
callback function. The callback function header needs to
be declared before the PubSubClient constructor and the
actual callback defined afterwards.
This ensures the client reference in the callback function
is valid.
*/
#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
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 loop()
{
client.loop();
}
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, 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()
{
client.publish("outTopic", payload, length);
}
void loop()
{
client.loop();
delay (15000);
update(); // Update after 15 seconds
}