Advertisement
Guest User

Untitled

a guest
Sep 11th, 2011
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.46 KB | None | 0 0
  1. #include <Time.h>
  2. #include <DS1307RTC.h>
  3. #include <SPI.h>
  4. #include <Ethernet.h>
  5. #include <Udp.h>
  6. #include <WProgram.h>
  7. #include <Wire.h>
  8.  
  9.  
  10. #define TSL1_FREQ_PIN 2
  11. #define TSL2_FREQ_PIN 3
  12. #define CS_ETHERNET 10
  13. #define CS_SD 4
  14. #define TSL1_SCALING_BITS 32
  15. #define TSL1_SCALING_DEC 2
  16. #define TSL2_SCALING_BITS 2
  17. #define TSL2_SCALING_DEC 2
  18. #define SHIFT_CLOCK 5
  19. #define SHIFT_LATCH 6
  20. #define SHIFT_DATA 7
  21. #define READ_TM 100
  22. #define READ_INT 1000
  23. #define DIVIDE_FACTOR 2
  24.  
  25. byte mac[] = { 0xDE, 0xAD, 0xBe, 0xEF, 0xFE, 0xED };
  26. byte ip[] = { 10, 1, 42, 231 };
  27.  
  28. unsigned int localPort = 8888; // local port to listen for UDP packets
  29.  
  30. byte timeServer[] = { 10, 1, 42, 1 }; // time.nist.gov NTP server
  31.  
  32. const int NTP_PACKET_SIZE= 48; // NTP time stamp is in the first 48 bytes of the message
  33.  
  34. byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
  35.  
  36. Server server(80);
  37.  
  38.  
  39. unsigned long freq = 0;
  40. unsigned long freq2 = 0;
  41. unsigned int lastSample = 0;
  42. unsigned int timeSinceLastSample = 0;
  43. unsigned long tsl1_sens = 1;
  44. unsigned long tsl2_sens = 1;
  45. float tsl1_energy1 = 0;
  46. float tsl1_energy2 = 0;
  47. float tsl2_energy1 = 0;
  48. float tsl2_energy2 = 0;
  49.  
  50. float intTime = 0;
  51. float jouleCounter = 0;
  52. unsigned long kilojouleCounter = 0;
  53.  
  54. unsigned int timeUpdated = 0;
  55.  
  56. // int scaling = 1;
  57.  
  58. void setup() {
  59.  
  60. Serial.begin(9600);
  61. pinMode(CS_ETHERNET, OUTPUT);
  62. pinMode(CS_SD, OUTPUT);
  63. digitalWrite(CS_ETHERNET, HIGH);
  64. digitalWrite(CS_SD, LOW);
  65.  
  66. Ethernet.begin(mac, ip);
  67. server.begin();
  68.  
  69. setSyncProvider(RTC.get);
  70. while(timeStatus()== timeNotSet);
  71.  
  72. // Before execution begins update the Real Time Clock from the NTP server
  73. Udp.begin(localPort);
  74. sendNTPpacket(timeServer); // send an NTP packet to a time server
  75. delay(1000);
  76.  
  77. if ( Udp.available() ) {
  78. Udp.readPacket(packetBuffer,NTP_PACKET_SIZE); // read the packet into the buffer
  79.  
  80. //the timestamp starts at byte 40 of the received packet and is four bytes,
  81. // or two words, long. First, esxtract the two words:
  82. unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
  83. unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
  84. // combine the four bytes (two words) into a long integer
  85. // this is NTP time (seconds since Jan 1 1900):
  86. unsigned long secsSince1900 = highWord << 16 | lowWord;
  87.  
  88. // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
  89. const unsigned long seventyYears = 2208988800UL;
  90. // subtract seventy years:
  91. unsigned long epoch = secsSince1900 - seventyYears;
  92.  
  93. time_t t = epoch;
  94. if(t >0) {
  95.  
  96. RTC.set(t); // set the RTC and the system time to the received value
  97. setTime(t);
  98. }
  99.  
  100. }
  101.  
  102.  
  103.  
  104. pinMode(TSL1_FREQ_PIN, INPUT);
  105. pinMode(TSL2_FREQ_PIN, INPUT);
  106. pinMode(SHIFT_CLOCK, OUTPUT);
  107. pinMode(SHIFT_LATCH, OUTPUT);
  108. pinMode(SHIFT_DATA, OUTPUT);
  109.  
  110. // Write initial values via shift register
  111. update_shift_reg();
  112.  
  113. }
  114.  
  115. void loop() {
  116.  
  117. // Check how long it's been since the last sample was called
  118. timeSinceLastSample = millis() - lastSample;
  119.  
  120. // If longer than READ_INT, take a sample
  121. if (timeSinceLastSample >= READ_INT) {
  122.  
  123. // Serial.println(millis(), DEC);
  124.  
  125. lastSample = millis();
  126.  
  127. // First sensor read here
  128.  
  129. freq = countFreq(TSL1_FREQ_PIN);
  130. tsl1_energy1 = calc_watts_m2((freq * TSL1_SCALING_DEC));
  131. tsl1_energy2 = tsl1_energy1 / tsl1_sens;
  132.  
  133. if (freq < 100) {
  134. // Turn up sensitivity
  135. if (tsl1_energy1 < 0.01) {
  136.  
  137. // Trick next if to think it's already 10 to go straight to 100
  138. tsl1_sens = 10;
  139.  
  140. }
  141.  
  142. if (tsl1_sens == 1) {
  143.  
  144. tsl1_sens = 10;
  145. update_shift_reg();
  146.  
  147. } else if (tsl1_sens == 10) {
  148.  
  149. tsl1_sens = 100;
  150. update_shift_reg();
  151.  
  152. }
  153.  
  154. // If changing sensitivity take another sample
  155. freq = countFreq(TSL1_FREQ_PIN);
  156. tsl1_energy1 = calc_watts_m2((freq * TSL1_SCALING_DEC));
  157. tsl1_energy2 = tsl1_energy1 / tsl1_sens;
  158.  
  159. }
  160.  
  161. if (freq > 1000) {
  162.  
  163. // Turn down scaling
  164. if (tsl1_sens == 10) {
  165.  
  166. tsl1_sens = 1;
  167. update_shift_reg();
  168.  
  169. } else if (tsl1_sens == 100) {
  170.  
  171. tsl1_sens = 10;
  172. update_shift_reg();
  173.  
  174. }
  175.  
  176. // If changing sensitivity take another sample
  177. freq = countFreq(TSL1_FREQ_PIN);
  178. tsl1_energy1 = calc_watts_m2((freq * TSL1_SCALING_DEC));
  179. tsl1_energy2 = tsl1_energy1 / tsl1_sens;
  180.  
  181. }
  182.  
  183. // Update Joule Counter from first sensor
  184. intTime = timeSinceLastSample / 1000;
  185. jouleCounter += tsl1_energy2 * intTime;
  186.  
  187. // Second sensor read here
  188.  
  189. freq2 = countFreq(TSL2_FREQ_PIN);
  190. tsl2_energy1 = calc_watts_m2((freq2 * TSL2_SCALING_DEC));
  191. tsl2_energy2 = tsl2_energy1 / tsl2_sens;
  192.  
  193. if (freq2 < 100) {
  194.  
  195. // Turn up scaling
  196. if (tsl2_energy1 < 0.01) {
  197.  
  198. // Go straight to 100
  199. tsl2_sens = 10;
  200.  
  201. }
  202.  
  203. if (tsl2_sens == 1) {
  204.  
  205. tsl2_sens = 10;
  206. update_shift_reg();
  207.  
  208. } else if (tsl2_sens == 10) {
  209.  
  210. tsl2_sens = 100;
  211. update_shift_reg();
  212.  
  213. }
  214.  
  215. // If changing sensitivity take another sample
  216. freq2 = countFreq(TSL2_FREQ_PIN);
  217. tsl2_energy1 = calc_watts_m2((freq2 * TSL2_SCALING_DEC));
  218. tsl2_energy2 = tsl2_energy1 / tsl2_sens;
  219.  
  220. }
  221.  
  222. if (freq2 > 1000) {
  223.  
  224. // Turn down scaling
  225. if (tsl2_sens == 10) {
  226.  
  227. tsl2_sens = 1;
  228. update_shift_reg();
  229.  
  230. } else if (tsl2_sens == 100) {
  231.  
  232. tsl2_sens = 10;
  233. update_shift_reg();
  234.  
  235. }
  236.  
  237. // If changing sensitivity take another sample
  238. freq2 = countFreq(TSL2_FREQ_PIN);
  239. tsl2_energy1 = calc_watts_m2((freq2 * TSL2_SCALING_DEC));
  240. tsl2_energy2 = tsl2_energy1 / tsl2_sens;
  241.  
  242. }
  243.  
  244. // Serial.print(millis(), DEC);
  245. // Serial.print("\t");
  246. // Serial.print(TSL1_SCALING_DEC, DEC);
  247. // Serial.print("\t");
  248. // Serial.print(tsl1_sens, DEC);
  249. // Serial.print("\t");
  250. // Serial.print(freq, DEC);
  251. // Serial.print("\t");
  252. // Serial.print(tsl1_energy2, DEC);
  253. // Serial.print("\t");
  254. // Serial.print(jouleCounter, DEC);
  255. // Serial.print("\t");
  256. // Serial.print(TSL2_SCALING_DEC, DEC);
  257. // Serial.print("\t");
  258. // Serial.print(tsl2_sens, DEC);
  259. // Serial.print("\t");
  260. // Serial.print(freq2, DEC);
  261. // Serial.print("\t"),
  262. // Serial.print(tsl2_energy2, DEC);
  263. // Serial.print("\n");
  264. }
  265.  
  266. Client client = server.available();
  267.  
  268. if (client) {
  269. // an http request ends with a blank line
  270. boolean currentLineIsBlank = true;
  271. while (client.connected()) {
  272. if (client.available()) {
  273. char c = client.read();
  274. // if you've gotten to the end of the line (received a newline
  275. // character) and the line is blank, the http request has ended,
  276. // so you can send a reply
  277. if (c == '\n' && currentLineIsBlank) {
  278. // send a standard http response header
  279. client.println("HTTP/1.1 200 OK");
  280. client.println("Content-Type: text/html");
  281. client.println();
  282.  
  283. client.println("<html>");
  284. client.println("<head>");
  285. client.println("<title>Pyranometer and UV Meter</title>");
  286. client.println("<meta http-equiv=\"refresh\" content=\"60\"> ");
  287. client.println("</head>");
  288. client.println("<body>");
  289.  
  290. // output the value of the light sensors
  291. client.println("<p>");
  292. client.print("<strong>System Time:</strong> ");
  293. client.print(day());
  294. client.print("/");
  295. client.print(month());
  296. client.print("/");
  297. client.print(year());
  298. client.print(" ");
  299. client.print(hour());
  300. client.print(":");
  301. client.print(minute());
  302. client.print(":");
  303. client.print(second());
  304.  
  305. client.println();
  306. client.print("<br />");
  307.  
  308. client.print("<strong>Current Light Power:</strong> ");
  309. client.print(tsl1_energy2, DEC);
  310. client.print(" w/m<sup>2</sup> - ");
  311. client.print(tsl1_sens, DEC);
  312. client.print("<br />");
  313. client.print("<strong>Accumulated Insolation:</strong> ");
  314. client.print(jouleCounter, DEC);
  315. client.print(" J/m<sup>2</sup>");
  316. client.print("<br />");
  317. client.print("<strong>UV:</strong> ");
  318. client.print(tsl2_energy2, DEC);
  319. client.print(" w/m<sup>2</sup> - ");
  320. client.print(tsl2_sens, DEC);
  321. client.print("</p>");
  322.  
  323. client.println("</body>");
  324. client.println("</html>");
  325.  
  326. break;
  327. }
  328. if (c == '\n') {
  329. // you're starting a new line
  330. currentLineIsBlank = true;
  331. }
  332. else if (c != '\r') {
  333. // you've gotten a character on the current line
  334. currentLineIsBlank = false;
  335. }
  336. }
  337. }
  338. // give the web browser time to receive the data
  339. // delay(1);
  340. // close the connection:
  341. client.stop();
  342.  
  343. }
  344.  
  345.  
  346.  
  347.  
  348. }
  349.  
  350.  
  351.  
  352.  
  353. int countFreq(int pin) {
  354.  
  355. #define SAMPLES 25
  356. unsigned long count = 0;
  357. unsigned long startTime = 0;
  358. unsigned long endTime = 0;
  359. unsigned long cycleDuration = 0;
  360. unsigned long sum = 0;
  361.  
  362. while (count < SAMPLES) {
  363.  
  364. while (digitalRead(pin) != LOW); // Wait for falling edge
  365. while (digitalRead(pin) != HIGH); // Catch rising edge
  366. noInterrupts();
  367. startTime = micros();
  368. interrupts();
  369.  
  370. while (digitalRead(pin) != LOW); // Wait for falling edge
  371. while (digitalRead(pin) != HIGH); // Catch rising edge
  372. noInterrupts();
  373. endTime = micros();
  374. interrupts();
  375.  
  376. if (startTime > endTime) {
  377.  
  378.  
  379. } else {
  380.  
  381. cycleDuration = endTime - startTime;
  382. sum = sum + (1000000 / cycleDuration);
  383. count++;
  384.  
  385. }
  386.  
  387. // Break out early if there's more than 10th of a second between pulses
  388. if (cycleDuration > 50000) {
  389.  
  390. sum = 0;
  391. count = SAMPLES + 1;
  392.  
  393. }
  394.  
  395. }
  396.  
  397. int retfreq = sum / SAMPLES;
  398. return retfreq;
  399.  
  400. }
  401.  
  402. float calc_watts_m2(unsigned long freq) {
  403.  
  404. float uw_cm2 = (float) freq / (float) 7.7;
  405. // Convert to watts per square meter
  406. float w_m2 = uw_cm2 / 100;
  407.  
  408. return(w_m2);
  409.  
  410. }
  411.  
  412. // Updates shift register based on global variables
  413. void update_shift_reg() {
  414.  
  415. unsigned int newRegValue = TSL1_SCALING_BITS + TSL2_SCALING_BITS;
  416.  
  417. if (tsl1_sens == 1) {
  418.  
  419. newRegValue = newRegValue + 128;
  420.  
  421. } else if (tsl1_sens == 10) {
  422.  
  423. newRegValue = newRegValue + 64;
  424.  
  425. } else if (tsl1_sens == 100) {
  426.  
  427. newRegValue = newRegValue + 192;
  428.  
  429. }
  430.  
  431. if (tsl2_sens == 1) {
  432.  
  433. newRegValue = newRegValue + 8;
  434.  
  435. } else if (tsl2_sens == 10) {
  436.  
  437. newRegValue = newRegValue + 4;
  438.  
  439. } else if (tsl2_sens == 100) {
  440.  
  441. newRegValue = newRegValue + 12;
  442.  
  443. }
  444.  
  445. digitalWrite(SHIFT_LATCH, LOW);
  446. shiftOut(SHIFT_DATA, SHIFT_CLOCK, LSBFIRST, newRegValue);
  447. digitalWrite(SHIFT_LATCH, HIGH);
  448.  
  449. // Serial.print("Shift Register Value: ");
  450. // Serial.print(newRegValue, DEC);
  451. // Serial.print("\n");
  452.  
  453. }
  454.  
  455. // send an NTP request to the time server at the given address
  456. unsigned long sendNTPpacket(byte *address)
  457. {
  458. // set all bytes in the buffer to 0
  459. memset(packetBuffer, 0, NTP_PACKET_SIZE);
  460. // Initialize values needed to form NTP request
  461. // (see URL above for details on the packets)
  462. packetBuffer[0] = 0b11100011; // LI, Version, Mode
  463. packetBuffer[1] = 0; // Stratum, or type of clock
  464. packetBuffer[2] = 6; // Polling Interval
  465. packetBuffer[3] = 0xEC; // Peer Clock Precision
  466. // 8 bytes of zero for Root Delay & Root Dispersion
  467. packetBuffer[12] = 49;
  468. packetBuffer[13] = 0x4E;
  469. packetBuffer[14] = 49;
  470. packetBuffer[15] = 52;
  471.  
  472. // all NTP fields have been given values, now
  473. // you can send a packet requesting a timestamp:
  474. Udp.sendPacket( packetBuffer,NTP_PACKET_SIZE, address, 123); //NTP requests are to port 123
  475. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement