Create a gist now

Instantly share code, notes, and snippets.

Listens for incoming RF-communication about the status of my front door. The Internet Arduino sends API calls to Pushover.
#include <SPI.h>
#include <Ethernet.h>
#include <HttpClient.h>
#include <SoftwareSerial.h>
// MAC address for your Ethernet shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// Pushover settings
char pushoversite[] = "api.pushover.net";
char apitoken[] = "xxxxx";
char userkey [] = "xxxxx";
int length;
int lastState = 4; //Last value that we received over RF (0 = door closed, 1 = door open, 2 = connection lost)
long lastConnectionTime = 0; // will store last connectioncheck
long maxTimeout = 86400000; // max timeout for connection (24H)
int led = 9; //output and light
SoftwareSerial mySerial(2, 3); // RX, TX
EthernetClient client;
//IPAddress server(64,94,18,120);
void setup() {
pinMode(led, OUTPUT);
// put your setup code here, to run once:
Serial.begin(9600);
// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
Serial.println("Starting receiver for Front door");
Serial.println();
while (Ethernet.begin(mac) != 1)
{
Serial.println("Error getting IP address via DHCP, trying again...");
delay(15000);
}
}
void loop() {
if (mySerial.available()) {
lastConnectionTime = millis();
char c = mySerial.read();
Serial.print(c);
if(c == 'O' && lastState != 1) {
lastState = 1;
pushover("Frontdoor opened");
digitalWrite(led,LOW);
} else if(c == 'C' && lastState != 0) {
lastState = 0;
digitalWrite(led,HIGH);
}
}
if ((millis() - lastConnectionTime) > maxTimeout && lastState != 2) {
//We have been waiting too long, there is something wrong with the connection
//Raise alert status
lastState = 2;
Serial.println("Versturen: voordeur storing");
pushover("Frontdoor error");
}
}
byte pushover(char *pushovermessage)
{
String message = pushovermessage;
Serial.println(message);
length = 81 + message.length();
if(client.connect(pushoversite,80))
{
Serial.println("connecting to Pushover...");
client.println("POST /1/messages.json HTTP/1.1");
client.println("Host: api.pushover.net");
client.println("Connection: close\r\nContent-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.print(length);
client.println("\r\n");;
client.print("token=");
client.print(apitoken);
client.print("&user=");
client.print(userkey);
client.print("&message=");
client.print(message);
while(client.connected())
{
while(client.available())
{
char ch = client.read();
Serial.write(ch);
}
}
client.stop();
} else {
Serial.println("connection error");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment