#include #include #include #include #define TRIGGER_PIN 8 #define ECHO_PIN 7 byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; IPAddress ip(192, 168, 0, 20); // Endereço 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 máscara 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_iot (id_sensor,valor1) VALUES ('',%s)"; char query[128]; char valor1[10]; EthernetClient client; MySQL_Connection conn((Client *)&client); Ultrasonic ultrasonic(TRIGGER_PIN, ECHO_PIN); void setup() { Serial.begin(9600); 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() { float cmMsec; long microsec = ultrasonic.timing(); cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM); MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn); //dtostrf(microsec, 1, 1, valor_1); dtostrf(cmMsec, 3, 2, valor1);//3casas para esquerda e 2 para direita 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);