无法使用Arduino Wifi-Shiled连接到本地服务器

时间:2015-06-25 13:33:40

标签: c arduino

我正在尝试使用Arduino wifi将数据从arduino上传到本地服务器。但是代码永远不会到达线路"已连接"。我正在使用WAMP服务器。这是arduino wifi的IP地址ping盾是没关系的.WLAN盾连接到网络。代码如下:

 #include <TinkerKit.h>
   #include <WiFi.h>
   #include <SPI.h>

    char ssid[] = "Connectify-moloi"; // your network SSID (name)
    char pass[] = "1234567890"; // your network password
    int status = WL_IDLE_STATUS;

    //WiFiServer server(80);
    long previousMillis = 0;
    unsigned long currentMillis = 0;
    long interval = 250000; // READING INTERVAL
    int sensor;
    int analog_val;

    String data;
    String Hall;
    String Temp;
    WiFiClient client;
    IPAddress server(192,168,164,101);
    void setup() { 
    Serial.begin(9600);

     while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
    delay(10000);
    }

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


              Hall = "50";
              Temp = "50";

        data = "";
    }

    void loop(){

        currentMillis = millis();
        if(currentMillis - previousMillis > interval) { // READ ONLY ONCE PER INTERVAL
            previousMillis = currentMillis;
                     Hall ="50"; //String(analog_val);
                     Temp ="50"; //String(sensor);

        }

        data = "temp1=" + Hall + "&hum1=" + Temp;
           client.flush();
        if (client.connect(server,80)) { // REPLACE WITH YOUR SERVER ADDRESS
            Serial.println("Connected");
            client.println("POST project/add.php HTTP/1.1"); 
            client.println("Host: 192.168.164.101"); // SERVER ADDRESS HERE TOO
            client.println("Content-Type: application/x-www-form-urlencoded"); 
            client.print("Content-Length: "); 
            client.println(data.length()); 
            client.println(); 
            client.print(data); 
        } 

        if (client.connected()) { 
            client.stop();  // DISCONNECT FROM THE SERVER
            client.flush();
        }

        delay(7000); // WAIT FIVE MINUTES BEFORE SENDING AGAIN
    }

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

1 个答案:

答案 0 :(得分:0)

请尝试使用POST请求,而不是GET命令。

POST project/add.php更改为GET /project/add.php?