Hi, I am using the bme280 sensor with a esp32-c3 from Adafruit & micropython which works fine. On my domain I uploaded to my domain esp-chart.php & post-data.php from tutorial visualize-esp32-esp8266-sensor-readings-from-anywhere/. This tutorial was based on the Arduino. I changed the client requests from Arduino to micropython and I am using this code:
api_key = “tPmAT5Ab3j7F9”
while True:
temp = bme.temperature
hum = bme.humidity
pres = round(bme.pressure, 1)
temp = (bme.read_temperature()/100) * (9/5) + 32
temp = round(temp, 2)
#print(‘Temperature: ‘, temp)
#print(‘Humidity: ‘, hum)
#print(‘Pressure: ‘, pres)
sensor_readings = “api_key={}&value1={}&value2={}&value3={}”.format(api_key, temp, hum, pres)
request_headers = {“Content-Type”: “application/x-www-form-urlencoded”}
request = urequests.post(url, json=sensor_readings, headers=request_headers)
print(request.text)
request.close()
sleep(5)
This is working fine for the data storage and presentation to esp-chart.php when $api_key_value = 0.
The problem I am having is the “api_key”, no matter what value it equals I always get a response “Wrong API Key provided.”. If I change my “api_key_value” to equal 0(zero) then all data is stored, “New record created successfully”.
In my client urequests post, I am thinking I may need to add or change a header or change the way I present data to the post.
I have your Micropython Programming eBook any info for posting data to a domain.
I am at a loss and would really appreciate any input.
Thank You
Hello! Can you post how your post-data.php file looks like?
Please leave the API key, but DELETE the database information.
Hi Rui,
Thank you for the response. Since I posted my question I discovered by adding an ampersand to the beginning of my sensor_readings string my request work.
sensor_readings = “&api_key={}&value1={}&value2={}&value3={}”.format(api_key, temp, hum, pres)
To answer your question I am using your post-data.php as follows:
<?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_YOUR_DATABASE_NAME"; // REPLACE with Database user $username = "REPLACE_WITH_YOUR_USERNAME"; // REPLACE with Database user password $password = "REPLACE_WITH_YOUR_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 = "tPmAT5Ab3j7F9"; $api_key = $value1 = $value2 = $value3 = ""; 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"]); // 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) VALUES ('" . $value1 . "', '" . $value2 . "', '" . $value3 . "')"; 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; }
Unfortunately the problem I face now, after so many randomly successful records created,
I receive an OSError:[Errno 113] ECONNABORTED after a try: s.connect(ai[-1]) in urequests.py
Thanks again
Oh! I see, yup you definitely need to format the query properly and leave the correct amount of &.
What does your variable sensor_readings is storing? Can you print it in the serial monitor and post it here to see if the variable formatted correctly in order to send the request properly?
This script might help:
That script is working, you just need to change toSorry for the late reply.
Regards,
Rui