Improved scripts and added proper README
This commit is contained in:
parent
79598c5c8b
commit
21594b69cb
33
README.md
33
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
|
|
@ -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,58 +111,50 @@ 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
|
// Check if wifi is still connected
|
||||||
if ( isnan(temp) || isnan(humi)) {
|
if(WiFi.status() == WL_CONNECTED){
|
||||||
Serial.println("Failed to read from DHT sensor!");
|
WiFiClient client;
|
||||||
temp = -273;
|
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 {
|
else {
|
||||||
// Check if wifi is still connected
|
// Cancels and waits for wifi connection
|
||||||
if(WiFi.status() == WL_CONNECTED){
|
Serial.print("Unable to connect to wifi, please wait for autoreconnect or restart the system");
|
||||||
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");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// Waits for 1 minute before scanning again
|
// Waits for 1 minute before scanning again
|
||||||
delay(60000);
|
delay(60000);
|
||||||
|
@ -173,20 +163,26 @@ void loop() {
|
||||||
// Used for comparison to old readings
|
// Used for comparison to old readings
|
||||||
float currentTemp = dht.readTemperature();
|
float currentTemp = dht.readTemperature();
|
||||||
|
|
||||||
// Gets current hour and minutes (E.g. 08:00) for scheduled checks
|
if ( isnan(currentTemp)) {
|
||||||
getLocalTime(&timeinfo);
|
Serial.println("Failed to read from DHT sensor!");
|
||||||
char timeAsString[5];
|
digitalWrite(LED_BUILTIN, HIGH);
|
||||||
strftime(timeAsString, sizeof(timeAsString), "%H%M", &timeinfo);
|
} else {
|
||||||
String plannedCheck(timeAsString);
|
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);
|
||||||
|
|
||||||
if(plannedCheck == "0158" || plannedCheck == "0159"){
|
if(plannedCheck == "0158" || plannedCheck == "0159"){
|
||||||
// Daily reboot
|
// Daily reboot
|
||||||
delay(120000);
|
delay(120000);
|
||||||
ESP.restart();
|
ESP.restart();
|
||||||
} else if(temp > currentTemp + 0.5 || temp < currentTemp - 0.5
|
} else if(temp > currentTemp + 0.5 || temp < currentTemp - 0.5
|
||||||
|| plannedCheck == "0800" || plannedCheck == "1200" || plannedCheck == "1500"){
|
|| plannedCheck == "0800" || plannedCheck == "1200" || plannedCheck == "1500"){
|
||||||
SendPOSTData();
|
SendPOSTData();
|
||||||
|
}
|
||||||
|
Serial.print('.');
|
||||||
}
|
}
|
||||||
delay(1000);
|
delay(1000);
|
||||||
Serial.print('.');
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,80 +121,79 @@ 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
|
// Check if wifi is still connected
|
||||||
if ( isnan(temp) || isnan(humi)) {
|
if(WiFi.status() == WL_CONNECTED){
|
||||||
Serial.println("Failed to read from DHT sensor!");
|
WiFiClient client;
|
||||||
temp = -273;
|
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 {
|
else {
|
||||||
// Check if wifi is still connected
|
// Cancels and waits for wifi connection
|
||||||
if(WiFi.status() == WL_CONNECTED){
|
Serial.print("Unable to connect to wifi, please wait for autoreconnect or restart the system");
|
||||||
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");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// 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();
|
||||||
|
|
||||||
// Gets current hour and minutes (E.g. 08:00) for scheduled checks
|
if ( isnan(currentTemp)) {
|
||||||
getLocalTime(&timeinfo);
|
Serial.println("Failed to read from DHT sensor!");
|
||||||
char timeAsString[5];
|
digitalWrite(LED_BUILTIN, LOW);
|
||||||
strftime(timeAsString, sizeof(timeAsString), "%H%M", &timeinfo);
|
} else {
|
||||||
String plannedCheck(timeAsString);
|
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);
|
||||||
|
|
||||||
if(plannedCheck == "0158" || plannedCheck == "0159"){
|
if(plannedCheck == "0158" || plannedCheck == "0159"){
|
||||||
// Daily reboot
|
// Daily reboot
|
||||||
delay(120000);
|
delay(120000);
|
||||||
ESP.restart();
|
ESP.restart();
|
||||||
} else if(temp > currentTemp + 0.5 || temp < currentTemp - 0.5
|
} else if(temp > currentTemp + 0.5 || temp < currentTemp - 0.5
|
||||||
|| plannedCheck == "0800" || plannedCheck == "1200" || plannedCheck == "1500"){
|
|| plannedCheck == "0800" || plannedCheck == "1200" || plannedCheck == "1500"){
|
||||||
SendPOSTData();
|
SendPOSTData();
|
||||||
|
}
|
||||||
|
Serial.print('.');
|
||||||
}
|
}
|
||||||
delay(1000);
|
delay(1000);
|
||||||
Serial.print('.');
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue