Bonjour,
J'essaye de configurer un ESP32 pour transmettre les valeurs relevees par des capteurs (DHT22 et BMP) a une base de donnee sur un raspberry pi en local.
J'ai suivi ce tuto: ESP32/ESP8266 Insert Data into MySQL Database | Random Nerd Tutorials
Et ben... ca marche pas.....
Voila ce que j'obtient dans le serial Moniteur d"Arduino IDE :
Connected to WiFi network with IP Address: 192.168.8.136
Pressure = 1008.00 mb
Altitude = 42.21 meters
Humidity = 40.80 %
BMP Temperature = 33.50 *C
DHT Temperature = 32.40 *C
DHT + BMP Average Temp = 32.95 *C
httpRequestData: api_key= tPmAT5Ab3j7F9&sensor=GY68&location=Living room&value1=32.40&value2=40.80&value3=1008.21
Error code: -1
J'ai bien LAMP installe sur le raspi, la DB existe, etc.....
Mais j'obtient cette erreur meme lorsque le raspi est eteint.... Je pense donc qu'il y a un bug dans le code sur l'ESP32.....
Voila le code en question:
/*
Rui Santos
Complete project details at https://RandomNerdTutorials.com/esp32-esp8266-mysql-database-php/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <HTTPClient.h>
#include "DHT.h"
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085.h>
// Replace with your network credentials
const char* ssid = "Yann";
const char* password = "16421642";
// REPLACE with your Domain name and URL path or IP address with path
const char* serverName = "HTTP://192.168.8.140";
// Keep this API Key value to be compatible with the PHP code provided in the project page.
// If you change the apiKeyValue value, the PHP file /post-esp-data.php also needs to have the same key
String apiKeyValue = "tPmAT5Ab3j7F9";
String sensorName = "GY68";
String sensorLocation = "Living room";
// Humidity qnd Temperature
#define DHTPIN 13
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#define SEALEVELPRESSURE_HPA (1013.25)
//Pressure and Altitude
Adafruit_BMP085 bmp;
//WiFi.mode(WIFI_STA);
//WiFiServer server(80);
//WiFiClient client = server.available();
void setup() {
Serial.begin(115200);
delay(1000);
dht.begin();
if (!bmp.begin()) {
Serial.println("Could not find a valid BMP085 sensor, check wiring!");
while (1) {}
}
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
}
void loop() {
//Check WiFi connection status
if(WiFi.status()== WL_CONNECTED){
WiFiClientSecure *client = new WiFiClientSecure;
client->setInsecure(); //don't use SSL certificate
HTTPClient https;
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
Serial.println("");
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("Failed to read from DHT sensor!");
return;
}
float p = bmp.readPressure()/100;
Serial.print("Pressure = ");
Serial.print(p);
Serial.println(" mb");
// Calculate altitude assuming 'standard' barometric
// pressure of 1013.25 millibar = 101325 Pascal
float a = bmp.readAltitude();
float bt = bmp.readTemperature();
Serial.print("Altitude = ");
Serial.print(a);
Serial.println(" meters");
Serial.print("Humidity = ");
Serial.print(h);
Serial.println(" %\t");
Serial.print("BMP Temperature = ");
Serial.print(bmp.readTemperature());
Serial.println(" *C ");
Serial.print("DHT Temperature = ");
Serial.print(t);
Serial.println(" *C ");
Serial.print("DHT + BMP Average Temp = ");
Serial.print((bt + t) /2);
Serial.println(" *C ");
Serial.println();
delay(1000);
// Your Domain name with URL path or IP address with path
https.begin(serverName);
// Specify content-type header
https.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Prepare your HTTP POST request data
String httpRequestData = "api_key=" + apiKeyValue + "&sensor=" + sensorName
+ "&location=" + sensorLocation + "&value1=" + String(dht.readTemperature())
+ "&value2=" + String(dht.readHumidity()) + "&value3=" + String(bmp.readPressure()/100.0F) + "";
Serial.print("httpRequestData: ");
Serial.println(httpRequestData);
// You can comment the httpRequestData variable above
// then, use the httpRequestData variable below (for testing purposes without the BME280 sensor)
//String httpRequestData = "api_key=tPmAT5Ab3j7F9&sensor=BME280&location=Office&value1=24.75&value2=49.54&value3=1005.14";
// Send HTTP POST request
int httpResponseCode = https.POST(httpRequestData);
// If you need an HTTP request with a content type: text/plain
//https.addHeader("Content-Type", "text/plain");
//int httpResponseCode = https.POST("Hello, World!");
// If you need an HTTP request with a content type: application/json, use the following:
//https.addHeader("Content-Type", "application/json");
//int httpResponseCode = https.POST("{\"value1\":\"19\",\"value2\":\"67\",\"value3\":\"78\"}");
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
https.end();
}
else {
Serial.println("WiFi Disconnected");
}
//Send an HTTP POST request every 30 seconds
delay(5000);
}
l'adresse IP du serveur apache sur le raspi est correcte (j'y accede depuis n'importe quel autre ordi sans probleme.....)
Une idee ? une suggestion?
Merci de votre aide