Advertisement
Guest User

Untitled

a guest
Apr 10th, 2023
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.17 KB | Source Code | 0 0
  1.  
  2.  
  3. #include <SPI.h>
  4. #include <SD.h>
  5. #include <DallasTemperature.h>
  6. #include <OneWire.h>
  7. #include<EEPROM.h>
  8. #include <ESP8266WiFi.h>
  9. #include<Wire.h>
  10. #include <RtcDS3231.h>
  11.  
  12. #define ONE_WIRE_BUS 9
  13. //const int chipSelect = 4;
  14. RtcDS3231<TwoWire> rtcObject(Wire);
  15. OneWire oneWire(ONE_WIRE_BUS);
  16. DeviceAddress Thermometer;
  17. DallasTemperature sensors(&oneWire);
  18. int inc=0;
  19. String SensorID;
  20. int deviceCount = 0;
  21.  
  22. void setup() {
  23.   // Open serial communications and wait for port to open:
  24.   Serial.begin(115200);
  25.   sensors.begin();
  26. rtcObject.Begin();
  27.   delay(10000);
  28.   Serial.println("Technoman Pvt Ltd");
  29.   Serial.println("Data Logger Version 2");
  30.   Serial.println("Initializing CPU .......");
  31.   Serial.println("EEPROM Begin");
  32.   Serial.println("Serial Begin");
  33.   Serial.println("RTC begin");
  34.        //Starts I2C
  35.  
  36.   RtcDateTime currentTime = RtcDateTime(__DATE__, __TIME__); //define date and time object // input the time stamp from the software api
  37.   rtcObject.SetDateTime(currentTime);        
  38.  
  39.  
  40.  
  41.   Serial.print("ESP Board MAC Address:  ");
  42.   Serial.println(WiFi.macAddress());
  43.   Serial.println("Initialising Sensor");
  44.   Serial.println("Locating devices...");
  45.   Serial.print("Found ");
  46.   deviceCount = sensors.getDeviceCount();
  47.   Serial.print(deviceCount, DEC);
  48.   Serial.println(" devices.");
  49.   Serial.println("");
  50.   Serial.println("Printing addresses...");
  51.   for (int i = 0;  i < deviceCount;  i++)
  52.   {
  53.     Serial.print("Sensor ");
  54.     Serial.print(i + 1);
  55.     Serial.print(" : ");
  56.     sensors.getAddress(Thermometer, i);
  57.     SensorID = printAddress(Thermometer);
  58.     Serial.println("Sensor ID :");
  59.     Serial.println(SensorID);
  60.   }
  61.   Serial.print("Initializing SD card...");
  62.  
  63.   // see if the card is present and can be initialized:
  64.   if (!SD.begin(4)) {
  65.     Serial.println("Card failed, or not present");
  66.     // don't do anything more:
  67.     return;
  68.   }
  69.   Serial.println("card initialized.");
  70. }
  71.  
  72. void loop(){
  73.   inc ++;
  74.  
  75.   RtcDateTime currentTime = rtcObject.GetDateTime();  
  76.    char str[20];   //declare a string as an array of chars
  77.  
  78.   sprintf(str, "%d/%d/%d %d:%d:%d",     //%d allows to print an integer to the string
  79.           currentTime.Year(),   //get year method
  80.           currentTime.Month(),  //get month method
  81.           currentTime.Day(),    //get day method
  82.           currentTime.Hour(),   //get hour method
  83.           currentTime.Minute(), //get minute method
  84.           currentTime.Second()  //get second method
  85.          );
  86.          Serial.println(str);
  87.           sensors.requestTemperatures();  
  88.   // make a string for assembling the data to log:
  89.   String dataString = String(inc)+ "!!" + str +"!!" +String(WiFi.macAddress()) +"!!" +String(SensorID)+  "!!" + String(sensors.getTempCByIndex(0))+"!!"+String(battery_level()) ;
  90.    File myFile = SD.open("datalog.csv", FILE_WRITE);
  91.   if (myFile && SD.begin(4)){
  92.     Serial.print("Writing to test.txt...");
  93.  
  94.     myFile.println(dataString);
  95.     // close the file:
  96.     myFile.close();
  97.     Serial.println("done.");
  98.   } else {
  99.  
  100.     Serial.println("error opening test.txt or SD card not found");
  101.  
  102.   Serial.println("Temperature is: ");
  103.   Serial.println(sensors.getTempCByIndex(0));
  104.    dataString += ",";
  105.     delay(500);
  106.  
  107.  
  108.   // open the file. note that only one file can be open at a time,
  109.   // so you have to close this one before opening another.
  110.  
  111.   }
  112.  
  113.   // if the file is available, write to it:
  114. //  if (dataFile) {
  115. //    dataFile.println(dataString);
  116. //    dataFile.close();
  117. //    delay(60000);
  118.     // print to the serial port too:
  119.     Serial.println(dataString);
  120.   //}
  121.   // if the file isn't open, pop up an error:
  122. //  else { Serial.println("error opening datalog.csv"); }
  123. delay(60000);
  124. }
  125.  
  126.  
  127. String printAddress(DeviceAddress deviceAddress)
  128. { String hex_add;
  129.   for (uint8_t i = 0; i < 8; i++)
  130.   {
  131.  
  132.     if (deviceAddress[i] < 10) {
  133.       Serial.print("0");
  134.       hex_add += String("0");
  135.     }
  136.     Serial.print(deviceAddress[i], HEX);
  137.     hex_add += String(deviceAddress[i], HEX);
  138.  
  139.  
  140.     //Serial.println(hex_add);
  141.   }
  142.   Serial.println("");
  143.   return hex_add;
  144. }
  145. float battery_level(){
  146.   float bat_level = ((analogRead(A0)*3.7)/1024)*2;
  147.    return bat_level;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement