2017-10-02 1 views
-5

Bonne journée! J'ai un problème avec mon code. Pourquoi est-ce que j'obtiens une connexion a échoué quand je me connecte à mon serveur Web. Au fait, j'utilise un NodeMCU. J'espère que vous pouvez m'aider. Merci d'avance.NodeMCU se connectant au serveur Web

+2

a) afficher le code. b) [arduino.se] –

+0

Serial.print ("Connexion à"); Serial.println (hôte); Client WiFiClient; const int httpPort = 80; if (! Client.connect (hôte, httpPort)) { Serial.println ("Échec de la connexion!"); retour; } – Phen

+0

Vous ne pouvez pas être si paresseux ... – dda

Répondre

0

Fournir le code complet serait utile. En outre, je peux voir que vous prenez part à l'esquisse WiFiClient comme prévu dans le paquet ESP8266 pour l'IDE Arduino qui fonctionne pour moi, au moins. Si je me trompe (que vous n'utilisez pas ce croquis) puis essayez celui-ci:

#include <ESP8266WiFi.h> 

const char* ssid  = "your-ssid"; 
const char* password = "your-password"; 

const char* host = "stackoverflow.com"; 
const char* streamId = "...................."; 
const char* privateKey = "...................."; 

void setup() { 
    Serial.begin(115200); 
    delay(10); 

    // We start by connecting to a WiFi network 
    Serial.println(); 
    Serial.println(); 
    Serial.print("Connecting to "); 
    Serial.println(ssid); 

    WiFi.begin(ssid, password); 

    while (WiFi.status() != WL_CONNECTED) { 
    delay(500); 
    Serial.print("."); 
    } 

    Serial.println(""); 
    Serial.println("WiFi connected"); 
    Serial.println("IP address: "); 
    Serial.println(WiFi.localIP()); 
} 

int value = 0; 

void loop() { 
delay(5000); 
++value; 

Serial.print("connecting to "); 
Serial.println(host); 

// Use WiFiClient class to create TCP connections 
WiFiClient client; 
const int httpPort = 80; 
if (!client.connect(host, httpPort)) { 
    Serial.println("connection failed"); 
    return; 
} 

// We now create a URI for the request 
String url = "/"; 

Serial.print("Requesting URL: "); 
Serial.println(url); 

// This will send the request to the server 
client.print(String("GET ") + url + " HTTP/1.1\r\n" + 
      "Host: " + host + "\r\n" + 
      "Connection: close\r\n\r\n"); 
unsigned long timeout = millis(); 
while (client.available() == 0) { 
if (millis() - timeout > 5000) { 
    Serial.println(">>> Client Timeout !"); 
    client.stop(); 
    return; 
} 
} 

// Read all the lines of the reply from server and print them to Serial 
while(client.available()){ 
    String line = client.readStringUntil('\r'); 
    Serial.print(line); 
} 

Serial.println(); 
Serial.println("closing connection"); 
} 

J'ai modifié cela pour obtenir la page d'accueil stackoverflow.com. Bien sûr, vous devez fournir votre mot de passe SSID et SSID et spécifier le nom d'hôte.

Si vous avez besoin d'aide pour installer la bibliothèque ESP8266/NodeMCU pour l'Arduino IDE, vérifier cela: https://www.teachmemicro.com/intro-nodemcu-arduino/

+0

merci monsieur. Je l'ai essayé mais j'ai eu une erreur à propos de la connexion à la base de données. Je suis – Phen