I am trying to create an IoT based smart house system. It contains of my android application which sends and receives messages from/to PubNub (subscribes and publishes on certain channels) and Arduino UNO board with some LEDs and movement sensor (PIR). My android app seems to be working fine, it sends and receives messages, but the problem is with Arduino. In the beginning it receives messages from PubNub but after couple (exactly 2) publishes it stops receiving messages, it can just publish. Here is my arduino code:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char pubkey[] = "<pub key>";
char subkey[] = "<sub key>";
char channel[] = "publish";
char channel1[] = "subscribe";
char uuid[] = "Arduino";
volatile int pirpin = 2;
iotbridge arduino;
void initialize(){
Serial.begin(9600);
Serial.println("Serial set up");
pinMode(6, OUTPUT);
digitalWrite(6, LOW);
pinMode(pirpin, INPUT);
while (!Ethernet.begin(mac)) {
Serial.println("Ethernet setup error");
delay(1000);
}
Serial.println("Ethernet set up");
}
void do_something(String value){
Serial.println("in the callback");
Serial.println(value);
}
void pin_ISR() {
arduino.send(channel1, "\"Movement\"");
digitalWrite(6, HIGH);
delay(50);
}
void setup()
{
initialize();
arduino.init( pubkey, subkey, uuid);
Serial.println("PubNub set up");
attachInterrupt(0, pin_ISR, RISING);
}
void loop()
{
String returnmessage;
Ethernet.maintain();
Serial.println("waiting for a message");
returnmessage = arduino.connect(channel);
// callback function of sorts, to work with the received message
do_something(returnmessage);
Serial.println();
}
I am just testing the receiving and sending messages so it does not contain LED control. It uses interrupt on pin2 and when it becomes HIGH Arduino publishes a message, which should be received by android app. Thank you for your help in advance.