MQTT payload to INT

I am having an issue settings a variable to an int.

The goal is to change the pwm write value when I send the message over MQTT (0-1023)

When i send 0-1023 over mqtt to the board I get really long numbers IE. when i send 1023 i get: 49485051

when i send 0 i get: 48

any help is appreciated!

Problem Code

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]);

    String pwmValue_str = "";
    pwmValue_str = payload[i];
    

    analogWrite(14, pwmValue_str.toInt());
    
  }
  Serial.println();

}

Whole Sketch

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

// Update these with values suitable for your network.

const char* ssid = "SSID";
const char* password = "PW";
const char* mqtt_server = "192.168.1.88";

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

void setup() {
  pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
  Serial.begin(115200);
  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]);

    String pwmValue_str = "";
    pwmValue_str = payload[i];
    

    analogWrite(14, pwmValue_str.toInt());
    
  }
  Serial.println();

  // Switch on the LED if an 1 was received as first character
  if ((char)payload[0] == '1') {
    digitalWrite(BUILTIN_LED, LOW);   // 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 {
    digitalWrite(BUILTIN_LED, HIGH);  // 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_020618-03")) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      //client.publish("outTopic", "hello world");
      // ... and resubscribe
      client.subscribe("inTopic");
    } 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();

  /*long now = millis();
  if (now - lastMsg > 2000) {
    lastMsg = now;
    ++value;
    snprintf (msg, 75, "hello world #%ld", value);
    Serial.print("Publish message: ");
    Serial.println(msg);
    client.publish("outTopic", msg);
  }
  */
}

ELECTRONIC_VALVE_CONTROLLER_MQTT.ino (2.23 KB)

sramirez6208:
when i send 0 i get: 48

I wonder what the integer value of an ASCII zero is.

    String pwmValue_str = "";
    pwmValue_str = payload[i];

Why are you converting the payload to an int, one character at a time?

What value do you suppose b will have when the ith element of payload is 7?

   byte b = payload[i] - '0';

If you don't KNOW, try this code out, and learn.

It REALLY seems to me, though, that you want to make a string (NOT a f**king String) out of payload, and convert that string to an int.

   payload[length] = '\0'; // Make payload a string by NULL terminating it.
   int pwmVal = atoi((char *)payload);

What device are you using that takes PWM values in the range 0 to 1023?

That's it!!!!!!

Please excuse my ignorance when it comes to programming I am just getting into it.

This was a huge help, I really appreciate it.

sramirez6208:
I am having an issue settings a variable to an int.

The goal is to change the pwm write value when I send the message over MQTT (0-1023)

When i send 0-1023 over mqtt to the board I get really long numbers IE. when i send 1023 i get: 49485051

when i send 0 i get: 48

any help is appreciated!

Problem Code

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]);

String pwmValue_str = "";
    pwmValue_str = payload[i];

analogWrite(14, pwmValue_str.toInt());
   
  }
  Serial.println();

}









Whole Sketch


#include <ESP8266WiFi.h>
#include <PubSubClient.h>

// Update these with values suitable for your network.

const char* ssid = "SSID";
const char* password = "PW";
const char* mqtt_server = "192.168.1.88";

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

void setup() {
  pinMode(BUILTIN_LED, OUTPUT);    // Initialize the BUILTIN_LED pin as an output
  Serial.begin(115200);
  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]);

String pwmValue_str = "";
    pwmValue_str = payload[i];

analogWrite(14, pwmValue_str.toInt());
   
  }
  Serial.println();

// Switch on the LED if an 1 was received as first character
  if ((char)payload[0] == '1') {
    digitalWrite(BUILTIN_LED, LOW);  // 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 {
    digitalWrite(BUILTIN_LED, HIGH);  // 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_020618-03")) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      //client.publish("outTopic", "hello world");
      // ... and resubscribe
      client.subscribe("inTopic");
    } 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();

/*long now = millis();
  if (now - lastMsg > 2000) {
    lastMsg = now;
    ++value;
    snprintf (msg, 75, "hello world #%ld", value);
    Serial.print("Publish message: ");
    Serial.println(msg);
    client.publish("outTopic", msg);
  }
  */
}