I have a DHT11 temperature sensor and an ultrasonic sensor HC-SR04
Each code works with my database but I can not assemble the two codes.
code dht11
#include <Ethernet.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
#include <dht.h>
#include <String.h>
byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(193,2,1,2); // IP of the MySQL server here
IPAddress server_addr(193,2,1,1); // IP of the MySQL server here
char user[] = "arduino"; // MySQL user login username
char password[] = "1234"; // MySQL user login password
String dbname="arduino";
//
dht DHT;
#define DHTPIN A0 // What pin we're connected to
int t = 0; // temperature
int h = 0; // humidity
// Sample query
char INSERT_SQL[] = "INSERT INTO arduino.templog (temperature,humidity) VALUES(%d,%d)";
EthernetClient client;
char query[128];
MySQL_Connection conn((Client *)&client);
void setup() {
Serial.begin(115200);
while (!Serial); // wait for serial port to connect
Ethernet.begin(mac_addr,ip);
Serial.println("Connecting...");
if (conn.connect(server_addr, 3306,user, password)) {
delay(1000);
}
else
Serial.println("Connection failed.");
}
void loop() {
delay(2000);
Serial.println("Humidity Module ACTION");
DHT.read11(DHTPIN);
t = DHT.temperature;
delay(500);
h = DHT.humidity;
delay(60000);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.println("Recording data.");
// Initiate the query class instance
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
sprintf(query, INSERT_SQL,t,h);
// Execute the query
cur_mem->execute(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;
}
code hc-sr04
#include <Ethernet.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
#include <String.h>
byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(193,2,1,2); // IP of the MySQL server here
IPAddress server_addr(193,2,1,1); // IP of the MySQL server here
char user[] = "arduino"; // MySQL user login username
char password[] = "1234"; // MySQL user login password
String dbname="arduino";
// defines pins numbers
const int trigPin = 8;
const int echoPin = 9;
// defines variables
long duration;
int distance;
// Sample query
char INSERT_SQL[] = "INSERT INTO arduino.distance (distance) VALUES(%d)";
EthernetClient client;
char query[128];
MySQL_Connection conn((Client *)&client);
void setup() {
Serial.begin(115200);
while (!Serial); // wait for serial port to connect
Ethernet.begin(mac_addr,ip);
Serial.println("Connecting...");
if (conn.connect(server_addr, 3306,user, password)) {
delay(1000);
}
else
Serial.println("Connection failed.");
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(5000);
// commencer en 10 millisecond
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// calcul de distance
distance= duration*0.034/2;
if (isnan(trigPin) || isnan(echoPin)) {
Serial.println("Failed to read from sensor!");
return;
}
Serial.println("Recording data.");
// Initiate the query class instance
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
sprintf(query, INSERT_SQL,distance);
// Execute the query
cur_mem->execute(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;
}