/*
Arduino DAQ using by Google Drive
A simple DAQ system that save the values of the 5 analog input pins(A0 to A4).
using an Arduino Wiznet Ethernet shield.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
* Analog inputs attached to pins A0 through A4
* Sample Acquire Switchs attached to pins 3, 5
* DEBUG mode LED attached to pin 4
created 4 Feb 2014
by Jelicle Lim
This example code was made with the two links.
>> https://github.com/PushingBox/PushingBox-for-Arduino
>> http://www.pushingbox.com
And this is the source-link that I learned about the main idea of using google drive as data storage unit.
>> https://github.com/PushingBox/PushingBox-for-Arduino/blob/master/PushingBox_Arduino_Ethernet_Official/PushingBox_Arduino_Ethernet_Official.ino
*/
#include <SPI.h>
#include <Ethernet.h>
#define DEBUG // Remove this line if there are no problems.
#define CONTINUSDAQ // if defined (CONTINUSDAQ) then, continuous-sample-acquire to 40000 samples.
#define CONTINUS_SW // if defined (CONTINUS_SW) then, continuous-sample-acquire while SWitch is ON.
// #define TACT_SW // if Not Defined (CONTINUS_SW) then, discrete-sample-acquire when Tact Switch clicked.
//Numeric Pin where you connect your switch
#define PINDEVID1 3 // The mailbox switch is connect to the Pin 3
#define PINSW1 5
#ifdef DEBUG
#define PINLED1 4 // Example : the mailbox switch is connect to the Pin 3
#endif
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// char DEVID1[] = "v3BA8F51C3F9EB8D"; //Replace with your Key made by pushingbox
char DEVID1[] = "v478626023164DCD";
int i = 0;
char serverName[] = "api.pushingbox.com";
boolean PINDEVID1State = false; // Save the last state of the Pin for DEVID1
boolean lastConnected = false; // State of the connection last time through the main loop
EthernetClient client;
void setup()
{
Serial.begin(9600);
pinMode(PINDEVID1, INPUT);
pinMode(PINSW1, INPUT);
#ifdef DEBUG
pinMode(PINLED1, OUTPUT);
debugled(5);
#endif
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
while(true);
}
else{
Serial.println("Ethernet ready");
// print the Ethernet board/shield's IP address:
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
}
// give the Ethernet shield a second to initialize:
delay(1000);
}
void loop(){
if (digitalRead(PINSW1) == HIGH) { // pin 5
#ifdef CONTINUS_SW
if (digitalRead(PINDEVID1) == HIGH) { // pin 3
sendToPushingBox(DEVID1);
#ifdef CONTINUSDAQ
i++;
if(i<40000) sendToPushingBox(DEVID1);
if(i>40000) i=40000;
#else
sendToPushingBox(DEVID1);
#endif
#ifdef DEBUG
debugled(2);
#else
delay(200);
#endif
}
#endif
}
////
// Listening for the PINDEVID1 state
////
if (digitalRead(PINDEVID1) == HIGH && PINDEVID1State == false) // switch on PINDEVID1 is ON
{
#ifdef DEBUG
Serial.println("PINDEVID1 is HIGH");
debugled(3);
#endif
PINDEVID1State = true;
//Sending request to PushingBox when the pin is HIGHT
sendToPushingBox(DEVID1);
}
if (digitalRead(PINDEVID1) == LOW && PINDEVID1State == true) // switch on PINDEVID1 is OFF
{
#ifdef DEBUG
Serial.println("PINDEVID1 is LOW");
debugled(1);
#endif
PINDEVID1State = false;
//Sending request to PushingBox when the pin is LOW
//sendToPushingBox(DEVID1); //Here you can run an other scenario by creating a DEVID2 variable
}
if (client.available()) {
char c = client.read();
#ifdef DEBUG
Serial.print(c);
#endif
}
// if there's no net connection, but there was one last time
// through the loop, then stop the client:
if (!client.connected() && lastConnected) {
#ifdef DEBUG
Serial.println();
Serial.println("disconnecting.");
#endif
client.stop();
}
lastConnected = client.connected();
}
//Function for sending the request to PushingBox
void sendToPushingBox(char devid[]) {
client.stop();
#ifdef DEBUG
Serial.println("connecting...");
#endif
if (client.connect(serverName, 80)) {
#ifdef DEBUG
Serial.println("connected");
Serial.println("sendind request");
#endif
String data;
data ="";
data+="&first=";
data+=SensorValue(A0);
data+="&second=";
data+=SensorValue(A1);
data+="&third=";
data+=SensorValue(A2);
data+="&fourth=";
data+=SensorValue(A3);
data+="&fifth=";
data+=SensorValue(A4);
client.print("POST /pushingbox?devid=");
client.print(devid);
client.print(data);
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(serverName);
client.println("User-Agent: Arduino");
client.println();
}
else {
#ifdef DEBUG
Serial.println("connection failed");
debugled(3);
#else
delay(300);
#endif
}
}
// Read Senser value 5 times, remove the min and max, and return the average
int SensorValue(int pin){
int i, value=0, vmax=0, vmin=1023;
int val[5];
for(i=0;i<5;i++) {
val[i] = analogRead(pin);
value += val[i];
}
for(i=0;i<5;i++) {
vmax = max(val[i],vmax);
vmin = min(val[i],vmin);
}
value = (value - vmax - vmin) / 3;
return value;
}
#ifdef DEBUG
void debugled(int times){
uint8_t i;
for(i=0;i<times;i++) {
digitalWrite(PINLED1, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100);
digitalWrite(PINLED1, LOW); // turn the LED on (HIGH is the voltage level)
delay(100);
}
}
#endif