2017-03-03 4 views
1

Ich versuche, meinen ESP8266 12F-Mikrocontroller mit einem auf hostinger.com gehosteten Remote-Apache-Server zu verbinden. Der Client kann jedoch keine Verbindung herstellen. Was ich tun muss, ist, einige Lesungen in meine MySQL-Datenbank auf dem Server zu schreiben. Ich habe den Mikrocontroller mit meinem mobilen Hotspot verbunden, kann aber keine Verbindung zum Server herstellen. Hier ist mein Code:Verbindung von ESP8266 zum Remote-Server kann nicht hergestellt werden

#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 

Hier ist der PHP-Code:

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

Bitte helfen Sie mir.

+0

sind Sie sicher, dass der Port sein sollte '8000'? (es ist normalerweise '80'). Ich kann es auf Port 80 von hier sehen, aber Port 8000 mal aus ... – dandavis

+0

Oh, es tut mir leid, dass das ein Tippfehler war. Wie es gekrochen ist. –

+0

Ich habe versucht, Daten über CURL zu senden, und es reagiert nicht richtig. Sind Sie sicher, dass Ihr Server funktioniert? – eyllanesc

Antwort

0

Hier ist, wie ich in Arbeit kam, falls es jemand hilft.

Server-Seite:

<?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 (busid,latitude,longitude,speed) VALUES ('$id','$lat','$lng','$speed')"; 



//Executing query to database 
mysqli_query($con,$sql); 


//Closing the database 
mysqli_close($con); 
} 
?> 

Client-Seite:

HTTPClient http; 
    http.begin("http://www.smartlbus.esy.es/add.php"); 
    http.addHeader("Content-Type", "application/x-www-form-urlencoded"); 
while (ss.available() > 0) 
    if (gps.encode(ss.read())) 
    { 
     if (gps.location.isValid()) 
       { 
       lat=gps.location.lat(); 
       lng=gps.location.lng(); 
       speed=gps.speed.value(); 
       } 
     if(lat!=plat&&lng!=plng){ 
     lat=0;lng=0;speed=0; 
      String data = "lat1=" + (String)lat + "&lng1=" + (String)lng + "&speed=" + (String)speed + "& busid=101"; 
      int httpCode = http.POST(data); 
     } 
      http.end(); 
      plat=lat; 
      plng=lng; 
      delay(500); 
     } 

// Look for new cards 
    if (! mfrc522.PICC_IsNewCardPresent()) { 
    return; 
    } 

    // Select one of the cards 
    if (! mfrc522.PICC_ReadCardSerial()) { 
    return; 
    } 

unsigned long UID_unsigned; 
UID_unsigned = mfrc522.uid.uidByte[0] << 24; 
UID_unsigned += mfrc522.uid.uidByte[1] << 16; 
UID_unsigned += mfrc522.uid.uidByte[2] << 8; 
UID_unsigned += mfrc522.uid.uidByte[3]; 

String UID_string = (String)UID_unsigned; 

if(UID_string.indexOf("3133482000")!=-1) 
    { 
    HTTPClient httpClient; 
    httpClient.begin("http://www.smartlbus.esy.es/notify.php"); 
    httpClient.addHeader("Content-Type", "application/x-www-form-urlencoded"); 
    String data = "child=" + UID_string+"lat1=" + (String)lat + "&lng1=" + (String)lng; 
    int httpCode = http.POST(data); 
    httpClient.end(); 
    } 
Verwandte Themen