I have an issue with my esp32. After 1-2 hours it crashes and doesn't work propertly.
here's the code
Code: Select all
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <DHT.h>
#include <MS5611.h>
#define DHTPIN 4 // what digital pin the DHT22 is conected to
#define DHTTYPE DHT22 //
#define PWM_PIN 15
#define SMOKE_PIN 25
#define ONE_WIRE_BUS_EXT_TEMP 19
float temperature = 0;
float humidity = 0;
float co2 = 0;
int measureTime =0;
double referencePressure;
volatile bool fireAlarmState = 0;
bool disableMS5611 = 0;
DHT dht(DHTPIN, DHTTYPE);
MS5611 ms5611;
OneWire oneWire(ONE_WIRE_BUS_EXT_TEMP);
DallasTemperature sensors(&oneWire);
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include "esp_wpa2.h"
#include <HTTPClient.h>
#define EAP_IDENTITY "login" //if connecting from another corporation, use identity@organisation.domain in Eduroam
#define EAP_PASSWORD "password" //your Eduroam password
#define EAP_ANONYMOUS_IDENTITY "login"
const char* ssid = "eduroam"; // Eduroam SSID
const char* host = "10.0.0.10"; //external server domain for HTTP connection after authentification
WiFiClientSecure espClient;
String serverName = "server address";
unsigned long lastTime = 0;
unsigned long timeOfLife = 300*1000;
unsigned long initTime;
unsigned long timerDelay = 30*1000;
void setup() {
int k = 0;
Serial.begin(115200);
Serial.println("serial start");
dht.begin();
while(!ms5611.begin() && k<10) {
Serial.println("Could not find a valid MS5611 sensor, check wiring!");
delay(500);
k++;
}
if (k==10) {
disableMS5611 = 1;
}
if (!disableMS5611) {
referencePressure = ms5611.readPressure();
}
sensors.begin();
pinMode(ONE_WIRE_BUS_EXT_TEMP,INPUT_PULLUP);
WiFi.disconnect(true);
WiFi.mode(WIFI_STA);
esp_wifi_sta_wpa2_ent_set_username((uint8_t *)EAP_IDENTITY, strlen(EAP_IDENTITY));
esp_wifi_sta_wpa2_ent_set_password((uint8_t *)EAP_PASSWORD, strlen(EAP_PASSWORD));
esp_wpa2_config_t config = WPA2_CONFIG_INIT_DEFAULT(); //set config settings to default
esp_wifi_sta_wpa2_ent_enable(&config); //set config settings to enable function
WiFi.begin(ssid);
Serial.println("Connecting");
k = 0;
while(WiFi.status() != WL_CONNECTED && k <10) {
delay(500);
Serial.print(".");
k++;
}
if (WiFi.status() != WL_CONNECTED) {
ESP.restart();
}
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
Serial.println("END OF INIT");
delay(100);
initTime = millis();
}
int getCO2PWM() {
unsigned long th, tl, ppm_pwm = 0;
do {
th = pulseIn(PWM_PIN, HIGH, 1004000) / 1000;
tl = 1004 - th;
ppm_pwm = 5000 * (th - 2) / (th + tl - 4);
} while (th == 0);
return ppm_pwm;
}
void sendGet(String measurement, String value) {
HTTPClient http;
String serverPath = serverName + "?" + measurement+"="+value;
http.begin(serverPath.c_str());
// Send HTTP GET request
int httpResponseCode = http.GET();
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
String payload = http.getString();
Serial.println(payload);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end();
delay(100);
}
void loop() {
if(WiFi.status()== WL_CONNECTED){
if ((millis() - lastTime) > timerDelay) {
temperature = dht.readTemperature();
if (isnan(temperature)) {
temperature = 19.0;
}
sendGet("dht22_temperature", String(temperature));
humidity = dht.readHumidity();
if (isnan(humidity)) {
humidity = 50;
}
sendGet("dht22_humidity", String(humidity));
int ppm_pwm = getCO2PWM();
sendGet("mhz14a_co2", String(ppm_pwm));
if (!disableMS5611) {
uint32_t rawTemp = ms5611.readRawTemperature();
uint32_t rawPressure = ms5611.readRawPressure();
double realTemperature = ms5611.readTemperature();
sendGet("gy63_temperature", String(realTemperature));
long realPressure = ms5611.readPressure();
sendGet("gy63_pressure", String(realPressure));
}
sensors.requestTemperatures();
delay(750);
temperature = sensors.getTempCByIndex(0);
if (temperature>-100) {
sendGet("ds18b20_temperature", String(temperature));
}
lastTime = millis();
}
if ((millis() - initTime) > timeOfLife) {
ESP.restart();
}
}
else {
ESP.restart();
}
}