Guest User

Arduino SD + ETH + DHT + RTC + LED

a guest
Sep 25th, 2015
69
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Logging (SD) and serving temperature and humidity data (via eth)
  2.  
  3. #include <EtherCard.h>
  4. #include <SPI.h>
  5. #include <SD.h>
  6. #include "DHT.h"
  7. #include "Wire.h"
  8.  
  9. #define SDPIN 10                    // chip select pin for SD
  10. #define DS3231_I2C_ADDRESS 0x68     // RTC clock
  11. #define DHTPIN 2                    // dht sensor pin
  12. #define DHTTYPE DHT11               // dht sensor type
  13. #define LEDPIN 3                    // led pin
  14.  
  15. DHT dht(DHTPIN, DHTTYPE);           // init dht
  16.  
  17. // ethernet interface mac, ip and gateway addresses
  18. static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
  19. static byte myip[] = { 192,168,0,2 };
  20. static byte gwip[] = { 192,168,0,255 };
  21.  
  22. byte Ethernet::buffer[250];
  23. BufferFiller bfill;
  24.  
  25. const char fileName[] PROGMEM = "templog.txt";
  26. File myFile;
  27.  
  28. bool sdRead = false;
  29.  
  30. struct MyDateTime {
  31.     byte second;
  32.     byte minute;
  33.     byte hour;
  34.     byte dayOfMonth;
  35.     byte month;
  36.     byte year;
  37. } dateTime;
  38.  
  39. // json header
  40. const char okHeader[] PROGMEM =
  41.     "HTTP/1.0 200 OK\r\n"
  42.     "Content-Type: application/json\r\n"
  43.     "Pragma: no-cache\r\n"
  44.     "\r\n"
  45. ;
  46.  
  47. void setup () {
  48.     Serial.begin(57600);
  49.  
  50.     pinMode(LEDPIN, OUTPUT);
  51.    
  52.     if ( ether.begin(sizeof Ethernet::buffer, mymac) == 0 )
  53.         Serial.println(F("Failed to access Ethernet controller"));
  54.  
  55.     ether.staticSetup(myip, gwip);
  56.  
  57.     pinMode(SS, OUTPUT);
  58.      
  59.     if ( ! SD.begin(SDPIN) )
  60.         Serial.println(F("sd init failed"));
  61.    
  62.     blinkLed(LEDPIN, 5);
  63.  
  64.     dht.begin();
  65.     Wire.begin();
  66. }
  67.  
  68. void loop ()
  69. {
  70.     buildTime();
  71.  
  72.     word len = ether.packetReceive();
  73.     word pos = ether.packetLoop(len);
  74.  
  75.     // check if valid tcp data is received
  76.     if ( pos )
  77.     {
  78.         analogWrite(LEDPIN, 150);
  79.        
  80.         if ( strstr((char *)Ethernet::buffer + pos, "GET /?sensor=current") != 0 )
  81.         {
  82.             float h = dht.readHumidity();
  83.             float t = dht.readTemperature();
  84.  
  85.             if ( isnan(h) || isnan(t) ) {
  86.                 Serial.println(F("Failed to read from the sensor"));
  87.                 return;
  88.             }
  89.  
  90.             ether.httpServerReply(jsonRsp(h, t)); // send web page data
  91.         }
  92.  
  93.         digitalWrite(LEDPIN, LOW);
  94.     }
  95.  
  96.     // read sensor every 30 seconds and store data
  97.     if ( dateTime.second % 30 == 0 && sdRead == true )
  98.     {
  99.         writeToFile(dht.readHumidity(), dht.readTemperature());
  100.        
  101.         sdRead = false;
  102.     }
  103.  
  104.     if ( dateTime.second % 10 != 0 )
  105.         sdRead = true;
  106. }
  107.  
  108. void writeToFile(float h, float t)
  109. {
  110.      myFile = SD.open("templog.txt", O_CREAT | O_APPEND | O_WRITE);
  111.  
  112.     if ( ! myFile )
  113.     {
  114.         Serial.println(F("error opening file"));
  115.         return;
  116.     }
  117.  
  118.     String entry = "";
  119.    
  120.     entry += String(dateTime.hour) + ":";
  121.     if ( dateTime.minute < 10 )
  122.         entry += "0";
  123.     entry += String(dateTime.minute) + ":";
  124.     if ( dateTime.second < 10 )
  125.         entry += "0";
  126.     entry += String(dateTime.second) + " ";
  127.     if ( dateTime.dayOfMonth< 10 )
  128.         entry += "0";
  129.     entry += String(dateTime.dayOfMonth) + "/";
  130.     if ( dateTime.month < 10 )
  131.         entry += "0";
  132.     entry += String(dateTime.month) + "/";
  133.     entry += String(dateTime.year) + ";";
  134.     entry += String(h) + ";";
  135.     entry += String(t);
  136.  
  137.     // write to the file
  138.     myFile.println(entry);
  139.     myFile.close();
  140.  
  141.     blinkLed(LEDPIN, 2);
  142. }
  143.  
  144. static word jsonRsp(float hum, float temp)
  145. {
  146.     byte h = hum / 1;
  147.     byte t = temp / 1;
  148.    
  149.     bfill = ether.tcpOffset();
  150.     bfill.emit_p(PSTR(
  151.         "$F"
  152.         "{\"d\":[{\"t\":\"$D\","
  153.         "\"h\":\"$D\","
  154.         "\"lt\":\"$D:$D $D/$D/$D\"}]}"
  155.         ),
  156.             okHeader, t, h,
  157.             dateTime.hour, dateTime.minute,
  158.             dateTime.dayOfMonth, dateTime.month, dateTime.year);
  159.     return bfill.position();
  160. }
  161.  
  162. void readDS3231time(byte *second, byte *minute, byte *hour, byte *dayOfWeek, byte *dayOfMonth, byte *month, byte *year)
  163. {
  164.     Wire.beginTransmission(DS3231_I2C_ADDRESS);
  165.     Wire.write(0); // set DS3231 register pointer to 00h
  166.     Wire.endTransmission();
  167.     Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  168.     // request seven bytes of data from DS3231 starting from register 00h
  169.     *second = bcdToDec(Wire.read() & 0x7f);
  170.     *minute = bcdToDec(Wire.read());
  171.     *hour = bcdToDec(Wire.read() & 0x3f);
  172.     *dayOfWeek = bcdToDec(Wire.read());
  173.     *dayOfMonth = bcdToDec(Wire.read());
  174.     *month = bcdToDec(Wire.read());
  175.     *year = bcdToDec(Wire.read());
  176. }
  177.  
  178. static void buildTime()
  179. {
  180.     byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  181.     // retrieve data from DS3231
  182.     readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  183.  
  184.     dateTime.second     = second;
  185.     dateTime.minute     = minute;
  186.     dateTime.hour       = hour;
  187.     dateTime.dayOfMonth = dayOfMonth;
  188.     dateTime.month      = month;
  189.     dateTime.year       = year;
  190. }
  191.  
  192. // Convert binary coded decimal to normal decimal numbers
  193. byte bcdToDec(byte val)
  194. {
  195.     return( (val/16*10) + (val%16) );
  196. }
  197.  
  198. void blinkLed(int ledPin, int times)
  199. {
  200.     for ( int i = 0; i < times; i++ )
  201.     {
  202.         analogWrite(ledPin, 30);
  203.         delay(150);
  204.         digitalWrite(ledPin, LOW);
  205.         delay(150);
  206.     }
  207. }
RAW Paste Data