首页 > 解决方案 > Cytron esp8266 Wifi Shield 无线连接并传输数据到 xampp localhost

问题描述

我有一个关于自动门的项目。我正在使用 Cytron ESP8266 Wifi Shield。我正在使用它将超声波数据传输并存储到我的 xampp localhost 端口 80。

但是我在客户端的代码中有一个错误。

这是我的代码:

#include <CytronWiFiShield.h>
#include <CytronWiFiClient.h>
#include <CytronWiFiServer.h>
#include <SoftwareSerial.h>

const int trigPin = 5;
const int echoPin = 4;
long duration;
int distance;

ESP8266Client client;

const char *ssid = "HRHS";
const char *pass = "06031960";
IPAddress ip(192, 168, 100, 9); //The IP address i got from cmd
ESP8266Server server(80);

const char htmlHeader[] = "HTTP/1.1 200 OK\r\n"
                          "Content-Type: text/html\r\n"
                          "Connection: close\r\n\r\n"
                          "<!DOCTYPE HTML>\r\n"
                          "<html>\r\n";

void setup()
{

    // put your setup code here, to run once:
    Serial.begin(9600);
    while (!Serial)
    {
        ; // wait for serial port to connect. Needed for Leonardo only
    }

    if (!wifi.begin(2, 3))
    {
        Serial.println(F("Error talking to shield"));
        while (1)
            ;
    }
    Serial.println(wifi.firmwareVersion());
    Serial.print(F("Mode: "));
    Serial.println(wifi.getMode()); // 1- station mode, 2- softap mode, 3- both

    Serial.println(F("Start wifi connection"));
    if (!wifi.connectAP(ssid, pass))
    {
        Serial.println(F("Error connecting to WiFi"));
        while (1)
            ;
    }
    Serial.print(F("Connected to "));
    Serial.println(wifi.SSID());
    Serial.println(F("IP address: "));
    Serial.println(wifi.localIP());
    wifi.updateStatus();
    Serial.println(wifi.status()); //2- wifi connected with ip, 3- got connection with servers or clients, 4- disconnect with clients or servers, 5- no wifi
    //clientTest();
    espblink(100);
    server.begin();
}

void loop()
{
    //Start of Program

    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    // Sets the trigPin on HIGH state for 10 micro seconds
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    // Reads the echoPin, returns the sound wave travel time in microseconds
    duration = pulseIn(echoPin, HIGH);
    // Calculating the distance
    distance = duration * 0.034 / 2;
    // Prints the distance on the Serial Monitor
    delay(1000);

    // Connect to the server (your computer or web page)
    if (client.connect("192.168.100.9", 80)) //Same local ip address from cmd
    {
        client.print("GET /write_data.php?"); // This
        client.print("value=");               // This
        client.print(distance);               // 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: ");             // 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 receive 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);
}



void espblink(int time)
{
    for (int i = 0; i < 12; i++)
    {
        wifi.digitalWrite(2, wifi.digitalRead(2) ^ 1);
        delay(time);
    }
}

}

你好,再次抱歉。我要更新这里的进度可以吗?

我已经更新了代码。如果我能做到这一点,请纠正我

好的,盾已经连接到wifi了。但是,仍然无法连接到数据库 xampp localhost。

我在谷歌上搜索了很多,但大多数操作系统的解决方案是使用以太网屏蔽和网页,而不是本地主机。

我被困在这里。非常感谢任何帮助。

标签: arduino

解决方案


您没有声明(全局)变量server,例如

const char server[] = "www.adafruit.com";

(就像您在 void clientTest() 中所做的那样)


推荐阅读