DHT11 sensor and internet communication

Hi, I'm trying to create a log of registered temperatures using arduino nano, temperature/moisture sensor DHT11 and Ethernet shield HR911105A.
I put together 2 examples of sketches from the EtherCard and the DHT Libraries.
So combining them I got this piece of code:

// Demo using DHCP and DNS to perform a web client request.
// 2011-06-08 <jc@wippler.nl>
//
// License: GPLv2
#include "DHT.h"
#include <EtherCard.h>

#define DHTPIN 9    
#define DHTTYPE DHT11   // DHT 11

// ethernet interface mac address, must be unique on the LAN
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
DHT dht(DHTPIN, DHTTYPE);

byte Ethernet::buffer[700];
static uint32_t timer;
const char website[] PROGMEM = "www.example.com";
const  char* webpage = "temp_log.php?temp=";
// called when the client request is complete
static void my_callback (byte status, word off, word len) {
  Serial.println(">>>");
  Ethernet::buffer[off+300] = 40;
  Serial.print((const char*) Ethernet::buffer + off);
  Serial.println("...");
}

void setup () {
  dht.begin();
  Serial.begin(57600);
  Serial.println(F("\n[webClient]"));
  ether.printIp("DHCP server: ", ether.dhcpip);
  // Change 'SS' to your Slave Select pin, if you arn't using the default pin
  if (ether.begin(sizeof Ethernet::buffer, mymac, SS) == 0)
    Serial.println(F("Failed to access Ethernet controller"));
  if (!ether.dhcpSetup())
    Serial.println(F("DHCP failed"));
  ether.printIp("IP:  ", ether.myip);
  ether.printIp("GW:  ", ether.gwip);
  ether.printIp("DNS: ", ether.dnsip);

#if 1
  // use DNS to resolve the website's IP address
  if (!ether.dnsLookup(website))
    Serial.println("DNS failed");
#elif 2
  // if website is a string containing an IP address instead of a domain name,
  // then use it directly. Note: the string can not be in PROGMEM.
  char websiteIP[] = "142.250.184.68";
  ether.parseIp(ether.hisip, websiteIP);
#else
  // or provide a numeric IP address instead of a string
  byte hisip[] = { 142,250,184,68 };
  ether.copyIp(ether.hisip, hisip);
#endif

  ether.printIp("SRV: ", ether.hisip);
}

void loop () {

  char* finstr;
  finstr = malloc(strlen(webpage)+1+5);
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  ether.packetLoop(ether.packetReceive());
  if (millis() > timer) {

    // Check if any reads failed and exit early (to try again).
    if (isnan(h) || isnan(t)) {
      Serial.println(F("Failed to read from DHT sensor!"));
      return;
    }
    timer = millis() + 5*60UL*1000;
    Serial.print("<<< REQ ");
    Serial.println(t);
    char result[8]; // Buffer big enough for 7-character float
    dtostrf(t, 3, 2, result); // Leave room for too large numbers!
    
    strcpy(finstr, webpage); /* copy name into the new var */
    strcat(finstr, result); /* add the extension */
    //Serial.println(finstr);
    ether.browseUrl(PSTR("/"), finstr, website, my_callback);
    
  }
}

Everything works fine when I upload the sketch, but after a couple of loop, it just stop woring; do you have any idea why?

In every loop you allocate memory but you never return it.
As the size is constant, allocate it on the stack (local variable) and don't use malloc() at all.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.