Go Down

Topic: Arduino temperature&presence sensor with web page and SNMP (Read 3898 times) previous topic - next topic

moscaciccia

Hi, we are develop a project with arduino for monitor temperature and presence. We have to reach the state by web and with SNMP polling, also save some data in a SD card.

We use Arduino Uno + Eth Shield (SD), an analog thermistor and one PIR.

The task are:

  • client webpage build on arduino showing the temperature and if there's a precence

  • write on SD card when is disclose a presence

  • response to SNMP request with the temperature state and other some parameter




The sketch seem to work fine, but it crash a lot of time, and we doesn't know if is a memory problem, or other software issue.  


Have someone find other similar problem and could help us?



moscaciccia

This is part of our code:

Code: [Select]

#include <Streaming.h>         // Include the Streaming library
#include <Ethernet.h>          // Include the Ethernet library
#include <Flash.h>
#include <SPI.h>
#include <MemoryFree.h>  
#include <Agentuino.h>
#include <SD.h>
#include <Time.h>
#include <Udp.h>



void setup()
{
 Serial.begin(9600);
 Ethernet.begin(mac, ip);
 
 
 //GESTIONE TIMER LOG
 Udp.begin(localPort);

 setSyncProvider(getNtpTime);
 
 while(timeStatus()== timeNotSet)
 
 //GESTIONE SD
 Serial.print("Initializing SD card...");
 pinMode(10, OUTPUT);
 if (!SD.begin(chipSelect)) {
   Serial.println("Card failed, or not present");
   return;
 }
   Serial.println("card initialized.");
   pinMode(PinMov,INPUT);
// api_status = Agentuino.begin();
// Agentuino.onPduReceive(pduReceived);
 return;
}

void loop()
{
 int Presenza;
 char data_presenza;
 String h,m,s;
 
 double Limite = 27;
 // listen/handle for incoming SNMP requests
 //  Agentuino.listen();
 //
 // sysUpTime - The time (in hundredths of a second) since
 // the network management portion of the system was last
 // re-initialized.
 if ( millis() - prevMillis > 1000 )
 {
   // increment previous milliseconds
   prevMillis += 1000;
   //
   // increment up-time counter
   locUpTime += 100;
 }
  Temperatura = int(Thermister(analogRead(Pinmode)));
  File dataFile = SD.open("Temp.log", FILE_WRITE);
      // if the file is available, write to it:
      if (dataFile) {
      dataFile.print("Temperatura: ");
      dataFile.println(Temperatura);
      Serial.println(Temperatura);
      dataFile.close();
     
     }  
     // if the file isn't open, pop up an error:
     else {
         Serial.println("Errore apertura file!!");
     }
     
  if (digitalRead(8)== 1)
  {
      Presenza=1;
      Time = now();
      // digitalClockDisplay();
       File data = SD.open("Presenze.log", FILE_WRITE);
      // if the file is available, write to it:
      if (data)
      {      
        h=String (hour());
        m=String (minute());
        s=String(second());
       
        String date = h+":"+m+":"+s;
        Serial.println(date);
       
        data.println("C'รจ stata una presenza!");
        data.close();
      }  
     // if the file isn't open, pop up an error:
     else
     {
         Serial.println("No presenza");
     }
  }
  else
  {
      Presenza=0;
  }
 
//PAGINA WEB HTTP
Client client = server.available();
if (client) {
boolean currentLineIsBlank = true;  
while (client.connected()) {    
 if (client.available())
   {      
     char c = client.read();        
     readString.concat(c); //store characters to string      

        // COSTRUZIONE PAGINA HTML
        client.println("HTTP/1.1 200 OK");        
        client.println("Content-Type: text/html");        
        client.println();        
        // inizializzo pagina (da togliere se uso ajax)        
        client.print("<html><head><title>ARDUINO Controllo Ambientale Meucci2</title><meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1' ></head><body>");
        //inizai il body
        client.println("<div style='width:360px; height:520px;'>"); //risoluzione per nokia 5800 640x360        
        client.println("<H3>STATUS SENSORI DC MEUCCI-2</H3><hr />");         //Scrive sul browser il valore del termistore        
        client.println("<p>TEMPERATURA = ");    
        client.print(Temperatura);
        client.println(" C <br /></p>");
        client.print("ALLARME PRESENZA: ");
        if (Presenza== 1){
            client.print("<font style='color:red'>HIGH</font>");  
        }
        else
        {
            client.print("LOW");  
        }
         client.println("<br>");      
        //Scrive sul browser il valore della fotoresistenza
        // firma        
        client.println("<hr />");        
        client.print("<h4>Visita <a href='http://www.infracom.it.it' target='_blank' />www.infracom.it</a></h4>");        
        client.println("<hr />");          
        // chiudo il div        
        client.println("</div>");        
        // chiudo pagina da togliere se uso ajax
        client.println("</body></html>");          
        // pulisco la stringa per la successiva lettura        
        readString="";        
        //fermo il client        
        client.stop();          
        }
        //if c == /n    
        }
      // if client available  
    }
  // while client connesso
  delay(500);
  // listen for incoming Ethernet connections:
}



draythomp

To check and see if you are running out of memory distribute some "Serial.println(freeMemory());" in the code.  I've found I need a couple of hundred bytes free or the device dies erratically.  If you are running out of memory, use PSTR to handle strings and get some back.  I noticed you already include MemoryFree.h.

strcpy_P(buffer,PSTR("string text goes in here"));
Serial.println(buffer);

Hope this helps
Trying to keep my house under control http://www.desert-home.com/

moscaciccia

Thanks!

I need some libraries in order to use strcpy_P and PSTR?

Daniele

draythomp

Yes, there is a MemoryFree library, google for the latest location.  I honestly can't remember where I got it a few months ago.  It's simple and works well.
Trying to keep my house under control http://www.desert-home.com/

Go Up