1
我試圖將我的ESP8266 12F微控制器連接到託管在hostinger.com上的遠程Apache服務器。但客戶端無法連接。 我需要做的是寫一些讀數到我的服務器上的MySQL數據庫。我已將微控制器連接到移動熱點,但無法連接到服務器。這裏是我的代碼:無法將ESP8266連接到遠程服務器
#include<ESP8266WiFi.h>
#define SS_PIN 4
#define RST_PIN 5
#define host http://smartlbus.esy.es/
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
static const int RXPin = 0, TXPin = 16;
static const uint32_t GPSBaud = 9600;
// The TinyGPS++ object
TinyGPSPlus gps;
// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // Initialize serial communications with the PC
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
WiFi.mode(WIFI_STA);
WiFi.begin("smartbus","qwerty123");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
if(WiFi.status()==WL_CONNECTED) Serial.println("Connected to wifi");
}
void loop() {
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (client.connect("http://www.smartlbus.esy.es", httpPort)) {
Serial.println("Connected to server");
while (ss.available() > 0)
if (gps.encode(ss.read()))
{
double lat,lng;
float speed;
if (gps.location.isValid())
{
lat=gps.location.lat();
lng=gps.location.lng();
speed=gps.speed.value();
}
String data = "lat1=" + (String) lat + "&lng1=" + (String)lng + "&speed=" + (String)speed;
client.println("POST /add.php HTTP/1.1");
client.println("Host: www.smartlbus.esy.es");
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(data.length());
client.println();
client.print(data);
delay(500);
}
}
else{
Serial.println("Cannot cannect to server..");
}
// Look for new cards
這裏是PHP代碼:
<?php
include 'dbConnect.php';
if($_SERVER['REQUEST_METHOD']=='POST'){
//Getting values
$lat = $_POST['lat1'];
$lng = $_POST['lng1'];
$speed = $_POST['speed'];
$id = $_POST['busid'];
//Creating an sql query
$sql = "INSERT INTO status (bus_id,latitude,longitude,speed) VALUES ('$id','$lat','$lng','$speed')";
//Executing query to database
mysqli_query($con,$sql);
//Closing the database
mysqli_close($con);
}
?>
請幫助我。
你確定該端口應該是'8000'? (通常是'80')。我可以從這裏看到80端口,但端口8000超時... – dandavis
哦,我很抱歉,這是一個錯字。 Idk它是如何穿過它的。 –
我曾嘗試通過CURL發送數據,但它沒有正確響應,您確定您的服務器有效嗎? – eyllanesc