2017-09-01 4 views
1

Il s'agit du code ESP8266 pour allumer/éteindre une LED. J'utilise l'exemple de code intégré IDE Arduino. La LED fonctionne correctement, mais je veux envoyer des requêtes HTTP à mon site web hébergé localement (qui envoie des emails) mais cela ne fonctionne pas.Le code ESP8266 ne fonctionne pas correctement

  1. Connecté avec mon Wifi
  2. locale
  3. Assigned une adresse IP statique
  4. Quand je frappe 192.168.1.101/gpio/1(Led ON)
  5. Quand je frappe 192.168.1.101/gpio/0(Led OFF) Ça marche mais je n'arrive pas à atteindre mon site web.
  6. Quand je frappe 192.168.1.101/gpio/1 alors il devrait frapper mon URL hébergé localement (192.168.1.100/home/ardchk)

me aider de bien vouloir régler cette question.

#include <ESP8266WiFi.h> 

const char* ssid = "SMART"; 
const char* password = "123456789"; 
const char* host = "192.168.1.100"; // Your domain 
IPAddress ip(192, 168, 1, 101); // where xx is the desired IP Address 
IPAddress gateway(192, 168, 1, 1); // set gateway to match your network 
IPAddress subnet(255, 255, 255, 0); 
WiFiServer server(80); 

void setup() { 
    Serial.begin(115200); 
    delay(10); 
    // prepare GPIO3 
    pinMode(3, OUTPUT); 
    digitalWrite(3, 0); 
    // Connect to WiFi network 
    Serial.println(); 
    Serial.println(); 
    Serial.print("Connecting to "); 
    Serial.println(ssid); 
    WiFi.config(ip,gateway,subnet); 
    WiFi.begin(ssid, password); 
    while (WiFi.status() != WL_CONNECTED) { 
    delay(500); 
    Serial.print("."); 
    } 
    Serial.println(""); 
    Serial.println("WiFi connected"); 
    // Start the server 
    server.begin(); 
    Serial.println("Server started"); 
    // Print the IP address 
    Serial.println(WiFi.localIP()); 
} 

void loop() { 
    // Check if a client has connected 
    WiFiClient client = server.available(); 
    if (!client) { 
    return; 
    } 
    // Wait until the client sends some data 
    Serial.println("new client"); 
    while(!client.available()) { 
    delay(1); 
    } 
    // Read the first line of the request 
    String req = client.readStringUntil('\r'); 
    Serial.println(req); 
    // Match the request 
    int val; 
    if (req.indexOf("/gpio/0") != -1) { 
    val = 0; 
    if (client.connect(host, 80)) { 
     //starts client connection, checks for connection 
     Serial.println("connected to website/server"); 
     client.println("GET /home/ardchk HTTP/1.1"); //Send data 
     client.println("Host: 192.168.1.100"); 
     Serial.println("Email Sended"); 
     client.println("Connection: close"); 
     //close 1.1 persistent connection 
     client.println(); //end of get request 
    } 
    } else if (req.indexOf("/gpio/1") != -1) { 
    val = 1; 
    } else { 
    Serial.println("invalid request"); 
    client.stop(); 
    return; 
    } 
    // Set GPIO2 according to the request 
    digitalWrite(3, val); 
    client.flush(); 
    // Prepare the response 
    String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now "; 
    s += (val)?"high":"low"; 
    s += "</html>\n"; 
    // Send the response to the client 
    client.print(s); 
    delay(1); 
    Serial.println("Client disconnected"); 
    // The client will actually be disconnected 
    // when the function returns and 'client' object is destroyed 
} 
+2

S'il vous plaît expliquer ce qui se passe lorsque vous essayez de frapper l'URL. "Ne fonctionne pas" n'est pas clair – Billa

+1

Vous devez apprendre à épeler (la majuscule au hasard ne compte pas comme une orthographe correcte), et indenter correctement votre code. Heureusement, l'EDI Arduino fait cela pour vous: appuyez sur ctrl-T ... – dda

+0

Le bloc de code: if (client.connect (hôte, 80)) {Ce code ne fonctionne pas parce que ne pas se connecter avec l'hôte} même le site Web est hébergé sur même réseau –

Répondre

0

Sur la base de votre question/commentaire, je suppose que client.connect(host, 80) est soit faux.

Je crois que vous ne pouvez pas vous connecter à votre host parce que vous essayez de vous connecter deux fois avec le même client.

Votre code ressemble à ceci:

// returns an already connected client, if available 
WiFiClient client = server.available() 

//... 

if (client.connect(host, 80)) {/*...*/} 

Vous voyez, vous utilisez le déjà connecté client pour tenter de se connecter à votre hôte. Au lieu de cela, essayez de créer une WiFiClient distincte pour ce travail:

WiFiClient requesting_client = server.available(); 
//... 
if (req.indexOf("/gpio/0") != -1) { 
    val = 0; 
    // create a fresh, new client 
    WiFiClient emailing_client; 
    if (emailing_client.connect(host, 80)) { 
    // ... 
    // don't forget that you need to close this client as well! 
    emailing_client.stop(); 
    } 

Hope this helps!