Skip to content

Instantly share code, notes, and snippets.

@wgbartley
Created March 7, 2014 22:20
Show Gist options
  • Save wgbartley/9421425 to your computer and use it in GitHub Desktop.
Save wgbartley/9421425 to your computer and use it in GitHub Desktop.
A simple sketch for detecting a button press (for example, a doorbell) that calls a web page to send a text message. The onboard RGB LED will be red when nothing is happening. It turns green as it makes the web request, and then turns blue for 1 second after the request.
int ddl = 0; // Ding dong last millis()
int btnpin = D0; // "Button" pin
const int ddd = 15000; // Time until button can be activated again
void setup() {
pinMode(btnpin, INPUT_PULLUP);
RGB.control(true);
}
void loop() {
if(digitalRead(btnpin)==LOW)
dingdong();
else
RGB.color(255, 0, 0);
}
void dingdong() {
if(ddl==0 || (ddl+ddd)<millis()) {
ddl = millis();
RGB.color(0, 255, 0);
httpGetRequest("IP_ADDRESS", "HOSTNAME", 80, "/path_to_doorbell/");
RGB.color(0, 0, 255);
delay(1000);
}
}
void httpGetRequest(char *ip, char *hostname, int port, char *url) {
String line;
char cline[100];
TCPClient client;
client.connect(ip, port);
line = "GET " + String(url) + " HTTP1/.1";
line.toCharArray(cline, 100);
client.println(cline);
line = "Host: " + String(hostname);
line.toCharArray(cline, 100);
client.println(cline);
line = "Content-Length: 0";
line.toCharArray(cline, 100);
client.println(cline);
client.println();
client.flush();
client.stop();
delay(250);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment