Hardware/dInfrastructure/qPowerMeasurment: PowerSensingCode.ino

File PowerSensingCode.ino, 14.8 KB (added by pavan717, 11 years ago)
Line 
1/*
2 Web Server
3
4 A simple web server that shows the value of the analog input pins.
5 using an Arduino Wiznet Ethernet shield.
6
7 Circuit:
8 * Ethernet shield is attached to the top of the Power Sensing Platform and Arduino is on the bottom of the Power Sensing Platform.
9 * Toroid current sensors are inputed through the Power Sensing Platform and are outputed through the Arduino analog pins A0 through A11.
10 * The ouput reading from the Arduino analog pins is a power reading measured in Watts.
11
12 The Webserver Template
13 Created 18 Dec 2009
14 By David A. Mellis
15 Modified 9 Apr 2012
16 By Tom Igoe
17 Modified 12 Jun 2013
18 By: Pavan A. Desai
19
20 */
21
22#include <SPI.h>
23#include <Ethernet.h>
24
25// Enter a MAC address for your controller below.
26// Newer Ethernet shields have a MAC address printed on a sticker on the shield
27 byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x05, 0x52 }; // Mac address from Ethernet shield used for this project.
28
29 EthernetClient client; //Creates a client which can connect to a specified internet IP address and port (defined in the client.connect() function).
30
31// Initialize the Ethernet client library
32// with the IP address and port of the server
33// that you want to connect to (port 80 is default for HTTP):
34
35 EthernetServer server(80); // Port 80 default for HTTP
36
37 void setup() {
38// Open serial communications and wait for port to open:
39 Serial.begin(9600); // Used for communication between the Arduino board and a computer or other devices, for more info please visit the Arduino Webservser website.
40
41// Start the Ethernet connection:
42 /*
43 DHCP should auto assign the Arduino an IP Address
44 DHCP-based IP printer
45 This sketch uses the DHCP extensions to the Ethernet library
46 to get an IP address via DHCP and print the address obtained.
47 using an Arduino Wiznet Ethernet shield.
48 created 12 April 2011
49 modified 9 Apr 2012
50 By Tom Igoe
51 modified 26 Jun 2013
52 By Pavan Desai
53 */
54 if (Ethernet.begin(mac) == 0) {
55 Serial.println("Failed to configure Ethernet using DHCP");
56 // no point in carrying on, so do nothing forevermore:
57 for(;;)
58 ;
59 }
60 // print your local IP address:
61 Serial.print("My IP Address: ");
62 for (byte thisByte = 0; thisByte < 4; thisByte++) {
63 // print the value of each byte of the IP address:
64 Serial.print(Ethernet.localIP()[thisByte], DEC);
65 Serial.print(".");
66 }
67 Serial.println(); // The IP address assign to the Arduino by the webserver in Orbit Network is 10.41.250.19.
68
69 }
70
71
72
73 void loop() {
74
75 analogReference(INTERNAL1V1); //Built in 1.1 voltage
76
77 EthernetClient client = server.available(); // Try to get client
78 if (client) { // Checks to see if there is a client.
79 Serial.println("new client");
80 // An http request ends with a blank line
81 boolean currentLineIsBlank = true;
82 while (client.connected()) {
83 if (client.available()) { // Client data available to read
84 char c = client.read(); // Read 1 byte (character) from client
85 // Last line of client request is blank and ends with \n
86 // Respond to client only after last line received
87 Serial.write(c);
88 // if you've gotten to the end of the line (received a newline
89 // character) and the line is blank, the http request has ended,
90 // so you can send a reply
91 if (c == '\n' && currentLineIsBlank) {
92 // send a standard http response header
93
94 client.println("HTTP/1.1 200 OK");
95 client.println("Content-Type: text/xml"); //Changed from html to xml
96 client.println("Connection: close");
97
98 //add a meta refresh tag, so the browser pulls again every 5 seconds:
99 // client.println("<meta http-equiv=\"refresh\" content=\"5\">");
100 client.println("Refresh: 1");
101
102 client.println();
103 client.println("<?xml version=\"1.0\"?>"); // Start of the XML output code, for more info on XML visit W3Schools website
104
105 //Output the value of each analog input pin
106 client.println("<Data>"); // The XML Root Element
107 for (int analogChannel = 0; analogChannel < 12; analogChannel++) //Only using the first 12 analog pins out of 16 total.
108 {
109 int sensorReading = analogRead(analogChannel); // analogRead reads the reading at the analog channel (pins A0-A11) and outputs a resolution reading between 0 to 1023
110 // the Arduino has a 10-bit analog to digital converter so it will map input voltages between 0 and 5 volts into
111 // integer values between 0 and 1023.
112 client.println("<Reading>"); // The XML child element start and end
113 client.println("<AnalogPin>");
114 client.print(analogChannel);
115 client.println("</AnalogPin>");
116 client.println("<Output>");
117
118 // If there is no input signal given to any Arduino pin, the output will still display a resolution reading due to the internal circuit noise.
119 // The following if statment will eliminate that noise, if the analog pin has no signal in the input then the if-statement will output a "0."
120 if (sensorReading == 0) //Displays a "0" for any analog pin that has to signal applied to its input
121 {client.print(sensorReading);} // prints the reading
122 else
123 {
124 if (analogChannel == 0)
125 {double d= 0.0;
126 d = static_cast<double>(sensorReading);
127 double power = 42 * square((((((((sensorReading/1024.0)*1100.0)/4.700)+2.468)/42)*40)/0.02623)/1000);
128 client.print(power); // Prints out Power Reading in Watts
129
130 //double voltage = (((sensorReading/1024.0)*1100.0)/4.700+2.468); //Prints out Voltage Reading
131 //client.print(voltage); // Prints out Voltage Reading in Volts
132 }
133 else if (analogChannel == 1)
134 {double d= 0.0;
135 d = static_cast<double>(sensorReading);
136 double power = 42 * square((((((((sensorReading/1024.0)*1100.0)/4.700)+3.784)/42)*40)/0.02623)/1000);
137 client.print(power); // Prints out Power Reading in Watts
138
139 //double voltage = (((sensorReading/1024.0)*1100.0)/4.700+3.784);
140 //client.print(voltage); // Prints out Voltage Reading in Volts
141 }
142 else if (analogChannel == 2)
143 {double d= 0.0;
144 d = static_cast<double>(sensorReading);
145 double power = 42 * square((((((((sensorReading/1024.0)*1100.0)/4.700)+3.841)/42)*40)/0.02623)/1000);
146 client.print(power); // Prints out Power Reading in Watts
147
148 //double voltage = (((sensorReading/1024.0)*1100.0)/4.700+3.841);
149 //client.print(voltage); // Prints out Voltage Reading in Volts
150 }
151 else if (analogChannel == 3)
152 {double d= 0.0;
153 d = static_cast<double>(sensorReading);
154 double power = 42 * square((((((((sensorReading/1024.0)*1100.0)/4.700)+3.208)/42)*40)/0.02623)/1000);
155 client.print(power); // Prints out Power Reading in Watts
156
157 //double voltage = (((sensorReading/1024.0)*1100.0)/4.700+3.208);
158 //client.print(voltage); // Prints out Voltage Reading in Volts
159 }
160 else if (analogChannel == 4)
161 {double d= 0.0;
162 d = static_cast<double>(sensorReading);
163 double power = 42 * square((((((((sensorReading/1024.0)*1100.0)/4.700)+4.443)/42)*40)/0.02623)/1000);
164 client.print(power); // Prints out Power Reading in Watts
165
166 //double voltage = (((sensorReading/1024.0)*1100.0)/4.700+4.443);
167 //client.print(voltage); // Prints out Voltage Reading in Volts
168 }
169 else if (analogChannel == 5)
170 {double d= 0.0;
171 d = static_cast<double>(sensorReading);
172 double power = 42 * square((((((((sensorReading/1024.0)*1100.0)/4.700)+2.643)/42)*40)/0.02623)/1000);
173 client.print(power); // Prints out Power Reading in Watts
174
175 //double voltage = (((sensorReading/1024.0)*1100.0)/4.700+2.643);
176 //client.print(voltage); // Prints out Voltage Reading in Volts
177 }
178 else if (analogChannel == 6)
179 {double d= 0.0;
180 d = static_cast<double>(sensorReading);
181 double power = 42 * square((((((((sensorReading/1024.0)*1100.0)/4.700)+4.347)/42)*40)/0.02623)/1000);
182 client.print(power); // Prints out Power Reading in Watts
183
184 //double voltage = (((sensorReading/1024.0)*1100.0)/4.700+4.347);
185 //client.print(voltage); // Prints out Voltage Reading in Volts
186 }
187 else if (analogChannel == 7)
188 {double d= 0.0;
189 d = static_cast<double>(sensorReading);
190 double power = 42 * square((((((((sensorReading/1024.0)*1100.0)/4.700)+3.093)/42)*40)/0.02623)/1000);
191 client.print(power); // Prints out Power Reading in Watts
192
193 //double voltage = (((sensorReading/1024.0)*1100.0)/4.700+3.093);
194 //client.print(voltage); // Prints out Voltage Reading in Volts
195 }
196 else if (analogChannel == 8)
197 {double d= 0.0;
198 d = static_cast<double>(sensorReading);
199 double power = 42 * square((((((((sensorReading/1024.0)*1100.0)/4.700)+3.309)/42)*40)/0.02623)/1000);
200 client.print(power); // Prints out Power Reading in Watts
201
202 //double voltage = (((sensorReading/1024.0)*1100.0)/4.700+3.309);
203 //client.print(voltage); // Prints out Voltage Reading in Volts
204 }
205 else if (analogChannel == 9)
206 {double d= 0.0;
207 d = static_cast<double>(sensorReading);
208 double power = 42 * square((((((((sensorReading/1024.0)*1100.0)/4.700)+2.964)/42)*40)/0.02623)/1000);
209 client.print(power); // Prints out Power Reading in Watts
210
211 //double voltage = (((sensorReading/1024.0)*1100.0)/4.700+2.964);
212 //client.print(voltage); // Prints out Voltage Reading in Volts
213 }
214 else if (analogChannel == 10)
215 {double d= 0.0;
216 d = static_cast<double>(sensorReading);
217 double power = 42 * square((((((((sensorReading/1024.0)*1100.0)/4.700)+3.330)/42)*40)/0.02623)/1000);
218 client.print(power); // Prints out Power Reading in Watts
219
220 //double voltage = (((sensorReading/1024.0)*1100.0)/4.700+3.330);
221 //client.print(voltage); // Prints out Voltage Reading in Volts
222 }
223 else if (analogChannel == 11)
224 {double d= 0.0;
225 d = static_cast<double>(sensorReading);
226 double power = 42 * square((((((((sensorReading/1024.0)*1100.0)/4.700)+2.935)/42)*40)/0.02623)/1000);
227 client.print(power); // Prints out Power Reading in Watts
228
229 //double voltage = (((sensorReading/1024.0)*1100.0)/4.700+2.935);
230 //client.print(voltage); // Prints out Voltage Reading in Volts
231 }
232 }
233 client.println("</Output>"); //The XML Child Element tags end
234 client.println("</Reading>");
235 }
236 client.println("</Data>"); // The XML Root Element Tag ends
237 break;
238 }
239
240 if (c == '\n') {
241 // you're starting a new line
242 currentLineIsBlank = true;
243 }
244 else if (c != '\r') {
245 // you've gotten a character on the current line
246 currentLineIsBlank = false;
247 }
248 }
249 }
250 // give the web browser time to receive the data
251 delay(1);
252 // close the connection:
253 client.stop();
254 Serial.println("client disonnected");
255 }
256}
257
258