Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Logging (SD) and serving temperature and humidity data (via eth)
- #include <EtherCard.h>
- #include <SPI.h>
- #include <SD.h>
- #include "DHT.h"
- #include "Wire.h"
- #define SDPIN 10 // chip select pin for SD
- #define DS3231_I2C_ADDRESS 0x68 // RTC clock
- #define DHTPIN 2 // dht sensor pin
- #define DHTTYPE DHT11 // dht sensor type
- #define LEDPIN 3 // led pin
- DHT dht(DHTPIN, DHTTYPE); // init dht
- // ethernet interface mac, ip and gateway addresses
- static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
- static byte myip[] = { 192,168,0,2 };
- static byte gwip[] = { 192,168,0,255 };
- byte Ethernet::buffer[250];
- BufferFiller bfill;
- const char fileName[] PROGMEM = "templog.txt";
- File myFile;
- bool sdRead = false;
- struct MyDateTime {
- byte second;
- byte minute;
- byte hour;
- byte dayOfMonth;
- byte month;
- byte year;
- } dateTime;
- // json header
- const char okHeader[] PROGMEM =
- "HTTP/1.0 200 OK\r\n"
- "Content-Type: application/json\r\n"
- "Pragma: no-cache\r\n"
- "\r\n"
- ;
- void setup () {
- Serial.begin(57600);
- pinMode(LEDPIN, OUTPUT);
- if ( ether.begin(sizeof Ethernet::buffer, mymac) == 0 )
- Serial.println(F("Failed to access Ethernet controller"));
- ether.staticSetup(myip, gwip);
- pinMode(SS, OUTPUT);
- if ( ! SD.begin(SDPIN) )
- Serial.println(F("sd init failed"));
- blinkLed(LEDPIN, 5);
- dht.begin();
- Wire.begin();
- }
- void loop ()
- {
- buildTime();
- word len = ether.packetReceive();
- word pos = ether.packetLoop(len);
- // check if valid tcp data is received
- if ( pos )
- {
- analogWrite(LEDPIN, 150);
- if ( strstr((char *)Ethernet::buffer + pos, "GET /?sensor=current") != 0 )
- {
- float h = dht.readHumidity();
- float t = dht.readTemperature();
- if ( isnan(h) || isnan(t) ) {
- Serial.println(F("Failed to read from the sensor"));
- return;
- }
- ether.httpServerReply(jsonRsp(h, t)); // send web page data
- }
- digitalWrite(LEDPIN, LOW);
- }
- // read sensor every 30 seconds and store data
- if ( dateTime.second % 30 == 0 && sdRead == true )
- {
- writeToFile(dht.readHumidity(), dht.readTemperature());
- sdRead = false;
- }
- if ( dateTime.second % 10 != 0 )
- sdRead = true;
- }
- void writeToFile(float h, float t)
- {
- myFile = SD.open("templog.txt", O_CREAT | O_APPEND | O_WRITE);
- if ( ! myFile )
- {
- Serial.println(F("error opening file"));
- return;
- }
- String entry = "";
- entry += String(dateTime.hour) + ":";
- if ( dateTime.minute < 10 )
- entry += "0";
- entry += String(dateTime.minute) + ":";
- if ( dateTime.second < 10 )
- entry += "0";
- entry += String(dateTime.second) + " ";
- if ( dateTime.dayOfMonth< 10 )
- entry += "0";
- entry += String(dateTime.dayOfMonth) + "/";
- if ( dateTime.month < 10 )
- entry += "0";
- entry += String(dateTime.month) + "/";
- entry += String(dateTime.year) + ";";
- entry += String(h) + ";";
- entry += String(t);
- // write to the file
- myFile.println(entry);
- myFile.close();
- blinkLed(LEDPIN, 2);
- }
- static word jsonRsp(float hum, float temp)
- {
- byte h = hum / 1;
- byte t = temp / 1;
- bfill = ether.tcpOffset();
- bfill.emit_p(PSTR(
- "$F"
- "{\"d\":[{\"t\":\"$D\","
- "\"h\":\"$D\","
- "\"lt\":\"$D:$D $D/$D/$D\"}]}"
- ),
- okHeader, t, h,
- dateTime.hour, dateTime.minute,
- dateTime.dayOfMonth, dateTime.month, dateTime.year);
- return bfill.position();
- }
- void readDS3231time(byte *second, byte *minute, byte *hour, byte *dayOfWeek, byte *dayOfMonth, byte *month, byte *year)
- {
- Wire.beginTransmission(DS3231_I2C_ADDRESS);
- Wire.write(0); // set DS3231 register pointer to 00h
- Wire.endTransmission();
- Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
- // request seven bytes of data from DS3231 starting from register 00h
- *second = bcdToDec(Wire.read() & 0x7f);
- *minute = bcdToDec(Wire.read());
- *hour = bcdToDec(Wire.read() & 0x3f);
- *dayOfWeek = bcdToDec(Wire.read());
- *dayOfMonth = bcdToDec(Wire.read());
- *month = bcdToDec(Wire.read());
- *year = bcdToDec(Wire.read());
- }
- static void buildTime()
- {
- byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
- // retrieve data from DS3231
- readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
- dateTime.second = second;
- dateTime.minute = minute;
- dateTime.hour = hour;
- dateTime.dayOfMonth = dayOfMonth;
- dateTime.month = month;
- dateTime.year = year;
- }
- // Convert binary coded decimal to normal decimal numbers
- byte bcdToDec(byte val)
- {
- return( (val/16*10) + (val%16) );
- }
- void blinkLed(int ledPin, int times)
- {
- for ( int i = 0; i < times; i++ )
- {
- analogWrite(ledPin, 30);
- delay(150);
- digitalWrite(ledPin, LOW);
- delay(150);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement