API: examples/TCPClientSingle/TCPClientSingle.ino Source File

ESP8266 Arduino API

TCPClientSingle.ino
1 
21 #include "ESP8266.h"
22 
23 #define SSID "ITEAD"
24 #define PASSWORD "12345678"
25 #define HOST_NAME "172.16.5.12"
26 #define HOST_PORT (8090)
27 
28 ESP8266 wifi(Serial1);
29 
30 void setup(void)
31 {
32  Serial.begin(9600);
33  Serial.print("setup begin\r\n");
34 
35  Serial.print("FW Version:");
36  Serial.println(wifi.getVersion().c_str());
37 
38  if (wifi.setOprToStationSoftAP()) {
39  Serial.print("to station + softap ok\r\n");
40  } else {
41  Serial.print("to station + softap err\r\n");
42  }
43 
44  if (wifi.joinAP(SSID, PASSWORD)) {
45  Serial.print("Join AP success\r\n");
46  Serial.print("IP:");
47  Serial.println( wifi.getLocalIP().c_str());
48  } else {
49  Serial.print("Join AP failure\r\n");
50  }
51 
52  if (wifi.disableMUX()) {
53  Serial.print("single ok\r\n");
54  } else {
55  Serial.print("single err\r\n");
56  }
57 
58  Serial.print("setup end\r\n");
59 }
60 
61 void loop(void)
62 {
63  uint8_t buffer[128] = {0};
64 
65  if (wifi.createTCP(HOST_NAME, HOST_PORT)) {
66  Serial.print("create tcp ok\r\n");
67  } else {
68  Serial.print("create tcp err\r\n");
69  }
70 
71  char *hello = "Hello, this is client!";
72  wifi.send((const uint8_t*)hello, strlen(hello));
73 
74  uint32_t len = wifi.recv(buffer, sizeof(buffer), 10000);
75  if (len > 0) {
76  Serial.print("Received:[");
77  for(uint32_t i = 0; i < len; i++) {
78  Serial.print((char)buffer[i]);
79  }
80  Serial.print("]\r\n");
81  }
82 
83  if (wifi.releaseTCP()) {
84  Serial.print("release tcp ok\r\n");
85  } else {
86  Serial.print("release tcp err\r\n");
87  }
88  delay(5000);
89 }
90 
bool send(const uint8_t *buffer, uint32_t len)
Send data based on TCP or UDP builded already in single mode.
Definition: ESP8266.cpp:386
bool joinAP(String ssid, String pwd, uint8_t pattern=3)
Join in AP.
Definition: ESP8266.cpp:178
String getVersion(void)
Get the version of AT Command Set.
Definition: ESP8266.cpp:80
bool releaseTCP(void)
Release TCP connection in single mode.
Definition: ESP8266.cpp:303
uint32_t recv(uint8_t *buffer, uint32_t buffer_size, uint32_t timeout=1000)
Receive data from TCP or UDP builded already in single mode.
Definition: ESP8266.cpp:396
String getLocalIP(void)
Get the IP address of ESP8266.
Definition: ESP8266.cpp:281
The definition of class ESP8266.
bool createTCP(String addr, uint32_t port)
Create TCP connection in single mode.
Definition: ESP8266.cpp:298
bool disableMUX(void)
Disable IP MUX(single connection mode).
Definition: ESP8266.cpp:293
bool setOprToStationSoftAP(uint8_t pattern1=3, uint8_t pattern2=3)
Set operation mode to station + softap.
Definition: ESP8266.cpp:146
Provide an easy-to-use way to manipulate ESP8266.
Definition: ESP8266.h:42
Generated on Thu Apr 9 2015 13:57:59 for API by   doxygen 1.8.7