Push-Nachrichten
Hier das Video:Und der Code:
Damit alles funktioniert werden ein paar Library-Dateien benötigt. Diese stehen hier zum Download bereit:
www.file-upload.net/download-7927102/Library.zip.html
// Dieses Projekt benutzt in Teilen den Code aus dem Projekt "Avviso" von Mike Colagrosso.
// Zum Benutzten ist die App "Prowl" notwendig.
#include <SPI.h>
#include <Ethernet.h>
#include <HTTPClient.h>
#include <Ethernet.h>
#include <Avviso.h>
#include <Time.h>
#define DEBUG 1
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,2, 23);
//Hier die IP-Adresse eingeben.
EthernetServer server(80);
int counterValue;
int aktivZaehler = 0;
void setup() {
counterValue = 10;
Serial.begin(115200);
if (DEBUG) Serial.println("Attempting to obtain a DHCP lease...");
Ethernet.begin(mac, ip);
server.begin();
if (DEBUG) {
Serial.print("My IP address: ");
Ethernet.localIP().printTo(Serial);
Serial.println();
Serial.print("Gateway IP address is ");
Ethernet.gatewayIP().printTo(Serial);
Serial.println();
Serial.print("DNS IP address is ");
Ethernet.dnsServerIP().printTo(Serial);
Serial.println();
pinMode(3, OUTPUT);
// LED rot (Fehler bei Nachrichtenübertragung)
pinMode(5, OUTPUT);
// LED grün (Nachricht gesendet)
pinMode(6, OUTPUT);
// LED blau (aktiv)
pinMode(A0, INPUT);
// Eingang von Fischertechnik
// Mit 10kOhm Pulldown-Widerstand
}
Avviso.begin();
Avviso.setApiKey("012345678910111213141516171819202122232425");
// Hier den Api-Key einsetzen
Avviso.setApplicationName("Bastis Alarmanlage");
}
void loop() {
EthernetClient client = server.available();
digitalWrite(3, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
if (digitalRead(A0) == 1) {
analogWrite(6, 0);
digitalWrite(5, LOW);
if (DEBUG) Serial.print(analogRead(A0));
if (DEBUG) Serial.print("Sending push notification...");
int returnCode = Avviso.push("Achtung!", "Das Zimmer wurde betreten!!!", 0);
if (returnCode == 200) {
if (DEBUG) Serial.println("OK.");
digitalWrite(3, LOW);
digitalWrite(5, HIGH);
setTime(0,0,0,1,1,2000);
delay(100);
while (analogRead(A0) > 400) {
if (DEBUG) Serial.print("WARTEN...");
if (DEBUG) Serial.print(analogRead(A0));
delay(100);
}
digitalWrite(5, LOW);
if (DEBUG) Serial.println("Fertig!");
}
else {
if (DEBUG) Serial.print("Error. Server returned: ");
setTime(0,0,0,1,1,2000);
if (DEBUG) Serial.print(returnCode);
digitalWrite(3, HIGH);
if (DEBUG) Serial.print(analogRead(A0));
delay(100);
}
}
else {
Serial.println("Letztes Betreten: ");
Serial.print(hour());
Serial.print(" : ");
Serial.print(minute());
Serial.print(" : ");
Serial.print(second());
Serial.println(" ");
Serial.println("");
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == 'n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
// add a meta refresh tag, so the browser pulls again every 1 second:
client.println("<meta http-equiv="refresh" content="1">");
// output the value of each analog input pin
client.println("<font size=7>");
client.print("Letztes Betreten vor ");
client.print(hour());
client.print(" : ");
client.print(minute());
client.print(" : ");
client.print(second());
client.print(" ");
client.println("</font><br>");
client.println("<font size=5>");
client.print(" Alarmanlage by Basti ");
client.println("</font><br>");
client.println("<br />");
client.println("</html>");
break;
}
if (c == 'n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != 'r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disonnected");
}
delay(100);
aktivZaehler = aktivZaehler +1;
if (aktivZaehler == 30) {
analogWrite(6, 255);
delay(20);
analogWrite(6, 0);
aktivZaehler = 0;
}
}
}