Improved scripts and added proper README

This commit is contained in:
unknown
2022-08-31 13:42:38 +02:00
parent 79598c5c8b
commit 21594b69cb
3 changed files with 156 additions and 135 deletions
@@ -14,7 +14,7 @@ DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor for normal 16mhz Arduino
//Variables
float humi; // Stores humidity value
float temp; // Stores temperature value
float temp = -273; // Stores temperature value, is sat at this beginning temp so it fires of immediately upon startup
String hostName = "mu7-5";
String ipAddress;
const char* apiService = "http://infotavle.itd-skp.sde.dk/TH_API/ClimateSensor_Api/api/climateSensor/create.php";
@@ -50,11 +50,6 @@ bool getLocalTime(struct tm * info)
}
void InitWifi() {
// Needed to set hostname
//WiFi.mode(WIFI_STA);
//WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE, INADDR_NONE);
//WiFi.setHostname(hostName.c_str());
// Connects to wifi
WiFi.begin(ssid);
//WiFi.begin(ssid, PASSWORD_IF_NEEDED);
@@ -115,10 +110,8 @@ void setup() {
// Sets time based on denmark
configTime(gmtOffset_sec, 0, ntpServer);
SetTimezone();
dht.begin();
//Sends data immediately upon startup
SendPOSTData();
dht.begin();
}
void SendPOSTData() {
@@ -128,80 +121,79 @@ void SendPOSTData() {
strftime(timeAsString, sizeof(timeAsString), "%Y-%m-%d %H:%M:%S", &timeinfo);
String stringified(timeAsString);
humi = dht.readHumidity();// Read humidity
temp = dht.readTemperature();// Read temperature
// Check whether the reading is successful or not
if ( isnan(temp) || isnan(humi)) {
Serial.println("Failed to read from DHT sensor!");
temp = -273;
}
else {
// Check if wifi is still connected
if(WiFi.status() == WL_CONNECTED){
WiFiClient client;
HTTPClient http;
http.begin(client, apiService);
// Check if wifi is still connected
if(WiFi.status() == WL_CONNECTED){
WiFiClient client;
HTTPClient http;
http.begin(client, apiService);
// The JSON that will be send to our api service
http.addHeader("Content-Type", "application/json");
String httpRequestData = "{\"ipaddress\": \"" + ipAddress;
httpRequestData += "\", \"zone\": " + zone;
httpRequestData += ", \"name\": \"" + deviceName;
httpRequestData += "\", \"updated\": \"" + String(timeAsString);
httpRequestData += "\", \"temperature\": " + String(temp);
httpRequestData += ", \"humidity\": " + String(humi);
httpRequestData += "}";
// The JSON that will be send to our api service
http.addHeader("Content-Type", "application/json");
String httpRequestData = "{\"ipaddress\": \"" + ipAddress;
httpRequestData += "\", \"zone\": " + zone;
httpRequestData += ", \"name\": \"" + deviceName;
httpRequestData += "\", \"updated\": \"" + String(timeAsString);
httpRequestData += "\", \"temperature\": " + String(temp);
httpRequestData += ", \"humidity\": " + String(humi);
httpRequestData += "}";
// Sends the POST request
Serial.print(httpRequestData);
int httpResponseCode = http.POST(httpRequestData);
// Sends the POST request
Serial.print(httpRequestData);
int httpResponseCode = http.POST(httpRequestData);
// Show the response code so we can determined if it was received by the api
// 400 = Error, 200 = Received
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
Serial.println("");
// Show the response code so we can determined if it was received by the api
// 400 = Error, 200 = Received
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
Serial.println("");
http.end();
client.stop();
digitalWrite(LED_BUILTIN, LOW);
delay(50);
digitalWrite(LED_BUILTIN, HIGH);
delay(50);
digitalWrite(LED_BUILTIN, LOW);
delay(50);
digitalWrite(LED_BUILTIN, HIGH);
delay(50);
}
else {
// Cancels and waits for wifi connection
Serial.print("Unable to connect to wifi, please wait for autoreconnect or restart the system");
}
http.end();
client.stop();
digitalWrite(LED_BUILTIN, LOW);
delay(50);
digitalWrite(LED_BUILTIN, HIGH);
delay(50);
digitalWrite(LED_BUILTIN, LOW);
delay(50);
digitalWrite(LED_BUILTIN, HIGH);
delay(50);
}
else {
// Cancels and waits for wifi connection
Serial.print("Unable to connect to wifi, please wait for autoreconnect or restart the system");
}
// Waits for 1 minute before scanning again
delay(60000);
}
void loop() {
// Used for comparison to old readings
float currentTemp = dht.readTemperature();
if ( isnan(currentTemp)) {
Serial.println("Failed to read from DHT sensor!");
digitalWrite(LED_BUILTIN, LOW);
} else {
digitalWrite(LED_BUILTIN, HIGH);
// Gets current hour and minutes (E.g. 08:00) for scheduled checks
getLocalTime(&timeinfo);
char timeAsString[5];
strftime(timeAsString, sizeof(timeAsString), "%H%M", &timeinfo);
String plannedCheck(timeAsString);
// Gets current hour and minutes (E.g. 08:00) for scheduled checks
getLocalTime(&timeinfo);
char timeAsString[5];
strftime(timeAsString, sizeof(timeAsString), "%H%M", &timeinfo);
String plannedCheck(timeAsString);
if(plannedCheck == "0158" || plannedCheck == "0159"){
// Daily reboot
delay(120000);
ESP.restart();
} else if(temp > currentTemp + 0.5 || temp < currentTemp - 0.5
|| plannedCheck == "0800" || plannedCheck == "1200" || plannedCheck == "1500"){
SendPOSTData();
if(plannedCheck == "0158" || plannedCheck == "0159"){
// Daily reboot
delay(120000);
ESP.restart();
} else if(temp > currentTemp + 0.5 || temp < currentTemp - 0.5
|| plannedCheck == "0800" || plannedCheck == "1200" || plannedCheck == "1500"){
SendPOSTData();
}
Serial.print('.');
}
delay(1000);
Serial.print('.');
}