char * to string

Hello,

I'm trying to process a feed into an 8266 from a mqtt server as follows:

  Adafruit_MQTT_Subscribe *subscription;
  while ((subscription = mqtt.readSubscription(5000))) {
    if (subscription == &maindoorlightsbutton) {
      Serial.print(F("Light Button was pressed: "));
      Serial.println((char *)maindoorlightsbutton.lastread);
    }

if (((char *)maindoorlightsbutton.lastread)=="on")
      {
          digitalWrite(12, LOW);
      }

if (((char *)maindoorlightsbutton.lastread)=="off")
      {
          digitalWrite(12, HIGH);
       }  
   }

However, non of the IF...conditions are True although console displays either on or off.

So, I appreciate a solution to change char * to something I can work with.

Thank you

You can have a look at strcmp or the String class

Works fine. Thank you.

For anybody interested, I’ve included the revised code as follows:

char buffer[80] = “on”;

Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000))) {
if (subscription == &maindoorlightsbutton) {
Serial.print(F("Light Button was pressed: "));
Serial.println((char *)maindoorlightsbutton.lastread);
}

if (strcmp (((char *)maindoorlightsbutton.lastread),buffer) != 0) //comparebuffer value with feed data
{
Serial.println(“White light mode”);
analogWrite(REDPIN, 255);
analogWrite(BLUEPIN, 255);
analogWrite(BLUEPIN, 240);
}

else
{
Serial.println(“navajoWhite light mode”);
analogWrite(REDPIN, 255);
analogWrite(BLUEPIN, 222);
analogWrite(BLUEPIN, 173);
}

}

Hello,

Following my thread (https://forum.arduino.cc/index.php?topic=479563.0), I would like to extend my question on how to get exact value from mqtt feed named “maindoorlightsbutton” (forgive the name please) to further process it with my RGB led strip.

Appreciate any input.

char buffer[80] = "on";

 Adafruit_MQTT_Subscribe *subscription;
 while ((subscription = mqtt.readSubscription(5000))) {
   if (subscription == &maindoorlightsbutton) {
     Serial.print(F("Light Button was pressed: "));
     Serial.println((char *)maindoorlightsbutton.lastread);
   }

if (strcmp (((char *)maindoorlightsbutton.lastread),buffer) != 0) //comparebuffer value with feed data
     {
   Serial.println("White light mode");
        analogWrite(REDPIN, 255);
        analogWrite(BLUEPIN, 255); 
        analogWrite(BLUEPIN, 240);
     }

else
     {
   Serial.println("navajoWhite light mode");
        analogWrite(REDPIN, 255);
        analogWrite(BLUEPIN, 222); 
        analogWrite(BLUEPIN, 173);
     }

  }

I would like to extend my question on how to get exact value from mqtt feed named "maindoorlightsbutton"

As opposed to some approximate value?

YOU see your serial data. We do not. You failed to share it. You failed to properly explain what you need help with. You failed to provide links to the library you are using, or any context.

You can not really expect help.

PaulS, Thank you for your input.

As the topic implies, I need to convert data from CHAR * to a String and I’m expecting (RGB Integer color code) numbers at ((char *)maindoorlightsbutton.lastread) and need to convert them into a String or Integer.

The example code clearly implies that data is received from an MQTT server.

I need to convert data from CHAR * to a String

You most certainly do NOT.

The example code clearly implies that data is received from an MQTT server.

So? You seem to expect everyone to know exactly what that means, without sharing. I'm done trying to help you.

I found this from another site and didn't test it yet. I'll post as soon as I get posivite response.

String value = String((char*)maindoorlightsbutton.lastread);
SoffitR = value.substring(0,value.indexOf(',')).toInt();
SoffitG = value.substring(value.indexOf(',')+1,value.lastIndexOf(',')).toInt();
SoffitB = value.substring(value.lastIndexOf(',')+1).toInt();

PaulS,
"So? You seem to expect everyone to know exactly what that means, without sharing. I'm done trying to help you."

Frankly, I didn't expect any help from you from your first reply. Not with this attitude anyway.

Apologies to all other viewers.

Fact is, you never specified what the MQTT data is. That is why you are being told you can't be helped.

Following the matter; here is the final code which processes a feed from an MQTT server and lits a strip of RGB Leds.

/***************************************************
code for esp8266-12f kitchen partition lightstrip node
reads whitegate value and change color of leds
 ****************************************************/
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#define REDPIN 4
#define GREENPIN 14
#define BLUEPIN 12
#define FADESPEED 5     // make this higher to slow down
#define WLAN_SSID       "****"
#define WLAN_PASS       "********"
#define AIO_SERVER      "192.168.1.11"
#define AIO_SERVERPORT  1883                   // use 8883 for SSL
#define AIO_USERNAME    ""
#define AIO_KEY         ""
int intensityR;
int intensityG;
int intensityB;

WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
Adafruit_MQTT_Subscribe maindoorcontact = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "maindoorcontact");

void MQTT_connect();

void setup() {
  Serial.begin(9600);
  pinMode(REDPIN, OUTPUT);
  pinMode(GREENPIN, OUTPUT);
  pinMode(BLUEPIN, OUTPUT);
  analogWrite(REDPIN, 0);
  analogWrite(GREENPIN, 0); 
  analogWrite(BLUEPIN, 0);
  Serial.begin(9600);
  Serial.print("Connecting to ");
  Serial.println(WLAN_SSID);
  WiFi.begin(WLAN_SSID, WLAN_PASS);
  while (WiFi.status() != WL_CONNECTED) 
    {
        delay(500);
        Serial.print(".");
    }
  Serial.println("WiFi connected");
  Serial.println("IP address: "); Serial.println(WiFi.localIP());
  mqtt.subscribe(&maindoorcontact);
}

uint32_t x=0;

void loop() {
  MQTT_connect();
  Adafruit_MQTT_Subscribe *subscription;
    while ((subscription = mqtt.readSubscription(5000))) 
    {
        if (subscription == &maindoorcontact) 
          {
                  Serial.print(F("Light Button was pressed: "));
                  Serial.println((char *)maindoorcontact.lastread);
                  String value = String((char*)maindoorcontact.lastread);
                  intensityR = value.substring(0,value.indexOf(',')).toInt();
                  intensityG = value.substring(value.indexOf(',')+1,value.lastIndexOf(',')).toInt();
                  intensityB = value.substring(value.lastIndexOf(',')+1).toInt();
                  Serial.println(intensityR);
                  Serial.println(intensityG);
                  Serial.println(intensityB);
          }
        if ((-1<intensityR<513) or (-1<intensityG<513) or (-1<intensityB<513)) //make sure its between 0 & 512
          {
                  analogWrite(REDPIN, intensityR);
                  analogWrite(GREENPIN, intensityG); 
                  analogWrite(BLUEPIN, intensityB);
          }
   
        else
          {
                  analogWrite(REDPIN, 255);
                  analogWrite(GREENPIN, 255); 
                  analogWrite(BLUEPIN, 255);
          }
      }
 
  if(! mqtt.ping()) //keep connection alive
    {
        mqtt.disconnect();
    }
  }
void MQTT_connect()
  {
    int8_t ret;
    if (mqtt.connected()) {
    return;
  }
  Serial.print("Connecting to MQTT... ");
  uint8_t retries = 3;
  while ((ret = mqtt.connect()) != 0) 
    { 
       Serial.println(mqtt.connectErrorString(ret));
       Serial.println("Retrying MQTT connection in 5 seconds...");
       mqtt.disconnect();
       delay(5000);  // wait 5 seconds
       retries--;
       if (retries == 0) 
          {
            // basically die and wait for WDT to reset me
            while (1);
          }
    }
       Serial.println("MQTT Connected!");
}