Komplettes Sketch (inkl. Litermessung im Wasserfass mittels Ultraschall-Distanzmessung, jedoch noch deaktiviert):
/*
* Klimadaten messen und an SQL übermitteln
*/
#include <WiServer.h>
#include <SHT1x.h>
#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2
// Pin fuer Ultraschallsignal ist digital 2
const int pingPin = 2;
const int pumpePin = 3;
// Specify data and clock connections and instantiate SHT1x object
#define dataPin 6
#define clockPin 7
SHT1x sht1x(dataPin, clockPin);
long cm = 0;
float liter = 0;
float akkuSpannung = 0;
float leistung = 0;
float temp_c = 0;
float humidity = 0;
char buffer[256];
// Hier wird die Fasshoehe in cm angegeben
float fassHoehe = 80;
// Hier wird der Fassdurchmesser in cm angegeben
float fassDurchmesser = 40;
long timer = 599900;
// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {192,168,1,2}; // IP address of WiShield
unsigned char gateway_ip[] = {192,168,1,1}; // router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0}; // subnet mask for the local network
const prog_char ssid[] PROGMEM = {"HOMENET"}; // max 32 bytes
unsigned char security_type = 1; // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2
// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"12345678"}; // max 64 characters
// WEP 128-bit keys
// sample HEX keys
prog_uchar wep_keys[] PROGMEM = { 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, // Key 0
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 1
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 2
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // Key 3
};
// setup the wireless mode
// infrastructure - connect to AP
// adhoc - connect to another WiFi device
unsigned char wireless_mode = WIRELESS_MODE_INFRA;
unsigned char ssid_len;
unsigned char security_passphrase_len;
// End of wireless configuration parameters ----------------------------------------
// This is our page serving function that generates web pages
boolean sendMyPage(char* URL) {
// Check if the requested URL matches "/"
if (strcmp(URL, "/") == 0 || (URL[0] == '/' && URL[1] == '0' && URL[2] == '?')) {
// Use WiServer's print and println functions to write out the page content
WiServer.print("<html><body>Wassermenge: ");
//WiServer.print(ping());
WiServer.print(" Liter");
WiServer.print("
Akku: ");
WiServer.print(akku());
WiServer.print(" Volt.
");
WiServer.print("Temperatur: ");
WiServer.print(temp());
WiServer.print(" Grad.
");
WiServer.print("Feuchte: ");
WiServer.print(humidityFunc());
WiServer.print(" Prozent.
");
pumpe(false);
WiServer.print("
");
WiServer.print("
");
WiServer.print("<form method=\"get\" action=\"1\"><input type=\"submit\" name=\"pumpe\" value=\"Pumpe ein\"></input></form>");
WiServer.print("</body></html>");
// URL was recognized
return true;
}
if (URL[0] == '/' && URL[1] == '1' && URL[2] == '?') {
// Use WiServer's print and println functions to write out the page content
WiServer.print("<html><body>Wassermenge: ");
//WiServer.print(ping());
WiServer.print(" Liter");
WiServer.print("
Akku: ");
WiServer.print(akku());
WiServer.print(" Volt.
");
WiServer.print("Temperatur: ");
WiServer.print(temp());
WiServer.print(" Grad.
");
WiServer.print("Feuchte: ");
WiServer.print(humidityFunc());
WiServer.print(" Prozent.
");
pumpe(true);
WiServer.print("
");
WiServer.print("
");
WiServer.print("<form method=\"get\" action=\"0\"><input type=\"submit\" name=\"pumpe\" value=\"Pumpe aus\"></input></form>");
WiServer.print("</body></html>");
// URL was recognized
return true;
}
// URL not found
return false;
}
void feedData()
{
//float postWasser = ping();
//Serial.println(postWasser);
//int postWasser1 = (postWasser - (int)postWasser) * 100;
float postAkku = akku();
int postAkku1 = (postAkku - (int)postAkku) * 100;
float postTemp = temp();
int postTemp1 = (postTemp - (int)postTemp) * 100;
float postFeuchte = humidityFunc();
int postFeuchte1 = (postFeuchte - (int)postFeuchte) * 100;
sprintf(buffer, "POST &wasser=%0d.%d&akku=%0d.%d&solar=%0d.%d&feuchte=%0d.%d& HTTP/1.1", 0, 0, (int)postAkku, postAkku1, (int)postTemp, postTemp1, (int)postFeuchte, postFeuchte1);
WiServer.print(buffer);
Serial.println(buffer);
}
void pumpe(boolean pumpe)
{
pinMode(pumpePin, OUTPUT);
if (pumpe == true) {
digitalWrite(pumpePin, HIGH);
}
if (pumpe == false) {
digitalWrite(pumpePin, LOW);
}
}
float akku() {
akkuSpannung = analogRead(5) * 0.0049;
akkuSpannung = 10837 * (akkuSpannung / 1002);
return akkuSpannung;
}
float solar() {
leistung = (sq(analogRead(4) * 0.0049))/0.32265;
return leistung;
}
// IP Address for arduino.andres.li
uint8 ip[] = {217,26,52,28};
char hostName[] = "arduino.andres.li\nConnection: close";
char url[] = "/XXXXXXX.php?";
// A request that POSTS data to Andres.li
POSTrequest postAndres(ip, 80, hostName, url, feedData);
// Function that prints data from the server
void printData(char* data, int len) {
// Print the data returned by the server
// Note that the data is not null-terminated, may be broken up into smaller packets, and
// includes the HTTP header.
while (len-- > 0) {
Serial.print(*(data++));
}
}
void setup() {
// Initialize WiServer and have it use the sendMyPage function to serve pages
WiServer.init(sendMyPage);
// Enable Serial output and ask WiServer to generate log messages (optional)
Serial.begin(57600);
WiServer.enableVerboseMode(true);
postAndres.setReturnFunc(printData);
}
void loop(){
// Run WiServer
WiServer.server_task();
if (timer >= 50000) { // 50000 entspricht ca. einem Intervall von 5 Minuten
timer = 0;
postAndres.submit();
Serial.print("IP Update");
}
timer = timer +1;
delay(6);
}
long ping () {
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration;
cm = 0;
while (cm == 0) {
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
// convert the time into a distance
cm = microsecondsToCentimeters(duration);
}
liter = cmToLiter(cm);
Serial.print(cm);
Serial.println();
Serial.print(liter);
Serial.println();
return liter;
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
float cmToLiter (long cm)
{
// Formel zur Berechnung der uebrigen Wassermenge in Liter
liter = ((sq(fassDurchmesser / 200)) * 3.141 * ((fassHoehe - cm)/100)) * 1000;
return liter;
}
float temp() {
temp_c = sht1x.readTemperatureC();
return temp_c;
}
float humidityFunc() {
humidity = sht1x.readHumidity();
return humidity;
}