Hi Folks…
I’m building a photon to push notifications system using pushing box… it works great up to a point…
The problem is the push button is supposed to be connected to D1, which is imposible to do as touching D1 with anything metal sets it of… even when not connected to the ground… even my finger sets it off and i get push notifications like crazy??? So i have no idea how i’m suposeed to connect a button to this…
Any ideas why?
Heres my code.
int inputPin = D1; //push-button pin
int val = 0; //variable for push-button status
int state = 1; //variable for on/off state
int led = D7; // integrated LED
TCPClient client;
char reply[512];
void setup() {
pinMode(inputPin, INPUT);
pinMode(led, OUTPUT);
Serial.begin(9600);
}
void loop() {
val = digitalRead(inputPin); //read the state of the push-button
if (val == LOW) { //if push-button pressed
state = !state; //reverse on/off state
delay(250); //primitive button debounce
Serial.println("button pushed!");
digitalWrite(led, HIGH); // Turn ON the LED pins
notification();
}
}
void notification()
{
if (client.connect("api.pushingbox.com", 80))
{
out("GET /pushingbox?devid=XXXXXXXXXXX HTTP/1.1\r\n");
out("Host: api.pushingbox.com\r\n");
out("User-Agent: Spark/1.0\r\n");
out("Content-Type: text/html\r\n");
out("Connection: close\r\n\r\n");
//DEBUG_PRINTLN("Closing Connection...");
in(reply, 3000);
Serial.println("Complete");
//
digitalWrite(led, LOW); // Blink LED
delay(250);
digitalWrite(led, HIGH);
delay(250);
digitalWrite(led, LOW);
}
else{
Serial.println("connection failed");
digitalWrite(led, LOW); // Pulse LED Quickly
delay(100);
digitalWrite(led, HIGH);
delay(100);
digitalWrite(led, LOW);
delay(100);
digitalWrite(led, HIGH);
delay(100);
digitalWrite(led, LOW);
delay(100);
digitalWrite(led, HIGH);
delay(100);
digitalWrite(led, LOW);
}
//
}
void out(const char *s)
{
client.write( (const uint8_t*)s, strlen(s) );
//Serial.println("Writing");
}
void in(char *ptr, uint8_t timeout)
{
int pos = 0;
unsigned long lastTime = millis();
while( client.available()==0 && millis()-lastTime<timeout)
{
} //do nothing
lastTime = millis();
unsigned long lastdata = millis();
while ( client.available() || (millis()-lastdata < 500))
{
if (client.available())
{
char c = client.read();
//DEBUG_PRINTLN(c);
lastdata = millis();
ptr[pos] = c;
pos++;
}
if (pos >= 512 - 1)
break;
}
ptr[pos] = '\0'; //end the char array
while (client.available()) client.read(); // makeshift client.flush()
client.flush(); //for safety
delay(400);
client.stop();
}