Browse code

Create stromzaehler.pde

Stefan Höhn authored on25/08/2013 06:30:27
Showing1 changed files

1 1
new file mode 100644
... ...
@@ -0,0 +1,121 @@
1
+/*
2
+  Web  Server + Sromzähler
3
+ 
4
+ A simple web server that shows the value of a Sromzähler Voltcraft DPM1L32-D.
5
+ using an Arduino Wiznet Ethernet shield. 
6
+ 
7
+ Circuit:
8
+ * Ethernet shield attached to pins 10, 11, 12, 13
9
+ * Digital Input =22
10
+ 
11
+ created 18 Dec 2009
12
+ by David A. Mellis
13
+ modified 4 Sep 2010
14
+ by Tom Igoe
15
+ 
16
+ und das Script von
17
+ http://blog.elektrowolle.de/2011/07/26/s0-messwerterfassung-von-stromzahlern/
18
+ 
19
+ Zusammengemixt am 08.02.2012 von Stefan Höhn http://www.untergang.de
20
+  
21
+ */
22
+
23
+#include <SPI.h>
24
+#include <Ethernet.h>
25
+
26
+// Enter a MAC address and IP address for your controller below.
27
+// The IP address will be dependent on your local network:
28
+byte mac[] = { 0x90 , 0xA2, 0xDA, 0x00, 0x6C, 0x8E };
29
+byte ip[] = { 192,168,1, 6 };
30
+
31
+//zähler <-
32
+const byte counterPin = 22;
33
+unsigned long millisBetween;
34
+unsigned long lastMillis;
35
+byte lastState;
36
+// zähler ->
37
+
38
+// Initialize the Ethernet server library
39
+// with the IP address and port you want to use 
40
+// (port 80 is default for HTTP):
41
+Server server(80);
42
+
43
+void setup()
44
+{
45
+  // start the Ethernet connection and the server:
46
+  Ethernet.begin(mac, ip);
47
+  server.begin();
48
+ // <-- Zähler
49
+       pinMode(counterPin, INPUT);
50
+     digitalWrite(counterPin, LOW);
51
+     millisBetween = 0;
52
+     lastMillis = 0;
53
+     lastState = 0;
54
+  
55
+  Serial.begin(9600);
56
+  // Zähler -->
57
+}
58
+
59
+void loop()
60
+{
61
+  //<--Zähler
62
+  
63
+ unsigned char bitMaskToSend = 0;
64
+  unsigned long time = millis();
65
+ 
66
+     byte val = digitalRead(counterPin);
67
+     if (val == HIGH && lastState == LOW) {
68
+       millisBetween = time-lastMillis;
69
+       lastMillis = time;
70
+       bitSet(bitMaskToSend,1);
71
+     }
72
+     lastState = val;
73
+ 
74
+     unsigned long dataToWrite = millisBetween;
75
+     if (bitRead(bitMaskToSend,1)) {
76
+       Serial.print((char)('A'));
77
+       Serial.println(1800000/dataToWrite);
78
+  
79
+  } 
80
+  
81
+  
82
+  // Zähler-->
83
+  
84
+  
85
+  
86
+  // listen for incoming clients
87
+  Client client = server.available();
88
+  if (client) {
89
+    // an http request ends with a blank line
90
+    boolean currentLineIsBlank = true;
91
+    while (client.connected()) {
92
+      if (client.available()) {
93
+        char c = client.read();
94
+        // if you've gotten to the end of the line (received a newline
95
+        // character) and the line is blank, the http request has ended,
96
+        // so you can send a reply
97
+        if (c == '\n' && currentLineIsBlank) {
98
+          // send a standard http response header
99
+          client.println("HTTP/1.1 200 OK");
100
+          client.println("Content-Type: text/html");
101
+          client.println();
102
+          client.print(1800000/dataToWrite);
103
+          client.print("Watt");   
104
+         break;
105
+        }
106
+        if (c == '\n') {
107
+          // you're starting a new line
108
+          currentLineIsBlank = true;
109
+        } 
110
+        else if (c != '\r') {
111
+          // you've gotten a character on the current line
112
+          currentLineIsBlank = false;
113
+        }
114
+      }
115
+    }
116
+    // give the web browser time to receive the data
117
+    delay(1);
118
+    // close the connection:
119
+    client.stop();
120
+  }
121
+}