I have your code running on my ESP32 to upload DHT22 sensor readings to a mysql database, along with another few variables.
I have a timer running so that it pushes the results every 15 minutes. All is working well (aside from the DHT22 frequently returning no data).
I’ve modified and styled the table with CSS and the help of https://www.w3schools.com/css/css_table.asp and it looks great!
Is there a simple way to modify the PHP file to show the most recent data first, rather than having to scroll to the bottom?
Thanks in advance!
Hello Vincent, thanks for letting me now and I’m glad you have the code running successfully.
It’s definitely possible to do it and you just need to edit the esp-data.php file: https://github.com/RuiSantosdotme/ESP32-ESP8266-PHP-MySQL/blob/master/code/esp-data.php
In line 32, where I have the SQL query:
$sql = "SELECT id, sensor, location, value1, value2, value3, reading_time FROM SensorData";
Modify it to:
$sql = "SELECT id, sensor, location, value1, value2, value3, reading_time FROM SensorData ORDER BY reading_time DESC";
I haven’t tried, but I’m almost sure that it will work. Let me know your results!
Regards,
Rui
Thanks Rui! Worked perfectly! Very excited to see your upcoming tutorial on publishing the charts with the database data. Keep up the great work 🙂
You’re welcome! It should be published later this week or next week. It’s almost ready!
Hello Vincent, I think you already saw my project (I think you posted a comment on my blog). But as a reference I’ve posted that project on my blog:
- Visualize Your Sensor Readings from Anywhere in the World on Charts (ESP32/ESP8266 + MySQL + PHP)
- https://randomnerdtutorials.com/visualize-esp32-esp8266-sensor-readings-from-anywhere/
Regards,
Rui
Thanks Rui,
Yes, I saw it and have it running! I love it. I’m now almost independent from the Blynk App except for notifications. I still want to figure out a simple way to send notifications if certain parameters are met. The only other questions I’d like to look in to would be displaying 2 values on the same chart (like temperature and heat index) and using other chart types like gauges. Any advice would be appreciated.
Vin
What type of notifications? Email notifications? I might create that as my next tutorial…
I recommend search HighChart.js 2 values on same chart (or a variation of that search). You can definitely add more than one series of values to each chart.
Okay great. I’ll look into it.
I like how Blynk is an easy way to get all data and push notifications to an iOS app, but any notifications would work. I am still testing at home, but plan to have the ESP and sensors on a boat and would like notifications when the temp or humidity gets too high or when a battery voltage drops to low, etc. I like that Blynk offers notifications without any additional hardware like a SIM card. I’m looking to get away from the Blynk app because I frequently loose connection (several times per day) and not sure why. I have updated the code to check for Blynk connection and if not, reset the ESP, which usually works.
One other thing I’m having trouble figuring out the best way to accomplish is to monitor a 12 volt pump, when it triggers and time how long it runs. What would be the best way to measure a 12 volt signal. Could I used a relay in reverse somehow. In other words, instead of triggering a pin high (or low) to activate the relay side, could the relay take the GPIO high when it sees 12 volts? Is there some other better way to do this?
Lastly, I’d like to mention that I had a terrible time getting my DHT sensor to work. Things worked great when I first programmed it then the sensor failed over 90% of the time. I tried everything to get it working again.
Bought new sensors, bought new breadboards, bought new ESPs, 3.3v, 5v…nothing worked. I thought I messed something up in my code, but even the DHT tester would fail to get readings. I tested with an Uno and it did work. Just before deciding to abandon the DHT altogether, I deleted the DHT library from the folder and re-installed it. Now working perfectly! I don’t know why or what could have happened, but this fixed my problem. May be worth noting if something like that comes up with other subscribers.
Anyway, thanks again for the great tutorials. Always looking forward to seeing the next one.
Best,
Vin
Hi Rui,
I did a bit more research on the notifications. I would like to get it setup using IFTTT (and their mobile app). The enhanced notifications seem great as you can make them work as a link and take you directly to your charts web page. Also, you can add pictures so make icons for things like temperature and humidity. I’m guessing it won’t be too far off the IFTTT Webhooks used to publish data to Google Sheets.
Hello Vincent, thank you so much for your suggestions. I appreciate your time letting me know what you would like to see implemented.
There are two easy solutions for notifications:
- Create a PHP script (also hosted in your server) to send an email. That way you don’t have to rely on another server/integration or 3rd party service
- Use a 3rd party service to send SMS notifications, Emails, push notifications, etc (like IFTTT). I like IFTTT and I already have tutorials on that subject
I’ll probably go ahead and create a guide that shows how to send emails using PHP. Basically the ESP will publish readings to your database, if a certain threshold is reached your PHP script sends an email.
I agree with you. The DHT has been proven to be unreliable. In fact, I’ve experienced your exact problem lately (sometimes it stops working).
I’ve been mostly using BME280 (learn why: DHT11 vs DHT22 vs LM35 vs DS18B20 vs BME280 vs BMP180)
- You can read my DHT troubleshooting guide: [SOLVED] DHT11/DHT22 – Failed to read from DHT sensor: https://randomnerdtutorials.com/solved-dht11-dht22-failed-to-read-from-dht-sensor/
In order to read a 12V signal with the ESP32, you should use a transistor. Basically when your pump is on, it will activate your transistor (it will be HIGH or LOW) and you can read that value with your ESP32.
I don’t have any tutorials on that subject…
Hi Rui,
Thanks again for your response!
A php script for email notifications would be interesting. I have the IFTTT webhook push notification running now and I love it! I included the IFTTTwebhook.h library and just need a snippet of code like this:
// start humidity alarm and notification
if (h < 65) humidWarning = true;
if ((h > 65) && (humidWarning == true)) {
IFTTTWebhook wh(IFTTT_API_KEY, IFTTT_EVENT);
wh.trigger();
postMySQL();
humidWarning = false;
}
// end humidity alarm and notificaiton
This will send the notification and also make sure that data point gets posted to my database and charts are updated. Otherwise I have the data getting posted to mysql every 10 minutes. Works great!
I am occasionally getting an error code of -1 on the http request. Any ideas on that. It seems that once it happens, it keeps happening, so I modified the part of your code so that if httprequestcode is < 0, it also triggers ESP.restart(). For whatever reason, when it restarts, it seems to work again for a long time but eventually will give another -1 and restart again. Any ideas?
I’ll look into the transistor option for monitoring pump activation and run time.