Eggzact Science

Entry for the “World’s Largest Arduino Maker Challenge” – An IoT Project with Windows10, the Arduino MKR1000, and Chickens.

 

Story

Things used in this project

Hardware components

Arduino MKR1000
Arduino MKR1000
× 1

Software apps and online services

Visual Studio 2015
Microsoft Visual Studio 2015

Project Theory

This project applies directly to the modern chicken farmer, but also to those interested in remote sensing using a windows apps and Arduino. Follow this process to update your chicken coop and bring it into the modern age.

The idea behind this project is that the eggs being laid are counted and recorded on an app for you. The eggs pass through a sensor which is recorded on a MKR1000. This data is then hosted on the MKR1000 as a local web server that is available for the Universal Windows Platform App to grab and use.

Hardware

Building the Coop: I made this prototype to have a slant of 15 degrees based off online research that said chickens won’t mind a sloped nesting box up to a 15 degrees. My prototype is made out of wood, and attached with hot glue and screws. I built rails to guide the egg down the shoot into the collector bin. I used extra gripping shelf liner to add a speed bump right below the sensor to help slow the egg down to insure the egg trips the sensor. See the schematic below.

Eggzact Science schematics

Note: I used an Arduino Uno as board in the schematic so the descriptions on the pins don’t all match up with the MKR1000.

Setting up the Sensor: I set up a generic infrared, IR, emitter and an IR detector across from each other and aimed directly at each other.  The IR emitter sends a “beam” of IR light to the detector.  The detector is able to sense when the egg passes and blocks the IR emitter as it rolls into the collector bin.  This sensor is hooked up to an LED that signifies that they are on and working and there is a button to reset the memory of how many eggs have passed.

Egg Sensor Validation

Software

All software created for this project is available on GitHub using the link at the end of this article.

Universal Windows Platform (UWP) App: Provides and organizes the data collected from the Arduino and displays it for the user. Visual Studio (VS) and UWP apps are pretty complicated if you don’t have previous exposure to them. I am by no means an expert and took many many hours to create this app. I did comment the code in hopes to make it understandable. See the video below for a brief walkthrough of both the code and the app.

Windows App Code Walkthrough

Arduino: The code that runs on the Arduino MKR1000 is responsible for three tasks.  The first task is to check and record if any eggs have gone past the IR sensor.  The second task is to check the button by the coop that resets the egg count, for use when the user picks up the eggs.  The final task is to act as a web server that hosts the egg count for any client request from the windows app created for this project.  See the video below for a brief walkthrough.

Arduino Code Walkthrough

Code

  /* -------------- READ ME ---------------- */
  /* The goal of this script is to use the arduino
   *  as a web server, to publish the number of eggs. 
   * Maybe so it could send it as json
   *  

     Known issuses:
      - Hardware:
          - Switch / wiring / connection causes "boucing" signal
          intermitantly when button is pushed or wires bumped.
  
  */

// Librarys Included
#include <SPI.h>
#include <WiFi101.h>

  // Definitions
  #define IRDetectorPin A1
  #define IREmmiterPin 5
  #define LEDPin 3
  #define SwitchPin 1

/* ------------- Global Variables ------------------ */
// Global Variables For WIFI
char ssid[] = "Insert Here";          // your network SSID (name)
char pass[] = "Insert Here";   // your network password
int keyIndex = 0;               // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
WiFiServer server(80);

// Global Variables - Other
int IRnow = 0;  // Current value of the IR sensor
int IRlast = 0; // Last value of the IR sensor
int NumberOfEggs = 0; // Current number of eggs
bool SwitchStateNow = false; // Current state of the switch
bool SwitchStateLast = true; // Last state of the switch

/* ----------- Setup Loop -------------------- */

void setup() {

  // FOR DEBUG ONLY, COMMENT OUT WHEN RELEASED
  
      //Initialize serial and wait for port to open:
      Serial.begin(9600);
      while (!Serial) {
       ; // wait for serial port to connect. Needed for native USB port only
      }

  // END DEBUG ONLY

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue:
    while (true);
  }

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }
  server.begin();
  // you're connected now, so print out the status:
  printWifiStatus();
}


void loop() {
  /* ----------------- Do chicken stuff first --------------- */
  
  // Check the IR Sensor;
  IRcheck();
  // TODO - would be good to set this up as an interrupt or something along those
  // lines or see how much time the web stuff takes, cause if it takes too long I
  // could miss an egg.

  // Check if values indicate egg passed by sensor
  EggCheck();

  // Check if switch was pressed
  SwitchCheck();

  /* ------------ Then do web stuff --------------------- */
  
  // listen for incoming clients
  WiFiClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          //client.println("Content-Type: text/html");
          client.println("Content-Type: application/json");
          client.println("Server: Arduino");
          client.println("Connection: close");  // the connection will be closed after completion of the response
          //client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.print("{ \"NumberOfEggs\":"); client.print(NumberOfEggs); client.println("}");
          client.println();

          //client.println("Number of Eggs");
          //client.println(NumberOfEggs);
          
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);

    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }

  
}

Egg_Functions

Arduino

// This function just reads the IR detector value.
void IRcheck() {
  // Move "current" value to last
  IRlast = IRnow;
  // Update IR now with current IR Detector reading
  IRnow = analogRead(IRDetectorPin);
}

// This function uses the IR detector values to determine
// if a egg rolled past and broke the IR beam and needs to
// be added to the count.
void EggCheck(){
  // If IR now is < .4 Volts count as egg and wasn't already counted.
  // 110 is approximately the Analog to Digital converter changes 0.4 Volts to.
  if((IRnow < 110) && (IRlast >= 110))
  {
   NumberOfEggs++; 
  }
  else
  {
    // No new eggs (do nothing)
  }
}


// This function check the button to see if it has been pushed.  It
// designed to be pushed when the user collects the eggs. So it resets
// the egg count.  It's a SPDT switch which is why the logic is set
// up like this.
void SwitchCheck(){
  SwitchStateLast = SwitchStateNow;
  if(SwitchStateNow != SwitchStateLast)
  {
  // Switch was pressed reset egg count (person picked up eggs)
  // First send the data to serial montoring for debugging purposes.
  //Serial.print("      Eggs Counted:");
  //Serial.println(NumberOfEggs);
  delay(1000);  // To deal with "switch bouncing" problem currently 
                // thought to be a wiring or connection issue.
  NumberOfEggs = 0;
  }  
}

Wifi_Functions

Arduino

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

 

Source : Eggzact Science


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top