Advertisement
Guest User

Untitled

a guest
Feb 12th, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.08 KB | None | 0 0
  1. #include <DHT.h>
  2. #include <Ethernet.h>
  3. #include <SPI.h>
  4.  
  5. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // RESERVED MAC ADDRESS
  6. EthernetClient client;
  7.  
  8. IPAddress server(192,168,0,102); // IP Adres (or name) of server to dump data to
  9.  
  10. #define DHTPIN 2 // SENSOR PIN
  11. #define DHTTYPE DHT11 // SENSOR TYPE - THE ADAFRUIT LIBRARY OFFERS SUPPORT FOR MORE MODELS
  12. DHT dht(DHTPIN, DHTTYPE);
  13.  
  14. long previousMillis = 0;
  15. unsigned long currentMillis = 0;
  16. long interval = 250000; // READING INTERVAL
  17.  
  18. float temperature = 0; // TEMPERATURE VAR
  19. float humidity = 0; // HUMIDITY VAR
  20. String data;
  21.  
  22. void setup() {
  23. Serial.begin(115200);
  24.  
  25. if (Ethernet.begin(mac) == 0) {
  26. Serial.println("Failed to configure Ethernet using DHCP");
  27. }
  28.  
  29. dht.begin();
  30. delay(10000); // GIVE THE SENSOR SOME TIME TO START
  31.  
  32. temperature = (float) dht.readTemperature();
  33. humidity = (float) dht.readHumidity();
  34.  
  35. }
  36.  
  37. void loop(){
  38.  
  39. currentMillis = millis();
  40. if(currentMillis - previousMillis > interval) { // READ ONLY ONCE PER INTERVAL
  41. previousMillis = currentMillis;
  42. temperature = (float) dht.readTemperature();
  43. humidity = (float) dht.readHumidity();
  44. }
  45.  
  46.  
  47. if (client.connect(server, 3306)) {
  48. Serial.print("temperature=" );
  49. Serial.println( temperature );
  50. Serial.print("humidity=" );
  51. Serial.println( humidity );
  52. Serial.println("-> Connected");
  53. if(client.connected()){
  54. // Make a HTTP request:
  55. client.print( "GET /add_data.php?");
  56. client.print("temperature=");
  57. client.print( temperature);
  58. client.print("humidity=");
  59. client.print( humidity);
  60. client.println( " HTTP/1.1");
  61. client.println( "Host: 192.168.0.102" );
  62. //client.println(server);
  63. client.println( "Connection: close" );
  64. client.println();
  65. client.println();
  66. client.stop();
  67. }
  68. }
  69. if (client.connected()) {
  70. client.stop(); // DISCONNECT FROM THE SERVER
  71. }
  72.  
  73. delay(30000); // WAIT FIVE MINUTES BEFORE SENDING AGAIN
  74. }
  75.  
  76. <?php
  77. // Connect to MySQL
  78. include("dbconnect.php");
  79.  
  80. // Prepare the SQL statement
  81. $SQL = "INSERT INTO test.test.temperature (temperature ,humidity) VALUES ('".$_GET["temperature"]."', '".$_GET["humidity="]."')";
  82.  
  83. // Execute SQL statement
  84. mysql_query($SQL);
  85.  
  86. // Go to the review_data.php (optional)
  87. header("Location: review_data.php");
  88. ?>
  89.  
  90. <?php
  91. $MyUsername = "root"; // enter your username for mysql
  92. $MyPassword = "fifa2005"; // enter your password for mysql
  93. $MyHostname = "localhost"; // this is usually "localhost" unless your database resides on a different server
  94.  
  95. $dbh = mysql_pconnect($MyHostname , $MyUsername, $MyPassword);
  96. $selected = mysql_select_db("test",$dbh);
  97. ?>
  98.  
  99. <html>
  100. <head>
  101. <title>Arduino Temperature Log</title>
  102. <style type="text/css">
  103. .table_titles, .table_cells_odd, .table_cells_even {
  104. padding-right: 20px;
  105. padding-left: 20px;
  106. color: #000;
  107. }
  108. .table_titles {
  109. color: #FFF;
  110. background-color: #666;
  111. }
  112. .table_cells_odd {
  113. background-color: #CCC;
  114. }
  115. .table_cells_even {
  116. background-color: #FAFAFA;
  117. }
  118. table {
  119. border: 2px solid #333;
  120. }
  121. body { font-family: "Trebuchet MS", Arial; }
  122. </style>
  123. </head>
  124.  
  125. <body>
  126. <h1>Arduino Temperature Log</h1>
  127. <table border="0" cellspacing="0" cellpadding="4">
  128. <tr>
  129. <td class="table_titles">ID</td>
  130. <td class="table_titles">Date and Time</td>
  131. <td class="table_titles">Temperature</td>
  132. <td class="table_titles">Humidity</td>
  133. </tr>
  134. <?php
  135. // Retrieve all records and display them
  136. $result = mysql_query("SELECT * FROM temperature ORDER BY id ASC");
  137.  
  138. // Used for row color toggle
  139. $oddrow = true;
  140.  
  141. // process every record
  142. while( $row = mysql_fetch_array($result) )
  143. {
  144. if ($oddrow)
  145. {
  146. $css_class=' class="table_cells_odd"';
  147. }
  148. else
  149. {
  150. $css_class=' class="table_cells_even"';
  151. }
  152.  
  153. $oddrow = !$oddrow;
  154.  
  155. echo '<tr>';
  156. echo ' <td'.$css_class.'>'.$row["id"].'</td>';
  157. echo ' <td'.$css_class.'>'.$row["timestamp"].'</td>';
  158. echo ' <td'.$css_class.'>'.$row["temperature"].'</td>';
  159. echo ' <td'.$css_class.'>'.$row["humidity"].'</td>';
  160. echo '</tr>';
  161. }
  162. ?>
  163. </table>
  164. </body>
  165. </html>
  166.  
  167. // Make a HTTP request:
  168. client.print( "GET /add_data.php?");
  169. client.print("temperature=");
  170. client.print( temperature);
  171. client.print("humidity=");
  172. client.print( humidity);
  173.  
  174. // Make a HTTP request:
  175. client.print( "GET /add_data.php?");
  176. client.print("temperature=");
  177. client.print( temperature);
  178. client.print("&humidity=");
  179. client.print( humidity);
  180.  
  181. #include <dht.h>
  182. #include <SPI.h>
  183. #include <Ethernet.h>
  184. EthernetClient client;
  185. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  186. char server[] = "192.168.0.101";
  187. dht DHT;
  188. int interval = 5000; // Wait between dumps
  189. #define DHT11_PIN 2
  190. void setup()
  191. {
  192. Ethernet.begin(mac);
  193. Serial.begin(115200);
  194.  
  195. //Serial.println("Humidity (%),tTemperature (C)");
  196. }
  197. void loop()
  198. {
  199. Serial.print("Temperature2 = ");
  200. Serial.print(DHT.temperature);
  201. Serial.println(" *C");
  202.  
  203. Serial.print("Humidity = ");
  204. Serial.print(DHT.humidity);
  205. Serial.println(" ");
  206.  
  207. // READ DATA
  208. //Serial.print("DHT11, t");
  209. int chk = DHT.read11(DHT11_PIN);
  210.  
  211. if (client.connect(server, 80)) {
  212.  
  213. client.print( "GET /diplomna/Arduino/add_data.php?");
  214. client.print("temperature=");;
  215. client.print( DHT.temperature );
  216. client.print("&&");
  217. client.print("humidity=");
  218. client.println( DHT.humidity );
  219. client.println( "HTTP/1.1");
  220. client.println( "Connection: close" );
  221. client.stop();
  222. Serial.println("CONNECTED");
  223. }
  224. else {
  225. // you didn’t get a connection to the server:
  226. Serial.println("–> connection failed/n");
  227. }
  228. delay(interval);
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement