source: firmware/InterlockArduino/src/webserver/webserver.ino@ 14249

Last change on this file since 14249 was 14159, checked in by neise, 12 years ago
initial commit
File size: 8.5 KB
Line 
1
2/*
3 Arduino Ethernet for FACT Interlock
4
5 Written by Dominik Neise
6 June 2012
7 TU Dortmund
8
9 The FACT Camera as well as the bias voltage supply for its G-APDs
10 may only be switched on, if the dedicated cooling systems are running.
11 This arduino is connected via optocouplers to some status LEDs,
12 and other coolings system output signals, which show
13 the status of both cooling systems:
14 * water cooling pump
15 - flow meter
16 - level meter
17 * bias crate fans
18 - rotation frequency
19 A simple WebServer is implemented for showing the
20 status and access the actual switches.
21
22 In order to switch on the camera one needs to switch on
23 the water pump first. In case this pump is running, the
24 Camera power suplly (51VDC Agilent) gets an enable signal
25 and will power the camera.
26 In order to enable the bias voltage supply, one needs to swtich on
27 the Bias Crate Fans.
28
29 Circuit:
30 IL_ON : Interlock on : digital input D1
31 FL_OK : level ok : digital input D2
32 FC_OK : current ok : digital input D3
33 FP_EN : pump enabled : digital input D4
34
35 FC_ON : enable pump : digital output D5
36 FC_OFF: disable pump : digital output D6
37 */
38
39#include <SPI.h>
40#include <Ethernet.h>
41#define BUF_LEN 256
42
43// inputs
44int IL_ON = 4;
45int FL_OK = 5;
46int FC_OK = 6;
47int FP_EN = 7;
48 // outputs
49int FC_ON = 2;
50int FC_OFF = 3;
51
52unsigned long last_time = millis();
53unsigned long current_time = millis();
54unsigned int fc_on_time = 0; // time in ms, for FC_ON to be HIGH
55unsigned int fc_off_time = 0;// time in ms, for FC_OFF to be HIGH
56
57byte mac[] = { 0xFA, 0xC7, 0xFC, 0xB1, 0x00, 0x01 };
58IPAddress ip(129,217,160,65);
59
60// Initialize the Ethernet server library
61// with the IP address and port you want to use
62// (port 80 is default for HTTP):
63EthernetServer server(80);
64
65// ethernet receive buffer
66
67char eth_rx_buf[BUF_LEN];
68unsigned int eth_rd = 0;
69unsigned int eth_wr = 0;
70byte eth_buff_full = false;
71
72
73
74
75void setup() {
76 // Open serial communications and wait for port to open:
77 Serial.begin(9600);
78 while (!Serial) {
79 ; // wait for serial port to connect. Needed for Leonardo only
80 }
81
82
83 // start the Ethernet connection and the server:
84 Ethernet.begin(mac, ip);
85 server.begin();
86 Serial.print("server is at ");
87 Serial.println(Ethernet.localIP());
88
89 pinMode( IL_ON, INPUT );
90 pinMode( FL_OK, INPUT );
91 pinMode( FC_OK, INPUT );
92 pinMode( FP_EN, INPUT );
93
94 pinMode( FC_ON, OUTPUT );
95 pinMode( FC_OFF,OUTPUT );
96
97}
98
99
100void loop() {
101 Check_n_Set_FC_ON_OFF();
102 unsigned long before = millis();
103 if (Check_for_clients() )
104 {
105 unsigned long duration = millis() - before;
106
107 Serial.println( duration, DEC);
108 }
109
110 delay(300);
111}
112/////////////////////////////////////////////////////////////////////////////////////////////
113int Check_for_clients()
114{
115
116 int got_client = 0;
117 // listen for incoming clients
118 EthernetClient client = server.available();
119 if (client) {
120 got_client = 1;
121 Serial.println("new client");
122 // an http request ends with a blank line
123 boolean currentLineIsBlank = true;
124 boolean get_http_found = false;
125 String eth = "";
126
127 while (client.connected()) {
128 if (client.available()) {
129 char c = client.read();
130 if (!get_http_found)
131 {
132 eth += c;
133// Serial.write(c);
134 //Serial.println(eth.length(), DEC);
135 if ( eth.indexOf('\n') != -1 )
136 {
137 if (eth.indexOf(String("GET /")) !=-1 && eth.indexOf(String("HTTP")) !=-1 )
138 {
139 get_http_found = true;
140 if (eth.indexOf(String("D2on=")) !=-1){
141 Serial.println("found D2on");
142 digitalWrite( 2, HIGH);
143 fc_on_time = 3000;
144 }
145
146 if (eth.indexOf(String("D2off=")) !=-1)
147 {
148 Serial.println("found D2off");
149 digitalWrite( 3, HIGH);
150 fc_off_time = 500;
151 }
152
153 Serial.println("found it");
154 }
155 }
156 }
157
158 // if you've gotten to the end of the line (received a newline
159 // character) and the line is blank, the http request has ended,
160 // so you can send a reply
161 if (c == '\n' && currentLineIsBlank) {
162 // send a standard http response header
163 html_header( &client );
164 html_page( &client );
165 html_footer( &client );
166 break; // this breaks out of the while loop!!!
167 }
168 if (c == '\n') {
169 // you're starting a new line
170 currentLineIsBlank = true;
171 }
172 else if (c != '\r') {
173 // you've gotten a character on the current line
174 currentLineIsBlank = false;
175 }
176 }
177 }
178 // give the web browser time to receive the data
179 delay(1);
180 // close the connection:
181 client.stop();
182 Serial.println("client disonnected");
183 }
184 return got_client;
185}
186/////////////////////////////////////////////////////////////////////////////////////////////
187
188//////////// HTML Generators ////////////////////////////////////////////////////////////////
189void html_header(EthernetClient *client)
190{
191 // send a standard http response header
192 client->println("HTTP/1.1 200 OK");
193 client->println("Content-Type: text/html");
194 client->println("Connnection: close");
195 client->println();
196 client->println("<!DOCTYPE HTML>");
197 client->println("<html><head>");
198
199 // add a meta refresh tag, so the browser pulls again every 5 seconds:
200 client->print("<meta http-equiv=\"refresh\" content=\"5\">");
201
202 client->println("<title>FACT camera power interlock control</title></head><body>");
203 client->println("<FORM NAME=\"Form\">");
204}
205
206void send_ana_outs_example( EthernetClient *client) {
207 // output the value of each analog input pin
208 for (int analogChannel = 0; analogChannel < 6; analogChannel++)
209 {
210 client->print("analog input ");
211 client->print(analogChannel);
212 client->print(" is ");
213 client->print(analogRead(analogChannel));
214 client->println("<br />");
215 }
216}
217
218void send_dig_ins( EthernetClient *client)
219{
220 for (int ch = 4; ch < 9; ch++)
221 {
222 client->print("digital input ");
223 client->print(ch);
224 client->print(" is ");
225 if (digitalRead(ch))
226 {
227 client->print("<font color=\"green\" size=20>HIGH</font>");
228 }
229 else
230 {
231 client->print("<font color=\"red\" size=20>LOW</font>");
232 }
233
234
235
236 client->println("<br />");
237 }
238
239}
240
241void html_page( EthernetClient *client)
242{
243 client->println("<P><INPUT TYPE=SUBMIT NAME=\"D2on\" VALUE=\"Enable Pump\"></P> Switches D2 HIGH for 3 seconds <br />");
244 client->println("<P><INPUT TYPE=SUBMIT NAME=\"D2off\" VALUE=\"Disable Pump\"></P> Switches D3 HIGH for 0.5 seconds <br />");
245
246 send_dig_ins( client );
247
248 //send_ana_outs_example( client );
249}
250
251void html_footer(EthernetClient *client)
252{
253 client->print("</FORM></body></html>\n");
254}
255
256
257//////////// HTML Generators ---- END /////////////////////////////////////////////////////////
258
259void
260Check_n_Set_FC_ON_OFF()
261{
262 Serial.print("FC ON TIME");
263 Serial.println(fc_on_time);
264 Serial.print("FC OFF TIME");
265 Serial.println(fc_off_time);
266
267 unsigned long since_last = millis() - last_time;
268
269 if (since_last == 0)
270 {
271 // not even a milisecond passed by
272 // strange, should not happen
273 }
274 else if (since_last >= fc_on_time)
275 {
276 // FC_ON was long enough HIGH
277 fc_on_time = 0;
278 }
279 else if (since_last < fc_on_time)
280 {
281 // fc_on_time is decreased
282 fc_on_time -= since_last;
283 }
284
285 if (fc_on_time == 0)
286 {
287 digitalWrite( FC_ON, LOW);
288 }
289 else
290 {
291 //digitalWrite( FC_ON, HIGH);
292 }
293
294 if (since_last == 0)
295 {
296 // not even a milisecond passed by
297 // strange, should not happen
298 }
299 else if (since_last >= fc_off_time)
300 {
301 // FC_OFF was long enough HIGH
302 fc_off_time = 0;
303 }
304 else if (since_last < fc_off_time)
305 {
306 // fc_on_time is decreased
307 fc_off_time -= since_last;
308 }
309
310 if (fc_off_time == 0)
311 {
312 digitalWrite( FC_OFF, LOW);
313 }
314 else
315 {
316 //digitalWrite( FC_OFF, HIGH);
317 }
318
319 last_time = millis();
320}
321/////////////////////////////////////////////////////////////////////////////////////////////
322
323
324///////////////Ethernet input buffer////////////////////////////////////////////////////////
325int eth_buf_add(char c)
326{
327 if (!eth_buff_full)
328 {
329 eth_rx_buf[eth_wr] = c;
330 eth_wr = (eth_wr+1)%BUF_LEN;
331 if (eth_wr == eth_rd) // buffer full
332 {
333 eth_buff_full = true;
334 }
335 return true;
336 }
337 else
338 {
339 return false;
340 }
341}
342
343inline int eth_len()
344{
345 return (eth_wr-eth_rd) % BUF_LEN;
346}
347
348char * buf[16];
349
350
351
352
353
354/////////////////////////////////////////////////////////////////////////////////////////////
355
Note: See TracBrowser for help on using the repository browser.