Go Down

Topic: Updating Two Rows in a Database at the Same Time... (Read 918 times) previous topic - next topic

analog56x

Hey everyone, ive got an automated heating system set up in my aquariums. I have it working on a ESP8266, and have been running one tank per ESP, but now im trying to streamline my system and make two or more tanks run on one ESP.

It works perfect with a single tank: it starts off with gathering information from my website (which is updated from an app i built). The information gathered is target temperature, lighting on time, lighting off time and lighting intensity.
the script then does whatever it needs to to make the parameters match the target parameters.

the thing im having an issue with is the uploading portion of the script... it needs to pull the temperature from two tanks and upload each value to a separate row in a single database. i can only seem to get it to do one or the other.

Code: [Select]
//Send information to database on website
  Serial.print("Connecting to ");
  Serial.println(host);

  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("Connection failed!");
    return;
  }

// Tank 1
  
  String url = "/arduino/postt1.php?tempc=";
  url += tempC1,1;
  url += "&heat=";
  url += heater1;
  
  Serial.print("Requesting URL: ");
  Serial.println(url);
  client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");

delay(500);

// Tank 2

  String url2 = "/arduino/postt2.php?tempc=";
  url2 += tempC2,1;
  url2 += "&heat=";
  url2 += heater2;

  Serial.print("Requesting URL: ");
  Serial.println(url2);
  client.print(String("GET ") + url2 + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");


  unsigned long timeout = millis();
  
  while (client.available() == 0) {
    if (millis() - timeout > 5000) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      return;
    }
  }



any help in getting it to post to both URL's would be greatly appreciated!

this is what the serial output looks like:

Code: [Select]
String:14.42, 19.44, 24, on, Tank 1, 226, 8.0, 22.0, 1
Arduino Time: 14.42
Current Temp:  19.44
Target Temp:  24
Heater Status:  on
Tank Name:  Tank 1
Light Intensity:  226
Lights On:  8.0
Lights Off:  22.0
ID:  1



String:14.42, 16.37, 24, on, Tank 2, 1023, 8, 20, 2
Arduino Time: 14.42
Current Temp:  16.37
Target Temp:  24
Heater Status:  on
Tank Name:  Tank 2
Light Intensity:  1023
Lights On:  8
Lights Off:  20
ID:  2


Connecting to *website*
Requesting URL: /arduino/postt1.php?tempc=19.44&heat=on
Requesting URL: /arduino/postt2.php?tempc=-127.00&heat=on


instead of posting the first string, then posting the second string, it just posts the first, and ignores the second. its pulling the old info off the database for tank 2.

mikb55

Have you checked the web server log file to see if both URLs are being received?

Install Wireshark on the web server and show the capture output that corresponds to the Arduino activity.

wildbill

What do you get if you hit the web pages manually from a browser?

steve_mcdonald

Depending on the database (and database engine) in use, you may be hitting a lock violation at the database.  How is locking implemented on your database?  If you are locking at the table or database level then the table  or database will need to be released (or otherwise unlocked) between writing two records.  If you are locking at the record level, then the database engine needs to be able to recognise that  the updates are intended for two separate records.  How depends on what database driver you're using. 

An earlier suggestion suggested trying to send two requests from a browser.  What did that show?

WHat happens if you close the database connection and open a new connection for the second record update - that should work because it would force release all locks.

Go Up