Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Jason Cowin
Published © LGPL

Eggzact Science

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

IntermediateShowcase (no instructions)12 hours4,885
Eggzact Science

Things used in this project

Story

Read more

Code

EggzactScienceV1.00

Arduino
  /* -------------- 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");
}

Credits

Jason Cowin

Jason Cowin

3 projects • 11 followers
Mechatronics Degree 2014 - Happily Married in 2015 - Maintain Robots for Work
Thanks to Amanda Cowin.

Comments