Project showcase
Eggzact Science

Eggzact Science © LGPL

Entry for the "World's Largest Arduino Maker Challenge" - An IoT Project with Windows10, the Arduino MKR1000, and Chickens.

  • 2,870 views
  • 0 comments
  • 6 respects

Components and supplies

Apps and online services

About this project

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. 

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

Bill of Material

- 1 - Arduino MKR1000

- 1 - Generic Infrared LED Emitter and Detector

- 1 - Generic Button - SPST

- 2 - Generic 330Ω Resistors

As Needed

- Jumper wires and cables

- Nest box(s) with sloped floor

CAD Files

- Not applicable

References

- Eggs in Nest (Picture), edited by creating a feathered white border around this image, used under - Creative Commons Attribution 4.0 International License

- Icon (Picture), created from the image at here used under the Non-profit terms of use listed on their website

- Screenshot Video - used Icecream Apps free Screen Recorder 

- Schematic View - used Fritzing

Code

EggzactScienceV1.00Arduino
  /* -------------- 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_FunctionsArduino
// 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_FunctionsArduino
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");
}

Comments

Similar projects you might like

Reef Controller

Project showcase by Shawn Leclair

  • 13,537 views
  • 0 comments
  • 19 respects

DogWatcher (MKR1000)

Project showcase by John Abbott

  • 3,611 views
  • 3 comments
  • 10 respects

IoT for coins

Project tutorial by Erik Moran

  • 8,377 views
  • 1 comment
  • 42 respects

MKR1000 Temp and Humidity Sensor

Project tutorial by Don Coleman

  • 18,138 views
  • 11 comments
  • 31 respects

DasCognitiveServices

by Marius Dima

  • 16,652 views
  • 8 comments
  • 68 respects

Temperature Controled Charcoal Smoker

Project tutorial by Team Up in Smoke

  • 13,228 views
  • 6 comments
  • 15 respects
Add projectSign up / Login