You can send SMTP (email) notifications using an Arduino Uno with WiFi and a web server with PHP using the mail() function in PHP. You POST data to the web server from the Arduino and then the server can parse the POST data using PHP.
Note: The mail() function uses the email server configured on the web server. Shared hosting plans should have this setup by default. If you plan to pin up your own server you’ll need to install and configure an email server in addition to Apache and PHP.
Functional Parts in the Project:
- Arduino WiFi Rev 2 – https://store.arduino.cc/usa/arduino-uno-wifi-rev2
- 560 Piece Jumper Wire Kit – https://amzn.to/2MsCLjL
- Analog Temperature Sensor – https://amzn.to/2Rkkl3k
#include <WiFiNINA.h>
#define sensorPin A5
char ssid[] = "test";
char pass[] = "";
int status = WL_IDLE_STATUS;
char server[] = "www.failednormal.com";
String postData;
String postVariable = "temp=";
WiFiClient client;
void setup() {
Serial.begin(9600);
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
delay(10000);
}
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
}
void loop() {
int reading = analogRead(sensorPin);
float voltage = reading * 5.0;
voltage /= 1024.0;
float temperatureC = (voltage - 0.5) * 100 ;
float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
postData = postVariable + temperatureF;
if (temperatureF >= 80){
if (client.connect(server, 80)) {
client.println("POST /test/emailScript.php HTTP/1.1");
client.println("Host: www.failednormal.com");
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(postData.length());
client.println();
client.print(postData);
Serial.println("Email Sent");
Serial.println(postData);
}
if (client.connected()) {
client.stop();
}
delay(60000);
}
Serial.println(postData);
delay(3000);
}
emailScript.php
<?php
$time = time();
$timestamp = date("H:i:s", $time);
$tempF = $_POST["temp"];
$to = "test@geekvid.com";
$subject = "Temperature ALERT";
$message = "At: ".$timestamp." the Temperature reached ".$tempF;
$headers = "From:AlertSystem@ETCG.com";
mail($to, $subject, $message, $headers);
print "Script Ran $time";
?>
I watched this video and acquired all the components and the shared hosting website. How do I put this script on the website?
you just upload it to your public HTML folder
I did that. I put the new URL in my browser and the little test script looks right on the page. It gives me the weird timestamp and says the script ran, just like in the video. But I never receive an email, and an error log is generated that says there is an index error:
[19-Jul-2020 02:03:45 America/New_York] PHP Notice: Undefined index: temp in /home/capscryk/public_html/emailScript.php on line 6
Line 6 is where the post command is in the php script. Very confusing.
you can create an html form and try to post to the script that way. if that works then you know the script works.
https://www.elithecomputerguy.com/2020/02/html-intro-forms/
client.println(“POST /test/emailScript.php HTTP/1.1”);
Does this line here mean that I need to have a folder called “test” in my public HTML folder, with the script in there?
This arduino sketch will say “email sent” in the serial monitor whether the email actually gets sent, or not right? I don’t see any feedback mechanism from the PHP script.