#include #include #include #include "DHT.h" #define DHTPIN 2 #define DHTTYPE DHT22 byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; IPAddress ip(192, 168, 0, 20); // Endereco IP que a Ethernet Shield terá. Deve ser alterado para um endereço livre da sua rede. IPAddress gateway(192, 168, 0, 10 ); //Define o gateway IPAddress subnet(255, 255, 255, 0); //Define a mascara de rede IPAddress server_addr(192, 168, 0, 10); // IP of the MySQL server here char user[] = "arduino"; // MySQL user login username char password[] = "arduino"; // MySQL user login password char INSERT_SQL[] = "INSERT INTO bd_arduino.tb_arduino (id_temperatura,temperatura) VALUES ('',%s)"; char query[128]; char valor1[10]; EthernetClient client; MySQL_Connection conn((Client *)&client); //Ultrasonic ultrasonic(TRIGGER_PIN, ECHO_PIN); DHT dht(DHTPIN, DHTTYPE); void setup() { Serial.begin(9600); Serial.println(F("DHTxx test!")); dht.begin(); Ethernet.begin(mac_addr, ip, gateway, subnet); Serial.println("Connecting..."); if (conn.connect(server_addr, 3306, user, password)) { delay(300); Serial.println("Conectado, Jenzura"); } else { Serial.println("Connection failed."); } } void loop() { delay(2000); //float cmMsec; //long microsec = ultrasonic.timing(); float h = dht.readHumidity(); // Read temperature as Celsius (the default) float t = dht.readTemperature(); // Read temperature as Fahrenheit (isFahrenheit = true) float f = dht.readTemperature(true); // Check if any reads failed and exit early (to try again). if (isnan(h) || isnan(t) || isnan(f)) { Serial.println(F("Failed to read from DHT sensor!")); return; } // Compute heat index in Fahrenheit (the default) float hif = dht.computeHeatIndex(f, h); // Compute heat index in Celsius (isFahreheit = false) float hic = dht.computeHeatIndex(t, h, false); Serial.print(F("Humidity: ")); Serial.print(h); Serial.print(F("% Temperature: ")); Serial.print(t); Serial.print(F("°C ")); Serial.print(f); Serial.print(F("°F Heat index: ")); Serial.print(hic); Serial.print(F("°C ")); Serial.print(hif); Serial.println(F("°F")); //cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM); MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn); // Save //dtostrf(microsec, 1, 1, valor_1); //3casas para esquerda e 2 para direita dtostrf(t, 3, 2, valor1); sprintf(query, INSERT_SQL, valor1); // 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; //Serial.print("MS: "); //Serial.print(microsec); //Serial.print(", CM: "); //Serial.println(cmMsec); delay(1000); }