Read Data from Sensor and POST to MySQL

Hi all, I have this script that I’ve put together to gather a reading from sensors, time stamp it and post it to a PHP script that will insert it into MySQL.

My problem is, nothing works. The serial print works in setup but nothing is printed during the loop. I’m new to this not sure what I’m doing wrong :frowning:

I’ve uploaded my code for you to see.

Thank you!

client.ino (8.97 KB)

OP’s code

#include <Ethernet.h>
#include <SPI.h>

byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 }; // RESERVED MAC ADDRESS
EthernetClient client;
IPAddress dnServer(192, 168, 131, 61);

long previousMillis = 0;
unsigned long currentMillis = 0;
long interval = 250000; // READING INTERVAL

int p1 = 0;  // PRESSURE SENSOR VARS
int p2 = 0;
int p3 = 0;
int p4 = 0;
int p5 = 0;
int p6 = 0;

const int pinSensor1 = A0;
const int pinSensor2 = A1;
const int pinSensor3 = A2;
const int pinSensor4 = A3;
const int pinSensor5 = A4;
const int pinSensor6 = A5;

void setup() {
  Serial.begin(9600);
  Serial.print("Hi");

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
  }

  p1 = (int) readPressure1();
  p2 = (int) readPressure2();
  p3 = (int) readPressure3();
  p4 = (int) readPressure4();
  p5 = (int) readPressure5();
  p6 = (int) readPressure6();
}

void loop() {

  p1 = (int) readPressure1();
  p2 = (int) readPressure2();
  p3 = (int) readPressure3();
  p4 = (int) readPressure4();
  p5 = (int) readPressure5();
  p6 = (int) readPressure6();

  if (client.connect("datalogger.com", 80)) { // REPLACE WITH YOUR SERVER ADDRESS
    client.println("POST /add.php HTTP/1.1");
    client.println("Host: 192.168.131.207"); // SERVER ADDRESS HERE TOO
    client.println("Content-Type: application/x-www-form-urlencoded");
    client.print("Content-Length: ");
    client.print(p1);
    client.print(p2);
    client.print(p3);
    client.print(p4);
    client.print(p5);
    client.print(p6);
    Serial.print(p1);
    Serial.print(p2);
    Serial.print(p3);
    Serial.print(p4);
    Serial.print(p5);
    Serial.print(p6);
  }

  if (client.connected()) {
    client.stop();  // DISCONNECT FROM THE SERVER
  }


}

float readPressure1() {
  // Calculate the average of a number of samples.
  // The analogRead() is 0...1023.
  // When they are all added into a total value, the total should be able to fit in the choosen variable.
  // The sketch has a software timing with millis() of 100ms.
  // That means a delay below 100ms will have only little influence on the timing of the sketch.
  // Perhaps 100 samples during 50ms will improve the result.
  // That is 500us per sample. The analogRead() takes 120us on a Arduino Uno.
  // So an extra delay of 500-120 = 380us is needed.
  // The 100 samples will fit in a unsigned long.


  // Calculating the formula used to convert voltage to pressure in KPA
  // Calculate the voltage with floating point
  float Vout = 5; // 1024 steps from 0V to 5V.
  float alpha = 0.036; //Alpha Value
  float beta = 0.04; //Beta Value
  float Pe = 0.375; //Pressure Error Value
  float Te = -70; // Temprature Error Value
  float Vdd = 4.95; // Voltage in
  float pressure = ( Vout / (alpha * Vdd) ) + ( beta * Pe * Te ); // Calculating Pressure from given variables in KPA
  float p1 = (pressure * 0.145037738); // Calculating PSI from the given pressure

  return ( p1);       // return the pressure in psi

}
float readPressure2() {
  float Vout = 5; // 1024 steps from 0V to 5V.
  float alpha = 0.036; //Alpha Value
  float beta = 0.04; //Beta Value
  float Pe = 0.375; //Pressure Error Value
  float Te = -70; // Temprature Error Value
  float Vdd = 4.95; // Voltage in
  float pressure = ( Vout / (alpha * Vdd) ) + ( beta * Pe * Te ); // Calculating Pressure from given variables in KPA
  float p2 = (pressure * 0.145037738); // Calculating PSI from the given pressure

  return ( p2);       // return the pressure in psi

}
float readPressure3() {
  float Vout = 5; // 1024 steps from 0V to 5V.
  float alpha = 0.036; //Alpha Value
  float beta = 0.04; //Beta Value
  float Pe = 0.375; //Pressure Error Value
  float Te = -70; // Temprature Error Value
  float Vdd = 4.95; // Voltage in
  float pressure = ( Vout / (alpha * Vdd) ) + ( beta * Pe * Te ); // Calculating Pressure from given variables in KPA
  float p3 = (pressure * 0.145037738); // Calculating PSI from the given pressure

  return ( p3);       // return the pressure in psi

}
float readPressure4() {
  float Vout = 5; // 1024 steps from 0V to 5V.
  float alpha = 0.036; //Alpha Value
  float beta = 0.04; //Beta Value
  float Pe = 0.375; //Pressure Error Value
  float Te = -70; // Temprature Error Value
  float Vdd = 4.95; // Voltage in
  float pressure = ( Vout / (alpha * Vdd) ) + ( beta * Pe * Te ); // Calculating Pressure from given variables in KPA
  float p4 = (pressure * 0.145037738); // Calculating PSI from the given pressure

  return ( p4);       // return the pressure in psi

}
float readPressure5() {
  float Vout = 5; // 1024 steps from 0V to 5V.
  float alpha = 0.036; //Alpha Value
  float beta = 0.04; //Beta Value
  float Pe = 0.375; //Pressure Error Value
  float Te = -70; // Temprature Error Value
  float Vdd = 4.95; // Voltage in
  float pressure = ( Vout / (alpha * Vdd) ) + ( beta * Pe * Te ); // Calculating Pressure from given variables in KPA
  float p5 = (pressure * 0.145037738); // Calculating PSI from the given pressure

  return ( p5);       // return the pressure in psi

}
float readPressure6() {
  float Vout = 5; // 1024 steps from 0V to 5V.
  float alpha = 0.036; //Alpha Value
  float beta = 0.04; //Beta Value
  float Pe = 0.375; //Pressure Error Value
  float Te = -70; // Temprature Error Value
  float Vdd = 4.95; // Voltage in
  float pressure = ( Vout / (alpha * Vdd) ) + ( beta * Pe * Te ); // Calculating Pressure from given variables in KPA
  float p6 = (pressure * 0.145037738); // Calculating PSI from the given pressure

  return ( p6);       // return the pressure in psi

}

What's the purpose of readPressure1() to readPressure6(), seems like all these function does the same logic.

Which pin Ethernet shield is connected? Hardware Serial? If Hardware serial then not sure Serial.print and Ethernet shield go hand in hand.

Remove the Ethernet shield and see whether you are able to see any data coming to Serial monitor.