Hello everyone,
I'm trying to build a program which turns off and on an LED based on what the PubNub user sends. The PubNub message is denoted as "c" and is a pointer. I'm getting the following error message:
ISO C++ forbids comparison between pointer and integer [-fpermissive]
on lines:
if (c == "ON")
if(c == "OFF")
I've also tried using constant strings such as 'on' but that gave me this error:
character constant too long for its type
Please let me know if you have any ideas to fix this. All responses are greatly appreciated. Thank you.
subscribemessageled.ino (1.48 KB)
You say:
if (c == "ON")
if(c == "OFF")
You have:
if (c == ‘ON’) “ is not the same as ‘
if(c == ‘OFF’)
char c = pclient->read();
But c is only a single character anyway so will never equal "ON", "OFF", 'ON' or 'OFF'
The code.
In code tags.
#include <SPI.h>
#include <Ethernet.h>
#include<PubNub.h>
byte mac [] = {0XDE,0XAD,0XBE,0XEF,0XFE,0XED};
char pubkey[] = "demo";
char subkey[] = "demo";
char channel[] = "iotchannel";
int ledPin = 9;
void setup() {
Serial.begin(9600);
Serial.println("Serial set up");
while(!Ethernet.begin(mac))
{
Serial.println("Ethernet setup error");
delay(1000);
}
PubNub.begin(pubkey,subkey);
Serial.println("PubNub set up");
pinMode(ledPin, OUTPUT);
}
void loop() {
Ethernet.maintain();
EthernetClient*client;
Serial.println("publishing message");
client = PubNub.publish(channel, "\"enter ON to turn on LED or OFF to turn off LED\"");
if(!client){
Serial.println("PubNub Error");
delay(1000);
return;
}
while(client->connected()&&client->available())
{
char c = client->read();
Serial.print(c);
}
/*stop disconnects it from the server*/
client->stop();
Serial.println();
Serial.println("Waiting for subscribe message");
//subscribe function
PubSubClient *pclient = PubNub.subscribe(channel);
if(!pclient){
Serial.println("Sub fail");
delay(1000);
return;
}
while(pclient->wait_for_data())
{
char c = pclient->read();
//turn on led if the subscribe message is ON
if (c == 'ON')
{
digitalWrite(ledPin, HIGH);
}
//turn off led if the subscribe message is OFF
if (c == 'OFF')
{
digitalWrite (ledPin, LOW);
}
//Serial.print(c);
}
pclient->stop();
Serial.println();
}
UKHeliBob:
char c = pclient->read();
But c is only a single character anyway so will never equal "ON", "OFF", 'ON' or 'OFF'
Thanks. I fixed it by changing c into a string
I fixed it by changing c into a string
Perhaps you could post your revised code for the benefit of anyone reading this thread in the future