Hi Experts,
I am building station that will measure humidity on hourly basis and will send it to MySQL database through basic REST php API. I have manged to get the code working - meaning, Arduino measures humidity and transmits it via php API to MySQL database BUT only the first three submissions are successful, after that Arduino fails to connect with DB.
- I have tried different lead-times between submissions and it always is 3 successful ones and then "that is it".
- I am sure that it is not the MySQL DB that is cutting the connection out because I manage to submit a lot of data-points with Postman
- It does look like something is up with my Arduino code... any Ideas?
#include <SPI.h>
#include <Ethernet.h>
//Calibration parameters for Sensor#1
const int Sensor1MaxValue_Dry = 610;
const int Sensor1MinValue_Wet = 300;
//Calibration parameters for Sensor#2
const int Sensor2MaxValue_Dry = 630;
const int Sensor2MinValue_Wet = 435;
//initial valuses for Sensor#1&#2
//int rh = 0;
//int rt = 0;
int h = 0;
int t = 0;
const char* host = "domain.com"; //Not sure about that * right behind const char
byte mac[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
EthernetServer server(80);
String readString;
void setup() {
Serial.begin(9600);
Ethernet.begin(mac);
server.begin();
Serial.println("");
Serial.print("Garden monitoring station is conected at (");
Serial.print(Ethernet.localIP());
Serial.println(").");
Serial.println("");
}
void loop() {
int rh = analogRead(A1);
int rt = analogRead(A2);
h = map(rh, Sensor1MaxValue_Dry, Sensor1MinValue_Wet, 0, 100);
if (h > 100)
{
h = 100;
}
else if (h < 0)
{
h = 0;
}
t = map(rt, Sensor2MaxValue_Dry, Sensor2MinValue_Wet, 0, 100);
if (t > 100)
{
t = 100;
}
else if (t < 0)
{
t = 0;
}
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from sensors!");
return;
}
Serial.print("Connecting to database at ");
Serial.println(host);
EthernetClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("Connection to database failed!");
return;
}
String url = "/api/weather/insert.php?temp=" + String(t) + "&hum="+ String(h);
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);
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("");
Serial.println("Transmitted datapoints were:");
Serial.print("Soil humidity [A1] is ");
Serial.print(h);
Serial.print(" (");
Serial.print(rh);
Serial.println(")");
Serial.print("Soil humidity [A2] is ");
Serial.print(t);
Serial.print(" (");
Serial.print(rt);
Serial.println(")");
Serial.println("");
Serial.println("Closing connection to database.");
Serial.println("");
delay(10000);
}