0

I want to build a web page on an ATmega328 that will link me to an SD card file with the saved values of a DHT11 sensor. I manage to program the "Web Server" on the atmega328 and the code that creates the file with the measures of the sensor but I have an issue, every time I access the IP address from a browser the sensor stops working and I did't manage to link the txt file on the HTML page that is created inside the EEPROM of the ATmega328. I use an Enc28j60 module, a SD module and an DS3231 Real Time clock module! I am programming the ATmega328 on a Breadboard with Arduino as ISP.

Here is the Code

#include <SD.h>     // sd library
#include <SPI.h>    // sd library
#include <dht11.h>  // temp sensor library
#include <Wire.h>   // this #include still required because the RTClib    depends on it
#include <RTClib.h> // clock module library
#include <EtherCard.h>  // Ethernet module library

dht11 DHT;
//Pin 4 of Arduino to Data of DHT11 (Pin 6 of ATmega328)
#define DHT11_PIN 4

int chipSelect=9;  // sd card cs pin
File mySensorData;  // file on sd card

RTC_Millis rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday",       "Thursday", "Friday", "Saturday"};

// Set up Ethernet settings
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
static byte myip[] = {192,168,1,10};
static byte gwip[] = { 192,168,1,1 };
byte Ethernet::buffer[256];
BufferFiller bfill;

void setup()
{
  Serial.begin(9600);  

  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  digitalWrite(10, HIGH);
  digitalWrite(9, HIGH);

  DHT.read(DHT11_PIN);
  SD.begin(chipSelect);  

// following line sets the RTC to the date & time this sketch was compiled
rtc.begin(DateTime(F(__DATE__), F(__TIME__)));

// EtherCard start connection | 10 is EtherCard cs pin
  ether.begin(sizeof Ethernet::buffer, mymac,10);
  ether.staticSetup(myip);
}

//  Here we build a web page and pass the temperature and humidity values into it
static word homePage()
{
  bfill = ether.tcpOffset();
  bfill.emit_p(PSTR(
  "HTTP/1.0 200 OK\r\n"
  "Content-Type: text/html\r\n"
  "Pragma: no-cache\r\n\r\n"
  "<meta http-equiv='refresh' content='5'/>"
  "<html>"
  "<head><Style>body { text-align: center; }</Style><title>Remote Temperature and Humidity Datalogger</title></head>"
  "<body>"
  "<title>Remote Server</title>"
  "<h2>Georgios Michos</h2><br>"
  "<h1>Temperature: $D C <br>Humidity   : $D %</h1>"
  "</body></html>"),DHT.temperature, DHT.humidity);
  return bfill.position();
}

void loop() 
{
  word len = ether.packetReceive();
  word pos = ether.packetLoop(len);

  if (pos)  // check if valid tcp data is received
  {
    DHT.read(DHT11_PIN);
    ether.httpServerReply(homePage()); // send web page data
  }

  DateTime now = rtc.now();

  mySensorData=SD.open("Data.txt", FILE_WRITE);  // opens file on sd
  //if no file found it will create it

  if (mySensorData) {  // only proceed if sd file opens

  char dateBuffer[12];

  Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
  Serial.print(' ');
  sprintf(dateBuffer,"%02u/%02u/%04u ",now.day(),now.month(),now.year());     // print Date to serial monitor
  Serial.print(dateBuffer);
  sprintf(dateBuffer,"%02u:%02u:%02u ",now.hour(),now.minute(),now.second()); // print Time to serial monitor
  Serial.print(dateBuffer);

  Serial.print(" - ");  
  Serial.print("Temperature: ");     // print temp/humid to serial    monitor
  Serial.print(DHT.temperature);
  Serial.print("C - Humidity: ");
  Serial.print(DHT.humidity);
  Serial.println("%");

  mySensorData.print(daysOfTheWeek[now.dayOfTheWeek()]);
  mySensorData.print(' ');
  sprintf(dateBuffer,"%02u/%02u/%04u ",now.day(),now.month(),now.year());     // Save Date to SD Card file
  mySensorData.print(dateBuffer);
  sprintf(dateBuffer,"%02u:%02u:%02u ",now.hour(),now.minute(),now.second()); // Save Time to SD Card file
  mySensorData.print(dateBuffer);

  mySensorData.print(" - ");
  mySensorData.print("Temperature: ");  // Save temp/humid to SD Card file
  mySensorData.print(DHT.temperature);
  mySensorData.print("C° - Humidity: ");
  mySensorData.print(DHT.humidity);
  mySensorData.println("%");
  mySensorData.close();
  delay(2000);
}
}
  • 1
    I do not believe it fits into memories. show us the memory usage printed by the IDE before uploading and print memory usage at runtime. – Juraj Mar 26 '18 at 11:38
  • Sketch uses 22,064 bytes (68%) of program storage space. Maximum is 32,256 bytes. Global variables use 1,638 bytes (79%) of dynamic memory, leaving 410 bytes for local variables. Maximum is 2,048 bytes. Low memory available, stability problems may occur. – George Mar 26 '18 at 12:35
  • and the memory usage at runtime? – Juraj Mar 27 '18 at 7:00
  • I am not sure how to find the memory usage at runtime!! Sorry... – George Mar 27 '18 at 14:21
  • I use this simple library github.com/maniacbug/MemoryFree – Juraj Mar 27 '18 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.