Issues with changing variable and char.toInt

Another quick question. How can I change this variable

void ledControl(char* topic, byte* payload, unsigned int length) {

int modeBrightness = 255;

if (strcmp(topic, "/hangingLamp/stream") == 0){
    (char)payload[0].toInt = modeBrightness;  // How can I make this assignment?
    Serial.println(modeBrightness);                   //toInt isn't working
  }
}

Yes handling various data types are usually a pain in the butt for me...

I'm trying to take data from the topic I'm calling
"/hangingLamp/stream"

this topic's payload will contain numbers from 0-255

this data will transmit over mqtt

I want use the int's to dim and brighten PWM pins.

yes, this doesn't compile...

OK, I get it. Here's me doing my best to provide all the details

raw or ascii?

I have no idea, I would assume ascii. The message is coming from Nodered through a mqtt broker.

the values will always be between 0-255.

Here is the entire function; messy as it is. the first 15 lines are most relevant.

void ledControl(char* topic, byte* payload, unsigned int length) {
int modeBrightness = 255;

  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println(" ");
  Serial.println(topic);

if (strcmp(topic, "/hangingLamp/stream") == 0){
    // strcpy(payload, itoa(modeBrightness));  // this does not compile
    Serial.println(modeBrightness);
    led.fade(modeBrightness, FADE_TIME);
  }

// below is what I was previously doing everything compiles and works
  if ((char)payload[0] == '1') {
    led.fade(modeBrightness, FADE_UP_TIME);
    // digitalWrite(12, HIGH);
    // Serial.println("LAMP 1 ON");
    client.publish("/hangingLamp/lamp", "on");
  }
  if ((char)payload[0] == '2') {
    led2.fade(0, FADE_DOWN_TIME);
    digitalWrite(14, LOW);
    // Serial.println("LAMP 2 OFF");
    client.publish(" /hangingLamp/lamp2", "off");
  }
  if ((char)payload[0] == '3') {
    led2.fade(255, FADE_UP_TIME);
    digitalWrite(14, HIGH);
    // Serial.println("LAMP 2 ON");
    client.publish("/hangingLamp/lamp2", "on");
  }
  if ((char)payload[0] == '4') {
    digitalWrite(fan, LOW);
    Serial.println("FAN OFF");
    client.publish("/hangingLamp/fan", "off");
  }
  if ((char)payload[0] == '5') {
    digitalWrite(fan, HIGH);
    Serial.println("FAN ON");
    client.publish("/hangingLamp/fan", "on");
  }
  if ((char)payload[0] == '6') {
    digitalWrite(15, LOW);
    // Serial.println("Lamp 3 OFF");
    client.publish("/hangingLamp/lamp3", "off");
  }
  if ((char)payload[0] == '7') {
    digitalWrite(15, HIGH);
    // Serial.println("Lamp 3 On");
    client.publish("/hangingLamp/lamp3", "on");
  }
}

ok, I did some reading and I'm 99% sure it's asii encoding

ok here's the entire file. I'm using tabs and I don't know how to copy them all into this without causing issues.

The tabs of interest are likely ledControl.ino and reconnect.ino

hangingLamp_v3.zip (4.0 KB)

Thanks for working with me on this. I truly appreciate it!

I've been over at the Nodered forums on this issue and the advice is send JSON data. Now I need to learn how to parse it.

Thanks for all your help Delta_G!

what did you see being printed from that ?

Hey All,
I'm having trouble converting an ascii payload to an int. See the code for comments on what I've tried.

How can I use the payload as an int?
Thanks!

void ledControl(char* topic, byte* payload, unsigned int length) {
int modeBrightness = 255;

if (strcmp(topic, "/hangingLamp/stream") == 0){
    modeBrightness = (char)payload[0];  
    Serial.println(modeBrightness);  // this renders as expected
    // modeBrightness = atoi(modeBrightness);  // does not compile
    // modeBrightness = modeBrightness.toInt;  // does not compile
    led.fade(modeBrightness, FADE_TIME); // this is where I need to use the int
  }
 // modeBrightness = atoi(modeBrightness);  // does not compile

It does not compile because atoi() expects a zero terminated C style string as its parameter and you are passing it a single char

I don't understand what you're saying. How do I terminate with a 0? Please give an example.
thx

What exactly is payload?

ascii characters representing 0-255

Which platform?
You have a char cast, and on some platforms, char is signed.
Edit: wait, are you saying payload is some sort of string?
You know how long it is, so copy to a buffer, terminate it, and apply atoi.

Null terminated? If not, is it always three characters, or does the length vary with the size of the number? If always three ASCII characters, does it have leading zero's or spaces? Is length the number of bytes in payload?

I'm receiving data from NodeRed over mqtt. Nodered calls it a number.

yes the amount varies from 0-9 one place holder, 10-99 two place holders, 100-255 3three place holders.

But "length" tells you how many. . ?

Here is the flow/code from NodeRed.

[
    {
        "id": "f77c465a8c64faee",
        "type": "ui_slider",
        "z": "cf08cb9713ba9cda",
        "name": "",
        "label": "brightness",
        "tooltip": "",
        "group": "65963440961b63cc",
        "order": 6,
        "width": 0,
        "height": 0,
        "passthru": true,
        "outs": "end",
        "topic": "topic",
        "topicType": "msg",
        "min": 0,
        "max": "255",
        "step": 1,
        "className": "",
        "x": 230,
        "y": 80,
        "wires": [
            [
                "902ba5af9d02cea1"
            ]
        ]
    },
    {
        "id": "902ba5af9d02cea1",
        "type": "mqtt out",
        "z": "cf08cb9713ba9cda",
        "name": "",
        "topic": "/hangingLamp/stream",
        "qos": "",
        "retain": "",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "1a947cf4658331dc",
        "x": 520,
        "y": 80,
        "wires": []
    },
    {
        "id": "65963440961b63cc",
        "type": "ui_group",
        "name": "HANGING LAMP",
        "tab": "f3a8a67ff0904e39",
        "order": 3,
        "disp": true,
        "width": "6",
        "collapse": true,
        "className": ""
    },
    {
        "id": "1a947cf4658331dc",
        "type": "mqtt-broker",
        "name": "",
        "broker": "192.168.1.217",
        "port": "1883",
        "clientid": "",
        "autoConnect": true,
        "usetls": false,
        "protocolVersion": "4",
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "birthMsg": {},
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "closeMsg": {},
        "willTopic": "",
        "willQos": "0",
        "willPayload": "",
        "willMsg": {},
        "userProps": "",
        "sessionExpiry": ""
    },
    {
        "id": "f3a8a67ff0904e39",
        "type": "ui_tab",
        "name": "HOME",
        "icon": "dashboard",
        "order": 1,
        "disabled": false,
        "hidden": false
    }
]

Your two or more topics on the same or similar subject have been merged.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.