This is part a code that receives data and the length of the data..
How do I convert the payload which is an array held in memory ( I think ?), into a string so that I can compare if it is equal to test ?
My attempt is obviously wrong and I have tried to find the answer but couldn't
Thanks
Regards Gary
void callback(char* topic, byte* payload, unsigned int length)
{
if (payload == "test")
{
Serial.println(payload);
}
}
/*
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 server[] = {192,168,0,2}; // If its a numerical address use this and comment out above line
byte ip[] = {192,168,0,3}; // Update this with value suitable for this network.
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
String store;
// 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);
Serial.begin(9600);
Serial.println("Arduino got payload ...");
for (int i = 0; i < length; i++)
{
Serial.println(payload[i]);
}
if(strcmp((char *)payload, "t") == 0)
{
Serial.println("Arduino got match ...");
}
}
// ____________________________________________________________________________________________
void setup()
{
Ethernet.begin(mac, ip);
if (client.connect("arduinoClient")) {
client.publish("outTopic","hello world");
client.subscribe("inTopic");
}
}
// ____________________________________________________________________________________________
void loop()
{
client.loop();
}
Regards Gary
There are many sites with C/C++ instruction, how-to's and examples on the net but beware that Arduino environment is very small and not suited to some approaches, especially those involving creating and deleting code objects (sometimes you don't know that's what's happening, C++ String objects for example automatically do it with -every- change in string length) and re-entrant code is ram-heavy as well.