Advertisement
Guest User

Untitled

a guest
May 24th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.42 KB | None | 0 0
  1. <?php
  2. //-----------------------------------------CONSTANTS-------------------------------------------
  3. //include push notification file to send a notification if necessary
  4. include 'push-notification.php';
  5.  
  6. // Prepare variables for database connection
  7. $dbusername = "test-minme"; // enter database username, I used "arduino" in step 2.2
  8. $dbpassword = "Minlab2016!"; // enter database password, I used "arduinotest" in step 2.2
  9. $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"
  10.  
  11. //temperature and humidity limits that cause push notifications
  12. $tempUpLimit = 24.5;
  13. $tempLowLimit = 21.5;
  14. $humidUpLimit = 70.0;
  15. $humidLowLimit = 0.0;
  16.  
  17. //minimum number of minutes between push notifications if out of range
  18. //seconds is needed for when times are subtracted
  19. $minPushMinutes = 30;
  20. $minPushSeconds = $minPushMinutes*60;
  21. //--------------------------------------------------------------------------------------------
  22.  
  23.  
  24. //current temperature and humidity in string form -- will use if push notification is sent out
  25. $tempString = $_GET["temperature"];
  26. $humidString = $_GET["humidity"];
  27.  
  28. //removes the unit on the end of the temperature and humidity and converts to a float
  29. $temperature = floatval(substr($tempString, 0, -1));
  30. $humidity = floatval(substr($humidString, 0, -1));
  31.  
  32. // Connect to your database
  33. $dbconnect = mysql_pconnect($server, $dbusername, $dbpassword);
  34. $dbselect = mysql_select_db($dbArduino,$dbconnect);
  35.  
  36. $sql = "INSERT INTO Arduinodata.sensor(temperature, humidity) VALUES ('".$_GET["temperature"]."', '".$_GET["humidity"]."')";
  37.  
  38. //Execute SQL statement to put new data point in database then close the connection
  39. mysql_query($sql);
  40.  
  41. //variables out of range and last notification was longer ago than threshold
  42. if (shouldSendNotification() == 1) {
  43. //message to send out with the push notification
  44. $message = '';
  45.  
  46. //custom message for all possibilities
  47. if ($temperature >= $tempUpLimit) {
  48. $message = 'Temperature is too high! The current temperature is ' + $tempString;
  49. } else if ($temperature <= $tempLowLimit) {
  50. $message = 'Temperature is too low! The current temperature is ' + $tempString;
  51. } else if ($humidity >= $humidUpLimit) {
  52. $message = 'Humidity is too high! The current humidity is ' + $humidString;
  53. } else if ($humidity <= $humidLowLimit) {
  54. $message = 'Humidity is too low! The current humidity is ' + $humidString;
  55. }
  56.  
  57. //construct the message to be sent based on which parameter is out of order
  58. sendPushNotification($message);
  59. }
  60. //push notification should not be sent
  61. else {
  62. //don't need to do anything here for now
  63. }
  64.  
  65. //close the database connection
  66. mysql_close($dbconnect);
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73. //-----------------------------------------FUNCTIONS-------------------------------------------
  74.  
  75. /**
  76. * Uses thresholds and time to check whether or not to send out a push notification.
  77. *
  78. * @return 1 if notification should be sent. -1 otherwise.
  79. */
  80. function shouldSendPushNotification() {
  81. //formatted this way so it is easier to add new variables. You can make it one if statement, but it will
  82. //get very long as more parameters are added
  83.  
  84. //last notification sent out was longer ago than the threshold. (specified in constants above)
  85. if (compareTime() == 1) {
  86. //variables are out of range -- return 1
  87. if ($temperature >= $tempUpLimit OR $temperature <= $tempLowLimit) {
  88. return 1;
  89. }
  90. else if ($humidity >= $humidUpLimit OR $humidity <= $humidLowLimit) {
  91. return 1;
  92. }
  93.  
  94.  
  95.  
  96. //ADD MORE PARAMETERS HERE AS NEEDED WITH 'ELSE IF' STATEMENTS
  97.  
  98.  
  99.  
  100. //none of the variables were out of range
  101. //don't send push notification -- return -1
  102. else {
  103. return -1;
  104. }
  105. }
  106.  
  107. }
  108.  
  109.  
  110. /**
  111. * Compares the current time and the time a push notification was last sent.
  112. *
  113. * @return 1 if a push notification should be sent and returns -1 if a push notification
  114. * should not be sent.
  115. */
  116. function compareTime() {
  117. //get the date/time the last push notification was sent from the database
  118. $dbselect = mysql_select_db($dbName, $dbconnect);
  119.  
  120. //mySQL query to get the date of last push notification
  121. //gets all data (only 1 entry) from the last push date table
  122. $result = mysql_query("SELECT * FROM PushNotificationData.LastPushData");
  123.  
  124. //make sure the time zone is correct
  125. date_default_timezone_set('America/Chicago');
  126. //get the current date and time
  127. $dateString = date('Y-m-d H:i:s');
  128.  
  129.  
  130. //check if the table is empty -- if empty just insert immediately and return 1 (send notification)
  131. if (mysql_num_rows($result) == 0) {
  132. //update the date/time of the last push notification sent to the server database
  133. //id will always be 1 so the date will just get overwritten each time
  134. $sql = "INSERT INTO PushNotificationData.LastPushData (id, date) VALUES (1, '$dateString')";
  135. $insertResult = mysql_query($sql);
  136.  
  137. if ($insertResult == TRUE) {
  138. echo "initial new date added successfully";
  139. }
  140. //insert failed try one more time
  141. else {
  142. echo "Error when adding new date. Trying again.";
  143. //echo mysql_error($dbconnect);
  144. mysql_query($sql);
  145. }
  146.  
  147. //no date in system -- send out notification
  148. return 1;
  149. }
  150. //there was a date in a system need to check if it was more than 30 min ago.
  151. else {
  152. $date = strtotime($dateString);
  153.  
  154. //gets the date string from the query result
  155. $prevDateString = mysql_fetch_assoc($result)['date'];
  156. //converts the date string into a time in seconds
  157. $prevDate = strtotime($prevDateString);
  158.  
  159. //check if the minutes apart is larger than the minimum limit
  160. if (($date-$minPushSeconds) >= $prevDate) {
  161. //update the date/time of the last push notification sent to the server database
  162. //id will always be 1 so the date will just get overwritten each time
  163. $sql = "UPDATE PushNotificationData.LastPushData SET date='$dateString' WHERE id=1";
  164. //$sql = "INSERT INTO PushNotificationData.LastPushData (id, date) VALUES (1, '$dateString') ON DUPLICATE id UPDATE date='$dateString'";
  165. $insertResult = mysql_query($sql);
  166.  
  167. if ($insertResult == TRUE) {
  168. echo "updated new date added successfully";
  169. }
  170. //insert failed try one more time
  171. else {
  172. echo "Error when adding new date. Trying again. ";
  173. echo msql_error($dbconnect);
  174. mysql_query($sql);
  175. }
  176.  
  177. //return 1 to send a push notification
  178. return 1;
  179. }
  180. //not enough time has passed to send another notification -- return -1
  181. else {
  182. return -1;
  183. }
  184. }
  185.  
  186. }
  187.  
  188.  
  189. /**
  190. * Sends a push notification out to all devices with the given message
  191. *
  192. * @param message the message to send with the notification
  193. */
  194. function sendPushNotification($message) {
  195. $push = new PushNotification();
  196.  
  197. //get the array of device tokens with a server request
  198. $dbselect = mysql_select_db($dbPushNotification,$dbconnect);
  199. //get the tokens from its database
  200. $token_query = mysql_query("SELECT token FROM PushNotificationData.Tokens");
  201.  
  202. //convert the data from the table into an array of tokens
  203. $tokens = array();
  204.  
  205. //loop through all rows of tokens and make an array
  206. while($row = mysql_fetch_assoc($token_query)) {
  207. // Add each token into an array
  208. array_push($tokens, $row['token']);
  209. }
  210.  
  211. //data for the push notification -- array of device tokens and the message
  212. $data = array('deviceToken' => $tokens,'message' => $message);
  213.  
  214. //send out the push notification
  215. $push->Notification($data);
  216. }
  217.  
  218. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement