Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Time.h>
- #include <DS1307RTC.h>
- #include <SPI.h>
- #include <Ethernet.h>
- #include <Udp.h>
- #include <WProgram.h>
- #include <Wire.h>
- #define TSL1_FREQ_PIN 2
- #define TSL2_FREQ_PIN 3
- #define CS_ETHERNET 10
- #define CS_SD 4
- #define TSL1_SCALING_BITS 32
- #define TSL1_SCALING_DEC 2
- #define TSL2_SCALING_BITS 2
- #define TSL2_SCALING_DEC 2
- #define SHIFT_CLOCK 5
- #define SHIFT_LATCH 6
- #define SHIFT_DATA 7
- #define READ_TM 100
- #define READ_INT 1000
- #define DIVIDE_FACTOR 2
- byte mac[] = { 0xDE, 0xAD, 0xBe, 0xEF, 0xFE, 0xED };
- byte ip[] = { 10, 1, 42, 231 };
- unsigned int localPort = 8888; // local port to listen for UDP packets
- byte timeServer[] = { 10, 1, 42, 1 }; // time.nist.gov NTP server
- const int NTP_PACKET_SIZE= 48; // NTP time stamp is in the first 48 bytes of the message
- byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
- Server server(80);
- unsigned long freq = 0;
- unsigned long freq2 = 0;
- unsigned int lastSample = 0;
- unsigned int timeSinceLastSample = 0;
- unsigned long tsl1_sens = 1;
- unsigned long tsl2_sens = 1;
- float tsl1_energy1 = 0;
- float tsl1_energy2 = 0;
- float tsl2_energy1 = 0;
- float tsl2_energy2 = 0;
- float intTime = 0;
- float jouleCounter = 0;
- unsigned long kilojouleCounter = 0;
- unsigned int timeUpdated = 0;
- // int scaling = 1;
- void setup() {
- Serial.begin(9600);
- pinMode(CS_ETHERNET, OUTPUT);
- pinMode(CS_SD, OUTPUT);
- digitalWrite(CS_ETHERNET, HIGH);
- digitalWrite(CS_SD, LOW);
- Ethernet.begin(mac, ip);
- server.begin();
- setSyncProvider(RTC.get);
- while(timeStatus()== timeNotSet);
- // Before execution begins update the Real Time Clock from the NTP server
- Udp.begin(localPort);
- sendNTPpacket(timeServer); // send an NTP packet to a time server
- delay(1000);
- if ( Udp.available() ) {
- Udp.readPacket(packetBuffer,NTP_PACKET_SIZE); // read the packet into the buffer
- //the timestamp starts at byte 40 of the received packet and is four bytes,
- // or two words, long. First, esxtract the two words:
- unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
- unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
- // combine the four bytes (two words) into a long integer
- // this is NTP time (seconds since Jan 1 1900):
- unsigned long secsSince1900 = highWord << 16 | lowWord;
- // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
- const unsigned long seventyYears = 2208988800UL;
- // subtract seventy years:
- unsigned long epoch = secsSince1900 - seventyYears;
- time_t t = epoch;
- if(t >0) {
- RTC.set(t); // set the RTC and the system time to the received value
- setTime(t);
- }
- }
- pinMode(TSL1_FREQ_PIN, INPUT);
- pinMode(TSL2_FREQ_PIN, INPUT);
- pinMode(SHIFT_CLOCK, OUTPUT);
- pinMode(SHIFT_LATCH, OUTPUT);
- pinMode(SHIFT_DATA, OUTPUT);
- // Write initial values via shift register
- update_shift_reg();
- }
- void loop() {
- // Check how long it's been since the last sample was called
- timeSinceLastSample = millis() - lastSample;
- // If longer than READ_INT, take a sample
- if (timeSinceLastSample >= READ_INT) {
- // Serial.println(millis(), DEC);
- lastSample = millis();
- // First sensor read here
- freq = countFreq(TSL1_FREQ_PIN);
- tsl1_energy1 = calc_watts_m2((freq * TSL1_SCALING_DEC));
- tsl1_energy2 = tsl1_energy1 / tsl1_sens;
- if (freq < 100) {
- // Turn up sensitivity
- if (tsl1_energy1 < 0.01) {
- // Trick next if to think it's already 10 to go straight to 100
- tsl1_sens = 10;
- }
- if (tsl1_sens == 1) {
- tsl1_sens = 10;
- update_shift_reg();
- } else if (tsl1_sens == 10) {
- tsl1_sens = 100;
- update_shift_reg();
- }
- // If changing sensitivity take another sample
- freq = countFreq(TSL1_FREQ_PIN);
- tsl1_energy1 = calc_watts_m2((freq * TSL1_SCALING_DEC));
- tsl1_energy2 = tsl1_energy1 / tsl1_sens;
- }
- if (freq > 1000) {
- // Turn down scaling
- if (tsl1_sens == 10) {
- tsl1_sens = 1;
- update_shift_reg();
- } else if (tsl1_sens == 100) {
- tsl1_sens = 10;
- update_shift_reg();
- }
- // If changing sensitivity take another sample
- freq = countFreq(TSL1_FREQ_PIN);
- tsl1_energy1 = calc_watts_m2((freq * TSL1_SCALING_DEC));
- tsl1_energy2 = tsl1_energy1 / tsl1_sens;
- }
- // Update Joule Counter from first sensor
- intTime = timeSinceLastSample / 1000;
- jouleCounter += tsl1_energy2 * intTime;
- // Second sensor read here
- freq2 = countFreq(TSL2_FREQ_PIN);
- tsl2_energy1 = calc_watts_m2((freq2 * TSL2_SCALING_DEC));
- tsl2_energy2 = tsl2_energy1 / tsl2_sens;
- if (freq2 < 100) {
- // Turn up scaling
- if (tsl2_energy1 < 0.01) {
- // Go straight to 100
- tsl2_sens = 10;
- }
- if (tsl2_sens == 1) {
- tsl2_sens = 10;
- update_shift_reg();
- } else if (tsl2_sens == 10) {
- tsl2_sens = 100;
- update_shift_reg();
- }
- // If changing sensitivity take another sample
- freq2 = countFreq(TSL2_FREQ_PIN);
- tsl2_energy1 = calc_watts_m2((freq2 * TSL2_SCALING_DEC));
- tsl2_energy2 = tsl2_energy1 / tsl2_sens;
- }
- if (freq2 > 1000) {
- // Turn down scaling
- if (tsl2_sens == 10) {
- tsl2_sens = 1;
- update_shift_reg();
- } else if (tsl2_sens == 100) {
- tsl2_sens = 10;
- update_shift_reg();
- }
- // If changing sensitivity take another sample
- freq2 = countFreq(TSL2_FREQ_PIN);
- tsl2_energy1 = calc_watts_m2((freq2 * TSL2_SCALING_DEC));
- tsl2_energy2 = tsl2_energy1 / tsl2_sens;
- }
- // Serial.print(millis(), DEC);
- // Serial.print("\t");
- // Serial.print(TSL1_SCALING_DEC, DEC);
- // Serial.print("\t");
- // Serial.print(tsl1_sens, DEC);
- // Serial.print("\t");
- // Serial.print(freq, DEC);
- // Serial.print("\t");
- // Serial.print(tsl1_energy2, DEC);
- // Serial.print("\t");
- // Serial.print(jouleCounter, DEC);
- // Serial.print("\t");
- // Serial.print(TSL2_SCALING_DEC, DEC);
- // Serial.print("\t");
- // Serial.print(tsl2_sens, DEC);
- // Serial.print("\t");
- // Serial.print(freq2, DEC);
- // Serial.print("\t"),
- // Serial.print(tsl2_energy2, DEC);
- // Serial.print("\n");
- }
- Client client = server.available();
- if (client) {
- // an http request ends with a blank line
- boolean currentLineIsBlank = true;
- while (client.connected()) {
- if (client.available()) {
- char c = client.read();
- // if you've gotten to the end of the line (received a newline
- // character) and the line is blank, the http request has ended,
- // so you can send a reply
- if (c == '\n' && currentLineIsBlank) {
- // send a standard http response header
- client.println("HTTP/1.1 200 OK");
- client.println("Content-Type: text/html");
- client.println();
- client.println("<html>");
- client.println("<head>");
- client.println("<title>Pyranometer and UV Meter</title>");
- client.println("<meta http-equiv=\"refresh\" content=\"60\"> ");
- client.println("</head>");
- client.println("<body>");
- // output the value of the light sensors
- client.println("<p>");
- client.print("<strong>System Time:</strong> ");
- client.print(day());
- client.print("/");
- client.print(month());
- client.print("/");
- client.print(year());
- client.print(" ");
- client.print(hour());
- client.print(":");
- client.print(minute());
- client.print(":");
- client.print(second());
- client.println();
- client.print("<br />");
- client.print("<strong>Current Light Power:</strong> ");
- client.print(tsl1_energy2, DEC);
- client.print(" w/m<sup>2</sup> - ");
- client.print(tsl1_sens, DEC);
- client.print("<br />");
- client.print("<strong>Accumulated Insolation:</strong> ");
- client.print(jouleCounter, DEC);
- client.print(" J/m<sup>2</sup>");
- client.print("<br />");
- client.print("<strong>UV:</strong> ");
- client.print(tsl2_energy2, DEC);
- client.print(" w/m<sup>2</sup> - ");
- client.print(tsl2_sens, DEC);
- client.print("</p>");
- client.println("</body>");
- client.println("</html>");
- break;
- }
- if (c == '\n') {
- // you're starting a new line
- currentLineIsBlank = true;
- }
- else if (c != '\r') {
- // you've gotten a character on the current line
- currentLineIsBlank = false;
- }
- }
- }
- // give the web browser time to receive the data
- // delay(1);
- // close the connection:
- client.stop();
- }
- }
- int countFreq(int pin) {
- #define SAMPLES 25
- unsigned long count = 0;
- unsigned long startTime = 0;
- unsigned long endTime = 0;
- unsigned long cycleDuration = 0;
- unsigned long sum = 0;
- while (count < SAMPLES) {
- while (digitalRead(pin) != LOW); // Wait for falling edge
- while (digitalRead(pin) != HIGH); // Catch rising edge
- noInterrupts();
- startTime = micros();
- interrupts();
- while (digitalRead(pin) != LOW); // Wait for falling edge
- while (digitalRead(pin) != HIGH); // Catch rising edge
- noInterrupts();
- endTime = micros();
- interrupts();
- if (startTime > endTime) {
- } else {
- cycleDuration = endTime - startTime;
- sum = sum + (1000000 / cycleDuration);
- count++;
- }
- // Break out early if there's more than 10th of a second between pulses
- if (cycleDuration > 50000) {
- sum = 0;
- count = SAMPLES + 1;
- }
- }
- int retfreq = sum / SAMPLES;
- return retfreq;
- }
- float calc_watts_m2(unsigned long freq) {
- float uw_cm2 = (float) freq / (float) 7.7;
- // Convert to watts per square meter
- float w_m2 = uw_cm2 / 100;
- return(w_m2);
- }
- // Updates shift register based on global variables
- void update_shift_reg() {
- unsigned int newRegValue = TSL1_SCALING_BITS + TSL2_SCALING_BITS;
- if (tsl1_sens == 1) {
- newRegValue = newRegValue + 128;
- } else if (tsl1_sens == 10) {
- newRegValue = newRegValue + 64;
- } else if (tsl1_sens == 100) {
- newRegValue = newRegValue + 192;
- }
- if (tsl2_sens == 1) {
- newRegValue = newRegValue + 8;
- } else if (tsl2_sens == 10) {
- newRegValue = newRegValue + 4;
- } else if (tsl2_sens == 100) {
- newRegValue = newRegValue + 12;
- }
- digitalWrite(SHIFT_LATCH, LOW);
- shiftOut(SHIFT_DATA, SHIFT_CLOCK, LSBFIRST, newRegValue);
- digitalWrite(SHIFT_LATCH, HIGH);
- // Serial.print("Shift Register Value: ");
- // Serial.print(newRegValue, DEC);
- // Serial.print("\n");
- }
- // send an NTP request to the time server at the given address
- unsigned long sendNTPpacket(byte *address)
- {
- // set all bytes in the buffer to 0
- memset(packetBuffer, 0, NTP_PACKET_SIZE);
- // Initialize values needed to form NTP request
- // (see URL above for details on the packets)
- packetBuffer[0] = 0b11100011; // LI, Version, Mode
- packetBuffer[1] = 0; // Stratum, or type of clock
- packetBuffer[2] = 6; // Polling Interval
- packetBuffer[3] = 0xEC; // Peer Clock Precision
- // 8 bytes of zero for Root Delay & Root Dispersion
- packetBuffer[12] = 49;
- packetBuffer[13] = 0x4E;
- packetBuffer[14] = 49;
- packetBuffer[15] = 52;
- // all NTP fields have been given values, now
- // you can send a packet requesting a timestamp:
- Udp.sendPacket( packetBuffer,NTP_PACKET_SIZE, address, 123); //NTP requests are to port 123
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement