首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Cytron esp8266 Wifi Shield将数据无线连接并传输到xampp本地主机

Cytron esp8266 Wifi Shield将数据无线连接并传输到xampp本地主机
EN

Stack Overflow用户
提问于 2019-03-04 01:26:54
回答 2查看 250关注 0票数 0

我有一个关于自动登机门的项目。我用的是赛创ESP8266无线防护盾。我使用它将超声波数据传输和存储到我的xampp localhost端口80。

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

下面是我的代码:

代码语言:javascript
复制
#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。

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

我被困在这里了。任何帮助都是非常感谢的。

EN

回答 2

Stack Overflow用户

发布于 2019-03-05 00:39:10

您没有声明一个(全局)变量服务器,比如

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

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

票数 0
EN

Stack Overflow用户

发布于 2019-03-14 11:57:47

但我做了其他事。我使我的wifiShield本身成为一个服务器。我可以从那里控制led,并从那里监控数据。不幸的是,没有数据存储,但只要我能从远处监控它,我很高兴笑

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54971640

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档