Datalogger With Sd Card Support
I Will Show You How To Make A Data Logger With Esp8266, Sensors, NTP Client & Save The Values To The Sd Card
Components and supplies
1
Sd Card Module
1
Flash Memory Card, SD Card
1
DHT22 Temperature Sensor
1
Jumper wires (generic)
1
NodeMCU ESP8266 Breakout Board
1
MQ-135 Air Quality Sensor
1
Arduino UNO
1
Breadboard (generic)
Tools and machines
1
Hot glue gun (generic)
Apps and platforms
1
Arduino IDE
Project description
Code
Arduino Code
c_cpp
Upload The Code To Nodemcu (ESP8266)
1/*feel free to contact 2 * sreeramaj53@gmail.com 3 * www.youtube.com/ZenoModiff 4 * last updated - time 09:16 AM - date 25 may 2021 5 Github Link :-- https://github.com/Zenomodiff/Nodemcu-Datalogger-With-DHT22-MQ135-NTP-Time-Saving-It-To-Sd-Card 6 */ 7 8#include "SPI.h" 9#include "SD.h" 10#include "DHT.h" 11#include <ESP8266WiFi.h> 12#include <NTPClient.h> 13#include <WiFiUdp.h> 14 15#define DHTPIN D2 16#define DHTYPE DHT22 17 18const int chipSelect = D8; 19const int mq135 = A0; 20DHT dht(DHTPIN, DHTYPE); 21 22const char *ssid = "PASTE SSID NAME"; 23const char *password = "PASTE PASSWORD"; 24 25WiFiUDP ntpUDP; 26NTPClient timeClient(ntpUDP, "pool.ntp.org"); 27 28#define SDREADLEDPIN D1 29 30String weekDays[7]={"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; 31String months[12]={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; 32 33float m = -0.6527; 34float b = 1.30; 35float R0 = 21.91; 36 37void setup() { 38 39 pinMode(mq135, INPUT); 40 Serial.begin(115200); 41 42 pinMode(SDREADLEDPIN, OUTPUT); 43 digitalWrite(SDREADLEDPIN, LOW); 44 45 while (!Serial) { 46 } 47 48 if (!SD.begin(chipSelect)) { 49 Serial.println("Initialization failed!"); 50 while (1); 51 } 52 53 dht.begin(); 54 55 Serial.print("Connecting to "); 56 Serial.println(ssid); 57 WiFi.begin(ssid, password); 58 while (WiFi.status() != WL_CONNECTED) { 59 delay(500); 60 Serial.print("."); 61 } 62 63 timeClient.begin(); 64 timeClient.setTimeOffset(19786); 65} 66 67void loop() { 68 69 timeClient.update(); 70 71 double mq135_value = analogRead(mq135); 72 float h = dht.readHumidity(); 73 float t = dht.readTemperature(); 74 unsigned long epochTime = timeClient.getEpochTime(); 75 String formattedTime = timeClient.getFormattedTime(); 76 int currentHour = timeClient.getHours(); 77 int currentMinute = timeClient.getMinutes(); 78 int currentSecond = timeClient.getSeconds(); 79 String weekDay = weekDays[timeClient.getDay()]; 80 struct tm *ptm = gmtime ((time_t *)&epochTime); 81 int monthDay = ptm->tm_mday; 82 int currentMonth = ptm->tm_mon+1; 83 String currentMonthName = months[currentMonth-1]; 84 int currentYear = ptm->tm_year+1900; 85 String currentDate = String(currentYear) + "-" + String(currentMonth) + "-" + String(monthDay); 86 87 String fullstring = String(weekDay) + "," + String(currentMonthName) + "," + String(monthDay)+ "," + String(currentYear) + "," + 88 String(currentHour) + "," + String(currentMinute) + "," + String(currentSecond) + "," + 89 String(t) + "," + String(h) + (",") + String(mq135_value); 90 91 Serial.print("Full Data: "); 92 Serial.println(fullstring); 93 Serial.println(""); 94 delay(1000); 95 96 File dataFile = SD.open("datalog.txt", FILE_WRITE); 97 digitalWrite(SDREADLEDPIN, HIGH); 98 if (dataFile) { 99 dataFile.println(fullstring); 100 dataFile.close(); 101 delay(1000); 102 digitalWrite(SDREADLEDPIN, LOW); 103 } 104 105 else { 106 Serial.println("error opening datalog.txt"); 107 } 108 delay(2000); 109}
Arduino Code
c_cpp
Upload The Code To Nodemcu (ESP8266)
1/*feel free to contact 2 * sreeramaj53@gmail.com 3 * www.youtube.com/ZenoModiff 4 5 * last updated - time 09:16 AM - date 25 may 2021 6 Github Link :-- https://github.com/Zenomodiff/Nodemcu-Datalogger-With-DHT22-MQ135-NTP-Time-Saving-It-To-Sd-Card 7 8 */ 9 10#include "SPI.h" 11#include "SD.h" 12#include "DHT.h" 13#include 14 <ESP8266WiFi.h> 15#include <NTPClient.h> 16#include <WiFiUdp.h> 17 18#define 19 DHTPIN D2 20#define DHTYPE DHT22 21 22const int chipSelect = D8; 23const 24 int mq135 = A0; 25DHT dht(DHTPIN, DHTYPE); 26 27const char *ssid = "PASTE 28 SSID NAME"; 29const char *password = "PASTE PASSWORD"; 30 31WiFiUDP ntpUDP; 32NTPClient 33 timeClient(ntpUDP, "pool.ntp.org"); 34 35#define SDREADLEDPIN D1 36 37String 38 weekDays[7]={"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", 39 "Saturday"}; 40String months[12]={"January", "February", "March", "April", 41 "May", "June", "July", "August", "September", "October", "November", 42 "December"}; 43 44float m = -0.6527; 45float b = 1.30; 46float R0 = 21.91; 47 48void 49 setup() { 50 51 pinMode(mq135, INPUT); 52 Serial.begin(115200); 53 54 pinMode(SDREADLEDPIN, 55 OUTPUT); 56 digitalWrite(SDREADLEDPIN, LOW); 57 58 while (!Serial) { 59 60 } 61 62 if (!SD.begin(chipSelect)) { 63 Serial.println("Initialization 64 failed!"); 65 while (1); 66 } 67 68 dht.begin(); 69 70 Serial.print("Connecting 71 to "); 72 Serial.println(ssid); 73 WiFi.begin(ssid, password); 74 while (WiFi.status() 75 != WL_CONNECTED) { 76 delay(500); 77 Serial.print("."); 78 } 79 80 81 timeClient.begin(); 82 timeClient.setTimeOffset(19786); 83} 84 85void loop() 86 { 87 88 timeClient.update(); 89 90 double mq135_value = analogRead(mq135); 91 92 float h = dht.readHumidity(); 93 float t = dht.readTemperature(); 94 unsigned 95 long epochTime = timeClient.getEpochTime(); 96 String formattedTime = timeClient.getFormattedTime(); 97 98 int currentHour = timeClient.getHours(); 99 int currentMinute = timeClient.getMinutes(); 100 101 int currentSecond = timeClient.getSeconds(); 102 String weekDay = weekDays[timeClient.getDay()]; 103 104 struct tm *ptm = gmtime ((time_t *)&epochTime); 105 int monthDay = ptm->tm_mday; 106 107 int currentMonth = ptm->tm_mon+1; 108 String currentMonthName = months[currentMonth-1]; 109 110 int currentYear = ptm->tm_year+1900; 111 String currentDate = String(currentYear) 112 + "-" + String(currentMonth) + "-" + String(monthDay); 113 114 String fullstring 115 = String(weekDay) + "," + String(currentMonthName) + "," + String(monthDay)+ 116 "," + String(currentYear) + "," + 117 String(currentHour) + "," + String(currentMinute) 118 + "," + String(currentSecond) + "," + 119 String(t) + "," + String(h) + 120 (",") + String(mq135_value); 121 122 Serial.print("Full Data: "); 123 Serial.println(fullstring); 124 125 Serial.println(""); 126 delay(1000); 127 128 File dataFile = SD.open("datalog.txt", 129 FILE_WRITE); 130 digitalWrite(SDREADLEDPIN, HIGH); 131 if (dataFile) { 132 dataFile.println(fullstring); 133 134 dataFile.close(); 135 delay(1000); 136 digitalWrite(SDREADLEDPIN, LOW); 137 138 } 139 140 else { 141 Serial.println("error opening datalog.txt"); 142 143 } 144 delay(2000); 145}
Downloadable files
Schematics
Wire As Per As The Schematics
Schematics

Comments
Only logged in users can leave comments