Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <Ethernet.h>
- #include <PubSubClient.h>
- #include <MySQL_Connection.h>
- #include <MySQL_Cursor.h>
- const char* ssid = "xxxxxxxxx"; // wifi ssid da rede
- const char* password = "xxxxxxxxxxxx"; // wifi password rede
- const char* mqttServer = "xxx.xxx.xxx.xxx"; // Endereço IP do Raspberry Pi
- const int mqttPort = 1883; // Porto por padrão do MQTT
- const char* mqttUser = "teste"; // MQTT Username, caso seja configurada a autenticação no broker
- const char* mqttPassword = "teste"; // MQTT Password, caso seja configurada a autenticação no broker
- float tempC;// Define variável tempC convertendo-a para o tipo de dado float.
- int reading;// Define variável reading convertendo-a para o tipo de dado int.
- float referenceVoltage; // Define variável referenceVoltage convertendo-a para o tipo de dado float.
- int tempPin = A0; //Pino analógico onde se encontra conetado o LM35
- String convert; // Inicialização da variável convert como String
- char array[6]; // Inicialização do Array de char com 6 posições
- WiFiClient espClient;// Cria o objeto espCliente
- PubSubClient client(espClient);// Instancia o Cliente MQTT passando o objecto espClient
- IPAddress server_addr(xxx,xxx,xxx,xxx); // IP of the MySQL *server* here
- char login_username[] = "xxxxx"; // MySQL user login username
- char login_password[] = "xxxx"; // MySQL user login password
- //// Sample query
- char INSERT_SQL[] = "INSERT INTO MQTT.TESTES (Temperatura) VALUES (%.3f)";
- char query[255];
- MySQL_Connection conn(&espClient);
- MySQL_Cursor* cursor;
- void setup() {
- Serial.begin(115200);
- /*Conexão á rede Wi-Fi*/
- referenceVoltage = 1;// Tensão de referência do ESP82666
- /*Conexão á rede Wi-Fi*/
- Serial.printf("nConnecting to %s", ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- /*Conexão ao MQTT*/
- client.setServer(mqttServer, mqttPort);
- while (!client.connected()) {
- Serial.println("Connecting to MQTT...");
- if (client.connect("ESP8266Client", mqttUser, mqttPassword )) {
- Serial.println("connected to the MQTT");
- } else {
- Serial.print("failed with state ");
- Serial.print(client.state());
- delay(5000);
- }
- }
- // print out info about the connection:
- Serial.println("nConnected to network");
- Serial.print("My IP address is: ");
- Serial.println(WiFi.localIP());
- /*Conexão á base de dados SQL*/
- Serial.println("Connecting to SQL... ");
- if (conn.connect(server_addr, 3306, login_username, login_password))
- Serial.println("OK.");
- else
- Serial.println("FAILED.");
- // cria o objecto MySQL cursor
- cursor = new MySQL_Cursor(&conn);
- }
- void loop() {
- reading = 0;
- for (int i = 0; i < 10; i++) { // Média de 10 leituras para que a leitura seja mais precisa
- reading += analogRead(tempPin);
- //Serial.println(reading);// Mostra os valores das 10 leituras
- delay(20);// Tempo de leitura do pino A0 a cada 20 milisegundos
- }
- // Conversão da tensão analógica em valor de temperatura em graus Celsius e para um array
- tempC = (referenceVoltage * reading * 10) / 1023;
- convert += tempC; // Converte o valor da variável tempC em string
- convert.toCharArray(array, 6);// converte a string num array de char com 6 posições
- client.publish("esp8266", array);
- Serial.println(array);
- convert = "";
- client.loop();
- delay(2000);
- Serial.print(query);
- if (conn.connected())
- sprintf(query, INSERT_SQL, tempC);
- cursor->execute(query);
- delay(2000);
Add Comment
Please, Sign In to add comment