Sketch:
#include "ClickButton.h"
#include <SPI.h>
#include <Ethernet.h>
//ETHERNET-SETTINGS
byte mac[] = { 0x5D, 0xA2, 0xFA, 0x2D, 0x36, 0x3C }; // MAC-Adresse des Arduino
byte ip[] = { 192, 168, 178, 178 }; // IP-Adresse des Arduino
byte gateway[] = { 192, 168, 178, 1 }; // Gateway
byte subnet[] = { 255, 255, 255, 0 }; // SubNet
byte server[] = { 192, 168, 178, 2 }; // IP-Adresse des Servers
EthernetClient client;
char host[] = "localhost"; // Domain
char schreiben[] = "/save.php"; // Pfad zur PHP-Datei zum schreiben
char key[] = "DEINPASSWORTEGALWELCHES"; // Kennwort aus PHP-Datei
char c; // Variable für Rückgabe des Servers
// the LED
const int ledPin = 8;
int ledState = 1;
// the Button
const int buttonPin1 = 3;
ClickButton button1(buttonPin1, LOW, CLICKBTN_PULLUP);
// Arbitrary LED function
int LEDfunction = 0;
void setup()
{
delay(1000);
Serial.begin(9600);
Serial.flush();
delay(200);
Serial.println("Ethernet initialisieren...");
Ethernet.begin(mac, ip);
delay(1000);
pinMode(ledPin,OUTPUT);
// Setup button timers (all in milliseconds / ms)
// (These are default if not set, but changeable for convenience)
button1.debounceTime = 20; // Debounce timer in ms
button1.multiclickTime = 250; // Time limit for multi clicks
button1.longClickTime = 1000; // time until "held-down clicks" register
}
void loop()
{
// Update button state
button1.Update();
// Save click codes in LEDfunction, as click codes are reset at next Update()
if (button1.clicks != 0) LEDfunction = button1.clicks;
// Simply toggle LED on single clicks
// (Cant use LEDfunction like the others here,
// as it would toggle on and off all the time)
if(button1.clicks == 1)
{
ledState = !ledState;
Daten_senden(ledState,ledState);
delay(700);
byte maxReads = 10; //Seconds
while ((maxReads-- > 0) && client.connected()) // Antwort des Servers lesen
{
delay(1000);
while (client.available())
{
char response = client.read();
Serial.print(response);
}
}
client.stop();
Serial.println("Done.");
client.flush();
}
// blink faster if double clicked
if(LEDfunction == 2) ledState = (millis()/500)%2;
// blink even faster if triple clicked
if(LEDfunction == 3) ledState = (millis()/200)%2;
// slow blink (must hold down button. 1 second long blinks)
if(LEDfunction == -1) ledState = (millis()/1000)%2;
// slower blink (must hold down button. 2 second loong blinks)
if(LEDfunction == -2) ledState = (millis()/2000)%2;
// even slower blink (must hold down button. 3 second looong blinks)
if(LEDfunction == -3) ledState = (millis()/3000)%2;
// update the LED
digitalWrite(ledPin,ledState);
}
/******************************
*
Daten an Server schickenn *
*
*******************************/
void Daten_senden(boolean T1, boolean T2)
{
if (client.connect(server, 80)) // Verbindung zum Server aufbauen
{
Serial.println("Verbunden, Sende Daten...");
client.print("GET " + String(schreiben));
Serial.println("GET " + String(schreiben));
client.print("?T1=");
Serial.print("?T1=");
client.print(T1);
Serial.println(T1);
client.print("&T2=");
Serial.print("&T2=");
client.print(T2);
Serial.println(T2);
client.print("&key=" + String(key));
Serial.print("&key=" + String(key));
client.println(" HTTP/1.1");
Serial.println(" HTTP/1.1");
client.print("Host: " + String(host));
Serial.print("Host: " + String(host));
client.println();
Serial.println();
client.println("User-Agent: Arduino");
Serial.println("User-Agent: Arduino");
client.println("Connection: close");
Serial.println("Connection: close");
client.println();
Serial.println();
}
else
{
Serial.println(" ***** VERBINDUNG KANN NICHT HERGESTELLT WERDEN *****");
}
}