posted an update

Photon Code by Eric Robrigado /* range_sense.ino 30-Jan, 2016

*/

include

const char server[] = "162.243.58.60"; // destination server

//const int POST_RATE = 100000; // Time between posts, in ms. unsigned long POST_RATE = 10000, lastPost = 0; // global variable to keep track of last post time unsigned int new_val, old_val = 0, max_val; unsigned char led0_state = 0, led7_state = 0; float f_val, a_voltage;

void setup() { // serial debug Serial.begin(9600);

pinMode(A1, INPUT);
// sample rate
pinMode(D0, OUTPUT);
// setup interrupt
attachInterrupt(D0, sampleCurrentSense, RISING);
// set interrupt rate
tone(D0, 120, 0);
// upload rate
pinMode(D7, OUTPUT);

}

void loop() {

if ((lastPost + POST_RATE) > millis())
{
    //max_val = 1000;
    //f_val = float max_val;
    a_voltage = 3.3 * max_val / 4096.0;
    postToSafeKitchen(a_voltage);
    lastPost = millis();

    led7_state = !led7_state;
    digitalWrite(D7, led7_state);

}

//delay(2);

}

void sampleCurrentSense() { // sample the current sensor new_val = analogRead(A1); max_val = max(new_val, old_val); old_val = new_val;

Serial.println("new_A1");
Serial.println(new_val);
// chk sample speed via oscilloscope
// change the digital out state
led0_state = !led0_state;
digitalWrite(D0, led0_state);

}

int postToSafeKitchen(float voltage_val) {

TCPClient client;
char response[512];
int i = 0;
int retVal = 0;
char get_data[256];
unsigned int a_time;

if (client.connect(server, 80))
{
    // Post message to indicate connect success
    Serial.println("Posting!"); 

    //a_time = millis();
    //sprintf(get_data, "GET /devices/2c0047000747343232363230/data_points/push?data_type=current&value=%u", a_time);
    sprintf(get_data, "GET /devices/2c0047000747343232363230/data_points/push?data_type=current&value=%.2f", voltage_val);
    Serial.println(get_data);
    client.println(get_data);
    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;
        Serial.println("retVal = -2");
    }
}
else
{   // If the connection failed, print a message:
    Serial.println("connection failed");
    retVal = -3;
}
client.stop();  // Close the connection to server.
return retVal;  // Return error (or success) code.

}

Log in or sign up for Devpost to join the conversation.