Seeking help with a code for arduino uno and 2x temp sensor

Hi there,

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 :slight_smile:


//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);
      }
    }    
  }
  }
}

storing numeric data as a string is no longer a good idea. And it's definitely not worth comparing them in this way.

What issue are you having?

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.

Show us your serial monitor in code tags also

Huh, ok...
I thought I need a string for this part so that I can compile a link.

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

1 Like

Thank you :slight_smile:

I have them both (sensors A & B) working, I receive the temp & humidity information.

image

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".

Have you tried removing the “”?

this is unlikely to help because temp1 is defined as String too

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

Thank you all, for your suggestions! :slight_smile:

I have fixed it, and it's working now :slight_smile:

Here is the updated code:

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

//IFTTT call
EthernetClient client;

int temp1;
int temp2;
int hum1;
int hum2;
int maxTemp = 35;
String str_temp1;
String str_hum1;
String str_temp2;
String str_hum2;

//event: sendemail

char MakerIFTTT_Key[] = "*********";
char MakerIFTTT_Event[] = "alarm";

int HTTP_PORT = 80;
String HTTP_METHOD = "GET";
char HOST_NAME[] = "maker.ifttt.com";
String PATH_NAME = "/trigger/alarm/with/key/*********;  // Replace with your event name and private key
String queryString;                                                   // For example, the temperature value is 33.5°C
String queryString2;
String queryString_hum1;
String queryString_hum2;

//ETH shield settings
byte mac[] = { 0x48, 0xC2, 0xA1, 0xF3, 0x8D, 0xB7 };
byte ip[] = { *, *, *, * };

//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);
  Serial.print("Temperature Server Room = ");
  Serial.println(DHT1.temperature);
  Serial.print("Humidity Server Room = ");
  Serial.println(DHT1.humidity);
  int chk2 = DHT2.read11(DHT11_PIN2);
  Serial.print("Temperature Communication Room = ");
  Serial.println(DHT2.temperature);
  Serial.print("Humidity Communication Room = ");
  Serial.println(DHT2.humidity);
  temp1 = DHT1.temperature;
  hum1 = DHT1.humidity;
  temp2 = DHT2.temperature;
  hum2 = DHT2.humidity;
  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>Arduino Web Server</h1>");

          client.println("<h3>Current Temperature</h3>");
          client.println(DHT1.temperature);
          client.println("<h3>Current Humidity</h3>");
          client.println(DHT1.humidity);
          client.println("<h3>Current Temperature</h3>");
          client.println(DHT2.temperature);
          client.println("<h3>Current Humidity</h3>");
          client.println(DHT2.humidity);

          break;
        }
        if (c == '\n') {
          BlankLine = true;
        } else if (c != '\r') {
          BlankLine = false;
        }
      }
    }
    delay(10);
    client.stop();
  }
  if ((temp1, temp2) > maxTemp) {
    if (client.connect(HOST_NAME, HTTP_PORT)) {
      String str_temp1 = String(temp1);
      String str_hum1 = String(hum1);
      String str_temp2 = String(temp2);
      String str_hum2 = String(hum2);
      queryString = "?value1=" + str_temp1;
      queryString2 = "value2=" + str_temp2;
      queryString_hum2 = "value3=" + str_hum1 + "%20Communication%20room:%20" + str_hum2;

      Serial.println("GET " + PATH_NAME + queryString + "&" + queryString2 + "&" + queryString_hum2 + " HTTP/1.1");
      Serial.println("Connected to server");
      client.println("GET " + PATH_NAME + queryString + "&" + queryString2 + "&" + queryString_hum2 + " 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);
        }
      }
    }
  }
}

Are you sure?
Your temp comparision is still completely wrong

try search in w3 school. Helps me a lot when it comes to problem like this

well that's what people on StackOverflow suggest, so I am not sure what should be different... what would you recommend?

Also, I have tested it multiple times and it is working without any issues.

I doubt it. Post a link were you got that code for comparing two values against a maximum.

yes, it works, but not as you expecting
The code:

is equivalent to this:

So you just threw out the first temperature

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.