Hello,
I am building a Wireless Sensor network using Arduino with an Ethernet shield and an MySQL database server running on Raspberry Pi. I am trying to read temperature and humidity values from a DHT22 sensor, and store this on a MySQL database.
#include <SPI.h>
#include <Ethernet.h>
#include <sha1.h>
#include "mysql.h"
#include <DHT22.h>
byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server_addr(10, 0, 0, 24);
char user[] = "root";
char password[] = "secret";
Connector my_conn; // The Connector/Arduino reference
#define DHT22_PIN 7 // DHT22 data is on pin 7
#define read_delay 5000 // 5 seconds
DHT22 myDHT22(DHT22_PIN); // DHT22 instance
void read_data() {
DHT22_ERROR_t errorCode;
errorCode = myDHT22.readData();
switch(errorCode)
{
case DHT_ERROR_NONE:
char buf[128];
sprintf(buf, "INSERT INTO dht22_test.temp_humid VALUES (NULL, %hi.%01hi, %i.%01i)",
myDHT22.getTemperatureCInt()/10,
abs(myDHT22.getTemperatureCInt()%10),
myDHT22.getHumidityInt()/10,
myDHT22.getHumidityInt()%10);
my_conn.cmd_query(buf);
Serial.println("Data read and recorded.");
break;
case DHT_ERROR_CHECKSUM:
Serial.print("check sum error ");
Serial.print(myDHT22.getTemperatureC());
Serial.print("C ");
Serial.print(myDHT22.getHumidity());
Serial.println("%");
break;
case DHT_BUS_HUNG:
Serial.println("BUS Hung ");
break;
case DHT_ERROR_NOT_PRESENT:
Serial.println("Not Present ");
break;
case DHT_ERROR_ACK_TOO_LONG:
Serial.println("ACK time out ");
break;
case DHT_ERROR_SYNC_TIMEOUT:
Serial.println("Sync Timeout ");
break;
case DHT_ERROR_DATA_TIMEOUT:
Serial.println("Data Timeout ");
break;
case DHT_ERROR_TOOQUICK:
Serial.println("Polled too quick ");
break;
}
}
void setup() {
Ethernet.begin(mac_addr);
Serial.begin(115200);
delay(1000);
Serial.println("Connecting...");
if (my_conn.mysql_connect(server_addr, 3306, user, password))
delay(500);
else
Serial.println("Connection failed.");
}
void loop() {
delay(read_delay);
read_data();
}
That is the code I am using for this project. It compiles correctly and I uploaded this sketch to the Arduino. I open the serial monitor and wait for the data values, but the serial monitor is just stuck at "Connecting..." for a really long time. I tried reading values from DHT22 and connecting to the MySQL server separately and that works fine. However, the integration does not seem to be working.
According to the code, it should display a "Connection Failed..." message 0.5s after attempting to connect if connection is not successful. However, this is not happening. Your help would be greatly appreciated in this matter.