Philip TaranskiSam BaldorjIan Eubanks
Published © Apache-2.0

Happy, Indifferent, Sad

We asked math teachers how they were feeling. They had the option of either responding with happy, indifferent, or sad.

IntermediateShowcase (no instructions)8 hours793
Happy, Indifferent, Sad

Things used in this project

Hardware components

Breadboard (generic)
Breadboard (generic)
×1
buttons
×3
wires
×3
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1
Photon
Particle Photon
×1

Software apps and online services

Arduino particle.io

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Fritzing diagram

The wiring of our Photon

screen_shot_2017-03-10_at_10_05_49_am_CknpQmBAwJ.png

Code

button code

Processing
// This #include statement was automatically added by the Particle IDE.
#include <SparkFunPhant.h>

/*  SparkFun Inventor's Kit for Photon
    Experiment 2 - Part 1: With a Touch of a Button
    This sketch was written by SparkFun Electronics
    August 31, 2015
    https://github.com/sparkfun

    This is a simple example sketch that turns on an LED
    when pushing down on the push button

    Development environment specifics:
    Particle Build environment (https://www.particle.io/build)
    Particle Photon RedBoard
    Released under the MIT License(http://opensource.org/licenses/MIT)
*/

 // LED is connected to D0
int pushButtonHappy = D0;
int pushButtonIndifferent = D1;
int pushButtonSad = D2;// Push button is connected to D2
///int counterHappy = 0;
///int counterIndifferent = 0;
///int counterSad = 0;
double counterHappy = 0;
double counterIndifferent = 0;
double counterSad = 0;

const char server[] = "data.sparkfun.com"; // Phant destination server
const char publicKey[] = "LQgKbzbqMrcQ47m3b59y"; // Phant public key - HAS TO BE CHANGED
const char privateKey[] = "A1WnaVavRNfYEX4D8ejw"; // Phant private key  - HAS TO BE CHANGED
Phant phant(server, publicKey, privateKey); // Create a Phant object

const int POST_RATE = 3000; // Time between posts, in ms.
unsigned long lastPost = 0; // global variable to keep track of last post time


// This routine runs only once upon reset
void setup() 
{
 // Initialize D0 pin as output
  pinMode(pushButtonHappy, INPUT_PULLUP); 
  pinMode(pushButtonIndifferent, INPUT_PULLUP); 
  pinMode(pushButtonSad, INPUT_PULLUP);
  Serial.begin(9600);

  // Initialize D2 pin as input with an internal pull-up resistor
}

// This routine loops forever
void loop() 
{
  int pushButtonStateHappy; 
  int pushButtonStateIndifferent; 
  int pushButtonStateSad; 
  pushButtonStateHappy = digitalRead(pushButtonHappy);
  pushButtonStateIndifferent = digitalRead(pushButtonIndifferent);
  pushButtonStateSad = digitalRead(pushButtonSad);

  if(pushButtonStateHappy == HIGH)
  { // If we push down on the push button
     counterHappy = counterHappy + 1;
     Serial.println("Happy: " + (String)counterHappy) ;
     postToPhant() ;
     delay(250) ;
     // Turn ON the LED
  }
  if(pushButtonStateIndifferent == HIGH)
  {
     counterIndifferent = counterIndifferent +1;
     Serial.println("Indifferent: " + (String)counterIndifferent) ;
     postToPhant() ;
     delay(250) ;
  }
  if(pushButtonStateSad == HIGH)
  {
    counterSad = counterSad +1 ;
    Serial.println("Sad: " + (String)counterSad) ;
    postToPhant() ;
    delay(250) ;
  }

}

int postToPhant()
{    
    // Use phant.add(<field>, <value>) to add data to each field.
    // Phant requires you to update each and every field before posting,
    // make sure all fields defined in the stream are added here.
    phant.add("happy", counterHappy);
    phant.add("indifferent", counterIndifferent);
    phant.add("sad", counterSad);

        	
    TCPClient client;
    char response[512];
    int i = 0;
    int retVal = 0;
    
    if (client.connect(server, 80)) // Connect to the server
    {
		// Post message to indicate connect success
        Serial.println("Posting!"); 
		
		// phant.post() will return a string formatted as an HTTP POST.
		// It'll include all of the field/data values we added before.
		// Use client.print() to send that string to the server.
        client.print(phant.post());
        delay(1000);
		// Now we'll do some simple checking to see what (if any) response
		// the server gives us.
        while (client.available())
        {
            char c = client.read();
            Serial.print(c);	// Print the response for debugging help.
            if (i < 512)
                response[i++] = c; // Add character to response string
        }
		// Search the response string for "200 OK", if that's found the post
		// succeeded.
        if (strstr(response, "200 OK"))
        {
            Serial.println("Post success!");
            retVal = 1;
        }
        else if (strstr(response, "400 Bad Request"))
        {	// "400 Bad Request" means the Phant POST was formatted incorrectly.
			// This most commonly ocurrs because a field is either missing,
			// duplicated, or misspelled.
            Serial.println("Bad request");
            retVal = -1;
        }
        else
        {
			// Otherwise we got a response we weren't looking for.
            retVal = -2;
        }
    }
    else
    {	// If the connection failed, print a message:
        Serial.println("connection failed");
        retVal = -3;
    }
    client.stop();	// Close the connection to server.
}

Credits

Philip Taranski

Philip Taranski

-1 projects • 0 followers
Sam Baldorj

Sam Baldorj

-1 projects • 0 followers
Ian Eubanks

Ian Eubanks

-1 projects • 0 followers
Lane Tech College Prep Computer Science Major

Comments