Mise en place d'une sonde pluviomètre avec un Arduino

L'objectif de ce tutoriel est de concevoir un système de comptabilisation des précipitations avec un sonde pluviomètre et un Arduino.

Publié le 26/08/2019

On va utiliser un shield Ethernet qui va fournir un serveur web. Ce dernier va poster un message JSON quand la sonde détecte de la pluie. Ce message va consommer par un back-end qui va stocker les valeurs de pluviométrie. Cette réalisation fait partie du projet GreenMine.

Matériel :

On connecte le pluviomètre au connecteur D2 de la carte grove.

On va affecter l'IP 192.168.0.14 à l'interface réseau de l'Arduino. Cette dernière va effectuer un requête GET vers l'API d'un back-end [projet GreenMine] lorsque le balancier du pluviomètre sera en mouvement.

Programme Arduino :


/*
  Web client

 This sketch connects to a website (http://www.google.com)
 using an Arduino Wiznet Ethernet shield.

 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13

 created 18 Dec 2009
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe, based on work by Adrian McEwen

 */

#include 
#include 

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEA };

// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
char server[] = "rain.fresh.local";    // name address for Google (using DNS)

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 14);
IPAddress myDns(192, 168, 0, 2);

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

// Variables to measure the speed
unsigned long beginMicros, endMicros;
unsigned long byteCount = 0;
bool printWebData = true;  // set to false for better speed measurement


void setup() {
  // Set up pin for rain sensor GRLEX002
  pinMode(13, OUTPUT);
  pinMode(2, INPUT);

  // You can use Ethernet.init(pin) to configure the CS pin
  //Ethernet.init(10);  // Most Arduino shields
  //Ethernet.init(5);   // MKR ETH shield
  //Ethernet.init(0);   // Teensy 2.0
  //Ethernet.init(20);  // Teensy++ 2.0
  //Ethernet.init(15);  // ESP8266 with Adafruit Featherwing Ethernet
  //Ethernet.init(33);  // ESP32 with Adafruit Featherwing Ethernet

  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection:
  Serial.println("Initialize Ethernet with DHCP:");
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // Check for Ethernet hardware present
    if (Ethernet.hardwareStatus() == EthernetNoHardware) {
      Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
      while (true) {
        delay(1); // do nothing, no point running without Ethernet hardware
      }
    }
    if (Ethernet.linkStatus() == LinkOFF) {
      Serial.println("Ethernet cable is not connected.");
    }
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip, myDns);
  } else {
    Serial.print("DHCP assigned IP");
    Serial.println(Ethernet.localIP());
  }


  // start profiling
  beginMicros = micros();


}

void postGreenMineRainAPI(String uri) {
    Serial.print("connected to ");
    Serial.println(client.remoteIP());
    // Make a HTTP request:
    client.println("GET "+uri+" HTTP/1.1");
    client.println("Host: greenmine.fresh.local");
    client.println("Connection: close");
    client.println();
}

void loop() {


  // if there are incoming bytes available
  // give the Ethernet shield a second to initialize:
  delay(1000);


  Serial.print("connecting to ");
  Serial.print(server);
  Serial.println("...");

  // get rain sensor value
  int BP = digitalRead(2);
  Serial.println(BP);

  if (BP == LOW) {
    digitalWrite(13, HIGH);
    // if you get a connection, report back via serial: - post data
    Serial.println("its raining");
    if (client.connect(server, 80)) {

      postGreenMineRainAPI("/rain/status/1/create");

    } else {
      // if you didn't get a connection to the server:
      Serial.println("connection failed");
    }
  } else {
    digitalWrite(13, LOW);
    // if you get a connection, report back via serial: - post data

    Serial.println("its sunny");

    if (client.connect(server, 80)) {

      //postGreenMineRainAPI("/api/rain/status/0/create");

    } else {
      // if you didn't get a connection to the server:
      Serial.println("connection failed");
    }
  }



  // from the server, read them and print them:
  int len = client.available();
  if (len > 0) {
    byte buffer[80];
    if (len > 80) len = 80;
    client.read(buffer, len);
    if (printWebData) {
      Serial.write(buffer, len); // show in the serial monitor (slows some boards)
    }
    byteCount = byteCount + len;
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    endMicros = micros();
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    Serial.print("Received ");
    Serial.print(byteCount);
    Serial.print(" bytes in ");
    float seconds = (float)(endMicros - beginMicros) / 1000000.0;
    Serial.print(seconds, 4);
    float rate = (float)byteCount / seconds / 1000.0;
    Serial.print(", rate = ");
    Serial.print(rate);
    Serial.print(" kbytes/second");
    Serial.println();

    // do nothing forevermore:
    while (true) {
      delay(1);
    }
  }

  // delay
  // 1 hour
  //delay(60UL * 60UL * 1000UL);
  // 1 seconds
  delay(1000);
}