Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //-----------------------------------------CONSTANTS-------------------------------------------
- //include push notification file to send a notification if necessary
- include 'push-notification.php';
- // Prepare variables for database connection
- $dbusername = "test-minme"; // enter database username, I used "arduino" in step 2.2
- $dbpassword = "Minlab2016!"; // enter database password, I used "arduinotest" in step 2.2
- $server = "mysql.cae.wisc.edu"; // IMPORTANT: if you are using XAMPP enter "localhost", but if you have an online website enter its address, ie."www.yourwebsite.com"
- //temperature and humidity limits that cause push notifications
- $tempUpLimit = 24.5;
- $tempLowLimit = 21.5;
- $humidUpLimit = 70.0;
- $humidLowLimit = 0.0;
- //minimum number of minutes between push notifications if out of range
- //seconds is needed for when times are subtracted
- $minPushMinutes = 30;
- $minPushSeconds = $minPushMinutes*60;
- //--------------------------------------------------------------------------------------------
- //current temperature and humidity in string form -- will use if push notification is sent out
- $tempString = $_GET["temperature"];
- $humidString = $_GET["humidity"];
- //removes the unit on the end of the temperature and humidity and converts to a float
- $temperature = floatval(substr($tempString, 0, -1));
- $humidity = floatval(substr($humidString, 0, -1));
- // Connect to your database
- $dbconnect = mysql_pconnect($server, $dbusername, $dbpassword);
- $dbselect = mysql_select_db($dbArduino,$dbconnect);
- $sql = "INSERT INTO Arduinodata.sensor(temperature, humidity) VALUES ('".$_GET["temperature"]."', '".$_GET["humidity"]."')";
- //Execute SQL statement to put new data point in database then close the connection
- mysql_query($sql);
- //variables out of range and last notification was longer ago than threshold
- if (shouldSendNotification() == 1) {
- //message to send out with the push notification
- $message = '';
- //custom message for all possibilities
- if ($temperature >= $tempUpLimit) {
- $message = 'Temperature is too high! The current temperature is ' + $tempString;
- } else if ($temperature <= $tempLowLimit) {
- $message = 'Temperature is too low! The current temperature is ' + $tempString;
- } else if ($humidity >= $humidUpLimit) {
- $message = 'Humidity is too high! The current humidity is ' + $humidString;
- } else if ($humidity <= $humidLowLimit) {
- $message = 'Humidity is too low! The current humidity is ' + $humidString;
- }
- //construct the message to be sent based on which parameter is out of order
- sendPushNotification($message);
- }
- //push notification should not be sent
- else {
- //don't need to do anything here for now
- }
- //close the database connection
- mysql_close($dbconnect);
- //-----------------------------------------FUNCTIONS-------------------------------------------
- /**
- * Uses thresholds and time to check whether or not to send out a push notification.
- *
- * @return 1 if notification should be sent. -1 otherwise.
- */
- function shouldSendPushNotification() {
- //formatted this way so it is easier to add new variables. You can make it one if statement, but it will
- //get very long as more parameters are added
- //last notification sent out was longer ago than the threshold. (specified in constants above)
- if (compareTime() == 1) {
- //variables are out of range -- return 1
- if ($temperature >= $tempUpLimit OR $temperature <= $tempLowLimit) {
- return 1;
- }
- else if ($humidity >= $humidUpLimit OR $humidity <= $humidLowLimit) {
- return 1;
- }
- //ADD MORE PARAMETERS HERE AS NEEDED WITH 'ELSE IF' STATEMENTS
- //none of the variables were out of range
- //don't send push notification -- return -1
- else {
- return -1;
- }
- }
- }
- /**
- * Compares the current time and the time a push notification was last sent.
- *
- * @return 1 if a push notification should be sent and returns -1 if a push notification
- * should not be sent.
- */
- function compareTime() {
- //get the date/time the last push notification was sent from the database
- $dbselect = mysql_select_db($dbName, $dbconnect);
- //mySQL query to get the date of last push notification
- //gets all data (only 1 entry) from the last push date table
- $result = mysql_query("SELECT * FROM PushNotificationData.LastPushData");
- //make sure the time zone is correct
- date_default_timezone_set('America/Chicago');
- //get the current date and time
- $dateString = date('Y-m-d H:i:s');
- //check if the table is empty -- if empty just insert immediately and return 1 (send notification)
- if (mysql_num_rows($result) == 0) {
- //update the date/time of the last push notification sent to the server database
- //id will always be 1 so the date will just get overwritten each time
- $sql = "INSERT INTO PushNotificationData.LastPushData (id, date) VALUES (1, '$dateString')";
- $insertResult = mysql_query($sql);
- if ($insertResult == TRUE) {
- echo "initial new date added successfully";
- }
- //insert failed try one more time
- else {
- echo "Error when adding new date. Trying again.";
- //echo mysql_error($dbconnect);
- mysql_query($sql);
- }
- //no date in system -- send out notification
- return 1;
- }
- //there was a date in a system need to check if it was more than 30 min ago.
- else {
- $date = strtotime($dateString);
- //gets the date string from the query result
- $prevDateString = mysql_fetch_assoc($result)['date'];
- //converts the date string into a time in seconds
- $prevDate = strtotime($prevDateString);
- //check if the minutes apart is larger than the minimum limit
- if (($date-$minPushSeconds) >= $prevDate) {
- //update the date/time of the last push notification sent to the server database
- //id will always be 1 so the date will just get overwritten each time
- $sql = "UPDATE PushNotificationData.LastPushData SET date='$dateString' WHERE id=1";
- //$sql = "INSERT INTO PushNotificationData.LastPushData (id, date) VALUES (1, '$dateString') ON DUPLICATE id UPDATE date='$dateString'";
- $insertResult = mysql_query($sql);
- if ($insertResult == TRUE) {
- echo "updated new date added successfully";
- }
- //insert failed try one more time
- else {
- echo "Error when adding new date. Trying again. ";
- echo msql_error($dbconnect);
- mysql_query($sql);
- }
- //return 1 to send a push notification
- return 1;
- }
- //not enough time has passed to send another notification -- return -1
- else {
- return -1;
- }
- }
- }
- /**
- * Sends a push notification out to all devices with the given message
- *
- * @param message the message to send with the notification
- */
- function sendPushNotification($message) {
- $push = new PushNotification();
- //get the array of device tokens with a server request
- $dbselect = mysql_select_db($dbPushNotification,$dbconnect);
- //get the tokens from its database
- $token_query = mysql_query("SELECT token FROM PushNotificationData.Tokens");
- //convert the data from the table into an array of tokens
- $tokens = array();
- //loop through all rows of tokens and make an array
- while($row = mysql_fetch_assoc($token_query)) {
- // Add each token into an array
- array_push($tokens, $row['token']);
- }
- //data for the push notification -- array of device tokens and the message
- $data = array('deviceToken' => $tokens,'message' => $message);
- //send out the push notification
- $push->Notification($data);
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement