MQTT, ESP8266, Analogwrite, Nodered

Im a bit stuck with my project and hope you guys can point me in the right direction.

Im using node red on a rpi to communicate with an ESP8266. I have a dashboard in node red with a slider. The slider (is supposed to) control an LED via PWM signal.

So far I have managed to come up (more or less copy paste -.-) with a code that allows me to use a switch to turn on/off the LED.

 void callback(String topic, byte* message, unsigned int length) {
  Serial.print("Message arrived on topic: ");
  Serial.print(". Message: ");
  String messageTemp;
  for (int i = 0; i < length; i++) {
    messageTemp += (char)message[i];
      Serial.print("Changing Room lamp to ");
      if(messageTemp == "on"){
        digitalWrite(lamp, HIGH);
      else if(messageTemp == "off"){
        digitalWrite(lamp, LOW);

this works..

What I want to do now is use the analogwrite function. So I recieve a MQTT mesage with a value between 0-255. This part already works.

Where I am stuck is, I dont know how to store the mqtt message I recieve inside an int or something so that I can do something like analogwrite(lamp, value_from_mqtt)...

I don't see a reason for the messageTemp variable. It just wastes memory. If you use the strncmp() function you can check the content of the message variable directly.

To parse a number string you can use the atoi() function.

As you have the message in the messageTemp string you could use the toInt() method to convert it to an integer.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.