I'm making a web-hosted fermenter controller with an esp8266. I have already managed to send the temperature sensor data to the server and save the data in the database. But I still don't know how to do it the other way around. Fill in a form with data (eg target temperature and time) and submit to esp8266.
<form action="????" method="get" name="form1">
<table>
<tr>
<td>Temperatura</td>
<td><input name="temp" type="text"></td>
</tr>
<tr>
<td>
<input name="enviar" type="submit" value="Enviar">
</td>
</tr>
</table>
</form>
This is the arduino code:
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecureBearSSL.h>
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is connected to GPIO 4 / D2
#define ONE_WIRE_BUS 4
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
// Replace with your network credentials
const char* ssid = "*****";
const char* password = "*****";
// REPLACE with your Domain name and URL path or IP address with path
const char* serverName = "https://mysite.com.br/fermentador/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-esp-data.php also needs to have the same key
String apiKeyValue = "*****";
void setup() {
Serial.begin(115200);
// Start up the DS18B20 library
sensors.begin();
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());
}
void loop() {
//Check WiFi connection status
if(WiFi.status()== WL_CONNECTED){
std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);
// Ignore SSL certificate validation
client->setInsecure();
//create an HTTPClient instance
HTTPClient https;
// Your Domain name with URL path or IP address with path
https.begin(*client, serverName);
// Specify content-type header
https.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Prepare your HTTP POST request data
sensors.requestTemperatures();
String httpRequestData = "api_key=" + apiKeyValue + "&value1=" + String(sensors.getTempCByIndex(0))
+ "";
Serial.print("httpRequestData: ");
Serial.println(httpRequestData);
// Send HTTP POST request
int httpResponseCode = https.POST(httpRequestData);
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
https.end();
}
else {
Serial.println("WiFi Disconnected");
}
//Send an HTTP POST request every 30 seconds
delay(30000);
}
This receives the data, on the web server and it works perfectly
<?php
include 'conecta.php';
// 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 = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$api_key = test_input($_POST["api_key"]);
if($api_key == $api_key_value) {
$value1 = test_input($_POST["value1"]);
$sql = "INSERT INTO Sensor (value1)
VALUES ('" . $value1 . "')";
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;
}
?>
How do I send form data to esp? What should I put in the "action" (if that's how it is)?
And how to catch it in esp?