knolleary / pubsubclient

A client library for the Arduino Ethernet Shield that provides support for MQTT.
http://pubsubclient.knolleary.net/
MIT License
3.83k stars 1.47k forks source link

MQTT using nodemcu v1.0, node-red,mosquitto broker #287

Open Nikitha-Kondapalli opened 7 years ago

Nikitha-Kondapalli commented 7 years ago

I am using a setup consisting of node mcu v1.0 esp12E and led connected to D1 pin. I am trying to control switching of LED from node-red using mqtt communication protocol using mosquitto broker. Whatever change (that is on and off) that is reflected in mosquitto terminal. The problem is that change is not reflected in my led. It is reflected around 5 to 6 times and then change doesnot reflect in the led.

I wanted to know whether this problem because of the parameter length? (which is in void callback function)

My code is as follows:

include

include

// Update these with values suitable for your network.

const char ssid = "OnePlus"; const char password = "anuj4525"; const char* mqtt_server = "192.168.43.238";

WiFiClient espClient; PubSubClient client(espClient); long lastMsg = 0; char msg[50]; //int value = 0; //int x; //String topic1; void setup() { pinMode(D1, OUTPUT); // Initialize the BUILTIN_LED pin as an output Serial.begin(9600); setup_wifi(); client.setServer(mqtt_server, 1883); client.setCallback(callback); }

void setup_wifi() { delay(10); // We start by connecting to a WiFi network Serial.println(); Serial.print("Connecting to "); Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); }

Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); }

void callback(char topic, byte payload, unsigned int length) { /Serial.print("Message arrived ["); Serial.print(topic); Serial.print("] ");/ /*for (int i = 0; i < length; i++) { Serial.print((char)payload[i]);

Serial.println();

// Switch on the LED if an 1 was received as first character }*/ if ((char)payload[0] == 't') { digitalWrite(D1, HIGH); Serial.println("on");
// Turn the LED on (Note that LOW is the voltage level // but actually the LED is on; this is because // it is acive low on the ESP-01) } else if((char)payload[0] == 'f') { digitalWrite(D1, LOW); Serial.println("off"); // Turn the LED off by making the voltage HIGH }

}

void reconnect() { // Loop until we're reconnected while (!client.connected()) { Serial.print("Attempting MQTT connection..."); // Attempt to connect if (client.connect("ESP8266Client")) { Serial.println("connected"); // Once connected, publish an announcement... //client.publish("nikitha_out", "nikitha_kondapalli"); // ... and resubscribe //client.subscribe("check"); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); // Wait 5 seconds before retrying delay(5000); } } }

void loop() {
if (!client.connected()) { reconnect(); } client.loop(); //client.publish("led"); client.subscribe("check");

} screenshot from 2017-05-29 15-42-57

lkaino commented 7 years ago

Sorry, I don't have the solution, but I'm experiencing similar issues on my nodemcu 1.0. I've subscribed to one topic and publishing to another at few seconds period.

The callback receives 4 to 5 messages and then stops receiving anything. Publishing continues to work.