Comparing const char* from thinger.io in[]

So I am assuming what I am trying to accomplish is actually rather simple but for some reason I have been unable to find a solution. I have searched around on the forums and google and haven’t really been able to identify my problem.

Basically, I want to pass in the string “!on” to the esp8266 using the thinger.io api and have it turn on a pin. I have had lots of luck getting it to print to the serial monitor, but no luck with the comparison. Why do I need to use const char* anyway? (I grabbed this from another guys code).

Anyway, let me know how ridiculous I am for having this problem. Sorry I’m a big fat noob.

#include <SPI.h>
#include <ESP8266WiFi.h>
#include <ThingerWifi.h>

#define USERNAME “xxx”
#define DEVICE_ID “xxx”
#define DEVICE_CREDENTIAL “xxxxxx”

#define SSID “xxxxx”
#define SSID_PASSWORD “xxxxxx”

ThingerWifi thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);

const char* holder =0;
const int pin = 7;

void setup() {

Serial.begin(19200);
thing.add_wifi(SSID, SSID_PASSWORD);

// input resource that prints follower info
thing[“onoff”] << (pson& in){

Serial.println((const char*)in[“Username”]);
Serial.println((const char*)in[“Channelid”]);
//Serial.println((const char*)in[“Message”]);
holder = (const char*)in[“Message”];
Serial.println(holder);
if (holder == “!on”){
Serial.println(“yes”);
digitalWrite(pin, HIGH);
}
if (holder == “!off”){
Serial.println(“no”);
digitalWrite(pin, LOW);
}

};
}

void loop() {
thing.handle();

}

if (holder == "!on")

holder is a pointer to a char, that means that variable holds the memory address of the char string you want. "!on" is a constant string, also a pointer to a char although that's not as obvious. Since the two things are not in the same place in memory, they'll never show as being equal with ==. To compare to strings you use strcmp.

const char* holder =0;

Also you need to remove the "const" here. If you're going to try to put a different value than 0 in there at some later time then it can't very well be const. By putting const you are promising the compiler that the value of that variable will never change. The compiler is going to complain loudly that you broke your promise when it gets to this line:

holder = (const char*)in["Message"];

So I tried this… probably something not right…

#include <SPI.h>
#include <ESP8266WiFi.h>
#include <ThingerWifi.h>

#define USERNAME “xxxx”
#define DEVICE_ID “xxxxx”
#define DEVICE_CREDENTIAL “xxxxxx”

#define SSID “hello”
#define SSID_PASSWORD “xxxxxx”

ThingerWifi thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);
const char* holder;
const int pin = 7;

void setup() {

Serial.begin(19200);
thing.add_wifi(SSID, SSID_PASSWORD);

// input resource that prints follower info
thing[“onoff”] << (pson& in){

Serial.println((const char*)in[“Username”]);
Serial.println((const char*)in[“Channelid”]);
//Serial.println((const char*)in[“Message”]);
holder = in[“Message”];
Serial.println(holder);
if (strcmp(holder, “!on”) == 1){
Serial.println(“yes”);
digitalWrite(pin, HIGH);
}
if (strcmp(holder, “!off”)==1){
Serial.println(“no”);
digitalWrite(pin, LOW);
}

};
}

void loop() {
thing.handle();

}

The const char* in front of the holder is not preventing it from being a new value and it successfully prints the new value every time it is sent. (and compiles obviously).

In fact if I try to delete it:

In file included from C:\Users\Aufek\Documents\Arduino\libraries\thinger.io\src/thinger/thinger.h:27:0,

from C:\Users\Aufek\Documents\Arduino\libraries\thinger.io\src/ThingerClient.h:27,

from C:\Users\Aufek\Documents\Arduino\libraries\thinger.io\src/ThingerWifi.h:27,

from C:\Users\Aufek\Documents\Arduino\Outlet\Outlet.ino:3:

C:\Users\Aufek\Documents\Arduino\libraries\thinger.io\src/thinger/pson.h: In instantiation of ‘T protoson::pson::get_value() [with T = char*]’:

C:\Users\Aufek\Documents\Arduino\libraries\thinger.io\src/thinger/pson.h:454:33: required from ‘protoson::pson::operator T() [with T = char*]’

C:\Users\Aufek\Documents\Arduino\Outlet\Outlet.ino:29:12: required from here

C:\Users\Aufek\Documents\Arduino\libraries\thinger.io\src/thinger/pson.h:465:28: error: invalid conversion from ‘int’ to ‘char*’ [-fpermissive]

return 1;

^

C:\Users\Aufek\Documents\Arduino\libraries\thinger.io\src/thinger/pson.h:467:37: error: cannot convert ‘float’ to ‘char*’ in return

return (float)value_;

^

C:\Users\Aufek\Documents\Arduino\libraries\thinger.io\src/thinger/pson.h:469:38: error: cannot convert ‘double’ to ‘char*’ in return

return (double)value_;

^

C:\Users\Aufek\Documents\Arduino\libraries\thinger.io\src/thinger/pson.h:471:45: error: invalid conversion from ‘uint64_t {aka long long unsigned int}’ to ‘char*’ [-fpermissive]

return pb_decode_varint();

^

C:\Users\Aufek\Documents\Arduino\libraries\thinger.io\src/thinger/pson.h:473:46: error: invalid conversion from ‘uint64_t {aka long long unsigned int}’ to ‘char*’ [-fpermissive]

return -pb_decode_varint();

^

exit status 1
Error compiling for board NodeMCU 1.0 (ESP-12E Module).

But using what you mentioned I was able to search a bit more and I came up with this which works:

#include <SPI.h>
#include <ESP8266WiFi.h>
#include <ThingerWifi.h>

#define USERNAME “xxxxx”
#define DEVICE_ID “xxxxxx”
#define DEVICE_CREDENTIAL “xxxxxxx”

#define SSID “xxxxxx”
#define SSID_PASSWORD “xxxxxxxx”

ThingerWifi thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);
const char* holder;
const int pin = 7;

void setup() {

Serial.begin(19200);
thing.add_wifi(SSID, SSID_PASSWORD);

// input resource that prints follower info
thing[“onoff”] << (pson& in){

Serial.println((const char*)in[“Username”]);
Serial.println((const char*)in[“Channelid”]);
//Serial.println((const char*)in[“Message”]);
holder = in[“Message”];
Serial.println(holder);
if (std::string(holder) == “!on”){
Serial.println(“yes”);
digitalWrite(pin, HIGH);
}
if (std::string(holder) == “!off”){
Serial.println(“no”);
digitalWrite(pin, LOW);
}

};
}

void loop() {
thing.handle();

}

Thanks again for your help.

The placement of const is weird. It actually makes the thing to the left of it constant, not the thing to the right. The exception is when there is nothing to the left of the keyword. Then, it affects the thing to the right. Really, how difficult would it have been to make the compiler generate an error about the misplaced const, instead of allowing the bizarre behavior?

Anyway, you can have a constant pointer. But, that can mean two different things. One thing it could mean is that you can't change where the pointer points to. The other thing that it can mean is that you change change the data that the pointer points to.

const char *ptr is a pointer that points to memory that you can't change (using the pointer).

char * const ptr is a pointer that you can't make point somewhere else. Of course, to use this pointer, you need to define where it points as part of the declaration/initialization.

Of course, you can have a const char * const ptr, too, which is a pointer that you can't make point to somewhere else and that doesn't allow you to change the pointed to data.

Thanks paul. I am going to have to go deep into pointers to understand this better.