Мне нужно реализовать сценарий прототипирования, который мигает светодиодом в arduino с протоколом MQTT. Я уже пробовал несколько библиотек MQTT, но они не работают отлично. Подключение к брокеру MQTT успешно работает, но когда я публикую сообщение с темой, которую я установил в arduino, не мигайте светодиодом. Arduno должен опубликовать сообщение, когда он успешно подключиться, но эта публикация часть также не работаетBlink Led с использованием MQTT в arduino
это мой код
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
// Set the MAC address
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 100);
IPAddress server(192, 168, 1, 20);
// Set what PINs our Led's are connected to
int redPin = 13;
//int greenPin = 6;
//int bluePin = 7;
// Set a generic code that will trigger our Blue Led
// think of this as a set of codes for automation you might write
byte triggerRed[13] = "12345";
// handles messages that are returned from the broker on our subscribed channel
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("New message from broker on topic:");
Serial.println(topic);
Serial.print("Payload:");
Serial.write(payload, length);
// Check and see if our payload matches our simple trigger test
if ((length == 5) & (memcmp(payload, triggerRed, 5) == 0))
{
//blink(redPin);
}
}
EthernetClient ethClient;
PubSubClient client(ethClient);
// Fire up our PubSub client
//PubSubClient client(server, 1883, callback);
void setup()
{
// Open serial communications
Serial.begin(9600);
client.setServer(server, 1883);
client.setCallback(callback);
// Setup our Leds
pinMode(redPin, OUTPUT);
// pinMode(greenPin, OUTPUT);
// pinMode(bluePin, OUTPUT);
// attempt a DHCP connection
Serial.println("Attempting to get an IP address using DHCP:");
if (!Ethernet.begin(mac))
{
// if DHCP fails, start with a hard-coded address:
Serial.println("failed to get an IP address using DHCP, trying manually");
Ethernet.begin(mac, ip);
}
Serial.print("My address:");
Serial.println(Ethernet.localIP());
// Connect to Broker, give it arduino as the name
if (client.connect("arduino")) {
// Good, we connected turn on the red led
digitalWrite(redPin, HIGH);
// Publish a message to the status topic
client.publish("status","Arduino is now online");
// Listen for messages on the control topic
client.subscribe("ultra");
}
}
void loop()
{
client.loop();
}
// Anything with flashing lights.
void blink(int targetLed)
{
digitalWrite(redPin, HIGH);
}
, как я могу это исправить?
Вы пытаетесь запустить пример эскиз из библиотеки Arduino MQTT? Я рекомендую вам поместить процедуру подключения клиента в цикл. Почему бы не попытаться подключиться к общедоступному серверу в Интернете, например http://test.mosquitto.org/, перед тем как подключиться к локальной? –
Я уже тестировал test.mosquitto.org, но это не сработало. Что вы подразумеваете под заказ клиентского соединения в цикле? Когда я публикую сообщение с правильной темой, светодиод мигает. но мне нужно, чтобы он всегда был «включен» –