dqo58772 2013-11-07 18:21
浏览 117
已采纳

通过以太网发布arduino数据以显示到php

I have difficulty to post arduino data into php via ethernet shield. I have data from arduino (ambient temperature, devices and so on) and want to store it into mysql databases and display on php web page. So I found someone do it on wireless from github: https://github.com/ericbenwa/POST-Arduino-Data-Wireless/blob/master/arduino_post/arduino_post.ino

So I decided to modify some code from it and do it on my localhost server. but when I run on arduino , it doesn't connect and failed connection. Please Help!

//#include //#include

// 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();
  }
}
  • 写回答

1条回答 默认 最新

  • dpjpo746884 2013-11-07 19:03
    关注

    The program you show here runs on your device, correct?

    The host ip number 127.0.0.1 is a special IP address. It always means "the machine upon which the present program is running." Accordingly, your arduino code is trying to connect to a web server on the same arduino device, not your server.

    You need to use the network IP address of the server machine to which you're connecting. Update this line.

     char server[] = "127.0.0.1"
    

    I would tell you what to put here, but I have no idea. You have to find out the IP address of your server.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝