Guest User

Untitled

a guest
Oct 1st, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <Ethernet.h>
  3. #include <PubSubClient.h>
  4. #include <MySQL_Connection.h>
  5. #include <MySQL_Cursor.h>
  6.  
  7.  
  8. const char* ssid = "xxxxxxxxx"; // wifi ssid da rede
  9. const char* password = "xxxxxxxxxxxx"; // wifi password rede
  10. const char* mqttServer = "xxx.xxx.xxx.xxx"; // Endereço IP do Raspberry Pi
  11. const int mqttPort = 1883; // Porto por padrão do MQTT
  12. const char* mqttUser = "teste"; // MQTT Username, caso seja configurada a autenticação no broker
  13. const char* mqttPassword = "teste"; // MQTT Password, caso seja configurada a autenticação no broker
  14.  
  15. float tempC;// Define variável tempC convertendo-a para o tipo de dado float.
  16. int reading;// Define variável reading convertendo-a para o tipo de dado int.
  17. float referenceVoltage; // Define variável referenceVoltage convertendo-a para o tipo de dado float.
  18. int tempPin = A0; //Pino analógico onde se encontra conetado o LM35
  19. String convert; // Inicialização da variável convert como String
  20. char array[6]; // Inicialização do Array de char com 6 posições
  21. WiFiClient espClient;// Cria o objeto espCliente
  22. PubSubClient client(espClient);// Instancia o Cliente MQTT passando o objecto espClient
  23.  
  24.  
  25. IPAddress server_addr(xxx,xxx,xxx,xxx); // IP of the MySQL *server* here
  26. char login_username[] = "xxxxx"; // MySQL user login username
  27. char login_password[] = "xxxx"; // MySQL user login password
  28. //// Sample query
  29. char INSERT_SQL[] = "INSERT INTO MQTT.TESTES (Temperatura) VALUES (%.3f)";
  30. char query[255];
  31. MySQL_Connection conn(&espClient);
  32. MySQL_Cursor* cursor;
  33.  
  34.  
  35. void setup() {
  36. Serial.begin(115200);
  37.  
  38. /*Conexão á rede Wi-Fi*/
  39. referenceVoltage = 1;// Tensão de referência do ESP82666
  40. /*Conexão á rede Wi-Fi*/
  41. Serial.printf("nConnecting to %s", ssid);
  42. WiFi.begin(ssid, password);
  43. while (WiFi.status() != WL_CONNECTED) {
  44. delay(500);
  45. Serial.print(".");
  46. }
  47. /*Conexão ao MQTT*/
  48. client.setServer(mqttServer, mqttPort);
  49. while (!client.connected()) {
  50. Serial.println("Connecting to MQTT...");
  51. if (client.connect("ESP8266Client", mqttUser, mqttPassword )) {
  52. Serial.println("connected to the MQTT");
  53. } else {
  54. Serial.print("failed with state ");
  55. Serial.print(client.state());
  56. delay(5000);
  57. }
  58. }
  59.  
  60. // print out info about the connection:
  61. Serial.println("nConnected to network");
  62. Serial.print("My IP address is: ");
  63. Serial.println(WiFi.localIP());
  64. /*Conexão á base de dados SQL*/
  65. Serial.println("Connecting to SQL... ");
  66. if (conn.connect(server_addr, 3306, login_username, login_password))
  67. Serial.println("OK.");
  68. else
  69. Serial.println("FAILED.");
  70. // cria o objecto MySQL cursor
  71. cursor = new MySQL_Cursor(&conn);
  72. }
  73. void loop() {
  74.  
  75. reading = 0;
  76. for (int i = 0; i < 10; i++) { // Média de 10 leituras para que a leitura seja mais precisa
  77. reading += analogRead(tempPin);
  78. //Serial.println(reading);// Mostra os valores das 10 leituras
  79. delay(20);// Tempo de leitura do pino A0 a cada 20 milisegundos
  80. }
  81. // Conversão da tensão analógica em valor de temperatura em graus Celsius e para um array
  82. tempC = (referenceVoltage * reading * 10) / 1023;
  83. convert += tempC; // Converte o valor da variável tempC em string
  84. convert.toCharArray(array, 6);// converte a string num array de char com 6 posições
  85. client.publish("esp8266", array);
  86. Serial.println(array);
  87. convert = "";
  88. client.loop();
  89. delay(2000);
  90.  
  91. Serial.print(query);
  92. if (conn.connected())
  93. sprintf(query, INSERT_SQL, tempC);
  94. cursor->execute(query);
  95. delay(2000);
Add Comment
Please, Sign In to add comment