实验要求
通过将ESPDuino开发板设置成TCP Client模式,从而达到联网通信
具体方法
登录http://tcp.doit.am获取要使用的IP和端口:
程序- <font _mstmutation="1"><font _mstmutation="1"><font _mstmutation="1">#include<ESP8266WiFi.h>
- const char*ssid ="STM32L476";
- const char*password ="284122348";
- const char*serverIP ="115.29.109.104";
- int serverPort =6558;
- WiFiClient client;
- bool bConnected =false;
- char buff[512];
- int nm =0;
- void setup(){
- Serial.begin(115200);
- delay(10);
- 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("WiFi connected");
- Serial.println("IP address:");
- Serial.println(WiFi.localIP());
- }
- void loop(){
- delay(1);
- if(bConnected ==false)
- {
- if(!client.connect(serverIP,serverPort))
- {
- Serial.println("connection failed");
- delay(5000);
- return;
- }
- bConnected=true;
- Serial.println("connection ok");
- }
- else if(client.available())
- {
- Serial.println("data is coming");
- while(client.available())
- {
- buff[nm++]=client.read();
- if(nm>=511)break;
- }
- buff[nm]=0x00;
- nm=0;
- Serial.print(buff);
- client.print(buff);
- client.flush();</font></font></font>
- }
- }