1
votes

Situation: I have a webpage that has two buttons on it. One sends to the PubNub channel a JSON string of {"lightRight":"1"} and one sends {"lightRight":"0"}, regardless of what I do in the Arduino Sketch I cannot make it into the "on" portion of the void light function.

I know from the serial output of the arduino that I am receiving communication from the web page. If I flip my boolean values to off then I am able to get the "LED HIGH" output but then no longer can I get the "led low".

The code I am trying to adapt can be found here.

https://gist.github.com/ianjennings/ada8fb1a91a486a0c73e

It is very possible I have hacked out to much code from the examples that is why I am including it, but running the stock code I cannot seem to get this to work.

I am not a developer, so I am very sorry if I do not call things as their proper terms. I will learn from correction.

#include <PubNub.h>
#include <SPI.h>
#include <EthernetV2_0.h>
#include <string.h>
#include <Servo.h>

byte mac[] = { MAC was here };
byte gateway[] = { Gate was here };
byte subnet[] = { Sub was here };

IPAddress ip(IP was here);

char pubkey[] = "Key was here";
char subkey[] = "Key was here";
char channel[] = "Channel was here";

int lightRight = 5;
int lightRoom = 6;
int lightGarage = 7;

int i;

EthernetClient *client;

#define W5200_CS  3
#define SDCARD_CS 4

void setup()
{

  pinMode(SDCARD_CS,OUTPUT);
  digitalWrite(SDCARD_CS,HIGH);

  Serial.begin(9600);
  Serial.println("Serial set up");

  while (!Ethernet.begin(mac)) {
    Serial.println("Ethernet setup error");
                blink(1000, 999999);
    delay(1000);
  }
  Serial.println("Ethernet set up");

  PubNub.begin(pubkey, subkey);
  Serial.println("PubNub set up");

  pinMode(lightRight, OUTPUT);
  pinMode(lightRoom, OUTPUT);
  pinMode(lightGarage, OUTPUT);

//  blink(100, 5);
  reset();

}

//void flash(int ledPin)
//{
//  for (int i = 0; i < 3; i++) {
//      Serial.println("flash");
//    digitalWrite(ledPin, HIGH);
//    delay(100);
//    digitalWrite(ledPin, LOW);
//    delay(100);
//  }
//}

void loop()
{

  Ethernet.maintain();

    PubSubClient *client;

  Serial.println("waiting for a message (subscribe)");
  client = PubNub.subscribe(channel);

  if (!client) {
    Serial.println("subscription error");
    return;
  }

  String messages[10];

  boolean inside_command = false; 
  int num_commands = 0;

  String message = "";
  char c;

  while (client->wait_for_data()) {

    c = client->read();

    if(inside_command && c != '"') {
      messages[num_commands] += c;
    }

    if(c == '"') {

      if(inside_command) {         

        num_commands = num_commands + 1;
        inside_command = false;

      } else {     
        inside_command = true;
      }

    }

    message += c;

  }
  client->stop();

  for (i = 0; i < num_commands; i++){

    int colonIndex = messages[i].indexOf(':');

    String subject = messages[i].substring(0, colonIndex);
    String valueString = messages[i].substring(colonIndex + 1, messages[i].length());
    boolean value = false;

    if(valueString == "1") {
      value = true;
    }

    if(subject == "lightRight") {
      light(lightRight, value);
    }

    if(subject == "lightRoom") {
      light(lightRoom, value);
    }

    if(subject == "lightGarage") {
      light(lightGarage, value);
    }

    if(subject == "blink") {
      blink(100, valueString.toInt());
    }

    Serial.println(subject);
    Serial.println(value);

  }

  Serial.print(message);

  Serial.println();

  delay(2000);

}

void light(int ledPin, boolean on) {
  if(on) {
    digitalWrite(ledPin, HIGH);
      Serial.println("LED HIGH");
  } else {
    digitalWrite(ledPin, LOW);
      Serial.println("led low");
  }

}

void reset() {
        Serial.println("Void reset");
  light(lightRight, false);
  light(lightRoom, false);
  light(lightGarage, false);
}

void on() {
        Serial.println("Void on");
  light(lightRight, true);
  light(lightRoom, true);
  light(lightGarage, true);
}

void off() {
        Serial.println("Void off");
  light(lightRight, false);
  light(lightRoom, false);
  light(lightGarage, false);
}

void blink(int delayn, int count) {

  for (int j=0; j <= count; j++){
    on();
    delay(delayn);
    off();
    delay(delayn);
  } 

}
1

1 Answers

4
votes

That example expects the PubNub publish to be "lightRight:1" and not {"lightRight": "1"}.