[EDITED]
Hey ! J'aurais besoin d'aide pour mon code en cours de développement.
Je débute dans le monde de la programmation Arduino.
Je suis en train de développer un détecteur de courrier/colis pour ma boîte aux lettres à l'aide d'un capteur de distance à ultrasons : le HC-SR04. J'utilise un Wemos D1 pour la partie Wifi.
Le projet final devrait comporter une alimentation externe et le code, inclure la fonction deep sleep pour augmenter l'autonomie et n'effectuer les mesures que 2 ou 3 fois dans la journée.
J'ai combiné 2 codes trouvés sur le net (email & mesure par le HC-SR04 + conversion en distance en cm).
Les 2 codes fonctionnent séparément.
La mesure se fait correctement | le mail est bien reçu sur ma messagerie.
MA QUESTION : Je voudrais que le mail contienne la valeur calculée, par quelle fonction puis-je y arriver ?
Quand je combine les 2 (voir le code plus bas), j'ai cette erreur :
'distance' was not declared in this scope, alors que j'ai déclaré dans le void setup() :
J'ai essayé de récupérer la valeur en l'intégrant dans un int mesure = distance;
J'arrive bien à afficher le contenu de int = mesure dans le moniteur série.
Ce que je ne comprends pas, c'est pourquoi j'ai cette erreur, alors que je l'ai déclarée dans le void setup ?
Je sèche pour intégrer cette valeur dans le mail à envoyer.
Merci de m'éclairer.
Voici le code :
#include <ESP8266WiFi.h>
const char* sketchname = "Boîte_aux_lettres_ALERTE_V1.0";
const char* ssid = "XXXXX"; // Enter the SSID of your WiFi Network.
const char* password = "XXXXX";// Enter the Password of your WiFi Network.
char server[] = "mail.smtp2go.com"; // The SMTP Server
#define TRIGGER_PIN D2 //
#define ECHO_PIN D1 //
WiFiClient espClient;
void setup()
{
Serial.begin(115200);
Serial.println(sketchname);
pinMode(TRIGGER_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
double duration, distance;
digitalWrite(TRIGGER_PIN, LOW); // Get Start
delayMicroseconds(2); // stable the line
digitalWrite(TRIGGER_PIN, HIGH); // sending 10 us pulse
delayMicroseconds(10); // delay
digitalWrite(TRIGGER_PIN, LOW); // after sending pulse wating to receive signals
duration = pulseIn(ECHO_PIN, HIGH); // calculating time
distance = (duration/2) / 29.1; // single path
Serial.print(distance);
Serial.println(" cm");
int mesure = distance;
delay(10);
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.");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
byte ret = sendEmail();
}
void loop()
{
}
byte sendEmail()
{
if (espClient.connect(server, 2525) == 1)
{
Serial.println(F("connected"));
}
else
{
Serial.println(F("connection failed"));
return 0;
}
if (!emailResp())
return 0;
//
Serial.println(F("Sending EHLO"));
espClient.println("EHLO www.example.com");
if (!emailResp())
return 0;
//
/*Serial.println(F("Sending TTLS"));
espClient.println("STARTTLS");
if (!emailResp())
return 0;*/
//
Serial.println(F("Sending auth login"));
espClient.println("AUTH LOGIN");
if (!emailResp())
return 0;
//
Serial.println(F("USERNAME"));
// Change this to your base64, ASCII encoded username
/*
For example, the email address test@gmail.com would be encoded as dGVzdEBnbWFpbC5jb20=
*/
espClient.println("XXXXXXX"); //base64, ASCII encoded Username
if (!emailResp())
return 0;
//
Serial.println(F("PASSWORD"));
// change to your base64, ASCII encoded password
/*
For example, if your password is "testpassword" (excluding the quotes),
it would be encoded as dGVzdHBhc3N3b3Jk
*/
espClient.println("XXXXXXX");//base64, ASCII encoded Password
if (!emailResp())
return 0;
//
Serial.println(F("Sending From"));
// change to sender email address
espClient.println(F("MAIL From: XXXXXX@gmail.com"));
if (!emailResp())
return 0;
// change to recipient address
Serial.println(F("Sending To"));
espClient.println(F("RCPT To: XXXXXX@gmail.com"));
if (!emailResp())
return 0;
//
Serial.println(F("Sending DATA"));
espClient.println(F("DATA"));
if (!emailResp())
return 0;
Serial.println(F("Sending email"));
// change to recipient address
espClient.println(F("To: XXXXXXX@gmail.com"));
// change to your address
espClient.println(F("From: XXXXXXX@gmail.com"));
espClient.println(F("Subject: BOÎTE AUX LETTRES\r\n"));
espClient.println(F("This is is a test e-mail sent from ESP8266.\n"));
espClient.println(mesure); // >>> ERREUR 'mesure' was not declared in this scope
espClient.println(F("Third line of the test e-mail."));
//
espClient.println(F("."));
if (!emailResp())
return 0;
//
Serial.println(F("Sending QUIT"));
espClient.println(F("QUIT"));
if (!emailResp())
return 0;
//
espClient.stop();
Serial.println(F("disconnected"));
return 1;
}
byte emailResp()
{
byte responseCode;
byte readByte;
int loopCount = 0;
while (!espClient.available())
{
delay(1);
loopCount++;
// Wait for 20 seconds and if nothing is received, stop.
if (loopCount > 20000)
{
espClient.stop();
Serial.println(F("\r\nTimeout"));
return 0;
}
}
responseCode = espClient.peek();
while (espClient.available())
{
readByte = espClient.read();
Serial.write(readByte);
}
if (responseCode >= '4')
{
// efail();
return 0;
}
return 1;
}