I am kinda stuck with my code since I am not a "PRO" in coding.
If I briefly describe my use case, I am working in a company where we have a server room and a communication equipment room. With that in mind since we are not a big company, I turned to Arduino for temp monitoring. However, it is significant that I receive a notification through email if the temperature exceeds the desired temperature.
My equipment:
Arduino UNO
2x temperature & humidity sensor DH11
I should add that I had combined 2 codes together:
1 part is for the webpage and 2nd part is for email notification over IFTTT, Webpage works and notification over email works however I have an issue with receiving a second temperature:
I would really appreciate it if someone would kick me in the right direction on how to solve this
//Libraries
#include <SPI.h>
#include <Ethernet.h>
#include <dht.h>
//defined pins, libs...
dht DHT1;
dht DHT2;
#define DHT11_PIN1 4 //sensor 1 location on board
#define DHT11_PIN2 5 //sensor 2 location on board
String temp1;
String temp2;
//IFTTT call
char MakerIFTTT_Key[] = "************************";
char MakerIFTTT_Event[] = "sendemail";
int HTTP_PORT = 80;
String HTTP_METHOD = "GET";
char HOST_NAME[] = "maker.ifttt.com";
String PATH_NAME = "/trigger/sendemail/with/key/************************"; // Replace with your event name and private key
String queryString1 = "?value1" ; // For example, the temperature value is 33.5°C
int HTTP_PORT2 = 80;
String HTTP_METHOD2 = "GET";
char HOST_NAME2[] = "maker.ifttt.com";
String PATH_NAME2 = "/trigger/sendemail2/with/key/************************"; // Replace with your event name and private key
String queryString2 = "?value1" ;
//ETH shield settings
byte mac[] = {
0x48, 0xC2, 0xA1, 0xF3, 0x8D, 0xB7};
byte ip[] = {
192,168,1,150};
//web server
EthernetServer server = EthernetServer(80);
void setup()
{
Serial.begin(9600);
Ethernet.begin(mac, ip);
server.begin();
}
//loop for temp
void loop()
{
int chk1 = DHT1.read11(DHT11_PIN1);
int chk2 = DHT2.read11(DHT11_PIN2);
Serial.print("Temperature1 = ");
Serial.println(DHT1.temperature);
Serial.print("Humidity1 = ");
Serial.println(DHT1.humidity);
Serial.print("Temperature2 = ");
Serial.println(DHT2.temperature);
Serial.print("Humidity2 = ");
Serial.println(DHT2.humidity);
temp1 = DHT1.temperature;
temp2 = DHT2.temperature;
delay(1500);
EthernetClient client = server.available();
if(client)
{
boolean BlankLine = true;
while(client.connected())
{
if(client.available())
{
char c = client.read();
if(c == '\n' && BlankLine)
{
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html\n");
client.println("<html><head><META HTTP-EQUIV=""refresh"" CONTENT=""5"">\n");
client.println("<title>Arduino Web Server</title></head>");
client.println("<body style=\"background-color:#5EA084\">\n");
client.println("<h1>Temperature Monitoring</h1>");
client.println("<h3>Current Temperature in Server Room</h3>");
client.println(DHT1.temperature);
client.println("<h3>Current Humidity in Server Room</h3>");
client.println(DHT1.humidity);
client.println("<h3>Current Temperature in Communication room</h3>");
client.println(DHT2.temperature);
client.println("<h3>Current Humidity in Communication room</h3>");
client.println(DHT2.humidity);
break;
}
if(c == '\n')
{
BlankLine = true;
}
else if(c != '\r')
{
BlankLine = false;
}
}
}
delay(10);
}
if (temp1>"30") {
if (client.connect(HOST_NAME, HTTP_PORT)) {
client.println("GET " + PATH_NAME + queryString1 + temp1 + " HTTP/1.1");
client.println("Host: " + String(HOST_NAME));
client.println("Connection: close");
client.println();
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.print(c);
}
}
}
}
if (temp2>"20") {
Serial.println(temp2);
if (client.connect(HOST_NAME2, HTTP_PORT2)) {
client.println("GET " + PATH_NAME2 + queryString2 + temp2 + " HTTP/1.1");
client.println("Host: " + String(HOST_NAME2));
client.println("Connection: close");
client.println();
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.print(c);
}
}
}
}
}
Welcome to the forum and well done for using code tags
Open a new sketch and work on 1 temp sensor only. Get it working. Then open a new sketch and add another temp sensor. Get it working. Then add your working email notification sketch. You can encapsulate code into functions returning a value.
If you need a string use a string but don’t convert to string until you have done all your comparing etc. once something is a string it is no longer an int and can’t be manipulated as one
I have them both (sensors A & B) working, I receive the temp & humidity information.
However, in the part where I send the second temp (temp2) information from Arduino to IFTTT, I am not sure how to include it in the one function if that makes any sense.
This part of the IFTTT call works:
if (temp1>"30") {
if (client.connect(HOST_NAME, HTTP_PORT)) {
client.println("GET " + PATH_NAME + queryString1 + temp1 + " HTTP/1.1");
client.println("Host: " + String(HOST_NAME));
client.println("Connection: close");
client.println();
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.print(c);
}
}
}
I think that I did something wrong with the second part of the function to send the second temp.
You CAN'T compare two string such way.
Or, more precisely - you can compare it, but the result won't that you expected.
For example, ("27.75" > "30") will get you TRUE, just because the string "27.75" is longer than "30".
Sorry, to be clear:
Have you tried removing the string from your variable and using an int to compare. Convert to string after if needed. As per post 5