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

View File

@ -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

View File

@ -14,7 +14,7 @@ DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor for normal 16mhz Arduino
//Variables //Variables
float humi; // Stores humidity value 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 hostName = "mu7-5";
String ipAddress; String ipAddress;
const char* apiService = "http://infotavle.itd-skp.sde.dk/TH_API/ClimateSensor_Api/api/climateSensor/create.php"; const char* apiService = "http://infotavle.itd-skp.sde.dk/TH_API/ClimateSensor_Api/api/climateSensor/create.php";
@ -46,10 +46,10 @@ void InitWifi() {
Serial.print("Connecting to WiFi .."); Serial.print("Connecting to WiFi ..");
while (WiFi.status() != WL_CONNECTED) { while (WiFi.status() != WL_CONNECTED) {
Serial.print('.'); Serial.print('.');
digitalWrite(LED_BUILTIN, LOW);
delay(250);
digitalWrite(LED_BUILTIN, HIGH); digitalWrite(LED_BUILTIN, HIGH);
delay(250); delay(250);
digitalWrite(LED_BUILTIN, LOW);
delay(250);
} }
// Enables autoreconnect to wifi, in case of wifi shutdown or weak connection // Enables autoreconnect to wifi, in case of wifi shutdown or weak connection
WiFi.setAutoReconnect(true); WiFi.setAutoReconnect(true);
@ -102,8 +102,6 @@ void setup() {
SetTimezone(); SetTimezone();
dht.begin(); dht.begin();
//Sends data immediately upon startup
SendPOSTData();
} }
void SendPOSTData() { void SendPOSTData() {
@ -113,16 +111,9 @@ void SendPOSTData() {
strftime(timeAsString, sizeof(timeAsString), "%Y-%m-%d %H:%M:%S", &timeinfo); strftime(timeAsString, sizeof(timeAsString), "%Y-%m-%d %H:%M:%S", &timeinfo);
String stringified(timeAsString); String stringified(timeAsString);
humi = dht.readHumidity();// Read humidity humi = dht.readHumidity();// Read humidity
temp = dht.readTemperature();// Read temperature 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 // Check if wifi is still connected
if(WiFi.status() == WL_CONNECTED){ if(WiFi.status() == WL_CONNECTED){
WiFiClient client; WiFiClient client;
@ -152,20 +143,19 @@ void SendPOSTData() {
http.end(); http.end();
client.stop(); client.stop();
digitalWrite(LED_BUILTIN, LOW);
delay(50);
digitalWrite(LED_BUILTIN, HIGH); digitalWrite(LED_BUILTIN, HIGH);
delay(50); delay(50);
digitalWrite(LED_BUILTIN, LOW); digitalWrite(LED_BUILTIN, LOW);
delay(50); delay(50);
digitalWrite(LED_BUILTIN, HIGH); digitalWrite(LED_BUILTIN, HIGH);
delay(50); delay(50);
digitalWrite(LED_BUILTIN, LOW);
delay(50);
} }
else { else {
// Cancels and waits for wifi connection // Cancels and waits for wifi connection
Serial.print("Unable to connect to wifi, please wait for autoreconnect or restart the system"); Serial.print("Unable to connect to wifi, please wait for autoreconnect or restart the system");
} }
}
// Waits for 1 minute before scanning again // Waits for 1 minute before scanning again
delay(60000); delay(60000);
} }
@ -173,6 +163,11 @@ void loop() {
// Used for comparison to old readings // Used for comparison to old readings
float currentTemp = dht.readTemperature(); 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 // Gets current hour and minutes (E.g. 08:00) for scheduled checks
getLocalTime(&timeinfo); getLocalTime(&timeinfo);
char timeAsString[5]; char timeAsString[5];
@ -187,6 +182,7 @@ void loop() {
|| plannedCheck == "0800" || plannedCheck == "1200" || plannedCheck == "1500"){ || plannedCheck == "0800" || plannedCheck == "1200" || plannedCheck == "1500"){
SendPOSTData(); SendPOSTData();
} }
delay(1000);
Serial.print('.'); Serial.print('.');
} }
delay(1000);
}

View File

@ -14,7 +14,7 @@ DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor for normal 16mhz Arduino
//Variables //Variables
float humi; // Stores humidity value 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 hostName = "mu7-5";
String ipAddress; String ipAddress;
const char* apiService = "http://infotavle.itd-skp.sde.dk/TH_API/ClimateSensor_Api/api/climateSensor/create.php"; 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() { 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 // Connects to wifi
WiFi.begin(ssid); WiFi.begin(ssid);
//WiFi.begin(ssid, PASSWORD_IF_NEEDED); //WiFi.begin(ssid, PASSWORD_IF_NEEDED);
@ -115,10 +110,8 @@ void setup() {
// Sets time based on denmark // Sets time based on denmark
configTime(gmtOffset_sec, 0, ntpServer); configTime(gmtOffset_sec, 0, ntpServer);
SetTimezone(); SetTimezone();
dht.begin();
//Sends data immediately upon startup dht.begin();
SendPOSTData();
} }
void SendPOSTData() { void SendPOSTData() {
@ -128,16 +121,9 @@ void SendPOSTData() {
strftime(timeAsString, sizeof(timeAsString), "%Y-%m-%d %H:%M:%S", &timeinfo); strftime(timeAsString, sizeof(timeAsString), "%Y-%m-%d %H:%M:%S", &timeinfo);
String stringified(timeAsString); String stringified(timeAsString);
humi = dht.readHumidity();// Read humidity humi = dht.readHumidity();// Read humidity
temp = dht.readTemperature();// Read temperature 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 // Check if wifi is still connected
if(WiFi.status() == WL_CONNECTED){ if(WiFi.status() == WL_CONNECTED){
WiFiClient client; WiFiClient client;
@ -180,14 +166,19 @@ void SendPOSTData() {
// Cancels and waits for wifi connection // Cancels and waits for wifi connection
Serial.print("Unable to connect to wifi, please wait for autoreconnect or restart the system"); Serial.print("Unable to connect to wifi, please wait for autoreconnect or restart the system");
} }
}
// Waits for 1 minute before scanning again // Waits for 1 minute before scanning again
delay(60000); delay(60000);
} }
void loop() { void loop() {
// Used for comparison to old readings // Used for comparison to old readings
float currentTemp = dht.readTemperature(); 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 // Gets current hour and minutes (E.g. 08:00) for scheduled checks
getLocalTime(&timeinfo); getLocalTime(&timeinfo);
char timeAsString[5]; char timeAsString[5];
@ -202,6 +193,7 @@ void loop() {
|| plannedCheck == "0800" || plannedCheck == "1200" || plannedCheck == "1500"){ || plannedCheck == "0800" || plannedCheck == "1200" || plannedCheck == "1500"){
SendPOSTData(); SendPOSTData();
} }
delay(1000);
Serial.print('.'); Serial.print('.');
} }
delay(1000);
}