Код: Выделить всё
#include
#include
#include
using namespace std;
#define HOST_NAME "WIZnet"
uint8_t macPLC[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
uint8_t ipPLC [] = {192, 168, 0, 41};
char broker[] = "192.168.0.42";
EthernetClient ethernet;
MQTTClient mqtt;
int n = 0;
void messageReceived(MQTTClient *client, char topic[], char bytes[], int length) {
if (length == 8) {
int32_t commandID = *reinterpret_cast(&bytes[0]);
float commandArg = *reinterpret_cast(&bytes[4]);
Serial.println(commandID);
Serial.println(commandArg);
n += commandID; // problem line
}
else
Serial.println("wrong length!!!");
}
void setup() { // the setup routine runs once:
Serial.begin(115200); // USB serial for logging
while (!P1.init())
delay(1); //Wait for Modules to Sign on
Ethernet.begin(macPLC, ipPLC);
mqtt.begin(broker, ethernet);
while (!mqtt.connect(broker)) {
Serial.print(".");
delay(1000);
}
Serial.println("\nConnected to MQTT broker.");
mqtt.subscribe("plc", 2);
mqtt.onMessageAdvanced(messageReceived);
delay(1000);
}
void loop() {
mqtt.loop();
Serial.println(n++);
delay(2000);
}
Подробнее здесь: https://stackoverflow.com/questions/786 ... the-system