Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Ethernet.h>
- #include <SPI.h>
- byte mac[] = {
- 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x03 };
- byte ip[] = { 192, 168, 254, 101 };
- byte gw[] = {192,168,254,254};
- byte subnet[] = { 255, 255, 255, 0 };
- // Enter the IP address for Arduino, as mentioned we will use 192.168.0.16
- // Be careful to use , insetead of . when you enter the address here
- byte server[] = { 192, 168, 254, 100 };
- int sensorPin = A0;
- int sensorValue = 0;
- int percent = 0;
- //IPAddress server(192,168,1,36);
- //char server[] = "192.168.1.36";
- // internet access via router
- //byte subnet[] = { 255, 255, 255, 0 };
- // Initialize the Ethernet server library
- EthernetClient client;
- void setup() {
- // Serial.begin starts the serial connection between computer and Arduino
- Serial.begin(9600);
- // start the Ethernet connection
- Ethernet.begin(mac);
- Serial.print("IP Address : ");
- Serial.println(Ethernet.localIP());
- Serial.print("Subnet Mask : ");
- Serial.println(Ethernet.subnetMask());
- Serial.print("Default Gateway IP: ");
- Serial.println(Ethernet.gatewayIP());
- Serial.print("DNS Server IP : ");
- Serial.println(Ethernet.dnsServerIP());
- }
- void loop() {
- sensorValue = analogRead(sensorPin);
- percent = convertToPercent(sensorValue);
- // Fill the sensorReading with the information from sensor
- // Connect to the server (your computer or web page)
- if (client.connect(server, 80)) {
- Serial.println("connecting...");
- client.print("GET /add.php?"); // This
- client.print("moisture=");
- 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
- client.println(" HTTP/1.1"); // Part of the GET request
- 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")
- client.println("Connection: close"); // Part of the GET request telling the server that we are over transmitting the message
- // client.println(); // Empty line
- // client.println(); // Empty line
- client.stop(); // Closing connection to server
- }
- else {
- // If Arduino can't connect to the server (your computer or web page)
- Serial.println("--> connection failedn");
- }
- printValuesToSerial();
- // 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.
- delay(10000);
- }
- int convertToPercent(int value)
- {
- int percentValue = 0;
- percentValue = map(value, 1023, 465, 0, 100);
- return percentValue;
- }
- void printValuesToSerial()
- {
- Serial.print("nnAnalog Value: ");
- Serial.print(sensorValue);
- Serial.print("nPercent: ");
- Serial.print(percent);
- Serial.print("%");
- }
- <!DOCTYPE html>
- <html>
- <?php
- // GET request
- if (isset($_GET['moisture'])){
- $data1 = $_GET['moisture'];
- }
- else{
- echo "Data not received";
- }
- // Initialize connection to MySQL database
- $servername = "192.168.254.100";
- $username = "arduino";
- $password = "arduinotest";
- $dbname = "test";
- // Create connection
- $conn = mysqli_connect($servername, $username, $password, $dbname);
- // Check connection
- if (!$conn) {
- die("Connection failed: " . mysqli_connect_error());
- }
- echo "Connected successfully <br>";
- //Insert into database
- $data1_sql = "INSERT INTO temperature (moisture) VALUES ($data1);";
- $data1_query = mysqli_query($conn, $data1_sql);
- //$device_list = array();
- mysqli_close($conn);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement