I want to call dtostrf in printf for getting dht11 data but that make hang and in serial monitor just stuck in connecting, but when I make dtostrf to comment that can running and the serial monitor is printing connected and printing query variable.

I use Arduino UNO, ethernet shield, dht11 and use MySQL connector library and use remote SQL for database

#include <Ethernet.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
#include <Dns.h>
#include <DHT.h>
DHT dht(8, DHT11);

byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char hostname[] = "***om";
char user[] = "******6";
char password[] = "******y";

EthernetClient client;
IPAddress server_ip;
MySQL_Connection conn((Client *)&client);
DNSClient dns_client;
MySQL_Cursor cur = MySQL_Cursor(&conn);
// Sample query

void setup() {
  Serial.begin(115200);
  dht.begin();
  Ethernet.begin(mac_addr);
  // Begin DNS lookup
  dns_client.begin(Ethernet.dnsServerIP());
  dns_client.getHostByName(hostname, server_ip);
  Serial.println(server_ip);
  // End DNS lookup
  Serial.println("Connecting...");
  if (conn.connect(server_ip, 3306, user, password)) {
    Serial.println("connected!");
  }
  else
    Serial.println("Connection failed.");
}

void loop() {
delay(2000);
char INSERT_SQL[] = "INSERT INTO ******6.dynamic (user,themp,humidity) VALUES ('%s',%d,%d)";
char temperature[20];
char humidity[20];
char query[128];

Serial.println("Recording data.");
    dtostrf(34.123, 1, 1, temperature);
    dtostrf(67.132, 1, 1, humidity);
    sprintf(query, INSERT_SQL, "trial", temperature, humidity);
  MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
  // Execute the query
  cur_mem->execute(query);
  Serial.print(query);
  // Note: since there are no results, we do not need to read any data
  // Deleting the cursor also frees up memory used
  delete cur_mem;
}

0 Answers