2017-10-02 8 views
-5

안녕하세요! 내 코드에 문제가있다. 웹 서버에 연결할 때 연결이 끊어지는 이유는 무엇입니까? 그건 그렇고, NodeMCU를 사용하고 있습니다. 너희들이 나를 도울 수 있기를 소망한다. 미리 감사드립니다.NodeMCU가 웹 서버에 연결 중

+2

가) 코드를 게시합니다. b) [arduino.se] –

+0

Serial.print ("Connecting to"); Serial.println (host); WiFiClient 클라이언트; const int httpPort = 80; if (! client.connect (host, httpPort)) { Serial.println ("연결에 실패했습니다!"); 반환; } – Phen

+0

그 게으른 수 없습니다 ... – dda

답변

0

전체 코드를 입력하면 도움이됩니다. 또한 적어도 나를 위해 일하는 Arduino IDE 용 ESP8266 패키지에 제공된 WiFiClient 스케치에 참여하고있는 것을 볼 수 있습니다. (당신이 스케치를 사용하지 않는 것을) 내가 틀렸다 경우,이 하나를 시도해보십시오 : 내가 stackoverflow.com의 홈페이지를 얻기 위해이를 수정 한

#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"); 
} 

. 물론 SSID 및 SSID 암호를 제공하고 호스트 이름을 지정해야합니다. 당신은 아두 이노 IDE에 대한 ESP8266/NodeMCU 라이브러리를 설치하는 데 도움이 필요하면

이 체크 아웃 : https://www.teachmemicro.com/intro-nodemcu-arduino/

+0

감사합니다. 나는 그것을 시도했지만 데이터베이스 연결에 대한 오류가 발생했습니다. 임 – Phen