Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <lvgl.h>
- #include <Ticker.h>
- #include <TFT_eSPI.h>
- #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; //setting hx711 as scale in library
- 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;
- #define LVGL_TICK_PERIOD 60
- Ticker tick; /* timer for interrupt handler */
- TFT_eSPI tft = TFT_eSPI(); /* TFT instance */
- static lv_disp_buf_t disp_buf;
- static lv_color_t buf[LV_HOR_RES_MAX * 10];
- static void create_tab1(lv_obj_t * parent);
- static void create_tab2(lv_obj_t * parent);
- static void create_tab3(lv_obj_t * parent);
- lv_obj_t * slider_label;
- int screenWidth = 480;
- int screenHeight = 320;
- #if USE_LV_LOG != 1
- /* Serial debugging */
- void my_print(lv_log_level_t level, const char * file, uint32_t line, const char * dsc)
- {
- Serial.printf("%s@%d->%s\r\n", file, line, dsc);
- delay(100);
- }
- #endif
- /* Display flushing */
- void my_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p)
- {
- uint16_t c;
- tft.startWrite(); /* Start new TFT transaction */
- tft.setAddrWindow(area->x1, area->y1, (area->x2 - area->x1 + 1), (area->y2 - area->y1 + 1)); /* set the working window */
- for (int y = area->y1; y <= area->y2; y++) {
- for (int x = area->x1; x <= area->x2; x++) {
- c = color_p->full;
- tft.writeColor(c, 1);
- color_p++;
- }
- }
- tft.endWrite(); /* terminate TFT transaction */
- lv_disp_flush_ready(disp); /* tell lvgl that flushing is done */
- }
- bool my_touchpad_read(lv_indev_drv_t * indev_driver, lv_indev_data_t * data)
- {
- uint16_t touchX, touchY;
- bool touched = tft.getTouch(&touchX, &touchY, 600);
- if(!touched)
- {
- return false;
- }
- if(touchX>screenWidth || touchY > screenHeight)
- {
- Serial.println("Y or y outside of expected parameters..");
- Serial.print("y:");
- Serial.print(touchX);
- Serial.print(" x:");
- Serial.print(touchY);
- }
- else
- {
- data->state = touched ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL;
- /*Save the state and save the pressed coordinate*/
- //if(data->state == LV_INDEV_STATE_PR) touchpad_get_xy(&last_x, &last_y);
- /*Set the coordinates (if released use the last pressed coordinates)*/
- data->point.x = touchX;
- data->point.y = touchY;
- Serial.print("Data x");
- Serial.println(touchX);
- Serial.print("Data y");
- Serial.println(touchY);
- }
- return false; /*Return `false` because we are not buffering and no more data to read*/
- }
- /* Interrupt driven periodic handler */
- static void lv_tick_handler(void)
- {
- lv_tick_inc(LVGL_TICK_PERIOD);
- }
- 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
- Serial.println("Watchdog durchlaufen");
- }
- void FirstStart()
- {
- Serial.println("Good mooooorning Vieeeetnaaam");
- delay(300); // Delay for preventing boot bug
- WiFi.mode(WIFI_STA);
- WiFi.begin("xxxxxxxx", "xxxxxxxxx"); // 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
- Serial.println("First start done");
- }
- 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)
- delay(500);
- 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)
- delay(500);
- pinMode (relaisPin1, OUTPUT); //switching relais on for 5.0V power (HX711 Scale)
- digitalWrite (relaisPin1, HIGH); //switching relais on for 5.0V power (HX711 Scale)
- delay(500);
- pinMode(BUTTON, INPUT_PULLUP); // define mode for tara button
- Serial.println("button");
- pinMode (4, OUTPUT); // define mode for led display backlight
- digitalWrite (4, HIGH); // define mode for led display backlight
- delay(500);
- ledcSetup(10, 5000/*freq*/, 10 /*resolution*/);
- ledcAttachPin(32, 10);
- analogReadResolution(10);
- ledcWrite(10,768);
- //const char test = '9' ;
- #define MAX_STRING_SIZE 5
- float test = 52.4;
- char test2[MAX_STRING_SIZE];
- snprintf(test2, MAX_STRING_SIZE, "%f", test);
- Serial.begin(115200); /* prepare for possible serial debug */
- Serial.println("Setup start");
- //configureWatchdog();
- ++BootCounter;
- Serial.println("Going to lv_init");
- delay(500);
- lv_init();
- #if USE_LV_LOG != 0
- lv_log_register_print(my_print); /* register print function for debugging */
- #endif
- tft.begin(); /* TFT init */
- tft.setRotation(1);
- uint16_t calData[5] = { 275, 3620, 264, 3532, 1 };
- tft.setTouch(calData);
- lv_disp_buf_init(&disp_buf, buf, NULL, LV_HOR_RES_MAX * 10);
- /*Initialize the display*/
- lv_disp_drv_t disp_drv;
- lv_disp_drv_init(&disp_drv);
- disp_drv.hor_res = 320;
- disp_drv.ver_res = 240;
- disp_drv.flush_cb = my_disp_flush;
- disp_drv.buffer = &disp_buf;
- lv_disp_drv_register(&disp_drv);
- lv_indev_drv_t indev_drv;
- lv_indev_drv_init(&indev_drv); /*Descriptor of a input device driver*/
- indev_drv.type = LV_INDEV_TYPE_POINTER; /*Touch pad is a pointer-like device*/
- indev_drv.read_cb = my_touchpad_read; /*Set your driver function*/
- lv_indev_drv_register(&indev_drv); /*Finally register the driver*/
- /*Initialize the touch pad*/
- // lv_indev_drv_t indev_drv;
- //lv_indev_drv_init(&indev_drv);
- //indev_drv.type = LV_INDEV_TYPE_ENCODER;
- //indev_drv.read_cb = read_encoder;
- //lv_indev_drv_register(&indev_drv);
- /*Initialize the graphics library's tick*/
- tick.attach_ms(LVGL_TICK_PERIOD, lv_tick_handler);
- //Set the theme..
- lv_theme_t * th = lv_theme_material_init(210, NULL); //Set a HUE value and a Font for the Night Theme
- lv_theme_set_current(th);
- lv_obj_t * scr = lv_cont_create(NULL, NULL);
- lv_disp_load_scr(scr);
- /*Create a Tab view object*/
- lv_obj_t *tabview;
- tabview = lv_tabview_create(lv_scr_act(), NULL);
- /*Add 3 tabs (the tabs are page (lv_page) and can be scrolled*/
- lv_obj_t *tab1 = lv_tabview_add_tab(tabview, "Uebersicht");
- lv_obj_t *tab2 = lv_tabview_add_tab(tabview, "Visualisierung");
- lv_obj_t *tab3 = lv_tabview_add_tab(tabview, "Einstellungen");
- /*Create a normal cell style*/
- static lv_style_t style_cell1;
- lv_style_copy(&style_cell1, &lv_style_plain);
- style_cell1.body.border.width = 2;
- style_cell1.body.border.color = LV_COLOR_BLACK;
- /*Crealte a header cell style*/
- static lv_style_t style_cell2;
- lv_style_copy(&style_cell2, &lv_style_plain);
- style_cell2.body.border.width = 2;
- style_cell2.body.border.color = LV_COLOR_BLACK;
- style_cell2.body.main_color = LV_COLOR_SILVER;
- style_cell2.body.grad_color = LV_COLOR_SILVER;
- lv_obj_t * table = lv_table_create(tab1, NULL);
- lv_table_set_style(table, LV_TABLE_STYLE_CELL1, &style_cell1);
- lv_table_set_style(table, LV_TABLE_STYLE_CELL2, &style_cell2);
- lv_table_set_style(table, LV_TABLE_STYLE_BG, &lv_style_transp_tight);
- lv_table_set_col_cnt(table, 2);
- lv_table_set_row_cnt(table, 5);
- lv_obj_align(table, NULL, LV_ALIGN_CENTER, -18, 0);
- /*Make the cells of the first row center aligned */
- lv_table_set_cell_align(table, 0, 0, LV_LABEL_ALIGN_CENTER);
- lv_table_set_cell_align(table, 0, 1, LV_LABEL_ALIGN_CENTER);
- /*Make the cells of the first row TYPE = 2 (use `style_cell2`) */
- lv_table_set_cell_type(table, 0, 0, 2);
- lv_table_set_cell_type(table, 0, 1, 2);
- /*Fill the first column*/
- lv_table_set_cell_value(table, 0, 0, "Sensor");
- lv_table_set_cell_value(table, 1, 0, "Gewicht" );
- lv_table_set_cell_value(table, 2, 0, "Temperatur");
- lv_table_set_cell_value(table, 3, 0, "Luftfeuchtigkeit");
- lv_table_set_cell_value(table, 4, 0, "Luftdruck");
- /*Fill the second column*/
- lv_table_set_cell_value(table, 0, 1, "Wert");
- lv_table_set_cell_value(table, 1, 1, test2 );
- lv_table_set_cell_value(table, 2, 1, "0");
- lv_table_set_cell_value(table, 3, 1, "0");
- lv_table_set_cell_value(table, 4, 1, "0");
- //Width of cell
- lv_table_set_col_width(table, 0, 130);
- // Cell grows not with words
- lv_table_set_cell_crop(table, 3, 0, true);
- /*Add content to the tabs*/
- lv_obj_t * label = lv_label_create(tab1, NULL);
- lv_label_set_text(label, "");
- label = lv_label_create(tab2, NULL);
- lv_label_set_text(label, "");
- label = lv_label_create(tab3, NULL);
- lv_label_set_text(label, "");
- // lv_obj_t * label;
- lv_obj_t * btn1 = lv_btn_create(tab3, NULL);
- lv_obj_set_event_cb(btn1, event_handler);
- lv_obj_align(btn1, NULL, LV_ALIGN_CENTER, 0, -40);
- label = lv_label_create(btn1, NULL);
- lv_label_set_text(label, "Deepsleep");
- lv_obj_t * btn2 = lv_btn_create(tab3, NULL);
- lv_obj_set_event_cb(btn2, event_handler);
- lv_obj_align(btn2, NULL, LV_ALIGN_CENTER, 0, 40);
- lv_btn_set_toggle(btn2, true);
- lv_btn_toggle(btn2);
- lv_btn_set_fit2(btn2, LV_FIT_NONE, LV_FIT_TIGHT);
- label = lv_label_create(btn2, NULL);
- lv_label_set_text(label, "Handy Nr.");
- lv_coord_t w = lv_page_get_scrl_width(tab2);
- lv_obj_t * chart = lv_chart_create(tab2, NULL);
- lv_chart_set_type(chart, LV_CHART_TYPE_AREA);
- lv_obj_set_size(chart, w / 1.5, lv_disp_get_ver_res(NULL) / 1.5);
- lv_obj_set_pos(chart, LV_DPI / 10, LV_DPI / 10);
- lv_chart_series_t * s1 = lv_chart_add_series(chart, LV_COLOR_RED);
- lv_chart_set_next(chart, s1, 30);
- lv_chart_set_next(chart, s1, 20);
- lv_chart_set_next(chart, s1, 10);
- lv_chart_set_next(chart, s1, 12);
- lv_chart_set_next(chart, s1, 20);
- lv_chart_set_next(chart, s1, 27);
- lv_chart_set_next(chart, s1, 35);
- lv_chart_set_next(chart, s1, 55);
- lv_chart_set_next(chart, s1, 70);
- lv_chart_set_next(chart, s1, test);
- Serial.println("Start Nr.: " + String(BootCounter));
- SerialAT.begin(9600, SERIAL_8N1);
- delay(1000);
- 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();
- Serial.println("GSM DONE");
- wakeup_cause = esp_sleep_get_wakeup_cause(); // fetching wakeupreason
- if (wakeup_cause != 3) FirstStart(); // new startup after manual reset
- Serial.println("Scale begin (void setup) ");
- Serial.println("Scale Done (void setup)");
- // Serial.println("SHT21 test");
- //SHT21.begin();
- delayTime = 1000;
- bool status;
- // default settings
- // (you can also pass in a Wire library object like &Wire2)
- Serial.println("Begin I2C BME Sensor (setup)");
- status = bme.begin(0x76);
- Serial.println("BME started(setup)");
- if (!status) {
- Serial.println("Could not find a valid BME280 sensor, check wiring!");
- while (1);
- }
- Serial.println("void setup done");
- }
- void loop() {
- lv_task_handler(); /* let the GUI do its work */
- delay(5);
- // 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 OUT");
- 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 0 if button is pressed otherwise 1
- Tare = (scale.get_units(10)); // saves average from 10 measurements
- Serial.println(Tare); // saves average from 10 measurements
- 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()
- {
- Serial.print("Scale set scale (void wiegen)");
- scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN); // beginig scale hx711 connection with pins from define
- Serial.print("Scale begin (void wiegen)");
- scale.set_scale(28633.1136); // Scalefactor of the scale
- Serial.print("Scale Power UP (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);
- 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 have left the Building...Powered by ESP32"); //text content
- updateSerialMon();
- SerialAT.write(26);
- delay(4000);
- }
- 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 connected make a http connection to a php skript on a server to save enviroment values to a mysql database
- if ((WiFi.status() == WL_CONNECTED)) {
- HTTPClient http;
- float temperatur = bme.readTemperature();
- float luftdruck = bme.readPressure() / 100.0F;
- float luftfeuchtigkeit = bme.readHumidity();
- String url;
- url += "xxxxxxxxxxx/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