gpt4 book ai didi

php - 尝试将以太网盾连接到本地 apache 服务器

转载 作者:行者123 更新时间:2023-11-29 20:32:36 27 4
gpt4 key购买 nike

所以在很大程度上,我最近购买的以太网扩展板非常幸运。我现在正在尝试将模拟数据从 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);
}

最佳答案

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

关于php - 尝试将以太网盾连接到本地 apache 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38963600/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com