Hi All
I have done this tutorial.
Visualize Your Sensor Readings from Anywhere in the World (ESP32/ESP8266 + MySQL + PHP)
and have added stuff to get where I am and very happy.
My problem now is that I have added another value for the MySQL database and have written the code to get it showing on the serial monitor and have added it to the database in MySQL but the value isn’t uploading with the other values.
I’ve tried to load a screen shot but I can’t.
[code]
// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>
#include “DHT.h”
#include “HX711.h”
#ifdef ESP32
#include <WiFi.h>
#include <HTTPClient.h>
#else
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#endif
// Replace with your network credentials
const char* ssid = “Pattos Place”;
const char* password = “MVrocks2020”;
// REPLACE with your Domain name and URL path or IP address with path
const char* serverName = “http://buzzyhunny.com/post-data.php”;
// Keep this API Key value to be compatible with the PHP code provided in the project page.
// If you change the apiKeyValue value, the PHP file /post-data.php also needs to have the same key
String apiKeyValue = “tPmAT5Ab3j7F9”;
#define DHTPIN 4 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
#define ONE_WIRE_BUS 15 // Data wire is connected to GPIO15
#define calibration_factor -20600.0
#define LOADCELL_DOUT_PIN 22
#define LOADCELL_SCK_PIN 23
HX711 scale;
// Setup a oneWire instance to communicate with a OneWire device
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
DeviceAddress sensor1 = { 0x28, 0x6A, 0x48, 0x7C, 0x0C, 0x00, 0x00, 0xC4 };
DeviceAddress sensor2 = { 0x28, 0xFA, 0x14, 0x7B, 0x0C, 0x00, 0x00, 0x47 };
DHT dht(DHTPIN, DHTTYPE);
// Time to sleep
uint64_t uS_TO_S_FACTOR = 1000000; // Conversion factor for micro seconds to seconds
// sleep for 30 minutes = 1800 seconds
uint64_t TIME_TO_SLEEP = 1800;
// Get Sensor Readings
String getSensorReadings(){
sensors.requestTemperatures();
}
void setup() {
Serial.begin(9600);
sensors.begin();
dht.begin();
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
scale.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0
Serial.println(“Starting…”);
WiFi.begin(ssid, password);
Serial.println(“Connecting”);
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(“.”);
}
Serial.println(“”);
Serial.print(“Connected to WiFi network with IP Address: “);
Serial.println(WiFi.localIP());
#ifdef ESP32
// enable timer deep sleep
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
Serial.println(“Going to sleep now”);
// start deep sleep for 3600 seconds (60 minutes)
#else
// Deep sleep mode for 3600 seconds (60 minutes)
Serial.println(“Going to sleep now”);
ESP.deepSleep(TIME_TO_SLEEP * uS_TO_S_FACTOR);
#endif
}
void loop(){
//Check WiFi connection status
if(WiFi.status()== WL_CONNECTED){
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(serverName);
// Specify content-type header
http.addHeader(“Content-Type”, “application/x-www-form-urlencoded”);
// Prepare your HTTP POST request data
String httpRequestData = “api_key=” + apiKeyValue + “&value1=” + String(dht.readTemperature())
+ “&value2=” + String(dht.readHumidity()) + “&value3=” + String(sensors.getTempC(sensor1)) + “&value4=” + String(sensors.getTempC(sensor2))+ “&value5=” + String(scale.get_units())+””;
Serial.print(“httpRequestData: “);
Serial.println(httpRequestData);
// You can comment the httpRequestData variable above
// then, use the httpRequestData variable below (for testing purposes without the BME280 sensor)
//String httpRequestData = “api_key=tPmAT5Ab3j7F9&value1=24.75&value2=49.54&value3=1005.14”;
// Send HTTP POST request
int httpResponseCode = http.POST(httpRequestData);
// If you need an HTTP request with a content type: text/plain
//http.addHeader(“Content-Type”, “text/plain”);
//int httpResponseCode = http.POST(“Hello, World!”);
// If you need an HTTP request with a content type: application/json, use the following:
//http.addHeader(“Content-Type”, “application/json”);
//int httpResponseCode = http.POST(“{\”value1\”:\”19\”,\”value2\”:\”67\”,\”value3\”:\”78\”}”);
if (httpResponseCode>0) {
Serial.print(“HTTP Response code: “);
Serial.println(httpResponseCode);
}
else {
Serial.print(“Error code: “);
Serial.println(httpResponseCode);
}
// Free resources
http.end();
}
else {
Serial.println(“WiFi Disconnected”);
}
//Send an HTTP POST request every 30 seconds
delay(30000);
// Wait a few seconds between measurements.
delay(20000);
//Serial.print(“Requesting temperatures…”);
sensors.requestTemperatures(); // Send the command to get temperatures
// Serial.println(“DONE”);
Serial.print(“8 Frame Hive: “);
Serial.print(sensors.getTempC(sensor1));
Serial.print(“°C: “);
Serial.print(“10 Frame Hive: “);
Serial.print(sensors.getTempC(sensor2));
Serial.print(“°C: “);
Serial.print(“Weight “);
Serial.print(scale.get_units(), 1);
Serial.print(” Kgs “);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds ‘old’ (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println(F(“Failed to read from DHT sensor!”));
return;
}
Serial.print(F(“8 Frame Roof Temp: “));
Serial.print(t);
Serial.print(F(“°C “));
Serial.print(F(“8 Frame Roof Humidity: “));
Serial.print(h);
Serial.print(F(“% “));
Serial. print(‘\n’);
esp_deep_sleep_start();
}
[/code]
The serial monitor prints this
8 Frame Hive: -127.00°C: 10 Frame Hive: -127.00°C: Weight 5.2 Kgs Failed to read from DHT sensor!
httpRequestData: api_key=tPmAT5Ab3j7F9&value1=nan&value2=nan&value3=-127.00&value4=-127.00&value5=5.18
HTTP Response code: 200
The Value in question is value 5 and as you can see its showing here and on the database its shown as a column but with Null as the value
Hello,
It looks like there’s an issue with your DHT sensor.
Can you please try the next recommended DHT debugging instructions to see if it helps make the sensor work?
Hi
The value in question – Value5 is the weight value and it shows 5.2 on the serial monitor but null on the database.
Why is that happening.
Its not the DHT as i don’t have it connected. when it is connected it shows the correct value
Hi.
I’m sorry for the misunderstanding about the DHT sensor. I thought it was crashing your code.
According to what you said, it looks like that the Arduino code is correct and that the board is actually trying to post all the values.
The problem must be related with your php script that receives those values and tries to insert them on the MySQL database.
- Can you share the php script?
- Can you double-check that you created the value5 column in the database exactly as the other values?
Regards,
Sara
I copy and pasted the script straight from the tutorial and then copied 2 of the value lines and changed them 2 value4 and value5. see below
CREATE TABLE Sensor ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, value1 VARCHAR(10), value2 VARCHAR(10), value3 VARCHAR(10),
value4 VARCHAR(10),
value5 VARCHAR(10), reading_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP )
Value 4 works but not value 5
Hi.
It seems that the table is created correctly.
Maybe there is something wrong with the php script. As I mentioned in my previous answer, can you share the complete php script (remove the username and password before posting)? Otherwise, it is very difficult to find out what is causing the issue.
Regards,
Sara
<?php
/*
Rui Santos
Complete project details at https://RandomNerdTutorials.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
$servername = “localhost”;
// REPLACE with your Database name
$dbname = “”;
// REPLACE with Database user
$username = “”;
// REPLACE with Database user password
$password = “”;
// Keep this API Key value to be compatible with the ESP32 code provided in the project page. If you change this value, the ESP32 sketch needs to match
$api_key_value = “”;
$api_key = $value1 = $value2 = $value3 = $value4 = $value5 = “”;
if ($_SERVER[“REQUEST_METHOD”] == “POST”) {
$api_key = test_input($_POST[“api_key”]);
if($api_key == $api_key_value) {
$value1 = test_input($_POST[“value1”]);
$value2 = test_input($_POST[“value2”]);
$value3 = test_input($_POST[“value3”]);
$value4 = test_input($_POST[“value4”]);
$value5 = test_input($_POST[“value5”]);
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die(“Connection failed: ” . $conn->connect_error);
}
$sql = “INSERT INTO Sensor (value1, value2, value3, value4, value5)
VALUES (‘” . $value1 . “‘, ‘” . $value2 . “‘, ‘” . $value3 . “‘, ‘” . $value4 . “‘,'” . $value5 . “‘)”;
if ($conn->query($sql) === TRUE) {
echo “New record created successfully”;
}
else {
echo “Error: ” . $sql . “<br>” . $conn->error;
}
$conn->close();
}
else {
echo “Wrong API Key provided.”;
}
}
else {
echo “No data posted with HTTP POST.”;
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}