Très attendue cette commande est enfin arrivée avec les esp8266 et surtout sa mise en œuvre Wemos:
et depuis j’utilise beaucoup moins mes arduinos….
La station météo ressemble désormais à ça :
C’est quand même plus simple….plus besoin d’Android et ses liaisons BlueTooth et Wifi, l’esp transmettant directement les données sur mon site
le code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
#include <ESP8266WiFi.h> #include <Wire.h> // required by BME280 library #include <BME280_t.h> #include <ESP8266HTTPClient.h> #define ASCII_ESC 27 #define MYALTITUDE 150.50 char bufout[10]; BME280<> BMESensor; const char* ssid = "XXXXXXXX"; const char* password = "XXXXXXXXX"; long previousMillis = 0; unsigned long currentMillis = 0; long interval = 300000; // READING INTERVAL String data; //EthernetClient Eclient; WiFiServer server(80); void setup() { Serial.begin(115200); Wire.begin(0, 2); // initialize I2C that connects to sensor BMESensor.begin(); delay(10); // Connect to WiFi network Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); // Start the server server.begin(); Serial.println("Server started"); // Print the IP address Serial.print("Use this URL to connect: "); Serial.print("http://"); Serial.print(WiFi.localIP()); Serial.println("/"); } void loop() { BMESensor.refresh(); // read current sensor data sprintf(bufout, "%c[1;0H", ASCII_ESC); Serial.print(bufout); //Serial.write(27); // ESC command //Serial.print("[2J"); // clear screen command //Serial.write(27); //Serial.print("[H"); Serial.print("Temperature: "); Serial.print(BMESensor.temperature); // display temperature in Celsius Serial.println("C"); Serial.print("Humidity: "); Serial.print(BMESensor.humidity); // display humidity in % Serial.println("%"); Serial.print("Pressure: "); Serial.print(BMESensor.pressure / 100.0F); // display pressure in hPa Serial.println("hPa"); float relativepressure = BMESensor.seaLevelForAltitude(MYALTITUDE); Serial.print("RelPress: "); Serial.print(relativepressure / 100.0F); // display relative pressure in hPa for given altitude Serial.println("hPa"); Serial.print("Altitude: "); Serial.print(BMESensor.pressureToAltitude(relativepressure)); // display altitude in m for given pressure Serial.println("m"); delay(5000); //*************************************************************** currentMillis = millis(); if (currentMillis - previousMillis > interval) { // READ ONLY ONCE PER INTERVAL previousMillis = currentMillis; String t = String(BMESensor.temperature, 2); String h = String(BMESensor.humidity, 2); String p = String( BMESensor.pressure); data = "?temp1=" + t + "&hum1=" + h + "&pres1=" + p; HTTPClient http; Serial.print("[HTTP] begin...\n"); http.begin("http://www.xxxxxxxxxxxxxxxxxx.php"+data); //HTTP Serial.print("[HTTP] GET...\n"); Serial.print("http://www.xxxxxxxxxxxxxxx.php");Serial.print(data);Serial.print("\n"); // start connection and send HTTP header int httpCode = http.GET(); // httpCode will be negative on error if (httpCode > 0) { // HTTP header has been send and Server response header has been handled Serial.printf("[HTTP] GET... code: %d\n", httpCode); // file found at server if (httpCode == HTTP_CODE_OK) { String payload = http.getString(); Serial.println(payload); } } else { Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); } http.end(); } // Check if a client has connected WiFiClient client = server.available(); if (!client) { return; } // Wait until the client sends some data Serial.println("new client"); while (!client.available()) { delay(1); } // Return the response client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println(""); // do not forget this one client.println("<!DOCTYPE HTML>"); client.println("<html>"); client.println("<head><META HTTP-EQUIV=\"refresh\" CONTENT=\"10\"></head><body>"); client.println("Mesures du <SCRIPT LANGUAGE=\"JavaScript\"> var maintenant=new Date();document.write(maintenant);</SCRIPT>: <br>"); client.println("Température:"); client.println(BMESensor.temperature); client.println("°C<br>"); client.println("Humidité: "); client.println(BMESensor.humidity); // display humidity in % client.println("%<br>"); client.println("Pression: "); client.println(BMESensor.pressure / 100.0F); // display pressure in hPa client.println("hPa"); client.println("</body></html>"); delay(1); Serial.println("Client disonnected"); Serial.println(""); } |
les données sont stockées dans une base mysql par le script .php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php include ("connexion.php"); $temp1=$_GET["temp1"]; $hum1=$_GET["hum1"]; $pres1=$_GET["pres1"]; $query = "INSERT INTO meteo2(date, temperature, humidite, pression) VALUES (CURRENT_TIMESTAMP,'$temp1','$hum1','$pres1')"; $result = mysqli_query($link, $query); mysqli_close($link); header("Location: index.php"); ?> |
les résultats ici: daniel974/meteo2/