【问题标题】:post arduino data via ethernet to display into php通过以太网发布 arduino 数据以显示到 php
【发布时间】:2013-11-19 13:18:50
【问题描述】:

我很难通过以太网屏蔽将 arduino 数据发布到 php 中。我有来自 arduino 的数据(环境温度、设备等),并希望将其存储到 mysql 数据库中并显示在 php 网页上。所以我发现有人在 github 上通过无线方式进行操作:https://github.com/ericbenwa/POST-Arduino-Data-Wireless/blob/master/arduino_post/arduino_post.ino

所以我决定从中修改一些代码并在我的本地主机服务器上进行。但是当我在 arduino 上运行时,它没有连接并且连接失败。请帮忙!

//#包括 //#包括

// EDIT: Change the 'ssid' and 'password' to match your network
//char ssid[] = "yournetwork";  // wireless network name
//char password[] = "yourpassword"; // wireless password
//int status = WL_IDLE_STATUS;
//WiFiClient client;
#include <Ethernet.h>
#include <SPI.h>

byte mac[] = { 0x90,0xA2,0xDA,0x0D,0x0D,0xB1};  //Replace with your Ethernet shield MAC
EthernetClient client;
IPAddress ip(198, 100, 130, 65); //ethernet ip address attatched to my ethernet shield

// EDIT: 'Server' address to match your domain
char server[] = "127.0.0.1"; // This could also be 192.168.1.18/~me if you are running a server on your computer on a local network.

// This is the data that will be passed into your POST and matches your mysql column
int yourarduinodata = 999;
String yourdatacolumn = "yourdata=";
String yourdata;

void setup()
{
Serial.begin(9600);
Ethernet.begin(mac,ip);
delay(1000);
Serial.println("connecting...");
postData();
}


// This is the data that will be passed into your POST and matches your mysql column
/*int yourarduinodata = 999;
String yourdatacolumn = "yourdata=";
String yourdata;

void setup() {
  Serial.begin(9600);

  connectWifi();

  // You're connected now, so print out the status
  printWifiStatus();

  postData();
}*/

void loop() {

}

/*void connectWifi() {
  // Attempt to connect to wifi network
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, password);
    // Wait 10 seconds for connection
    delay(10000);
  }
}*/

/*void printWifiStatus() {
  // Print the SSID of the network you're attached to
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // Print your WiFi shield's IP address
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // Print the received signal strength
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}*/

// This method makes a HTTP connection to the server and POSTs data
void postData() {
  // Combine yourdatacolumn header (yourdata=) with the data recorded from your arduino
  // (yourarduinodata) and package them into the String yourdata which is what will be
  // sent in your POST request
  yourdata = yourdatacolumn + yourarduinodata;

  // If there's a successful connection, send the HTTP POST request
  if (client.connect(server, 80)) {
    Serial.println("connecting...");

    // EDIT: The POST 'URL' to the location of your insert_mysql.php on your web-host
    client.println("POST /data/insert_mysql.php HTTP/1.1");

    // EDIT: 'Host' to match your domain
    client.println("Host:127.0.0.1 ");
    client.println("User-Agent: Arduino/1.0");
    client.println("Connection: close");
    client.println("Content-Type: application/x-www-form-urlencoded;");
    client.print("Content-Length: ");
    client.println(yourdata.length());
    client.println();
    client.println(yourdata); 
  } 
  else {
    // If you couldn't make a connection:
    Serial.println("Connection failed");
    Serial.println("Disconnecting.");
    client.stop();
  }
}

【问题讨论】:

    标签: php mysql arduino


    【解决方案1】:

    您在此处显示的程序在您的设备上运行,对吗?

    主机 ip 号 127.0.0.1 是一个特殊的 IP 地址。它总是意味着“运行当前程序的机器”。因此,您的 arduino 代码正在尝试连接到同一 arduino 设备上的 Web 服务器,而不是您的服务器。

    您需要使用您要连接的服务器计算机的网络 IP 地址。更新这一行。

     char server[] = "127.0.0.1"
    

    我会告诉你在这里放什么,但我不知道。您必须找出服务器的 IP 地址。

    【讨论】:

    • 是的,这是在 arduino 上运行的草图。我可以在本地服务器上运行吗,localhost,这就是为什么我把它放在 127.0.0.1
    • 我把服务器IP地址改成了自己的服务器地址。但是串行监视器上的结果是永远“连接..”。
    猜你喜欢
    • 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
    相关资源
    最近更新 更多