我有一个有关自动门的项目。我正在使用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。
我在Google上搜索了许多内容,但是大多数操作系统解决方案都是使用以太网屏蔽和网页,而不是本地主机。
我被困在这里。任何帮助都非常感谢。
答案 0 :(得分:0)
您未声明(全局)变量 server ,如
const char server[] = "www.adafruit.com";
(就像您在void clientTest()中所做的一样)
答案 1 :(得分:0)
但是我做了其他事情。我将wifiShield设置为服务器本身。我能够从那里控制LED并从那里监视数据。不幸的是,没有数据存储,但是只要我能从远处监视它,我就很开心