fmarzocca
Published © CC BY

Postino: Did the Postman Deliver Anything?

IntermediateFull instructions provided8
Postino: Did the Postman Deliver Anything?

Story

Read more

Schematics

Schematic

Code

Postino.ino

Arduino
/*
  Postino: did the postman deliver anything?
   Fabio Marzocca - 2019 - marzoccafabio@gmail.com
  v.1.0.0 - December 2019
  */

  
#include <ESP8266WiFi.h>
#include <DNSServer.h>            //Local DNS Server used for redirecting all requests to the configuration portal
#include <ESP8266WebServer.h>     //Local WebServer used to serve the configuration portal
#include <WiFiManager.h>          //https://github.com/tzapu/WiFiManager WiFi Configuration Magic
#include <ESP8266HTTPClient.h>
#include <VL6180X.h>
#include <DS3231.h>
#include <Wire.h>



HTTPClient http;    //Declare object of class HTTPClient
WiFiClient  client;

//#define DEBUG        // Verbose output

#define CHIP "esp8266"  // or "NodeMCU"

// To try different scaling factors, change the following define.
// Valid scaling factors are 1, 2, or 3.
#define SCALING 1
#define REST_MSG "http://192.168.1.90:3000/postino"

VL6180X sensor;
DS3231 Clock;

byte TEST_PIN;

void Stat_WiFi() {
  #ifdef DEBUG
    Serial.println();
    WiFi.printDiag(Serial);
    Serial.print(F("IP: "));
    Serial.println(WiFi.localIP());
    Serial.println();
  #endif
}

void setup() {
   delay(1500); //stabilize power line
   WiFiManager wifiManager;
   
   
   // Start the I2C interface
   if (CHIP == "esp8266") {
     Wire.begin(0,2);
     //GPIO 3 (RX) swap the pin to a GPIO.
     TEST_PIN = 3;
     pinMode(TEST_PIN, FUNCTION_3);
   } else {
    Wire.begin();
    TEST_PIN = 14;
    pinMode(TEST_PIN, INPUT);
   }

   sensor.init();
   sensor.configureDefault();
   sensor.setScaling(SCALING);
   sensor.setTimeout(500);
   
   #ifdef DEBUG
   Serial.begin(115200);
   delay(200);
   Serial.println(F("\n"));
   #endif

   //Reset RTC
   Clock.setHour(0);
   Clock.setMinute(0);
   Clock.setSecond(1);

   Clock.setClockMode(false);
    // 0b1111 // each second
    // 0b1110 // Once per minute (when second matches)
    // 0b1100 // Once per hour (when minute and second matches)
    // 0b1000 // Once per day (when hour, minute and second matches)
    // 0b0000 // Once per month when date, hour, minute and second matches. Once per week if day of the week and A1Dy=true
    if (!digitalRead(TEST_PIN)) {
        // Set alarm to happen every minute (change to your wanted interval)
        Clock.setA1Time(1, 1, 1, 0, 0b1110, false, false, false);
      } else {
        // Set alarm to 00.00 once a day 
        Clock.setA1Time(0,0,0,0,8,false,false,false);
      }
    Clock.turnOnAlarm(1);
    Clock.turnOffAlarm(2);


   wifiManager.setAPStaticIPConfig(IPAddress(8,8,8,8), IPAddress(8,8,8,8), IPAddress(255,255,255,0));

   //wait for configuration for a max of 5 minutes
   wifiManager.setConfigPortalTimeout(300);

   if (!wifiManager.autoConnect("Postino")) {
    #ifdef DEBUG
      Serial.println("failed to connect, we should reset and see if it connects");
    #endif
    delay(3000);
    ESP.reset();
    delay(5000);
    }

    if (WiFi.status() != WL_CONNECTED) {
      #ifdef DEBUG
        Serial.println(F("No WiFi connection ...resetting."));
      #endif
    delay(3000);
    ESP.reset();
    delay(5000);
    }
  
    #ifdef DEBUG
     Stat_WiFi();
    #endif

   getReading();

     // Reset alarm to turn off the device
   Clock.checkIfAlarm(1);


}

void getReading() { 
  int posta = sensor.readRangeSingleMillimeters();
  byte i = 0;
  while (i < 3) {
    if (posta < 255) {
      SendNotification();
      return;
    }
    delay(1000);
    i++;
  }
  return;
}

void SendNotification() {
    //send notification to REST server
    http.setTimeout(2500); // 2.5s connection timeout
    http.begin(REST_MSG);      //Send the REST call
    http.addHeader("Content-Type", "text/plain");  //Specify content-type header
    int httpCode = http.GET();   //Send the request
    String payload = http.getString();                  //Get the response payload
    http.end();  //Close connection
    #ifdef DEBUG
        Serial.print("comando REST:");
        Serial.println(REST_MSG);
        Serial.print("httpCode:");
        Serial.println(httpCode);
        Serial.print("payload:");
        Serial.println(payload);
    #endif
}

void loop(){
  //Nothing to do, we never reach here
}

Credits

fmarzocca
3 projects • 13 followers
Contact

Comments

Please log in or sign up to comment.