La plante a besoin d’être arrosée

image_pdfimage_print

Un capteur d’humidité est connecté à un module Arduino pour savoir si une plante a besoin d’être arrosée. Si la plante a besoin d’être arrosée, une notification est envoyée sur un téléphone Android (voir le second lien ci-dessous) et la matrice de leds affiche une tête triste.

Le module ESP8266 doit être configuré en 9600 bds (voir premier lien ci-dessous). Il faut une alimentation externe, n’alimenter que par le port USB du PC ne suffit pas.

L’envoi de SMS doit être possible (en particulier via Free mobile) mais il faut probablement utiliser une autre librairie pour la connexion WIFI (elle doit gérer le https).

https://home.et.utwente.nl/slootenvanf/2018/03/17/connect-esp8266-control-blynk/

https://www.geekstips.com/android-push-notifications-esp8266-arduino-tutorial/

  • Matériel
    • 1 module Arduino Nano
    • 1 module ESP8266 pour la connexion WIFI
    • 1 capteur d’humidité comme sur le schéma ci-dessous
    • 1 matrice de leds avec MAX7219
    • 1 résistance de 10 kO
    • 1 plaque à insertion
    • Des fils
    • Une alimentation externe 5V et 3.3V
  • Le montage

  • Le code
#include "LowPower.h"
#include "LedControl.h"
#include "binary.h"
#include "WiFiEsp.h"

// Emulate WifiSerial on pins 2/3 if not present
//#ifndef HAVE_HWWifiSerial
#include "SoftwareSerial.h"
SoftwareSerial WifiSerial(7, 6); // RX, TX
//#endif

// PushingBox scenario DeviceId code and API
String deviceId = "xxxxxxx";
const char* logServer = "api.pushingbox.com";
char ssid[] = "SON_SSID";
char pass[] = "SON_MOT_DE_PASSE";

int status = WL_IDLE_STATUS;     // the Wifi radio's status

LedControl lc=LedControl(12,11,10,1);

// delay time between faces
unsigned long delaytime=1000;

unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds

// happy face
byte hf[8]= {B00111100,B01000010,B10100101,B10000001,B10100101,B10011001,B01000010,B00111100};
// neutral face
byte nf[8]={B00111100, B01000010,B10100101,B10000001,B10111101,B10000001,B01000010,B00111100};
// sad face
byte sf[8]= {B00111100,B01000010,B10100101,B10000001,B10011001,B10100101,B01000010,B00111100};

void setup()
{
  Serial.begin(115200);
  
  // No setup is required for this library

 lc.shutdown(0,false);
  // Set brightness to a medium value
  lc.setIntensity(0,8);
  // Clear the display
  lc.clearDisplay(0);  

WifiSerial.begin(9600);
  // initialize ESP module
  WiFi.init(&WifiSerial);

  // check for the presence of the shield
 if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue
    while (true);
  }

 
  // attempt to connect to WiFi network
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }

  // you're connected now, so print out the data
  Serial.println("You're connected to the network");
   Serial.println();
  printCurrentNet();
  printWifiData();

}


void sendNotificationAndroid(String message){
 // initialize serial for ESP module
  
WiFiEspClient client;
    
  Serial.println("- connecting to pushing server: " + String(logServer));
  if (client.connect(logServer, 80)) {
    Serial.println("- succesfully connected");
    
    String postStr = "devid=";
    postStr += String(deviceId);
    postStr += "&message_parameter="+message;
    postStr += "\r\n\r\n";
    
    Serial.println("- sending data...");
    
    client.print("POST /pushingbox HTTP/1.1\n");
    client.print("Host: api.pushingbox.com\n");
    client.print("Connection: close\n");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
    client.print("Content-Length: ");
    client.print(postStr.length());
    client.print("\n\n");
    client.print(postStr);
  }
  client.stop();
  Serial.println("- stopping the client");
}

void printWifiData()
{
  // print your WiFi shield's IP address
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print your MAC address
  byte mac[6];
  WiFi.macAddress(mac);
  char buf[20];
  sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", mac[5], mac[4], mac[3], mac[2], mac[1], mac[0]);
  Serial.print("MAC address: ");
  Serial.println(buf);
}

void printCurrentNet()
{
  // print the SSID of the network you're attached to
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print the MAC address of the router you're attached to
  byte bssid[6];
  WiFi.BSSID(bssid);
  char buf[20];
  sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", bssid[5], bssid[4], bssid[3], bssid[2], bssid[1], bssid[0]);
  Serial.print("BSSID: ");
  Serial.println(buf);

  // print the received signal strength
  long rssi = WiFi.RSSI();
  Serial.print("Signal strength (RSSI): ");
  Serial.println(rssi);
}

void drawSadFace(){
  // Display sad face
  lc.setRow(0,0,sf[0]);
  lc.setRow(0,1,sf[1]);
  lc.setRow(0,2,sf[2]);
  lc.setRow(0,3,sf[3]);
  lc.setRow(0,4,sf[4]);
  lc.setRow(0,5,sf[5]);
  lc.setRow(0,6,sf[6]);
  lc.setRow(0,7,sf[7]);
  delay(600);
}
  
  void drawHappyFace(){
  // Display happy face
  lc.setRow(0,0,hf[0]);
  lc.setRow(0,1,hf[1]);
  lc.setRow(0,2,hf[2]);
  lc.setRow(0,3,hf[3]);
  lc.setRow(0,4,hf[4]);
  lc.setRow(0,5,hf[5]);
  lc.setRow(0,6,hf[6]);
  lc.setRow(0,7,hf[7]);
  delay(600);
}


void loop()
{
 
 int sensorValue = analogRead(A0); 
Serial.println(sensorValue);
Serial.println(millis());

if ( sensorValue > 700 )  // A ajuster !!!!!!
{
 if (millis() - lastConnectionTime > 50000) {
sendNotificationAndroid("Arrose moi !");
lastConnectionTime = millis();
drawSadFace();
delay(10000);
 }
}
else
{
if (millis() - lastConnectionTime > 50000) {
sendNotificationAndroid("C'est OK !");
lastConnectionTime = millis();
drawHappyFace();
delay(10000);
 }
}
  lc.clearDisplay(0);  
}