Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <SD.h>
- #include <DallasTemperature.h>
- #include <OneWire.h>
- #include<EEPROM.h>
- #include <ESP8266WiFi.h>
- #include<Wire.h>
- #include <RtcDS3231.h>
- #define ONE_WIRE_BUS 9
- //const int chipSelect = 4;
- RtcDS3231<TwoWire> rtcObject(Wire);
- OneWire oneWire(ONE_WIRE_BUS);
- DeviceAddress Thermometer;
- DallasTemperature sensors(&oneWire);
- int inc=0;
- String SensorID;
- int deviceCount = 0;
- void setup() {
- // Open serial communications and wait for port to open:
- Serial.begin(115200);
- sensors.begin();
- rtcObject.Begin();
- delay(10000);
- Serial.println("Technoman Pvt Ltd");
- Serial.println("Data Logger Version 2");
- Serial.println("Initializing CPU .......");
- Serial.println("EEPROM Begin");
- Serial.println("Serial Begin");
- Serial.println("RTC begin");
- //Starts I2C
- RtcDateTime currentTime = RtcDateTime(__DATE__, __TIME__); //define date and time object // input the time stamp from the software api
- rtcObject.SetDateTime(currentTime);
- Serial.print("ESP Board MAC Address: ");
- Serial.println(WiFi.macAddress());
- Serial.println("Initialising Sensor");
- Serial.println("Locating devices...");
- Serial.print("Found ");
- deviceCount = sensors.getDeviceCount();
- Serial.print(deviceCount, DEC);
- Serial.println(" devices.");
- Serial.println("");
- Serial.println("Printing addresses...");
- for (int i = 0; i < deviceCount; i++)
- {
- Serial.print("Sensor ");
- Serial.print(i + 1);
- Serial.print(" : ");
- sensors.getAddress(Thermometer, i);
- SensorID = printAddress(Thermometer);
- Serial.println("Sensor ID :");
- Serial.println(SensorID);
- }
- Serial.print("Initializing SD card...");
- // see if the card is present and can be initialized:
- if (!SD.begin(4)) {
- Serial.println("Card failed, or not present");
- // don't do anything more:
- return;
- }
- Serial.println("card initialized.");
- }
- void loop(){
- inc ++;
- RtcDateTime currentTime = rtcObject.GetDateTime();
- char str[20]; //declare a string as an array of chars
- sprintf(str, "%d/%d/%d %d:%d:%d", //%d allows to print an integer to the string
- currentTime.Year(), //get year method
- currentTime.Month(), //get month method
- currentTime.Day(), //get day method
- currentTime.Hour(), //get hour method
- currentTime.Minute(), //get minute method
- currentTime.Second() //get second method
- );
- Serial.println(str);
- sensors.requestTemperatures();
- // make a string for assembling the data to log:
- String dataString = String(inc)+ "!!" + str +"!!" +String(WiFi.macAddress()) +"!!" +String(SensorID)+ "!!" + String(sensors.getTempCByIndex(0))+"!!"+String(battery_level()) ;
- File myFile = SD.open("datalog.csv", FILE_WRITE);
- if (myFile && SD.begin(4)){
- Serial.print("Writing to test.txt...");
- myFile.println(dataString);
- // close the file:
- myFile.close();
- Serial.println("done.");
- } else {
- Serial.println("error opening test.txt or SD card not found");
- Serial.println("Temperature is: ");
- Serial.println(sensors.getTempCByIndex(0));
- dataString += ",";
- delay(500);
- // open the file. note that only one file can be open at a time,
- // so you have to close this one before opening another.
- }
- // if the file is available, write to it:
- // if (dataFile) {
- // dataFile.println(dataString);
- // dataFile.close();
- // delay(60000);
- // print to the serial port too:
- Serial.println(dataString);
- //}
- // if the file isn't open, pop up an error:
- // else { Serial.println("error opening datalog.csv"); }
- delay(60000);
- }
- String printAddress(DeviceAddress deviceAddress)
- { String hex_add;
- for (uint8_t i = 0; i < 8; i++)
- {
- if (deviceAddress[i] < 10) {
- Serial.print("0");
- hex_add += String("0");
- }
- Serial.print(deviceAddress[i], HEX);
- hex_add += String(deviceAddress[i], HEX);
- //Serial.println(hex_add);
- }
- Serial.println("");
- return hex_add;
- }
- float battery_level(){
- float bat_level = ((analogRead(A0)*3.7)/1024)*2;
- return bat_level;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement