2016-04-12 10 views
3

私はArduino UnoとC++で書かれたサーバを持っています。ESP8266 Arduinoから私のサーバに接続します

#include <SoftwareSerial.h> 

SoftwareSerial esp8266(3, 2); 

void setup() { 
    // Open serial communications and wait for port to open: 
    Serial.begin(115200); 
    while (!Serial) { 
    ; // wait for serial port to connect. Needed for native USB port only 
    } 
    Serial.println("Started"); 
    // set the data rate for the SoftwareSerial port 
    esp8266.begin(115200); 
    esp8266.write("AT\r\n"); 
} 

void loop() { 
    if (esp8266.available()) { 
    Serial.write(esp8266.read()); 
    } 
    if (Serial.available()) { 
    esp8266.write(Serial.read()); 
    } 
} 

は今、私はESP8266は、同じLAN(私は、サーバーIPを持っている)で、クライアントとしての私のサーバーに接続したい:私は正常に次のコードを使用して、私のルータにESP8266を接続します。私はSoftwareSerialでどうすればいいですか?それを行う別の方法がありますか?

答えて

2

HTTPリクエストを作成するには、ATコマンドを送信する必要があります。 これは、あなたがより多くの詳細が必要な場合は、このリンクは助けるべき80

// Connect to the server 
esp8266.write("AT+CIPSTART=\"TCP\",\"192.168.88.35\",80\r\n"); //make this command: AT+CPISTART="TCP","192.168.88.35",80 

//wait a little while for 'Linked' 
delay(300); 

//This is our HTTP GET Request change to the page and server you want to load. 
String cmd = "GET /status.html HTTP/1.0\r\n"; 
cmd += "Host: 192.168.88.35\r\n\r\n"; 

//The ESP8266 needs to know the size of the GET request 
esp8266.write("AT+CIPSEND="); 
esp8266.write(cmd.length()); 
esp8266.write("\r\n"); 

esp8266.write(cmd); 
esp8266.write("AT+CIPCLOSE\r\n"); 

ポートに192.168.88.35のサーバに接続しますます。http: http://blog.huntgang.com/2015/01/20/arduino-esp8266-tutorial-web-server-monitor-example/

+0

こんにちは、私は、内のコードを見た//ブログ。 huntgang.com/2015/01/20/arduino-esp8266-tutorial-web-server-monitor-example/ですが、毎回(ループ機能で)サーバーを接続しますが、サーバーを1回だけ接続する必要があります。 ..どうすればいい? –

+0

loop()の代わりにsetup()に接続ロジックを置きます。 setup()にあるコードは、arduinoが起動するとすぐに1回だけ実行されます。その後、ループ内でloop()を実行します。 –

+0

はい、それは分かりますが、1回ではesp8266が接続するには不十分です...試してみる必要があります... espが接続されているかどうかをどのように検出できますか? –

関連する問題