Tech Thursday

Part 2 of our chicken coop home automation project is sending a message to your phone or to your email from an Arduino! What good is a home automation project if it does not notify you if something is wrong or if the status of something is changing, etc.

This is a continuation of our Home Automation Chicken Coop – Part 1. This is Part 2 of a multi part posting of the chicken coop home automation IoT (Internet of Things)

The Arduino Uno is used to push messages using a API called PushingBox. The Pushing Box notices can be set-up to send instant messages, emails, etc. priority can be set, etc. So a low priority notice is sent when the lights are turned on or off, etc. A high priority message is sent if smoke or methane levels are high, etc. I have installed a Arduino Uno and set it up as client to send the messages, all we do with it is send messages. A Ethernet Shield is install on the Arduino Uno. A serial packet from the Arduino Mega, which is set up as a server tells the Uno if status has changed. If status has changed, the Uno relays the message!

This is the code used on the Arduino Uno.

// ************************************************************************
// Name                : chicken_coop_pushingbox.ino 
// Author              : Charlie Payne 
// Notice              : Copyright (c) 2015 SuperDroid Robots
//                     : All Rights Reserved 
// Date                : April 23, 2015
//  
// Arduino             : Arduino Uno R3 
// Shield(s)           : Arduino Ethernet Shield  
//
// Notes               : Program to send notices using PushingBox
//                       Serial Comminication from Mega stating Status
//  
//  
// ************************************************************************
// ************************************************************************
#include 
#include 

#define LED1 5
#define LED2 6
#define LED3 7

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0xEB, 0x87 };   //physical mac address
byte ip[] = { 192, 168, 0, 149 };                      // ip in lan 
byte gateway[] = { 192, 168, 0, 1 };                   // internet access via router
byte subnet[] = { 255, 255, 255, 0 };                  //subnet mask

//Your secret DevID from PushingBox.com. You can use multiple DevID  on multiple Pin if you want
//some of the outputs wanted status messages for on and off, so just make it easy and gave them each bits..
char DEVID_Lost_Connection[] =  "Provided by PushingBox";  // CoopStatus1.0
char DEVID_Smoke[] =            "Provided by PushingBox";  // CoopStatus1.1
char DEVID_Methane[] =          "Provided by PushingBox";  // CoopStatus1.2
char DEVID_Motion[] =           "Provided by PushingBox";  // CoopStatus1.3
char DEVID_High_Temperature[] = "Provided by PushingBox";  // CoopStatus1.4
char DEVID_Low_Temperature[] =  "Provided by PushingBox";  // CoopStatus1.5
char DEVID_Battery_Chg_Off[] =	"Provided by PushingBox";  // CoopStatus1.6 
char DEVID_Battery_Chg_On[] =	"Provided by PushingBox";  // CoopStatus1.7 

char DEVID_Reboot[] =           "Provided by PushingBox";  // CoopStatus2.0
char DEVID_Door_Closing[] =     "Provided by PushingBox";  // CoopStatus2.1	
char DEVID_Door_Opening[] =     "Provided by PushingBox";  // CoopStatus2.2
char DEVID_Fan_Off[] =          "Provided by PushingBox";  // CoopStatus2.3
char DEVID_Fan_On[] =           "Provided by PushingBox";  // CoopStatus2.4
char DEVID_Lights_Off[] =       "Provided by PushingBox";  // CoopStatus2.5
char DEVID_Lights_On[] =        "Provided by PushingBox";  // CoopStatus2.6

char DEVID_Manual_Cntrl[] =     "Provided by PushingBox";  // CoopStatus3.0 
char DEVID_Auto_Cntrl[] =       "Provided by PushingBox";  // CoopStatus3.1
char DEVID_High_Cntrl_Temp[] =  "Provided by PushingBox";  // CoopStatus3.2

boolean DEVID_Lost_Connection_Status = 0;
boolean DEVID_Smoke_Status = 0;
boolean DEVID_Methane_Status = 0;
boolean DEVID_Motion_Status = 0;
boolean DEVID_High_Temperature_Status = 0;
boolean DEVID_Low_Temperature_Status = 0;
boolean DEVID_Battery_Chg_Off_Status = 0;
boolean DEVID_Battery_Chg_On_Status = 0;
boolean DEVID_Reboot_Status = 0;
boolean DEVID_Door_Closing_Status = 0;
boolean DEVID_Door_Opening_Status = 0;
boolean DEVID_Fan_Off_Status = 0;
boolean DEVID_Fan_On_Status = 0;
boolean DEVID_Lights_Off_Status = 0;
boolean DEVID_Lights_On_Status = 0;
boolean DEVID_Manual_Cntrl_Status = 0;
boolean DEVID_Auto_Cntrl_Status = 0;
boolean DEVID_High_Cntrl_Temp_Status =  0;
byte Rebooted_Flag = 0;

// Sender Information
byte START_BYTE = 0x53; // ASCII "S"
byte counterValue = 0;
byte CoopStatus1 = 0;
byte CoopStatus2 = 0;
byte CoopStatus3 = 0;
byte checksum = 0;
byte Rx_Failed = 0;
const byte Rx_Failed_limit = 5;
const unsigned long Rx_Lost_limit = 5000000L; // works out to be about 90 seconds b4 alert
unsigned long Rx_Lost = 0;
unsigned long Rx_Lost_Last = 0;
boolean syncByteFound = 0; // Sync Byte flag

char serverName[] = "api.pushingbox.com";
EthernetClient ClientPush;  // Initialize the Ethernet ClientPush library

void setup() {
  Serial.begin(9600);

  //pinMode(pinDevid1, INPUT);

  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);  //red

  //Serial.println("Initializing Ethernet...");
  
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    //Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }
  else{
    //Serial.println("Ethernet ready");
    // print the Ethernet board/shield's IP address:
    //Serial.print("My IP address: ");
    //Serial.println(Ethernet.localIP());
  }
  // give the Ethernet shield and main controller time to get going:
  for (int i=0; i <= 10; i++)  //blink lights
  {
    digitalWrite(LED1, HIGH);  
    delay(210-i*20);               
    digitalWrite(LED1, LOW);   
    delay(210-i*20);               
    digitalWrite(LED2, HIGH);   
    delay(210-i*20);              
    digitalWrite(LED2, LOW);    
    delay(210-i*20);              
    digitalWrite(LED3, HIGH);   
    delay(210-i*20);               
    digitalWrite(LED3, LOW);    
    delay(210-i*20);               
  } 
}

void loop()
{
  unsigned char rxByte = 0;
  unsigned char calculatedChecksum = 0;

  ++Rx_Lost;
  if (Rx_Lost > Rx_Lost_Last)
  {
    Serial.println(Rx_Lost);
    Rx_Lost_Last = Rx_Lost + 10000;
    if (digitalRead(LED1)) 
    {
      digitalWrite(LED1, LOW);
    }
    else
    {
      digitalWrite(LED1, HIGH);
    }
  }
  
  if (Serial.available() > 0 ) // Check to see if there's something to read
  {
    if (syncByteFound == 0) // If we're waiting for a new packet, check for the sync byte
    {
      rxByte = Serial.read();
      if (rxByte == 0x53) syncByteFound = 1;
    }
    if (Serial.available() > 4 && syncByteFound) // If we've found our sync byte, check for expected number of bytes
    {
      counterValue = Serial.read();
      CoopStatus1 = Serial.read();
      CoopStatus2 = Serial.read();
      CoopStatus3 = Serial.read();
      checksum = Serial.read();
      calculatedChecksum =  counterValue + CoopStatus1 + CoopStatus2 + CoopStatus3;
      if (calculatedChecksum == checksum) //Checksum Passed - Good Packacket!
      {
        Rx_Lost = 0; // reset counter for good package...
        Rx_Failed = 0; // reset counter for good package...
        Rx_Lost_Last = 0; // // reset counter for good package...
        DEVID_Lost_Connection_Status = 0; //reset flag for good package
        digitalWrite(LED3, LOW);
        if (digitalRead(LED2)) 
        {
          digitalWrite(LED2, LOW);
        }
        else
        {
          digitalWrite(LED2, HIGH);
        }
        // Analize the packet and do stuff....  

        if (bitRead(CoopStatus2, 0) == 1 && DEVID_Reboot_Status == 0 )  // Main Controller just Reset
        {
          sendToPushingBox(DEVID_Reboot);
          DEVID_Reboot_Status = 1; 
          Rebooted_Flag = 0;         
        }
        if (bitRead(CoopStatus2, 0) == 0)  
        {
          DEVID_Reboot_Status = 0;  
        }

        ++Rebooted_Flag;
        if (Rebooted_Flag >= 10 && DEVID_Reboot_Status == 0) // skip all other notifications if the main controller just rebooted
        {
          Rebooted_Flag = 100; // prevent overflow

          if (bitRead(CoopStatus1, 1) == 1 && DEVID_Smoke_Status == 0 )  // Smoke Detected
          {
            sendToPushingBox(DEVID_Smoke);
            DEVID_Smoke_Status = 1;          
          }
          if (bitRead(CoopStatus1, 1) == 0)  
          {
            DEVID_Smoke_Status = 0;  
          }
          
          if (bitRead(CoopStatus1, 2) == 1 && DEVID_Methane_Status == 0 )  // Methane Detected
          {
            sendToPushingBox(DEVID_Methane);
            DEVID_Methane_Status = 1;          
          }
          if (bitRead(CoopStatus1, 2) == 0)  
          {
            DEVID_Methane_Status = 0;  
          }
          
          if (bitRead(CoopStatus1, 3) == 1 && DEVID_Motion_Status == 0 )  // Motion Detected
          {
            sendToPushingBox(DEVID_Motion);
            DEVID_Motion_Status = 1;          
          }
          if (bitRead(CoopStatus1, 3) == 0)  
          {
            DEVID_Motion_Status = 0;  
          }
          
          if (bitRead(CoopStatus1, 4) == 1 && DEVID_High_Temperature_Status == 0 )  // High Temperature Alert
          {
            sendToPushingBox(DEVID_High_Temperature);
            DEVID_High_Temperature_Status = 1;          
          }
          if (bitRead(CoopStatus1, 4) == 0)  
          {
            DEVID_High_Temperature_Status = 0;  
          }
          
          if (bitRead(CoopStatus1, 5) == 1 && DEVID_Low_Temperature_Status == 0 )  // Low Temperature Alert
          {
            sendToPushingBox(DEVID_Low_Temperature);
            DEVID_Low_Temperature_Status = 1;          
          }
          if (bitRead(CoopStatus1, 5) == 0)  
          {
            DEVID_Low_Temperature_Status = 0;  
          }

          if (bitRead(CoopStatus1, 6) == 1 && DEVID_Battery_Chg_Off_Status == 0 )  // Charger Off Alert
          {
            sendToPushingBox(DEVID_Battery_Chg_Off);
            DEVID_Battery_Chg_Off_Status = 1;          
          }
          if (bitRead(CoopStatus1, 6) == 0)  
          {
            DEVID_Battery_Chg_Off_Status = 0;  
          }
          
          if (bitRead(CoopStatus1, 7) == 1 && DEVID_Battery_Chg_On_Status == 0 )  // Charger On Alert
          {
            sendToPushingBox(DEVID_Battery_Chg_On);
            DEVID_Battery_Chg_On_Status = 1;          
          }
          if (bitRead(CoopStatus1, 7) == 0)  
          {
            DEVID_Battery_Chg_On_Status = 0;  
          }
          
          if (bitRead(CoopStatus2, 1) == 1 && DEVID_Door_Closing_Status == 0 )  // Door Closing
          {
            sendToPushingBox(DEVID_Door_Closing);
            DEVID_Door_Closing_Status = 1;          
          }
          if (bitRead(CoopStatus2, 1) == 0)  
          {
            DEVID_Door_Closing_Status = 0;  
          }
          
          if (bitRead(CoopStatus2, 2) == 1 && DEVID_Door_Opening_Status == 0 )  // Door opening
          {
            sendToPushingBox(DEVID_Door_Opening);
            DEVID_Door_Opening_Status = 1;          
          }
          if (bitRead(CoopStatus2, 2) == 0)  
          {
            DEVID_Door_Opening_Status = 0;  
          }

          if (bitRead(CoopStatus2, 3) == 1 && DEVID_Fan_Off_Status == 0 )  // Fan Off Alert
          {
            sendToPushingBox(DEVID_Fan_Off);
            DEVID_Fan_Off_Status = 1;          
          }
          if (bitRead(CoopStatus2, 3) == 0)  
          {
            DEVID_Fan_Off_Status = 0;  
          }

          if (bitRead(CoopStatus2, 4) == 1 && DEVID_Fan_On_Status == 0 )  // Fan On Alert
          {
            sendToPushingBox(DEVID_Fan_On);
            DEVID_Fan_On_Status = 1;          
          }
          if (bitRead(CoopStatus2, 4) == 0)  
          {
            DEVID_Fan_On_Status = 0;  
          }

          if (bitRead(CoopStatus2, 5) == 1 && DEVID_Lights_Off_Status == 0 )  // Lights off Alert
          {
            sendToPushingBox(DEVID_Lights_Off);
            DEVID_Lights_Off_Status = 1;          
          }
          if (bitRead(CoopStatus2, 5) == 0)  
          {
            DEVID_Lights_Off_Status = 0;  
          }

          if (bitRead(CoopStatus2, 6) == 1 && DEVID_Lights_On_Status == 0 )  // Lights on Alert
          {
            sendToPushingBox(DEVID_Lights_On);
            DEVID_Lights_On_Status = 1;          
          }
          if (bitRead(CoopStatus2, 6) == 0)  
          {
            DEVID_Lights_On_Status = 0;  
          }

         if (bitRead(CoopStatus3, 0) == 1 && DEVID_Manual_Cntrl_Status == 0 )  // Coop in Manual Mode
          {
            sendToPushingBox(DEVID_Manual_Cntrl);
            DEVID_Manual_Cntrl_Status = 1;          
          }
          if (bitRead(CoopStatus3, 0) == 0)  
          {
            DEVID_Manual_Cntrl_Status = 0;  
          }

         if (bitRead(CoopStatus3, 1) == 1 && DEVID_Auto_Cntrl_Status == 0 )  // Coop in Automiatic Mode
          {
            sendToPushingBox(DEVID_Auto_Cntrl);
            DEVID_Auto_Cntrl_Status = 1;          
          }
          if (bitRead(CoopStatus3, 1) == 0)  
          {
            DEVID_Auto_Cntrl_Status = 0;  
          }

          if (bitRead(CoopStatus3, 2) == 1 && DEVID_High_Cntrl_Temp_Status == 0 )  // Control Box High Temperature
          {
            sendToPushingBox(DEVID_High_Cntrl_Temp);
            DEVID_High_Cntrl_Temp_Status = 1;          
          }
          if (bitRead(CoopStatus3, 2) == 0)  
          {
            DEVID_High_Cntrl_Temp_Status = 0;  
          }
        }  
  
      }
      else //Serial Failed
      {
        digitalWrite(LED3, HIGH);
        delay(500);
        ++Rx_Failed;
      }      
      syncByteFound = 0;
    }
  }

  if (Rx_Failed > Rx_Failed_limit || Rx_Lost > Rx_Lost_limit)
  {
    Rx_Lost = 0; // reset counter...
    Rx_Failed = 0; // reset counter...
    Rx_Lost_Last = 0; // // reset counter...
    digitalWrite(LED1, LOW);   
    digitalWrite(LED2, LOW);    
    for (int i=0; i <= 10; i++)  //blink lights
    {
      digitalWrite(LED3, HIGH);   
      delay(30);               
      digitalWrite(LED3, LOW);    
      delay(20);               
    } 
    if (DEVID_Lost_Connection_Status == 0) //send message, the Serial package has failed
    {
      sendToPushingBox(DEVID_Lost_Connection);
      DEVID_Lost_Connection_Status = 1;
    }
  }
}


//Function for sending the request to PushingBox
void sendToPushingBox(char devid[])
{
  ClientPush.stop();
  if (ClientPush.connect(serverName, 80)) // client connected sucessfully
  {
    ClientPush.print("GET /pushingbox?devid=");
    ClientPush.print(devid);
    ClientPush.println(" HTTP/1.1");
    ClientPush.print("Host: ");
    ClientPush.println(serverName);
    ClientPush.println("User-Agent: Arduino");
    ClientPush.println();
    for (int i=0; i <= 5; i++)  //blink lights
    {
      digitalWrite(LED1, HIGH);  
      digitalWrite(LED2, HIGH);   
      digitalWrite(LED3, HIGH);        
      delay(50);              
      digitalWrite(LED1, LOW);   
      digitalWrite(LED2, LOW);    
      digitalWrite(LED3, LOW);
      delay(20);              
    } 
  } 
  else // connection failed 
  { 
    digitalWrite(LED1, LOW);   
    digitalWrite(LED2, LOW);    
    for (int i=0; i <= 10; i++)  //blink lights
    {
      digitalWrite(LED3, HIGH);   
      delay(400);               
      digitalWrite(LED3, LOW);    
      delay(75);               
    } 
  }
}

#IOT #homeautomation #techthursday #arduino

0 Comments

Leave a reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

©2024 SDRobots.com | All rights reserved.

Log in with your credentials

or    

Forgot your details?

Create Account