Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WiFi.h>
- #include "HX711.h"
- #include <Wire.h>
- #include <Adafruit_Sensor.h>
- #include <Adafruit_BME280.h>
- #include <Preferences.h>
- #define TINY_GSM_MODEM_SIM800
- #include <TinyGsmClient.h>
- #include <HTTPClient.h>
- #define ConversionSeconds 1000000 // Conversion factor for micro seconds to seconds //
- #define SleepTime 10 // Time ESP32 will go to sleep (in seconds)
- #define NTP_SERVER "de.pool.ntp.org"
- #define TZ_INFO "WEST-1DWEST-2,M3.5.0/02:00:00,M10.5.0/03:00:00" // Western European Time
- #define BME_SCL 22 // Pin Define BME280
- #define BME_SDA 21 // Pin Define BME280
- #define SEALEVELPRESSURE_HPA (1013.25)
- #define BUTTON 12 // Pin Define for a Button to set the scale to Tare
- Adafruit_BME280 bme; // I2C BME 280
- //SHT21 SHT21;
- #define SerialMon Serial
- #define RX2 16 // Pin Define für GSM Modul SIM800L
- #define TX2 17 // Pin Define für GSM Modul SIM800L
- #define SerialAT Serial2
- #define TINY_GSM_RX_BUFFER 1024
- #define TINY_GSM_DEBUG SerialMon
- #define GSM_PIN ""
- const int relaisPin = 13; //Pin Define relais 3.3 Volt hx711 + bme180
- const int relaisPin1 = 02; //Pin Define relais 4.1 Volt Sim800L
- const int relaisPin2 = 15; //Pin Define relais 5v hx711
- const int LOADCELL_DOUT_PIN = 35; // Define for HX711 Scale Pins
- const int LOADCELL_SCK_PIN = 32; // Define for HX711 Scale Pins
- int buttonState = 0;
- bool buttonPressed = false;
- HX711 scale;
- Preferences preferences; // Methode for saving Tare Results
- unsigned long delayTime;
- const int wdtTimeout = 20000; //time in ms to trigger the watchdog
- hw_timer_t *timer = NULL;
- RTC_DATA_ATTR int BootCounter = 0; // Variables that stays in RTC memory after reset
- RTC_DATA_ATTR float Tare;
- RTC_DATA_ATTR float temperature = 0;
- RTC_DATA_ATTR float pressure = 0;
- RTC_DATA_ATTR float approx_altitude = 0;
- RTC_DATA_ATTR float humidity = 0;
- //RTC_DATA_ATTR float g_temperature = 0;
- void IRAM_ATTR resetModule() //Function to Reboot ESP through Watchdog
- {
- ets_printf("reboot durch watchdog\n");
- esp_restart();
- }
- void configureWatchdog()
- {
- timer = timerBegin(0, 80, true); //timer 0, div 80
- timerAttachInterrupt(timer, &resetModule, true); //attach callback
- timerAlarmWrite(timer, wdtTimeout * 1000, false); //set time in us
- timerAlarmEnable(timer); //enable interrupt
- }
- void FirstStart()
- {
- Serial.println("Good mooooorning Vieeeetnaaam");
- delay(300); // Delay for preventing boot bug
- WiFi.mode(WIFI_STA);
- WiFi.begin("xxxxxxx", "xxxxxx"); // Start wifimode
- Serial.println("");
- while (WiFi.status() != WL_CONNECTED) //
- {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.print("IP Address: ");
- Serial.println(WiFi.localIP());
- Serial.println("Fetching NTP Time");
- struct tm local;
- configTzTime(TZ_INFO, NTP_SERVER); // Sync systemtime timezone with NTP
- getLocalTime(&local, 1000); // Sync local time for 1 seconds
- //WiFi.disconnect();
- //WiFi.mode(WIFI_OFF); //End wifimode
- }
- void setup()
- {
- esp_sleep_wakeup_cause_t wakeup_cause; // Variable for wakeup reason
- setenv("TZ", TZ_INFO, 1); // Setting timezone after reset
- tzset();
- pinMode (relaisPin, OUTPUT); //switching relais on for 3.3V power (Display + Touch, HX711 Scale, BME Sensor)
- digitalWrite (relaisPin, HIGH); //switching relais on for 3.3V power (Display + Touch, HX711 Scale, BME Sensor)
- pinMode (relaisPin2, OUTPUT); //switching relais on for 4.1V power (SIM800l GSM Modul)
- digitalWrite (relaisPin2, HIGH); //switching relais on for 4.1V power (SIM800l GSM Modul)
- pinMode (relaisPin1, OUTPUT); //switching relais on for 5.0V power (HX711 Scale)
- digitalWrite (relaisPin1, HIGH); //switching relais on for 5.0V power (HX711 Scale)
- pinMode(BUTTON, INPUT_PULLUP); // define mode for tara button
- Serial.begin(115200);
- Serial.println("Setup start");
- configureWatchdog();
- ++BootCounter;
- Serial.println("Start Nr.: " + String(BootCounter));
- SerialAT.begin(9600, SERIAL_8N1);
- delay(2000);
- Serial.println("Display AN" );
- pinMode (4, OUTPUT);
- digitalWrite (4, HIGH);
- Serial.println("Initializing...SIM800L GSM Modul");
- delay(1000);
- SerialAT.println("AT"); //Once the handshake test is successful, it will back to OK
- updateSerialMon();
- SerialAT.println("AT+CSQ"); //Once the handshake test is successful, it will back to OK
- updateSerialMon();
- SerialAT.println("AT+CREG?"); //Once the handshake test is successful, it will back to OK
- updateSerialMon();
- SerialAT.println("AT+COPS?"); //Once the handshake test is successful, it will back to OK
- updateSerialMon();
- wakeup_cause = esp_sleep_get_wakeup_cause(); // fetching wakeupreason
- if (wakeup_cause != 3) FirstStart(); // new startup after manual reset
- scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN); // beginig scale hx711 connection with pins from define
- scale.set_scale(28633.1136); // Scalefactor of the scale
- delayTime = 1000;
- bool status;
- // default settings
- // (you can also pass in a Wire library object like &Wire2)
- status = bme.begin(0x76);
- if (!status) {
- Serial.println("Could not find a valid BME280 sensor, check wiring!");
- while (1);
- }
- Serial.println();
- }
- void loop()
- {
- timerWrite(timer, 0);
- tm local;
- getLocalTime(&local);
- Serial.println(&local, "Date: %d.%m.%y Local Time from NTP: %H:%M:%S"); // Setting format for print in serial monitor
- button(); // calling tare button function
- wiegen(); // calling weight function
- klima(); // calling temprature and humidity and pressure function
- Serial.println("Entering Deepsleepmode \r\n \r\n");
- Serial.println("Good night sweet prince :* \r\n \r\n");
- digitalWrite (relaisPin, LOW);
- digitalWrite (relaisPin1, LOW);
- digitalWrite (relaisPin2, LOW);
- Serial.println("Display off");
- digitalWrite (4, LOW);
- esp_sleep_enable_timer_wakeup(SleepTime * ConversionSeconds); // setting time interval for deep sleep
- esp_deep_sleep_start(); // starting deep sleep mode
- }
- void button ()
- {
- Serial.println("Bitte drücken Sie den Tara knopf für 5 sekunden");
- delay(500);
- buttonState = digitalRead(BUTTON); // define variable for tare button
- delay(100);
- Serial.println(buttonState); // print zero if button is pressed
- if (buttonState == LOW){
- Tare = (scale.get_units(10)); // saves average from 10 measurements
- Serial.println(Tare); // tells the value auf tare
- preferences.begin("Tara", false); // open folder on esp to save tare value to be reset safe
- preferences.putFloat("TaraGewicht", Tare); // saving tare value
- preferences.end(); // closing folder
- }
- }
- void wiegen()
- {
- scale.power_up(); //HX711 ADC booting
- float gewicht = 0; // declaration of variable for weight
- Serial.print("Einzelablesung:\t");
- Serial.print(scale.get_units()); // reading actual wieght without tare result
- preferences.begin("Tara", false); // opening a folder for saving and writing tara
- Tare = preferences.getFloat("TaraGewicht", 0); // den Geschützten Wert aufrufen und in der Variablen Tare speichern
- gewicht = (scale.get_units(10)); // save the average from 10 meassurements
- gewicht = (gewicht - Tare); // calculate actual weight
- Serial.print(gewicht, 1); // printing result
- Serial.print("\t| Schnitt:\t");
- Serial.print(gewicht, 2); // printing result
- Serial.print("\t| Schnitt:\t");
- Serial.println(gewicht, 3); // printing result
- preferences.end(); // close folder
- scale.power_down(); // put the ADC in sleep mode
- if (gewicht < 0) // if the result saved in gewicht is to low the alert sms function will be called
- {
- sms_weight ();
- }
- }
- void sms_weight ()
- {
- delay(3000); //delay for making sure gsm is connected
- SerialAT.println("AT+CSQ"); //Once the handshake test is successful, it will back to OK
- updateSerialMon();
- SerialAT.println("AT+CMGF=1"); // Configuring TEXT mode
- updateSerialMon();
- SerialAT.println("AT+CMGS=\"+xxxxxxxxxxx\"");//change ZZ with country code and xxxxxxxxxxx with phone number to sms
- updateSerialMon();
- SerialAT.println("ALAAARM!!! Bees left the building!!!...Powered by ESP32"); //text content
- updateSerialMon();
- SerialAT.write(26);
- delay(4000); // delay for making sure sms is sent
- }
- void klima ()
- {
- float temperatur = bme.readTemperature(); //define variables for enviroment measurement
- float luftdruck = bme.readPressure() / 100.0F; //define variables for enviroment measurement
- float luftfeuchtigkeit = bme.readHumidity(); //define variables for enviroment measurement
- //float g_temperature = (SHT21.getTemperature()); //define variables for enviroment measurement
- Serial.print("Temperature = ");
- Serial.print(temperatur);
- Serial.println(" °C");
- Serial.print("Pressure = ");
- Serial.print(luftdruck);
- Serial.println(" hPa");
- Serial.print("Humidity = ");
- Serial.print(luftfeuchtigkeit);
- Serial.println(" %");
- // Serial.print("Case Temperature = ");
- // Serial.print(g_temperature);
- //Serial.println(" *C");
- Serial.println();
- delay(500);
- if ((WiFi.status() == WL_CONNECTED)) { //if connected make a http connection to a php skript on a server to save
- enviroment values to a mysql database
- HTTPClient http;
- float temperatur = bme.readTemperature();
- float luftdruck = bme.readPressure() / 100.0F;
- float luftfeuchtigkeit = bme.readHumidity();
- String url;
- url += "https://xxxxxx.de/writemysql.php?temp=";
- url += String(temperatur);
- url += "&luftd=";
- url += String(luftdruck);
- url += "&luftf=";
- url += String(luftfeuchtigkeit);
- http.begin(url);
- http.GET();
- http.end();
- delay(1000);
- WiFi.disconnect(true);
- WiFi.mode(WIFI_OFF); //End wifimode
- }
- }
- void updateSerialMon()
- {
- delay(500);
- while (Serial.available())
- {
- SerialAT.write(SerialMon.read());//Forward what Serial received to Software Serial Port
- }
- while(SerialAT.available())
- {
- SerialMon.write(SerialAT.read());//Forward what Software Serial received to Serial Port
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement