source: rtos_arduino/trunk/arduino_lib/libraries/Milkcocoa_Arduino_SDK/Client_ESP8266.h@ 144

Last change on this file since 144 was 144, checked in by ertl-honda, 8 years ago

Milkcocoa用のMQTTライブラリの追加と,ESP8266用ライブラリの名称変更.

File size: 2.9 KB
Line 
1/*
2 Copyright (C) 2015 Embedded and Real-Time Systems Laboratory
3 Graduate School of Information Science, Nagoya Univ., JAPAN
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13 You should have received a copy of the GNU Lesser General Public
14 License along with this library; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17
18#ifndef _CLIENT_ESP8366_H_
19#define _CLIENT_ESP8366_H_
20#include "Print.h"
21#include "Stream.h"
22#include "IPAddress.h"
23#include "ESP8266.h"
24
25#define ESP8266CLIENT_BUFFER_LEN 512
26
27class ESP8266Client: public Client, public ESP8266 {
28
29 public:
30 /*
31 * @retval 0 : faild
32 * @retval 1 : success
33 */
34 int connect(IPAddress ip, uint16_t port) {
35 String ip_str;
36 bufferlen= 0;
37 ip_str.concat(ip[0]);
38 ip_str.concat(".");
39 ip_str.concat(ip[1]);
40 ip_str.concat(".");
41 ip_str.concat(ip[2]);
42 ip_str.concat(".");
43 ip_str.concat(ip[3]);
44 _connected = (createTCP(ip_str, port))? 1 : 0;
45 return (int)_connected;
46 };
47
48 int connect(const char *host, uint16_t port) {
49 String ip_str(host);
50 bufferlen= 0;
51 _connected = (createTCP(ip_str, port))? 1 : 0;
52 return (int)_connected;
53 };
54
55 size_t write(uint8_t data) {
56 if(send(&data, 1)){
57 return 1;
58 }
59 else {
60 return 0;
61 }
62 };
63 size_t write(const uint8_t *buf, size_t size){
64 if(send(buf, size)){
65 return size;
66 }
67 else {
68 return 0;
69 }
70 };
71 int available(){return (bufferlen > 0 || dataAvailable());};
72 int read(){
73 if (bufferlen == 0) {
74 bufferlen = recv(buffer, ESP8266CLIENT_BUFFER_LEN);
75 pbuffer = buffer;
76 }
77 if (bufferlen > 0){
78 bufferlen--;
79 return *pbuffer++;
80 }
81 else {
82 return (int)-1;
83 }
84 };
85 int read(uint8_t *buf, size_t size){return recv(buf, size);};
86 int peek(){return 1;};
87 void flush(){};
88 void stop(){releaseTCP();};
89 uint8_t connected(){return _connected;};
90 operator bool(){return (bool)(_connected == 1);};
91 private:
92 uint8_t _connected;
93 uint8_t buffer[ESP8266CLIENT_BUFFER_LEN];
94 uint8_t bufferlen;
95 uint8_t *pbuffer;
96};
97
98#endif /* _CLIENT_ESP8366_H_ */
Note: See TracBrowser for help on using the repository browser.