Servermonitor Betriebsumgebung

Aus AirSpaceWatch & More
Zur Navigation springen Zur Suche springen
  1 /*
  2  * BlueDot_BME280_TSL2591 Version 1.0.4
  3  * Wire Version 1.0.1
  4  * WiFi Version 1.0
  5  * Server ENV http-Monitor for Node-Red by Raz0rsEdge 2020
  6  */
  7 #include <Wire.h>
  8 #include <WiFi.h>
  9 #include "BlueDot_BME280_TSL2591.h"
 10 #include "time.h" // for ntp and time conversion
 11 #include "driver/adc.h"
 12 #include "esp_system.h"
 13 #include "esp_adc_cal.h"
 14 #include "soc/soc.h"
 15 #include "soc/rtc_cntl_reg.h"
 16 
 17 // constants
 18 const char* ssid       = "     ASW     ";
 19 const char* password   = "*************";
 20 const char* ntpServer = "raz0rsedge.de"; //my very own ntp server ;)
 21 const long  gmtOffset_sec = 3600;
 22 const int   daylightOffset_sec = 0; //3600 for summer time
 23 const int LED = 3; //Internal LED gpio depends on board, usin WIDORA AIR
 24 uint32_t voltageR;
 25 
 26 // vars
 27 #define INT_STR_SIZE 16
 28 #define V_REF   1100
 29 #define ADC1_TEST_CHANNEL (ADC1_CHANNEL_6)      //GPIO 34
 30 #define V_REF_TO_GPIO  //Remove comment on define to route v_ref to GPIO
 31 #define uS_TO_S_FACTOR 1000000  /* Conversion factor for micro seconds to seconds */
 32 #define TIME_TO_SLEEP  120      /* Time ESP32 will go to sleep (in seconds) */
 33 #define SEALEVELPRESSURE_HPA (1013.25) //Standarddruckflaeche Meereshoehe
 34 char buffer[INT_STR_SIZE];
 35 bool touchdetected = false; // init bool for touch
 36 
 37 BlueDot_BME280_TSL2591 bme280;
 38 BlueDot_BME280_TSL2591 tsl2591;
 39 WiFiServer server(80); // sensor status site init port
 40 
 41 void setup() {
 42   Serial.begin(9600);
 43   delay(2500); //settlement
 44   fflush(stdout);  
 45   Serial.println(F("Basic IP Weather Station"));
 46 
 47   bme280.parameter.I2CAddress = 0x77;     //The BME280 is hardwired to use the I2C Address 0x77
 48   tsl2591.parameter.I2CAddress = 0x29;
 49   Wire.begin(21, 22);                     //Use this for NodeMCU boards, SDA, SCL
 50 
 51   tsl2591.parameter.gain = 0b01;
 52   tsl2591.parameter.integration = 0b000;
 53   tsl2591.config_TSL2591();
 54   bme280.parameter.sensorMode = 0b11;                   //Choose sensor mode, this.default
 55   //0b000:      factor 0 (filter off)
 56   //0b001:      factor 2
 57   //0b010:      factor 4
 58   //0b011:      factor 8
 59   //0b100:      factor 16 (default value)
 60   bme280.parameter.IIRfilter = 0b100;                    //Setup for IIR Filter
 61   bme280.parameter.humidOversampling = 0b101;            //Setup Humidity Oversampling
 62   bme280.parameter.tempOversampling = 0b101;             //Setup Temperature Ovesampling
 63   bme280.parameter.pressOversampling = 0b101;            //Setup Pressure Oversampling
 64   bme280.parameter.pressureSeaLevel = 1013.25;           //default value of 1013.25 hPa
 65   bme280.parameter.tempOutsideCelsius = 15;              //default value of 15°C
 66   //bme280.parameter.tempOutsideFahrenheit = 59;           //default value of 59°F
 67 
 68   // LED SetUp for internal LED
 69   pinMode(LED, OUTPUT);
 70   digitalWrite(LED, LOW);
 71 
 72   // connecting to a WiFi network progress output
 73   Serial.print("Connecting to: "); Serial.println(ssid);
 74   WiFi.begin(ssid, password);
 75 
 76   while (WiFi.status() != WL_CONNECTED) {
 77     delay(1500);
 78     Serial.print(".");
 79   }
 80   if (WiFi.status() == WL_CONNECTED) {
 81     Serial.print(WiFi.localIP()); Serial.println(" <-- IP");
 82   }
 83   delay(1500);
 84 
 85   //init and get the local time from ntp
 86   struct tm timeinfo;
 87   configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
 88   while (!getLocalTime(&timeinfo)){
 89     Serial.println("Failed to obtain time...");
 90     delay(1500);
 91   }
 92   Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
 93   server.begin();
 94   if (bme280.init_BME280() != 0x60)
 95   {
 96     Serial.println(F("Ops! BME280 could not be found!"));
 97     //while(1);
 98   }
 99   else
100   {
101     Serial.println(F("BME280 detected!"));
102   }
103 
104   if (tsl2591.init_TSL2591() != 0x50)
105   {
106     Serial.println(F("Ops! TSL2591 could not be found!"));
107     //while(1);
108   }
109   else
110   {
111     Serial.println(F("TSL2591 detected!"));
112   }
113 
114   Serial.println();
115   Serial.println();
116 }
117 void loop() {
118   WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector
119   /*Note: Different ESP32 modules may have different reference voltages varying from
120     1000mV to 1200mV. Use #define GET_VREF to route v_ref to a GPIO
121   */
122   //Get v_ref
123   esp_err_t status;
124   status = adc2_vref_to_gpio(GPIO_NUM_25);
125   if (status == ESP_OK) {
126     Serial.println(" ");
127     Serial.println("v_ref succesfully routed to GPIO34\n");
128   } else {
129     Serial.println("failed to route v_ref\n");
130   }
131 
132   while (1)
133   {
134     statussite();
135     delay(1500);
136   }
137 }
138 void getVoltage() {
139   //Init ADC and Characteristics
140   esp_adc_cal_characteristics_t characteristics;
141   adc1_config_width(ADC_WIDTH_BIT_12);
142   adc1_config_channel_atten(ADC1_TEST_CHANNEL, ADC_ATTEN_DB_0);
143   esp_adc_cal_get_characteristics(V_REF, ADC_ATTEN_DB_0, ADC_WIDTH_BIT_12, &characteristics);
144   voltageR = adc1_to_voltage(ADC1_TEST_CHANNEL, &characteristics);
145   vTaskDelay(pdMS_TO_TICKS(1500));
146 }
147 void statussite() {
148   WiFiClient client = server.available();   // listen for incoming clients
149   if (client) {                             // if you get a client,
150     Serial.println("New Client.");          // print a message out the serial port
151     String currentLine = "";                // make a String to hold incoming data from the client
152     while (client.connected()) {            // loop while the client's connected
153       if (client.available()) {             // if there's bytes to read from the client,
154         char c = client.read();             // read a byte, then
155         //Serial.write(c);                  // print it out the serial monitor
156         if (c == '\n') {                    // if the byte is a newline character
157           // if the current line is blank, you got two newline characters in a row.
158           // that's the end of the client HTTP request, so send a response:
159           if (currentLine.length() == 0) {
160             // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
161             // and a content-type so the client knows what's coming, then a blank line:
162             client.println("HTTP/1.1 200 OK");
163             client.println("Content-type:text/html");
164             client.println();
165             client.print("<html><head><meta http-equiv=\"refresh\" content=\"30\" /><meta charset=\"utf-8\"></head><body>");
166             client.print("<font face=\"Arial\">");
167             // the content of the HTTP response follows the header:
168             client.print("<br><b>");
169             float temp = bme280.readTempC();
170             float press = bme280.readPressure() / 100.0F;
171             float alt = bme280.readAltitudeMeter();
172             float illum = tsl2591.readIlluminance_TSL2591();
173             float hum = bme280.readHumidity();
174             //Init ADC and Characteristics
175             esp_adc_cal_characteristics_t characteristics;
176             adc1_config_width(ADC_WIDTH_BIT_12);
177             adc1_config_channel_atten(ADC1_TEST_CHANNEL, ADC_ATTEN_DB_0);
178             esp_adc_cal_get_characteristics(V_REF, ADC_ATTEN_DB_0, ADC_WIDTH_BIT_12, &characteristics);
179             voltageR = adc1_to_voltage(ADC1_TEST_CHANNEL, &characteristics);
180             getVoltage();
181 
182             // create datestamp
183             struct tm timeinfo;
184             if (getLocalTime(&timeinfo)) {
185               client.print("Date: "); client.print(&timeinfo, "%A, %B %d %Y %H:%M:%S"); client.print(" Uhr<br>");
186               client.print("IPv4: " + WiFi.localIP()); client.print("<br>");
187             }
188             client.println("</b><br>");
189             client.print("Temperature: "); 
190             client.print(temp); client.print(" Celsius <br>");
191             client.print("Pressure: "); 
192             client.print(press*100); client.print(" Pa <br>");
193             client.print("Altitude: "); 
194             client.print(alt); client.print(" Meters <br>");
195             client.print("Illuminance: "); 
196             client.print(illum); client.print(" Lux <br>");
197             client.print("Humidity: "); 
198             client.print(hum); client.print(" Percent <br>");
199             client.print("Voltage: "); 
200             client.print(voltageR); client.print(" milliVolt <br>");
201             client.print("</font></body>");
202             client.print("</html>");
203             // The HTTP response ends with another blank line:
204             client.println();
205             // break out of the while loop:
206             break;
207           } else {    // if you got a newline, then clear currentLine:
208             currentLine = "";
209           }
210         } else if (c != '\r') {  // if you got anything else but a carriage return character,
211           currentLine += c;      // add it to the end of the currentLine
212         }
213       }
214     }
215     // close the connection:
216     delay(0.9);
217     client.stop();
218   }
219 }