【问题标题】:Trying to connect ethernet shield to local apache server尝试将以太网屏蔽连接到本地 apache 服务器
【发布时间】:2016-08-15 21:50:39
【问题描述】:

因此,在大多数情况下,我最近购买的以太网防护罩真的很幸运。我现在正在尝试将模拟数据从 arduino 上传到本地 mysql 数据库。我的 write_data.php 文件似乎运行良好,每当我在 url 中调用 write_data.php 文件时,我都可以将数据上传到数据库。虽然 arduino 总是无法连接。我正在使用 netgear 路由器,我检查了 netgear genie 网络管理员上的允许设备列表,并且列出了 arduino,这是有道理的,因为它适用于我的所有其他项目。真的会在这里获得一些建议和想法。另外,不确定这是否会有所作为,但我使用 mamp 作为我的本地服务器环境。审查文件如下:

write_data.php:

<?php

    // Prepare variables for database connection

    $dbusername = "test";  // enter database username, I used "arduino"    in step 2.2
    $dbpassword = "test";  // enter database password, I used   "arduinotest" in step 2.2
    $server = "50.135.xxx.xxx"; // IMPORTANT: if you are using XAMPP enter "localhost", but if you have an online website enter its address, ie."www.yourwebsite.com"

    // Connect to your database

    $dbconnect = mysql_pconnect($server, $dbusername, $dbpassword);
    $dbselect = mysql_select_db("Test",$dbconnect);

    // Prepare the SQL statement

    $sql = "INSERT INTO Test.Sensor (value) VALUES   ('".$_GET["value"]."')";    

    // Execute SQL statement

    mysql_query($sql);

?>

arduino 草图:

#include SPI.h

#include Ethernet.h

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// 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

IPAddress ip(192,xxx,xx,xx);

int photocellPin = 2;  // Analog input pin on Arduino we connected the    SIG pin from sensor
int photocellReading;  // Here we will place our reading

char server[] = "50.135.xxx.xxx"; // 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. "www.yourwebpage.com")

// 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 ,ip);

}



void loop() {

  photocellReading = analogRead(photocellPin); // Fill the    sensorReading with the information from sensor

  // Connect to the server (your computer or web page)  
  if (client.connect(server,8888)) {
    client.print("GET /write_data.php?"); // This
    client.print("value="); // This
    client.print(photocellReading); // 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: 50.135.xxx.xxx"); // 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 failed\n");
  }

  // 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);
}

【问题讨论】:

标签: php mysql apache arduino ethernet


【解决方案1】:

几周前遇到了同样的问题。 您“最近”购买了以太网屏蔽,因此它可能是以太网 2 屏蔽而不是以太网屏蔽。 您应该包含“Ethernet2.h”而不是“Ethernet.h”,后者仅在 Arduino.org 的 IDE 中,而不在 Arduino.cc 中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多