| API For Arduino developers

- Main Page
- Related Pages
- Classes
- Files
- Examples

- File List
- File Members

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