HTTP GET request: timing issue?

Hi all,

As a noob I am about to finalize my first project: An Arduino Uno Wifi Rev2 sends sensor data to a MySQL table on my Raspberry Pi webserver. Most of it wroks fina, and it was awesome fun to learn about all the tools involved!

However, I have one last issue to be solved: I have about 6 pieces sensor data to be posted in my table: temperature, humidity, waterlevel and the voltage of 3 different batteries. Temp, humidity and water level get posted perfectly, however the three voltages do not! I can see actual values in the serial monitor, but in MySQL they show up as 0. The php script posting the data to MySQL works fine, since I can enter data into the table by specifying values in my URL.

As far as I can tell, it seems like a timing issue?
Below is the relevant part of the code, and here is what I get in serial monitor:

First part of serial monitor info:
4:00:38.954 -> Attempting to connect to Network named: telenet-1EE6128
14:00:53.246 -> SSID: telenet-1EE6128
14:00:53.246 -> IP Address: 192.168.0.170
14:00:53.795 -> Humidity: 40.00 % Temperature: 20.50 C Water: 1
14:00:53.864 -> VoltageMotor: 0.00V
14:00:53.864 -> VoltageServ1: 3.17V
14:00:53.898 -> VoltageServ2: 5.18V
14:00:54.173 -> connected
14:00:54.173 -> GET /post.php?humidity=

Then, about 20 sec. later, this is added ("=Humidity: 40.00% Temp....." being added to the last existing line!)
14:00:54.173 -> GET /post.php?humidity=Humidity: 40.00 % Temperature: 20.60 C Water: 0
14:01:14.247 -> VoltageMotor: 0.02V
14:01:14.282 -> VoltageServ1: 3.29V
14:01:14.282 -> VoltageServ2: 5.37V
14:01:14.456 -> connected
14:01:14.456 -> GET /post.php?humidity=

void loop() {
  // Read the DHT11 sensor data:
  humidity=HT.readHumidity();
  tempC=HT.readTemperature();

  // Read the water sensor data:
  valueWater = analogRead(SIGNAL_PIN);

  // Read the motor voltage sensor data
  float voltmotor = analogRead(A0);// read the input
  double voltagemotor = map(voltmotor,0,1023, 0, 2500) + offset;// map 0-1023 to 0-2500 and add correction offset
  voltagemotor /=100;// divide by 100 to get the decimal values

  // Read the service batteries voltage sensor data
  float voltserv1 = analogRead(A1);// read the input
  double voltageserv1 = map(voltserv1,0,1023, 0, 2500) + offset;// map 0-1023 to 0-2500 and add correction offset
  voltageserv1 /=100;// divide by 100 to get the decimal values
  float voltserv2 = analogRead(A2);// read the input
  double voltageserv2 = map(voltserv2,0,1023, 0, 2500) + offset;// map 0-1023 to 0-2500 and add correction offset
  voltageserv2 /=100;// divide by 100 to get the decimal values

  // Print all data to serial monitor
  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.print(" % ");
  Serial.print(" Temperature: ");
  Serial.print(tempC);
  Serial.print(" C ");
  Serial.print(" Water: ");
  Serial.println(valueWater);
  Serial.print("VoltageMotor: ");
  Serial.print(voltagemotor);//print the voltagemotor
  Serial.println("V");
  Serial.print("VoltageServ1: ");
  Serial.print(voltageserv1);
  Serial.println("V");
  Serial.print("VoltageServ2: ");
  Serial.print(voltageserv2);
  Serial.println("V");

  //delay(5000); // interval before sending to webserver
  Sending_To_phpmyadmindatabase(); 

}

 void Sending_To_phpmyadmindatabase() {  //CONNECTING WITH MYSQL
 
   if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    Serial.print("GET /post.php?humidity=");
    client.print("GET /post.php?humidity=");
    client.print(humidity);
    client.print("&temperature=");
    client.print(tempC);
    client.print("&water=");
    client.print(valueWater);
    client.print("&voltagemotor=");
    client.print(voltagemotor);
    client.print("&voltageserv1=");
    client.print(voltageserv1);
    client.print("&voltageserv2=");
    client.print(voltageserv2);   
    client.print(" ");      //SPACE BEFORE HTTP/1.1
    client.print("HTTP/1.1");
    client.println();
    client.println("Host: secondrival.ddns.net");
    client.println("Connection: close");
    client.println();
    } 
  
    else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
    }
   
  //Delay for defined time before next reading
  delay(delayTime);
}

The timing issue may simply be the delay after you send. Hard to say without seeing all your code though.

Here is the whole sketch:

// DHT sensor library - Version: Latest 
// Settings for DHT11
#include <DHT.h>
#include <DHT_U.h>
#define Type DHT11
int sensePin=2;

DHT HT (sensePin,Type);
float humidity;
float tempC;
float tempF;
int setTime=500;
int delayTime=20000;

// Settings for water sensor
#define SIGNAL_PIN A0
int valueWater = 0; // variable to store the sensor value

// Settings for voltage sensors
int offset =0;
float voltmotor;
float voltagemotor;
float voltserv1;
float voltageserv1;
float voltserv2;
float voltageserv2;
float current;

//Settings for wifi
#include <WiFiNINA.h>
char ssid[] = "******";
char pass[] = "******";
int status = WL_IDLE_STATUS;
char server[] = "************";
WiFiClient client;

void setup() {
  //Setup for Wifi connection
    Serial.begin(9600);

  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);
    delay(10000);
  }

  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
  IPAddress ip = WiFi.localIP();
  IPAddress gateway = WiFi.gatewayIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
  
  // Setup for DHT11:
  Serial.begin(9600);
  HT.begin();
  delay(setTime);
}

void loop() {
  // Read the DHT11 sensor data:
  humidity=HT.readHumidity();
  tempC=HT.readTemperature();

  // Read the water sensor data:
  valueWater = analogRead(SIGNAL_PIN);

  // Read the motor voltage sensor data
  float voltmotor = analogRead(A0);// read the input
  double voltagemotor = map(voltmotor,0,1023, 0, 2500) + offset;// map 0-1023 to 0-2500 and add correction offset
  voltagemotor /=100;// divide by 100 to get the decimal values

  // Read the service batteries voltage sensor data
  float voltserv1 = analogRead(A1);// read the input
  double voltageserv1 = map(voltserv1,0,1023, 0, 2500) + offset;// map 0-1023 to 0-2500 and add correction offset
  voltageserv1 /=100;// divide by 100 to get the decimal values
  float voltserv2 = analogRead(A2);// read the input
  double voltageserv2 = map(voltserv2,0,1023, 0, 2500) + offset;// map 0-1023 to 0-2500 and add correction offset
  voltageserv2 /=100;// divide by 100 to get the decimal values

  // Print all data to serial monitor
  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.print(" % ");
  Serial.print(" Temperature: ");
  Serial.print(tempC);
  Serial.print(" C ");
  Serial.print(" Water: ");
  Serial.println(valueWater);
  Serial.print("VoltageMotor: ");
  Serial.print(voltagemotor);//print the voltagemotor
  Serial.println("V");
  Serial.print("VoltageServ1: ");
  Serial.print(voltageserv1);
  Serial.println("V");
  Serial.print("VoltageServ2: ");
  Serial.print(voltageserv2);
  Serial.println("V");

  //delay(5000); // interval before sending to webserver
  Sending_To_phpmyadmindatabase(); 

}

 void Sending_To_phpmyadmindatabase() {  //CONNECTING WITH MYSQL
 
   if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    Serial.print("GET /post.php?humidity=");
    client.print("GET /post.php?humidity=");
    client.print(humidity);
    client.print("&temperature=");
    client.print(tempC);
    client.print("&water=");
    client.print(valueWater);
    client.print("&voltagemotor=");
    client.print(voltagemotor);
    client.print("&voltageserv1=");
    client.print(voltageserv1);
    client.print("&voltageserv2=");
    client.print(voltageserv2);   
    client.print(" ");      //SPACE BEFORE HTTP/1.1
    client.print("HTTP/1.1");
    client.println();
    client.println("Host: *************");
    client.println("Connection: close");
    client.println();
    } 
  
    else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
    }
   
  //Delay for defined time before next reading
  delay(delayTime);
}

The twenty seconds is just the delay at the end of Sending_To_phpmyadmindatabase.

What do you put in your browser URL when you can push data successfully into the database?

wildbill:
The twenty seconds is just the delay at the end of Sending_To_phpmyadmindatabase.

What do you put in your browser URL when you can push data successfully into the database?

Correct: the 20 seconds is there for now just to be able to test fluently. In the end I will have the data sent to my webserver every 4 to 5 hours, using the same delay function.

Anyway: below is the URL leading to succesfull DB-update, being confirmed by the message “new record created succesfully”:
/post.php?temperature=32&humidity=64&water=3&voltagemotor=12.8&voltageserv1=12.9&voltageserv2=13.0

Do you see those voltage numbers in the DB?

You are declaring local variables for the voltage readings in loop() with the same names as the global variables. The function that sends data to your database uses the global variables.

Thanks for your help guys!
David was totally right, I deleted the local declaration of the variables and that did it!

Super happy !!!

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