Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Copilot Lvl 2
Message 1 of 2

Controlling LEDS (using Arduino and PubNub)

I'm getting an error on line (c=="ON"). Does anyone know why? All help is appericated.

 

#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();
}

1 Reply
Commander Lvl 2
Message 2 of 2

Re: Controlling LEDS (using Arduino and PubNub)

Language is C. Strings are double-quoted, not single-quoted.

Needs to be:

if (c == "ON")

Likewise for the line which currently reads:

if (c == 'OFF')

Please follow-up to let us know how you made out. For good karma, mark a reply as the answer if it helped!