From e3dfe2cede583961f5b2c50dfa0ebd0fe888f2db Mon Sep 17 00:00:00 2001
From: Becascastro
Date: Wed, 30 Sep 2020 09:57:14 +0200
Subject: [PATCH] NodeMCU reading temperature values from one DS18B20 and
publish on a specific web link
---
Benni_rafvirki_5.ino | 276 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 276 insertions(+)
create mode 100644 Benni_rafvirki_5.ino
diff --git a/Benni_rafvirki_5.ino b/Benni_rafvirki_5.ino
new file mode 100644
index 0000000..3131346
--- /dev/null
+++ b/Benni_rafvirki_5.ino
@@ -0,0 +1,276 @@
+
+//Codigo a funcionar bem. Temperatura a ser enviada para a base de dados
+//http://bennirafvirki.is/sensors/ui/readings
+
+//Working well for Fífusel 7
+//Bennirafvirki.is/sensors/
+//Benni +3547738039
+
+#ifdef ESP32 // Import required libraries
+#include
+
+#else
+#include
+#include
+#include
+#include
+#include
+#endif
+#include
+#include
+#define ONE_WIRE_BUS 4 // Data wire is connected to GPIO 4 = pin D2
+OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices
+DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature sensor
+
+//#include
+#include
+#include
+//#define SERVER_IP "bennirafvirki.is"
+
+
+// Replaced parameters
+
+// Rikki
+ const char* ssid = "Asus2"; //network credentials
+ const char* password = "lukas1234";
+
+/* F7
+//const char* ssid = "Hringdu-2.4G-2dfM"; //network credentials F7
+const char* password = "8hs9gmmV";
+*/
+const int tmin = -3; // min temperature
+const int tmax = 3; // max temperature
+String address = "electropepper";
+int time_of_delay = 50000; //if 50000 ms, than will measure each minute interval
+
+
+// pinout D0=pin16; D1=pin5/GPIO5; D2=pin4/GPIO4; D3=0/Flash; D4=2/Tx1; D5=14; D6=12; D7=13=Rx2; D8=15=Tx2; D9=3=Rx0; D10=1=Tx0;
+//const int relay1 = 5; //pin D1 = GPIO5 , relay 1
+//const int relay2 = 14; //pin D5 = GPIO14, relay 2
+const int relay1 = 16;
+const int relay2 = 5;
+
+/// Serial port for debugging purposes Serial.begin(115200); Serial monitoring
+
+AsyncWebServer server(80); // Create AsyncWebServer object on port 80
+
+
+String readDSTemperatureC() // Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus
+{
+ sensors.requestTemperatures();
+ float tempC = sensors.getTempCByIndex(0);
+
+ if (tempC == -127.00 || tempC == 85.00 )
+ {
+ Serial.println("Failed to read from DS18B20 sensor");
+ /////////////////////////////////////////////////////////////////////////////////////
+ ////////////////////////////////////////////////////could you created README.MD file in your project folder with those notes?
+ return "--";
+ }
+ else
+ {
+ //Serial.print("Temperature Celsius: ");
+ //Serial.print(tempC);
+
+ }
+ return String(tempC);
+}
+
+
+
+const char index_html[] PROGMEM = R"rawliteral(
+
+
+
+
+
+
+
+
+ Fifusel, Reykjavik
+
+
+
+ Temperature Celsius
+ %TEMPERATUREC%
+ °C
+
Heaters are ON between -3 and 3 celcius
+
+
+ Benni 7738039
+ Bennirafvirki.is
+
+
+
+ )rawliteral";
+
+
+
+String processor(const String& var) // Replaces placeholder with DHT values
+ {
+ //Serial.println(var);
+ if (var == "TEMPERATUREC")
+ {
+ return readDSTemperatureC();
+ }
+ return String();
+ }
+
+
+void setup()
+{
+ pinMode(relay1, OUTPUT);
+ digitalWrite(relay1, LOW);
+ pinMode(relay2, OUTPUT);
+ digitalWrite(relay2, LOW);
+
+
+ Serial.begin(115200); // Serial port for debugging purposes ( baud)
+ Serial.println();
+ sensors.begin(); // Start up the DS18B20 library
+
+ WiFi.begin(ssid, password); // Connect to Wi-Fi
+ Serial.println("Connecting to WiFi");
+ while (WiFi.status() != WL_CONNECTED)
+ {
+ delay(500);
+ Serial.print(".");
+ }
+ Serial.println();
+ Serial.println(WiFi.localIP()); // Print ESP Local IP Address 192.168.1.18
+
+
+
+ server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) // Route for root / web page
+ {
+ request->send_P(200, "text/html", index_html, processor);
+ });
+ server.on("/temperaturec", HTTP_GET, [](AsyncWebServerRequest * request)
+ {
+ request->send_P(200, "text/plain", readDSTemperatureC().c_str());
+ });
+
+ server.begin(); // Start server
+
+}
+
+
+void loop()
+{
+
+ float tempC = sensors.getTempCByIndex(0);
+
+ if (tempC > tmin && tempC < tmax)
+ {
+ digitalWrite(relay1, LOW);
+ digitalWrite(relay2, LOW);
+ Serial.print("Temperature is ");
+ Serial.print(tempC);
+ Serial.println(" Celcius and the heaters are ON ");
+ }
+ else
+ {
+ digitalWrite(relay1, HIGH);
+ digitalWrite(relay2, HIGH);
+ Serial.print("Temperature is ");
+ Serial.print(tempC) ;
+ Serial.println(" Celcius and the heaters are OFF ");
+ }
+ delay(10000);
+
+ // wait for WiFi connection
+ if ((WiFi.status() == WL_CONNECTED))
+ {
+
+ WiFiClient client;
+ HTTPClient http;
+
+ Serial.print("[HTTP] begin...\n");
+ // configure traged server and url
+
+
+///////////////////////////////////////////////////////////////////////////////////////////////////////////
+ //could you created README.MD file in your project folder with those notes?
+ //change to: if tempC is between min and max, so it´s: 0 (OFF), 1 (ON), 2 sensor disconnected, 3 wire or resistance fail, 4
+ // if tempC = -127, sensor or resistance disconnect, if tempC = 85, sensor
+
+ int sensor_status = 0;
+
+ String link1 = "http://bennirafvirki.is/sensors/add/temperature/";
+ String link2 = String(tempC,DEC%4);
+ String link3 = "/address/";
+ String link4 = String(address);
+ String link5 = "/state/";
+ String link6 = String(sensor_status,DEC);
+ String link = link1 + link2 + link3 + link4 + link5 + link6 ;
+
+ http.begin(client,link); //Serial.print(link);
+
+
+ http.addHeader("Content-Type", "application/json");
+
+ Serial.print("[HTTP] POST...\n");
+ // start connection and send HTTP header and body
+ int httpCode = http.POST("{\"hello\":\"world\"}");
+
+ // httpCode will be negative on error
+ if (httpCode > 0)
+ {
+ // HTTP header has been send and Server response header has been handled
+ Serial.printf("[HTTP] POST... code: %d\n", httpCode);
+
+ // file found at server
+ if (httpCode == HTTP_CODE_OK)
+ {
+ const String& payload = http.getString();
+ Serial.println("received payload:\n<<");
+ Serial.println(payload);
+ Serial.println(">>");
+ }
+ }
+ else
+ {
+ Serial.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str());
+ }
+
+ http.end();
+ }
+
+ delay(time_of_delay);
+
+
+}