Advertisement
Guest User

Untitled

a guest
Sep 27th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. #include <Ethernet.h>
  2. #include <SPI.h>
  3.  
  4.  
  5. byte mac[] = {
  6. 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x03 };
  7. byte ip[] = { 192, 168, 254, 101 };
  8. byte gw[] = {192,168,254,254};
  9. byte subnet[] = { 255, 255, 255, 0 };
  10. // Enter the IP address for Arduino, as mentioned we will use 192.168.0.16
  11. // Be careful to use , insetead of . when you enter the address here
  12.  
  13. byte server[] = { 192, 168, 254, 100 };
  14. int sensorPin = A0;
  15. int sensorValue = 0;
  16. int percent = 0;
  17.  
  18. //IPAddress server(192,168,1,36);
  19. //char server[] = "192.168.1.36";
  20. // internet access via router
  21. //byte subnet[] = { 255, 255, 255, 0 };
  22. // Initialize the Ethernet server library
  23. EthernetClient client;
  24.  
  25. void setup() {
  26.  
  27. // Serial.begin starts the serial connection between computer and Arduino
  28. Serial.begin(9600);
  29.  
  30. // start the Ethernet connection
  31. Ethernet.begin(mac);
  32.  
  33. Serial.print("IP Address : ");
  34. Serial.println(Ethernet.localIP());
  35. Serial.print("Subnet Mask : ");
  36. Serial.println(Ethernet.subnetMask());
  37. Serial.print("Default Gateway IP: ");
  38. Serial.println(Ethernet.gatewayIP());
  39. Serial.print("DNS Server IP : ");
  40. Serial.println(Ethernet.dnsServerIP());
  41.  
  42. }
  43.  
  44. void loop() {
  45.  
  46. sensorValue = analogRead(sensorPin);
  47. percent = convertToPercent(sensorValue);
  48. // Fill the sensorReading with the information from sensor
  49.  
  50. // Connect to the server (your computer or web page)
  51. if (client.connect(server, 80)) {
  52. Serial.println("connecting...");
  53. client.print("GET /add.php?"); // This
  54. client.print("moisture=");
  55. client.print(sensorValue); // And this is what we did in the testing section above. We are making a GET request just like we would from our browser but now with live data from the sensor
  56. client.println(" HTTP/1.1"); // Part of the GET request
  57. client.println("Host: 192.168.254.100"); // IMPORTANT: If you are using XAMPP you will have to find out the IP address of your computer and put it here (it is explained in previous article). If you have a web page, enter its address (ie.Host: "www.yourwebpage.com")
  58. client.println("Connection: close"); // Part of the GET request telling the server that we are over transmitting the message
  59. // client.println(); // Empty line
  60. // client.println(); // Empty line
  61. client.stop(); // Closing connection to server
  62.  
  63. }
  64.  
  65. else {
  66. // If Arduino can't connect to the server (your computer or web page)
  67. Serial.println("--> connection failedn");
  68. }
  69.  
  70. printValuesToSerial();
  71. // Give the server some time to recieve the data and store it. I used 10 seconds here. Be advised when delaying. If u use a short delay, the server might not capture data because of Arduino transmitting new data too soon.
  72. delay(10000);
  73. }
  74.  
  75. int convertToPercent(int value)
  76. {
  77. int percentValue = 0;
  78. percentValue = map(value, 1023, 465, 0, 100);
  79. return percentValue;
  80. }
  81. void printValuesToSerial()
  82. {
  83. Serial.print("nnAnalog Value: ");
  84. Serial.print(sensorValue);
  85. Serial.print("nPercent: ");
  86. Serial.print(percent);
  87. Serial.print("%");
  88. }
  89.  
  90. <!DOCTYPE html>
  91. <html>
  92.  
  93. <?php
  94.  
  95. // GET request
  96. if (isset($_GET['moisture'])){
  97. $data1 = $_GET['moisture'];
  98. }
  99. else{
  100. echo "Data not received";
  101. }
  102.  
  103. // Initialize connection to MySQL database
  104. $servername = "192.168.254.100";
  105. $username = "arduino";
  106. $password = "arduinotest";
  107. $dbname = "test";
  108.  
  109. // Create connection
  110. $conn = mysqli_connect($servername, $username, $password, $dbname);
  111. // Check connection
  112. if (!$conn) {
  113. die("Connection failed: " . mysqli_connect_error());
  114. }
  115. echo "Connected successfully <br>";
  116.  
  117.  
  118. //Insert into database
  119. $data1_sql = "INSERT INTO temperature (moisture) VALUES ($data1);";
  120. $data1_query = mysqli_query($conn, $data1_sql);
  121. //$device_list = array();
  122.  
  123. mysqli_close($conn);
  124. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement