Advertisement
Guest User

Code without Littlevgl - working 100%

a guest
Oct 19th, 2019
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.42 KB | None | 0 0
  1. #include <WiFi.h>
  2. #include "HX711.h"
  3. #include <Wire.h>
  4. #include <Adafruit_Sensor.h>
  5. #include <Adafruit_BME280.h>
  6. #include <Preferences.h>
  7.  
  8. #define TINY_GSM_MODEM_SIM800
  9. #include <TinyGsmClient.h>
  10. #include <HTTPClient.h>
  11.  
  12. #define ConversionSeconds 1000000 // Conversion factor for micro seconds to seconds //
  13. #define SleepTime 10 //  Time ESP32 will go to sleep (in seconds)
  14. #define NTP_SERVER "de.pool.ntp.org"
  15. #define TZ_INFO "WEST-1DWEST-2,M3.5.0/02:00:00,M10.5.0/03:00:00" // Western European Time
  16. #define BME_SCL 22    // Pin Define BME280
  17. #define BME_SDA 21    // Pin Define BME280
  18. #define SEALEVELPRESSURE_HPA (1013.25)
  19. #define BUTTON 12     // Pin Define for a Button to set the scale to Tare
  20.  
  21.  
  22. Adafruit_BME280 bme; // I2C BME 280
  23. //SHT21 SHT21;
  24.  
  25.  
  26. #define SerialMon Serial
  27. #define RX2 16      // Pin Define für GSM Modul SIM800L
  28. #define TX2 17      // Pin Define für GSM Modul SIM800L
  29. #define SerialAT Serial2
  30. #define TINY_GSM_RX_BUFFER 1024
  31. #define TINY_GSM_DEBUG SerialMon
  32. #define GSM_PIN ""
  33.  
  34. const int relaisPin = 13;  //Pin Define relais 3.3 Volt hx711 + bme180
  35. const int relaisPin1 = 02; //Pin Define relais 4.1 Volt Sim800L
  36. const int relaisPin2 = 15; //Pin Define relais 5v hx711
  37.  
  38.  
  39.  
  40. const int LOADCELL_DOUT_PIN = 35; // Define for HX711 Scale Pins  
  41. const int LOADCELL_SCK_PIN = 32; // Define for HX711 Scale Pins  
  42. int buttonState = 0;
  43. bool buttonPressed = false;
  44. HX711 scale;
  45. Preferences preferences;  // Methode for saving Tare Results
  46.  
  47. unsigned long delayTime;
  48.  
  49. const int wdtTimeout = 20000;  //time in ms to trigger the watchdog
  50. hw_timer_t *timer = NULL;
  51.  
  52. RTC_DATA_ATTR int BootCounter = 0;   // Variables that stays in RTC memory after reset
  53. RTC_DATA_ATTR float Tare;
  54. RTC_DATA_ATTR float temperature = 0;
  55. RTC_DATA_ATTR float pressure = 0;
  56. RTC_DATA_ATTR float approx_altitude = 0;
  57. RTC_DATA_ATTR float humidity = 0;
  58. //RTC_DATA_ATTR float g_temperature = 0;
  59.  
  60.  
  61.  
  62. void IRAM_ATTR resetModule()    //Function to Reboot ESP through Watchdog
  63. {
  64.   ets_printf("reboot durch watchdog\n");
  65.   esp_restart();
  66. }
  67.  
  68. void configureWatchdog()
  69. {
  70.   timer = timerBegin(0, 80, true);                  //timer 0, div 80
  71.   timerAttachInterrupt(timer, &resetModule, true);  //attach callback
  72.   timerAlarmWrite(timer, wdtTimeout * 1000, false); //set time in us
  73.   timerAlarmEnable(timer);                          //enable interrupt
  74. }
  75.  
  76.              
  77.  
  78. void FirstStart()
  79. {
  80.   Serial.println("Good mooooorning Vieeeetnaaam");
  81.   delay(300);                             // Delay for preventing boot bug
  82.   WiFi.mode(WIFI_STA);
  83.   WiFi.begin("xxxxxxx", "xxxxxx");             // Start wifimode
  84.   Serial.println("");
  85.  
  86.   while (WiFi.status() != WL_CONNECTED)  //
  87.   {
  88.     delay(500);
  89.     Serial.print(".");
  90.   }
  91.   Serial.println("");
  92.   Serial.print("IP Address: ");
  93.   Serial.println(WiFi.localIP());
  94.   Serial.println("Fetching NTP Time");
  95.   struct tm local;
  96.   configTzTime(TZ_INFO, NTP_SERVER); // Sync systemtime timezone with NTP
  97.   getLocalTime(&local, 1000);      // Sync local time for 1 seconds
  98.   //WiFi.disconnect();
  99.   //WiFi.mode(WIFI_OFF);              //End wifimode
  100. }
  101.  
  102. void setup()
  103. {
  104.   esp_sleep_wakeup_cause_t wakeup_cause; // Variable for wakeup reason
  105.   setenv("TZ", TZ_INFO, 1);             // Setting timezone after reset
  106.   tzset();
  107.   pinMode (relaisPin, OUTPUT);          //switching relais on for 3.3V power (Display + Touch, HX711 Scale, BME Sensor)
  108.   digitalWrite (relaisPin, HIGH);       //switching relais on for 3.3V power (Display + Touch, HX711 Scale, BME Sensor)
  109.   pinMode (relaisPin2, OUTPUT);         //switching relais on for 4.1V power (SIM800l GSM Modul)   
  110.   digitalWrite (relaisPin2, HIGH);      //switching relais on for 4.1V power (SIM800l GSM Modul)
  111.   pinMode (relaisPin1, OUTPUT);         //switching relais on for 5.0V power (HX711 Scale)
  112.   digitalWrite (relaisPin1, HIGH);      //switching relais on for 5.0V power (HX711 Scale)
  113.   pinMode(BUTTON, INPUT_PULLUP);       // define mode for tara button
  114.   Serial.begin(115200);
  115.   Serial.println("Setup start");
  116.   configureWatchdog();
  117.   ++BootCounter;
  118.   Serial.println("Start Nr.: " + String(BootCounter));
  119.   SerialAT.begin(9600, SERIAL_8N1);
  120.   delay(2000);
  121.   Serial.println("Display AN" );
  122.   pinMode (4, OUTPUT);
  123.   digitalWrite (4, HIGH);
  124.  
  125.   Serial.println("Initializing...SIM800L GSM Modul");
  126.   delay(1000);
  127.  
  128.   SerialAT.println("AT"); //Once the handshake test is successful, it will back to OK
  129.   updateSerialMon();
  130.   SerialAT.println("AT+CSQ"); //Once the handshake test is successful, it will back to OK
  131.   updateSerialMon();
  132.   SerialAT.println("AT+CREG?"); //Once the handshake test is successful, it will back to OK
  133.   updateSerialMon();
  134.   SerialAT.println("AT+COPS?"); //Once the handshake test is successful, it will back to OK
  135.   updateSerialMon();
  136.  
  137.   wakeup_cause = esp_sleep_get_wakeup_cause(); // fetching wakeupreason
  138.   if (wakeup_cause != 3) FirstStart();     // new startup after manual reset
  139.  
  140.   scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);   // beginig scale hx711 connection with pins from define
  141.   scale.set_scale(28633.1136);                        // Scalefactor of the scale
  142.  
  143.  
  144.  
  145.  
  146.   delayTime = 1000;
  147.   bool status;
  148.   // default settings
  149.   // (you can also pass in a Wire library object like &Wire2)
  150.   status = bme.begin(0x76);
  151.   if (!status) {
  152.     Serial.println("Could not find a valid BME280 sensor, check wiring!");
  153.     while (1);
  154.   }
  155.   Serial.println();
  156.  
  157. }
  158.  
  159. void loop()
  160. {
  161.   timerWrite(timer, 0);
  162.   tm local;
  163.   getLocalTime(&local);
  164.   Serial.println(&local, "Date: %d.%m.%y  Local Time from NTP: %H:%M:%S"); // Setting format for print in serial monitor
  165.  
  166.   button();     // calling tare button function
  167.   wiegen();     // calling weight function
  168.   klima();      // calling temprature and humidity and pressure function
  169.  
  170.   Serial.println("Entering Deepsleepmode \r\n \r\n");  
  171.   Serial.println("Good night sweet prince :* \r\n \r\n");
  172.   digitalWrite (relaisPin, LOW);
  173.   digitalWrite (relaisPin1, LOW);
  174.   digitalWrite (relaisPin2, LOW);
  175.   Serial.println("Display off");
  176.   digitalWrite (4, LOW);
  177.  
  178.   esp_sleep_enable_timer_wakeup(SleepTime * ConversionSeconds);    // setting time interval for deep sleep
  179.   esp_deep_sleep_start();                       // starting deep sleep mode
  180. }
  181.  
  182. void button ()
  183. {
  184.   Serial.println("Bitte drücken Sie den Tara knopf für 5 sekunden");
  185.   delay(500);  
  186.   buttonState = digitalRead(BUTTON); // define variable for tare button
  187.   delay(100);
  188.   Serial.println(buttonState);  // print zero if button is pressed
  189.   if (buttonState == LOW){
  190.     Tare = (scale.get_units(10)); // saves average from 10 measurements
  191.     Serial.println(Tare);         // tells the value auf tare
  192.     preferences.begin("Tara", false);   // open folder on esp to save tare value to be reset safe
  193.     preferences.putFloat("TaraGewicht", Tare);    // saving tare value
  194.     preferences.end();   // closing folder
  195.   }
  196.  }
  197.  
  198. void wiegen()
  199. {
  200.   scale.power_up(); //HX711 ADC booting
  201.   float gewicht = 0;   // declaration of variable for weight
  202.   Serial.print("Einzelablesung:\t");
  203.   Serial.print(scale.get_units());    // reading actual wieght without tare result
  204.   preferences.begin("Tara", false);   // opening a folder for saving and writing tara
  205.   Tare = preferences.getFloat("TaraGewicht", 0);  // den Geschützten Wert aufrufen und in der Variablen Tare speichern
  206.   gewicht = (scale.get_units(10));   // save the average from 10 meassurements
  207.   gewicht = (gewicht - Tare);        // calculate actual weight  
  208.   Serial.print(gewicht, 1);         // printing result
  209.   Serial.print("\t| Schnitt:\t");
  210.   Serial.print(gewicht, 2);         // printing result
  211.   Serial.print("\t| Schnitt:\t");
  212.   Serial.println(gewicht, 3);       // printing result
  213.   preferences.end();                // close folder
  214.   scale.power_down();             // put the ADC in sleep mode
  215.  
  216.   if (gewicht < 0)              // if the result saved in gewicht is to low the alert sms function will be called
  217.   {
  218.   sms_weight ();
  219.   }
  220. }
  221.  
  222. void sms_weight ()
  223. {
  224.  
  225.  delay(3000);       //delay for making sure gsm is connected
  226.  SerialAT.println("AT+CSQ"); //Once the handshake test is successful, it will back to OK
  227.  updateSerialMon();
  228.  SerialAT.println("AT+CMGF=1"); // Configuring TEXT mode
  229.  updateSerialMon();
  230.  SerialAT.println("AT+CMGS=\"+xxxxxxxxxxx\"");//change ZZ with country code and xxxxxxxxxxx with phone number to sms
  231.  updateSerialMon();
  232.  SerialAT.println("ALAAARM!!! Bees left the building!!!...Powered by ESP32"); //text content
  233.  updateSerialMon();
  234.  SerialAT.write(26);
  235.  delay(4000);       // delay for making sure sms is sent
  236. }
  237.  
  238. void klima ()
  239. {
  240.   float temperatur = bme.readTemperature();                 //define variables for enviroment measurement
  241.   float luftdruck = bme.readPressure() / 100.0F;            //define variables for enviroment measurement
  242.   float luftfeuchtigkeit = bme.readHumidity();              //define variables for enviroment measurement
  243.   //float g_temperature = (SHT21.getTemperature());         //define variables for enviroment measurement
  244.  
  245.   Serial.print("Temperature = ");
  246.   Serial.print(temperatur);
  247.   Serial.println(" °C");
  248.  
  249.   Serial.print("Pressure = ");
  250.   Serial.print(luftdruck);
  251.   Serial.println(" hPa");
  252.  
  253.   Serial.print("Humidity = ");
  254.   Serial.print(luftfeuchtigkeit);
  255.   Serial.println(" %");
  256.  
  257.  
  258.  // Serial.print("Case Temperature = ");
  259.  // Serial.print(g_temperature);
  260.   //Serial.println(" *C");
  261.  
  262.   Serial.println();
  263.   delay(500);
  264.  
  265.   if ((WiFi.status() == WL_CONNECTED)) {                    //if connected make a http connection to a php skript on a server to save        
  266.                                                                  enviroment values to a mysql database
  267.      HTTPClient http;
  268.      
  269.     float temperatur = bme.readTemperature();
  270.     float luftdruck = bme.readPressure() / 100.0F;
  271.     float luftfeuchtigkeit = bme.readHumidity();
  272.  
  273.     String url;
  274.     url += "https://xxxxxx.de/writemysql.php?temp=";
  275.     url += String(temperatur);
  276.     url += "&luftd=";
  277.     url += String(luftdruck);
  278.     url += "&luftf=";
  279.     url += String(luftfeuchtigkeit);
  280.    
  281.     http.begin(url);
  282.     http.GET();                                        
  283.     http.end();
  284.     delay(1000);
  285.     WiFi.disconnect(true);
  286.     WiFi.mode(WIFI_OFF);              //End wifimode
  287. }
  288. }
  289.  
  290.  
  291.  
  292. void updateSerialMon()
  293. {
  294.   delay(500);
  295.   while (Serial.available())
  296.   {
  297.     SerialAT.write(SerialMon.read());//Forward what Serial received to Software Serial Port
  298.   }
  299.   while(SerialAT.available())
  300.   {
  301.     SerialMon.write(SerialAT.read());//Forward what Software Serial received to Serial Port
  302.   }
  303. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement