Hi,
I need advice regarding my project that will notify user via email and log the event in google spreadsheet. I can do notification but can't do log. meanwhile, i can do log, but no notification. It seems my coding is not good. Any idea or solution to make it happen?
////
//
// General code from http://www.pushingbox.com for Arduino + Ethernet Shield (official) v1.2
//
////
#include <SPI.h>
#include <Ethernet.h>
/////////////////
// MODIFY HERE //
/////////////////
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x19 }; // Be sure this address is unique in your network
//IPAddress ip(192, 168, 1, 177);
//Your secret DevID from PushingBox.com. You can use multiple DevID on multiple Pin if you want
char DEVID1[] = "vDBE4E0BA0****"; //Scenario : "OLS Closed"
char DEVID2[] = "v72CE836C4*****"; //Scenario : "OLS Open"
//Numeric Pin where you connect your switch
uint8_t pinDevid1 = 2; // Example : the mailbox switch is connect to the Pin 3
int lightOutput = 3;
int lightOutput2 = 5;
int lightOutput3 = 6;
int supply = 9;
unsigned long previousMillis = 0;
int buttonState = 0;
int lastState = 0; // previous state of the button
String x;
String data;
// Debug mode
boolean DEBUG = true;
char serverName[] = "api.pushingbox.com";
boolean pinDevid1State = false;
boolean lastConnected = false; // State of the connection last time through the main loop
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
void setup()
{
pinMode(lightOutput, OUTPUT);
pinMode(lightOutput2, OUTPUT);
pinMode(lightOutput3, OUTPUT);
pinMode(supply, OUTPUT);
pinMode(pinDevid1, INPUT);
digitalWrite(pinDevid1, HIGH);
digitalWrite(supply, HIGH);
Serial.begin(9600);
// 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);
// Ethernet.begin(mac);
}
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()
{
buttonState = digitalRead(pinDevid1);
unsigned long currentMillis = millis();
if ((buttonState == HIGH) && (pinDevid1State == false))
{
x = "OLS_Close";
if (DEBUG) {
Serial.println("pinDevid1 is HIGH");
}
pinDevid1State = true;
//Add fungsi notifikasi disini
kemasData();
Serial.println("connecting...");
client.stop();
// if (client.connect(serverName, 80)) {
sendData();
client.stop();
// koneksi = true; //connected = true
// }
sendToPushingBox(DEVID1);
//Kesini ya
previousMillis = currentMillis;
// lastState = buttonState;
}
if ((buttonState == HIGH) && (pinDevid1State == true))
{
if (currentMillis - previousMillis >= 2000)
{
digitalWrite(lightOutput, HIGH);
}
if (currentMillis - previousMillis >= 3000)
{
digitalWrite(lightOutput2, HIGH);
}
if (currentMillis - previousMillis >= 4000)
{
digitalWrite(lightOutput3, HIGH);
}
// lastState = buttonState;
}
if ((buttonState == LOW) && (pinDevid1State == true))
{
x = "OLS_Open";
digitalWrite(lightOutput, LOW);
digitalWrite(lightOutput2, LOW);
digitalWrite(lightOutput3, LOW);
if (DEBUG) {
Serial.println("pinDevid1 is LOW");
}
//Add fungsi notifikasi dan logger
kemasData();
Serial.println("connecting...");
client.stop();
// if (client.connect(serverName, 80)) {
sendData();
// koneksi = true; //connected = true
// }
client.stop();
sendToPushingBox(DEVID2);
// lastState = buttonState;
pinDevid1State = false;
previousMillis = currentMillis;
}
// //DEBUG part
// // this write the respons from PushingBox Server.
// // You should see a "200 OK"
// if (client.available()) {
// char c = client.read();
// if (DEBUG) {
// Serial.print(c);
// }
// }
// // if there's no net connection, but there was one last time
// // through the loop, then stop the client:
// if (!client.connected() && lastConnected) {
// if (DEBUG) {
// Serial.println(data);
// }
// if (DEBUG) {
// Serial.println("disconnecting.");
// }
// data = ""; //data reset
// client.stop();
// }
// lastConnected = client.connected();
}
//Function for sending the request to PushingBox
void sendToPushingBox(char devid[]) {
client.stop();
if (DEBUG) {
Serial.println("connecting...");
}
if (client.connect(serverName, 80)) {
if (DEBUG) {
Serial.println("connected");
}
if (DEBUG) {
Serial.println("sendind request");
}
client.print("GET /pushingbox?devid=");
client.print(devid);
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(serverName);
client.println("User-Agent: Arduino");
client.println();
}
else {
if (DEBUG) {
Serial.println("connection failed");
}
}
//DEBUG part
// this write the respons from PushingBox Server.
// You should see a "200 OK"
if (client.available()) {
char c = client.read();
if (DEBUG) {
Serial.print(c);
}
}
// if there's no net connection, but there was one last time
// through the loop, then stop the client:
if (!client.connected() && lastConnected) {
if (DEBUG) {
Serial.println(data);
}
if (DEBUG) {
Serial.println("disconnecting.");
}
data = ""; //data reset
client.stop();
}
lastConnected = client.connected();
}
void kemasData() {
data += "";
data += "GET /pushingbox?devid=v03AC53F******&tempData="; //GET request query to pushingbox API
data += x;
data += " HTTP/1.1";
}
void sendData() {
client.stop();
if (DEBUG) {
Serial.println("connecting...");
}
if (client.connect(serverName, 80)) {
if (DEBUG) {
Serial.println("connected");
}
if (DEBUG) {
Serial.println("sendind request");
}
Serial.println("connected");
client.println(data);
client.println("Host: api.pushingbox.com");
client.println("Connection: close");
client.println();
}
//DEBUG part
// this write the respons from PushingBox Server.
// You should see a "200 OK"
if (client.available()) {
char c = client.read();
if (DEBUG) {
Serial.print(c);
}
}
// if there's no net connection, but there was one last time
// through the loop, then stop the client:
if (!client.connected() && lastConnected) {
if (DEBUG) {
Serial.println(data);
}
if (DEBUG) {
Serial.println("disconnecting.");
}
data = ""; //data reset
client.stop();
}
lastConnected = client.connected();
}
Thanks