diff --git a/README.md b/README.md index e69de29..37770d1 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,33 @@ +# Arduino Temperature SensorScript +**Preparations** + +To start you will need either an ESP32S or an ESP8266 as these scripts are made with them in mind. +The temperature sensor used for these scripts is the DHT22 + +**Setup** + +To upload the script to the ESP you will first need to follow these steps + + 1. Download Arduino IDE from Arduino's website https://www.arduino.cc/en/software (be sure to checkmark Install USB driver and Associate .ino files in setup installation), then plug in your board + 2. Download the script fitting your board from this Git and open it in the IDE + 3. In the upper navigation bar click on `File`, then click on `Preferences` + 4. In Preferences there should be a field called `Additional Boards Manager URLs:`, + copy and paste below text to that field, and then click `OK` + > https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json,http://arduino.esp8266.com/stable/package_esp8266com_index.json + 5. Once more in the navigation bar, click on `Tools`, then `Board:` and then `Boards Manager...` + 6. In Boards Manager search either esp32 or esp 8266 depending on your board, and then click Install + 7. Back in `Board:` select the appropriate board (for ESP32 I use Node32s, and for ESP8266 Boards I use LOLIN(WeMos) D1 R1) + 6. Still in the `Tools` click on `Port:` and select the port your Arduino is in + 7. Now in the navigation bar click on `Sketch`, then `Include Library`, and then `Manage Libraries...` + 8. In the upper search in Library Manager search __DHT sensor library__ and choose the library "DHT sensor library" by Adafruit, install all other libraries needed for the download to complete + 9. Finally upload the sketch by clicking the arrow pointing right (for the ESP32S you will need to hold down the Boot button and let go when it says Connecting... in the console) + 10. You should now be able to see it connecting to the internet through the serial monitor, or by observing the boards built in led + +**Connecting Sensor** + +In these scripts I use pin 21 for ESP32 and pin D4 for ESP8266, feel free to change them if needed +The DHT22 needs to be connected to either 3V3 or 5V, ground (G), and the serial pin stated above. + +**Notes** +Once it connects to the wifi, the board should send a POST request upon startup. +When connecting to wifi the built in LED will blink, when sending POST request it will flicker, and when it can't read the sensor it will be lit up \ No newline at end of file diff --git a/TemperatureSensorScript_for_ESP32/TemperatureSensorScript_for_ESP32.ino b/TemperatureSensorScript_for_ESP32/TemperatureSensorScript_for_ESP32.ino index 03d96c9..873ef74 100644 --- a/TemperatureSensorScript_for_ESP32/TemperatureSensorScript_for_ESP32.ino +++ b/TemperatureSensorScript_for_ESP32/TemperatureSensorScript_for_ESP32.ino @@ -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"; @@ -46,9 +46,9 @@ void InitWifi() { Serial.print("Connecting to WiFi .."); while (WiFi.status() != WL_CONNECTED) { Serial.print('.'); - digitalWrite(LED_BUILTIN, LOW); + digitalWrite(LED_BUILTIN, HIGH); delay(250); - digitalWrite(LED_BUILTIN, HIGH); + digitalWrite(LED_BUILTIN, LOW); delay(250); } // Enables autoreconnect to wifi, in case of wifi shutdown or weak connection @@ -102,8 +102,6 @@ void setup() { SetTimezone(); dht.begin(); - //Sends data immediately upon startup - SendPOSTData(); } void SendPOSTData() { @@ -113,58 +111,50 @@ 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; - } + // 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 += "}"; + + // 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(""); + + http.end(); + client.stop(); + digitalWrite(LED_BUILTIN, HIGH); + delay(50); + digitalWrite(LED_BUILTIN, LOW); + delay(50); + digitalWrite(LED_BUILTIN, HIGH); + delay(50); + digitalWrite(LED_BUILTIN, LOW); + delay(50); + } else { - // 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 += "}"; - - // 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(""); - - 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"); - } + // 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); @@ -172,21 +162,27 @@ void SendPOSTData() { 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, HIGH); + } else { + digitalWrite(LED_BUILTIN, LOW); + // 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('.'); } diff --git a/TemperatureSensorScript_for_ESP8266/TemperatureSensorScript_for_ESP8266.ino b/TemperatureSensorScript_for_ESP8266/TemperatureSensorScript_for_ESP8266.ino index 63c894f..460215d 100644 --- a/TemperatureSensorScript_for_ESP8266/TemperatureSensorScript_for_ESP8266.ino +++ b/TemperatureSensorScript_for_ESP8266/TemperatureSensorScript_for_ESP8266.ino @@ -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('.'); }