├── .project
├── ArduinoLibs
├── Common
│ ├── Common.cpp
│ ├── Common.h
│ ├── Debug.cpp
│ ├── ESP_Debug.h
│ ├── ESP_Log.cpp
│ └── ESP_Log.h
├── ESP_PCF8574
│ ├── ESP_PCF8574.cpp
│ ├── ESP_PCF8574.h
│ └── examples
│ │ └── sample1.cpp
└── ESP_RESTClient
│ ├── ESP_RESTClient.cpp
│ ├── ESP_RESTClient.h
│ ├── RESTProcessor.cpp
│ └── RESTProcessor.h
├── CPPLibs
├── ESPString
│ ├── ESPString.cpp
│ └── ESPString.h
└── List
│ ├── List.cpp
│ └── List.h
├── README.md
└── SDKLibs
└── CommonSDK
├── CommonSDK.h
├── commonSDK.c
├── espmissingincludes.h
├── getdata.c
├── getdata.h
├── startListening.c
├── startListening.h
├── wificonnect.c
└── wificonnect.h
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | ESPLibs
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/ArduinoLibs/Common/Common.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | /**
4 | * Return a string representation of the IPAddress passed in address. The format
5 | * of the string is "xxx.xxx.xxx.xxx".
6 | */
7 | String Common::ipAddressToString(IPAddress address) {
8 | return String(address[0]) + "." + String(address[1]) + "." + String(address[2]) + "." + String(address[3]);
9 | } // End of ipAddressToString
10 |
--------------------------------------------------------------------------------
/ArduinoLibs/Common/Common.h:
--------------------------------------------------------------------------------
1 | #ifndef Common_h
2 | #define Common_h
3 |
4 | #include
5 | #include
6 |
7 | class Common {
8 | public:
9 | /**
10 | * Return a string representation of the IPAddress passed in address. The format
11 | * of the string is "xxx.xxx.xxx.xxx".
12 | */
13 | static String ipAddressToString(IPAddress address);
14 | };
15 | #endif
16 |
--------------------------------------------------------------------------------
/ArduinoLibs/Common/Debug.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "ESP_Debug.h"
3 |
4 | extern "C" {
5 | #include "user_interface.h"
6 | }
7 |
8 | void Debug::lastException() {
9 | struct rst_info *pRstInfo;
10 | pRstInfo = system_get_rst_info();
11 | Serial1.println("reason=" + String(pRstInfo->reason) + ", exccause=0x" + String(pRstInfo->exccause, HEX) + ", epc1=0x" + String(pRstInfo->epc1, HEX) + ", epc2=0x" + String(pRstInfo->epc2,HEX) + ", epc3=0x" + String(pRstInfo->epc3, HEX) + ", excvaddr=" + String(pRstInfo->excvaddr, HEX) + ", depc=0x" + String(pRstInfo->depc, HEX));
12 | }
13 |
--------------------------------------------------------------------------------
/ArduinoLibs/Common/ESP_Debug.h:
--------------------------------------------------------------------------------
1 | #ifndef Debug_h
2 | #define Debug_h
3 | class Debug {
4 | public:
5 | static void lastException();
6 | };
7 | #endif
8 |
--------------------------------------------------------------------------------
/ArduinoLibs/Common/ESP_Log.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Logging function
3 | */
4 |
5 | #include
6 | #include //To allow function to run from any file in a project
7 | #include "ESP_Log.h"
8 |
9 | #define ARDBUFFER 16 //Buffer for storing intermediate strings. Performance may vary depending on size.
10 |
11 | /**
12 | * Perform a "printf" to the Arduino Serial port. The supported
13 | * printf flags are:
14 | * - %d - Print an integer
15 | * - %l - Print a long integer
16 | * - %c - Print a character
17 | * - %s - Print a string
18 | * - %x - Print an integer in hex
19 | * - %i - Print an IP address
20 | */
21 | void ESPLog::ardprintf(const char *str, ...) {
22 | int i, j;
23 | char temp[ARDBUFFER + 1];
24 |
25 | va_list vargs;
26 | va_start(vargs, str);
27 | //Iterate over formatting string
28 | for (i = 0, j = 0; str[i] != '\0'; i++) {
29 | if (str[i] == '%') {
30 | //Clear buffer
31 | temp[j] = '\0';
32 | Serial1.print(temp);
33 | j = 0;
34 | temp[0] = '\0';
35 |
36 | //Process argument
37 | switch (str[++i]) {
38 | case 'd':
39 | Serial1.print(va_arg(vargs, int));
40 | break;
41 | case 'l':
42 | Serial1.print(va_arg(vargs, long));
43 | break;
44 | case 'f':
45 | Serial1.print(va_arg(vargs, double));
46 | break;
47 | case 'c':
48 | Serial1.print((char) va_arg(vargs, int));
49 | break;
50 | case 's':
51 | Serial1.print(va_arg(vargs, char *));
52 | break;
53 | case 'x':
54 | Serial1.print(va_arg(vargs, int), HEX);
55 | break;
56 | case 'i':
57 | char *p;
58 | p = va_arg(vargs, char *);
59 | Serial1.print(String((int)p[0]) + "." + String((int)p[1]) + "." + String((int)p[2]) + "." + String((int)p[3]));
60 | break;
61 | default:
62 | ;
63 | };
64 | } else {
65 | //Add to buffer
66 | temp[j] = str[i];
67 | j = (j + 1) % ARDBUFFER;
68 | if (j == 0) //If buffer is full, empty buffer.
69 | {
70 | temp[ARDBUFFER] = '\0';
71 | Serial1.print(temp);
72 | temp[0] = '\0';
73 | }
74 | }
75 | };
76 | // If there are any output characters not yet sent, send them now.
77 | if (j != 0) {
78 | temp[j] = '\0';
79 | Serial1.print(temp);
80 | }
81 |
82 | //Serial1.println(); //Print trailing newline
83 | va_end(vargs);
84 | } // End of ardprintf
85 |
86 |
87 | /**
88 | * Create and dump a hex array of the content of memory.
89 | */
90 | void ESPLog::dumpHex(const char *buf, int size) {
91 | ESPLog::ardprintf("Dump hex of address: 0x%x for %d\n", buf, size);
92 | int diff = ((int)buf)% 16;
93 | int lineAddr = (int)buf - diff;
94 |
95 | ESPLog::ardprintf("%x ", (int)lineAddr);
96 | for (int i=0; i0 && i%16==0) {
103 | ESPLog::ardprintf("\n");
104 | lineAddr+=16;
105 | ESPLog::ardprintf("%x ", (int)lineAddr);
106 | }
107 | char c = buf[i];
108 | if (c<=0xf) {
109 | ESPLog::ardprintf("0");
110 | }
111 | ESPLog::ardprintf("%x ", (int)c);
112 | }
113 | ESPLog::ardprintf("\n");
114 | } // End of dumpHex
115 |
116 | /**
117 | * Create and dump a hex array of the content of memory.
118 | */
119 | void ESPLog::dumpHex(const char *from, const char *to) {
120 | int size = to - from;
121 | if (size <= 0) {
122 | ESPLog::ardprintf("ESPLog::dumpHex: Error end (0x%x) is < start (0x%x)\n", from, to);
123 | return;
124 | }
125 | ESPLog::dumpHex(from, size);
126 | } // End of dumpHex
127 | // End of file
128 |
--------------------------------------------------------------------------------
/ArduinoLibs/Common/ESP_Log.h:
--------------------------------------------------------------------------------
1 | #ifndef ESP_Log_h
2 | #define ESP_Log_h
3 | class ESPLog {
4 | public:
5 | static void ardprintf(const char *str, ...);
6 | static void dumpHex(const char *buf, int size);
7 | static void dumpHex(const char *from, const char *to);
8 | };
9 | #define LOG ESPLog::ardprintf
10 | #endif
11 | // End of file
12 |
--------------------------------------------------------------------------------
/ArduinoLibs/ESP_PCF8574/ESP_PCF8574.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "ESP_PCF8574.h"
4 |
5 | /**
6 | * Constructor for the class
7 | */
8 | ESP_PCF8574::ESP_PCF8574() {
9 | m_address = 0;
10 | } // End of constructor
11 |
12 | /**
13 | * Set the address and pins to be used for SDA and CLK in the Two Wire (I2C) protocol.
14 | */
15 | void ESP_PCF8574::begin(uint8_t address, uint8_t sda, uint8_t clk) {
16 | m_address = address;
17 | Wire.begin(sda,clk);
18 | } // End of setPins
19 |
20 | /**
21 | * Set the bit (range 0-7) of the GPIOs to the supplied value.
22 | */
23 | void ESP_PCF8574::setBit(uint8_t bit, bool value) {
24 | // Check that the bit is in range. Must be 0-7.
25 | if (bit < 0 || bit > 7) {
26 | return;
27 | }
28 | if (value == true) {
29 | setByte(m_currentOutput | (1 << bit));
30 | } else {
31 | setByte(m_currentOutput & ~(1 << bit));
32 | }
33 | } // End of setBit
34 |
35 | /**
36 | * Write the value of the byte as the output of the GPIOs.
37 | */
38 | void ESP_PCF8574::setByte(uint8_t value) {
39 | // Guard that we have been initialized
40 | if (m_address == 0) {
41 | return;
42 | }
43 | m_currentOutput = value;
44 | Wire.beginTransmission(m_address);
45 | Wire.write(m_currentOutput);
46 | Wire.endTransmission();
47 | } // End of setByte
48 |
49 | /**
50 | * Get the value of the byte of the GPIOs as input.
51 | */
52 | uint8_t ESP_PCF8574::getByte() {
53 | Wire.requestFrom(m_address, 1);
54 | return Wire.read();
55 | } // End of getByte
56 |
57 | /**
58 | * Get the value of the bit of the GPIO as input.
59 | */
60 | bool ESP_PCF8574::getBit(uint8_t bit) {
61 | if (bit < 0 || bit > 7) {
62 | return 0;
63 | }
64 | uint8_t byteValue = getByte();
65 | return (byteValue & (1<
2 | #include
3 | #include
4 | // SDA - Yellow - 4
5 | // CLK - White - 5
6 |
7 | #define SDA_PIN 4
8 | #define CLK_PIN 5
9 | #define PCF8574_ADDRESS (0x20)
10 |
11 | Ticker ticker;
12 | ESP_PCF8574 myPCF8574;
13 | int counter = 0;
14 | int dir = 1;
15 |
16 | void timerCB() {
17 | //myPCF8574.setByte(~((uint8_t)1< 0);
19 | counter += dir;
20 | if (counter == 8) {
21 | counter = 7;
22 | dir = -1;
23 | } else if (counter == -1) {
24 | counter = 0;
25 | dir = 1;
26 | }
27 | }
28 |
29 |
30 | void setup()
31 | {
32 | Serial1.begin(115200);
33 | myPCF8574.begin(PCF8574_ADDRESS, SDA_PIN, CLK_PIN);
34 | ticker.attach(0.1, timerCB);
35 | }
36 |
37 | // The loop function is called in an endless loop
38 | void loop()
39 | {
40 | }
41 |
--------------------------------------------------------------------------------
/ArduinoLibs/ESP_RESTClient/ESP_RESTClient.cpp:
--------------------------------------------------------------------------------
1 | #include "ESP_RESTClient.h"
2 |
3 | #define DEBUG
4 | #ifdef DEBUG
5 | #include
6 | #include
7 | #else
8 | #define LOG(message)
9 | #endif
10 |
11 | extern "C" {
12 | #include
13 | }
14 |
15 | enum {
16 | ESP_REST_CLIENT_ERROR_SIG = 0x10001,
17 | ESP_REST_CLIENT_RESPONSE_SIG = 0x10002
18 | };
19 |
20 |
21 | #define TASK_QUEUE_LEN 10
22 | #define REST_TASK_PRI USER_TASK_PRIO_1
23 |
24 | class URL {
25 | public:
26 | bool ssl;
27 | int port;
28 | String hostname;
29 | String path;
30 | void dump() {
31 | LOG("URL: hostname: %s, port: %d, path: %s, useSSL: %d\n", hostname.c_str(), port, path.c_str(), ssl);
32 | }
33 | };
34 |
35 | void taskHandler(os_event_t *event) {
36 | ESP_RestClient *pESP_RestClient;
37 | switch(event->sig) {
38 | case ESP_REST_CLIENT_ERROR_SIG:
39 | pESP_RestClient= (ESP_RestClient *)event->par;
40 | pESP_RestClient->_handleError();
41 | break;
42 | case ESP_REST_CLIENT_RESPONSE_SIG:
43 | pESP_RestClient= (ESP_RestClient *)event->par;
44 | pESP_RestClient->_handleResponse();
45 | break;
46 | }
47 | }
48 |
49 | /**
50 | * Parse a URL string of the format:
51 | * ://[:]
52 | *
53 | */
54 | // http://a.b.c.com[:]/
55 | static bool parseURL(String url, URL *pURL) {
56 |
57 | int i;
58 | if (url.startsWith("http://")) {
59 | pURL->ssl = false;
60 | i = 7; // Skip 7 characters
61 | } else if (url.startsWith("https://")) {
62 | pURL->ssl = true;
63 | i = 8; // Skip 8 characters
64 | } else {
65 | return false;
66 | }
67 | // Look for either an end of string, a ':' or a '/'
68 | // i
69 | // |
70 | // V
71 | // http://[:]/path
72 | //
73 | int j = i;
74 | char c = url.charAt(j);
75 | while (c != 0 && c != ':' && c != '/') {
76 | j++;
77 | c = url.charAt(j);
78 | }
79 | // url[i] - url[j] = hostname
80 | pURL->hostname = url.substring(i, j);
81 |
82 | // Let us now see if we have a port or just a path
83 | if (c == 0 || c == '/') {
84 | pURL->port = 80;
85 | } else {
86 | j++;
87 | int s = j;
88 | char c = url.charAt(j);
89 | while (c != 0 && c != '/') {
90 | j++;
91 | c = url.charAt(j);
92 | }
93 | String portStr = url.substring(s, j);
94 | pURL->port = portStr.toInt();
95 | }
96 | pURL->path = url.substring(j);
97 | // Now we have ssl, host, port and path
98 | return true;
99 | } // End of parseURL
100 |
101 | /**
102 | * The possible types of HTTP commands that can be sent by REST.
103 | */
104 | enum HTTPCommand {
105 | GET, POST, PUT
106 | };
107 |
108 | /**
109 | * A representation of an HTTP header.
110 | */
111 | class Header {
112 | public:
113 | void setHeader(char *name, char *value) {
114 | m_name = name;
115 | m_value = value;
116 | }
117 | char *getName() {
118 | return m_name;
119 | }
120 | char *getValue() {
121 | return m_value;
122 | }
123 | private:
124 | char *m_name; // The name of the header
125 | char *m_value; // The value of the header
126 | };
127 | // End of Header
128 |
129 | static void dnsFoundCallback(const char *hostname, ip_addr_t *pIpAddr,
130 | void *arg) {
131 | struct espconn *pEspconn = (struct espconn *) arg;
132 | ESP_RestClient *pESP_RestClient = (ESP_RestClient *) pEspconn->reverse;
133 | if (pIpAddr == 0 || pIpAddr->addr == 0) {
134 | pESP_RestClient->_handleError();
135 | return;
136 | }
137 | LOG("gethostbyname callback ... hostname %s, IP: %i\n", hostname, pIpAddr);
138 | pESP_RestClient->m_serverIP = *pIpAddr;
139 | pESP_RestClient->_connect();
140 | //dumpEspConn(pEspconn);
141 | } // End of dnsFoundCallback
142 |
143 | /**
144 | * The receive callback called when data is received.
145 | */
146 | static void recvCB(void *arg, char *pData, unsigned short len) {
147 | LOG("recvCB\n");
148 | struct espconn *pEspconn;
149 | pEspconn = (struct espconn *) arg;
150 | ESP_RestClient *pESP_RestClient = (ESP_RestClient *) pEspconn->reverse;
151 | LOG("Data: length received = %d\n", len);
152 | char *pBuf = (char *)malloc(len + 1);
153 | memcpy(pBuf, pData, len);
154 | pBuf[len] = '\0';
155 | pESP_RestClient->m_response = String(pBuf);
156 | free(pBuf);
157 | // Don't try and print the data here ... too much...
158 | pESP_RestClient->_close();
159 | system_os_post(REST_TASK_PRI, ESP_REST_CLIENT_RESPONSE_SIG, (os_param_t)pESP_RestClient);
160 | } // End of recvCB
161 |
162 | /**
163 | * The connect callback when a connection to a partner is made.
164 | */
165 | static void connectCB(void *arg) {
166 | LOG("connectCB\n");
167 | //ESPLog::dumpHex((const char *)s1, (const char *)e1);
168 | struct espconn *pEspconn;
169 | pEspconn = (struct espconn *) arg;
170 | ESP_RestClient *pESP_RestClient = (ESP_RestClient *) pEspconn->reverse;
171 | //LOG("Payload in connectCB: %x", pESP_RestClient->m_payload);
172 | LOG("Address of ESP_RestClient = 0x%x\n", pESP_RestClient);
173 | pESP_RestClient->_send();
174 | } // End of connectCB
175 |
176 | static void sendCB(void *arg) {
177 | LOG("sendCB\n");
178 | /*
179 | struct espconn *pEspconn;
180 | pEspconn = (struct espconn *) arg;
181 | ESP_RestClient *pESP_RestClient = (ESP_RestClient *) pEspconn->reverse;
182 | */
183 | //pESP_RestClient->_send();
184 | } // End of sendCB
185 |
186 | static void reconnectCB(void *arg, sint8 err) {
187 | LOG("reconnectCB\n");
188 | struct espconn *pEspconn;
189 | pEspconn = (struct espconn *) arg;
190 | ESP_RestClient *pESP_RestClient = (ESP_RestClient *) pEspconn->reverse;
191 | LOG("Error: %d: %s\n", err, errorToString(err));
192 | // We had detected an error.
193 | pESP_RestClient->_handleError();
194 | } // End of reconnectCB
195 |
196 | static void disconnectCB(void *arg) {
197 | LOG("disconnectCB\n");
198 | struct espconn *pEspconn;
199 | pEspconn = (struct espconn *) arg;
200 | ESP_RestClient *pESP_RestClient = (ESP_RestClient *) pEspconn->reverse;
201 | pESP_RestClient->_close();
202 | } // End of disconnectCB
203 |
204 | void ESP_RestClient::_close() {
205 | LOG("_close\n");
206 | if (m_pSendBuffer != NULL) {
207 | free(m_pSendBuffer);
208 | m_pSendBuffer = NULL;
209 | }
210 | espconn_disconnect(&m_conn);
211 | } // End of __close
212 |
213 | void ESP_RestClient::_send() {
214 | String data;
215 | switch (m_commandType) {
216 | case GET:
217 | data += "GET ";
218 | break;
219 | case PUT:
220 | data += "PUT ";
221 | break;
222 | case POST:
223 | data += "POST ";
224 | break;
225 | }
226 | // Build the start line of the request.
227 | data += String(m_path) + String(" HTTP/1.1\r\n");
228 |
229 | // Add the headers to the HTTP request
230 | int size = m_headers.size();
231 | for (int i = 0; i < size; i++) {
232 | Header *pHeader = (Header *) m_headers.get(i);
233 | data += pHeader->getName() + String(": ") + pHeader->getValue()
234 | + String("\r\n");
235 | }
236 |
237 | // Add the payload length if the type is PUT or POST.
238 | if (m_commandType == PUT || m_commandType == POST) {
239 | data += "Content-Length: " + String(strlen(m_payload)) + String("\r\n");
240 | }
241 | // Add the data separator line.
242 | data += String("\r\n");
243 |
244 | // Add the payload if we have any.
245 | if (m_payload != NULL) {
246 | data += m_payload;
247 | }
248 |
249 | // Send the buffer.
250 | int length = data.length() + 1;
251 | m_pSendBuffer = (uint8_t *) malloc(length);
252 | data.getBytes(m_pSendBuffer, length, 0);
253 | int rc = espconn_sent(&m_conn, m_pSendBuffer, length);
254 | if (rc != 0) {
255 | // Error
256 | LOG("Error with send: %d\n", rc);
257 | return;
258 | }
259 | } // End of _send
260 |
261 | ESP_RestClient::ESP_RestClient() {
262 | m_conn.type = ESPCONN_TCP;
263 | m_conn.state = ESPCONN_NONE;
264 | m_conn.proto.tcp = &m_tcp;
265 | m_conn.reverse = this;
266 | m_serverIP.addr = 0; // Initially, no IP address of server
267 |
268 | os_event_t *taskQueue;
269 | taskQueue = (os_event_t *)malloc(sizeof(os_event_t) * TASK_QUEUE_LEN);
270 | system_os_task(taskHandler, REST_TASK_PRI, taskQueue, TASK_QUEUE_LEN);
271 |
272 | // Define the callbacks to be called when ESP events are detected.
273 | espconn_regist_recvcb(&m_conn, recvCB);
274 | espconn_regist_connectcb(&m_conn, connectCB);
275 | espconn_regist_sentcb(&m_conn, sendCB);
276 | espconn_regist_disconcb(&m_conn, disconnectCB);
277 | espconn_regist_reconcb(&m_conn, reconnectCB);
278 | } // End of constructor
279 |
280 | /**
281 | * Set the URL to be used when sending requests.
282 | * o server - The IP address of the server
283 | * o port - The port number of the server
284 | * o path - The URL path part of the REST request
285 | */
286 | void ESP_RestClient::setURL(const char *hostname, int port, const char *path,
287 | bool useSSL) {
288 | m_hostname = (char *)hostname;
289 | m_port = port;
290 | m_path = (char *) path;
291 | m_secure = useSSL;
292 | m_serverIP.addr = 0;
293 | } // End of setURL
294 |
295 | void ESP_RestClient::setURL(String url) {
296 | URL urlClass;
297 | parseURL(url, &urlClass);
298 | urlClass.dump();
299 | m_hostname = urlClass.hostname;
300 | m_port = urlClass.port;
301 | m_path = urlClass.path;
302 | m_secure = urlClass.ssl;
303 | m_serverIP.addr = 0;
304 | }
305 |
306 | /**
307 | * Set a header to be sent.
308 | */
309 | void ESP_RestClient::setHeader(const char *name, const char *value) {
310 | Header *pHeader = new Header();
311 | pHeader->setHeader((char *) name, (char *) value);
312 | m_headers.add((void *) pHeader);
313 | } // End of setHeader
314 |
315 | /**
316 | * Send a GET request.
317 | */
318 | bool ESP_RestClient::get() {
319 | m_commandType = GET;
320 | return m_send(NULL);
321 | } // End of get
322 |
323 | /**
324 | * Send a PUT request.
325 | */
326 | bool ESP_RestClient::put(const char *payload) {
327 | m_commandType = PUT;
328 | return m_send(payload);
329 | } // End of put
330 |
331 | /**
332 | * Send a POST request.
333 | */
334 | bool ESP_RestClient::post(const char *payload) {
335 | m_commandType = POST;
336 | return m_send(payload);
337 | } // End of post
338 |
339 | /**
340 | * Register a callback function to be called when a response is detected.
341 | */
342 | void ESP_RestClient::onResponse(void (*onResponse)(String response)) {
343 | m_onResponse = onResponse;
344 | } // End of onResponse
345 |
346 | /**
347 | * Record a callback function to be called if an error is detected.
348 | */
349 | void ESP_RestClient::onError(void (*onError)()) {
350 | m_onError = onError;
351 | } // End of onError
352 |
353 | /**
354 | * Handle an error having been detected.
355 | */
356 | void ESP_RestClient::_handleError() {
357 | // Close the connection to the partner and then invoke the callback function
358 | // if one exists of the error callback.
359 | //LOG("_handleError");
360 | _close();
361 | if (m_onError != NULL) {
362 | //LOG("Calling error handler");
363 | m_onError();
364 | }
365 | } // End of _handleError
366 |
367 | /**
368 | * Handle a response.
369 | */
370 | void ESP_RestClient::_handleResponse() {
371 | _close();
372 | if (m_onResponse != NULL) {
373 | m_onResponse(m_response);
374 | }
375 | } // End of _handleResponse
376 |
377 |
378 |
379 | /**
380 | * Build and send a REST request
381 | */
382 | bool ESP_RestClient::m_send(const char *payload) {
383 | m_payload = (char *) payload;
384 | /*
385 | if (m_secure == false) {
386 | int rc = espconn_connect(&m_conn);
387 | if (rc != 0) {
388 | // Error
389 | LOG("Error with connect\n");
390 | _handleError();
391 | return false;
392 | }
393 | } else {
394 | //int rc = espconn_secure_connect(&m_conn);
395 | }
396 | */
397 | m_resolveHostname();
398 | return true;
399 | } // End of send
400 |
401 | void ESP_RestClient::m_resolveHostname() {
402 | LOG("About to resolve hostname of: %s\n", m_hostname.c_str());
403 | if (m_serverIP.addr != 0) {
404 | _connect();
405 | return;
406 | }
407 | int rc = espconn_gethostbyname(&m_conn, m_hostname.c_str(), &m_serverIP,
408 | dnsFoundCallback);
409 | if (rc == ESPCONN_OK) {
410 | LOG("We immediately have an IP address\n");
411 | _connect();
412 | return;
413 | }
414 | if (rc != ESPCONN_INPROGRESS) {
415 | _handleError();
416 | return;
417 | }
418 | return;
419 | } // End of m_resolveHostname
420 |
421 | void ESP_RestClient::_connect() {
422 | LOG("_connect: %i\n", &m_serverIP);
423 | memcpy(m_conn.proto.tcp->remote_ip, &m_serverIP, 4);
424 | m_conn.proto.tcp->remote_port = m_port;
425 | dumpEspConn(&m_conn);
426 | if (m_secure == false) {
427 | int rc = espconn_connect(&m_conn);
428 | if (rc != 0) {
429 | // Error
430 | LOG("Error with connect\n");
431 | system_os_post(REST_TASK_PRI, ESP_REST_CLIENT_ERROR_SIG, (os_param_t)this);
432 | return;
433 | }
434 | } else {
435 | //int rc = espconn_secure_connect(&m_conn);
436 | }
437 | } // End of m_connect
438 |
439 | // End of file
440 |
--------------------------------------------------------------------------------
/ArduinoLibs/ESP_RESTClient/ESP_RESTClient.h:
--------------------------------------------------------------------------------
1 | #ifndef ESP_RestClient_h
2 | #define ESP_RestClient_h
3 | #include
4 | #include
5 |
6 | extern "C" {
7 | #include
8 | #include
9 | #include
10 | }
11 | class ESP_RestClient {
12 | public:
13 | ESP_RestClient();
14 | void setURL(const char *server, int port, const char *path, bool useSSL=false);
15 | void setURL(String url);
16 | void setHeader(const char *name, const char *value);
17 |
18 | bool get();
19 | bool put(const char *payload);
20 | bool post(const char *payload);
21 | void onResponse(void (*onResponse)(String response));
22 | void onError(void (*onError)());
23 | void _send();
24 | void _close();
25 | void _handleError();
26 | void _handleResponse();
27 | void _connect();
28 | ip_addr_t m_serverIP;
29 | String m_response;
30 |
31 | private:
32 | bool m_send(const char *payload);
33 | void m_resolveHostname();
34 |
35 |
36 |
37 | uint16 m_port;
38 | bool m_secure;
39 | char *m_payload;
40 | int m_commandType;
41 | String m_hostname;
42 | String m_path;
43 | void (*m_onResponse)(String response);
44 | void (*m_onError)();
45 | uint8_t *m_pSendBuffer;
46 | struct espconn m_conn;
47 | esp_tcp m_tcp;
48 | List m_headers;
49 | };
50 | #endif
51 |
--------------------------------------------------------------------------------
/ArduinoLibs/ESP_RESTClient/RESTProcessor.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * RESTProcessor
3 | * A REST processor for the ESP/Arduino.
4 | *
5 | * This class provides a set of routines for manipulating REST requests to be sent
6 | * or received over a TCP/IP connection. It does not deal with actual communications
7 | * but rather concentrates solely on building and parsing REST requests and responses.
8 | * This makes it a partner to communication routines.
9 | *
10 | *
11 | * Neil Kolban
12 | * August 2015
13 | */
14 | #include "RESTProcessor.h"
15 |
16 | /**
17 | * The definition of a header.
18 | */
19 | class Header {
20 | public:
21 | void setHeader(String name, String value) {
22 | m_name = name;
23 | m_value = value;
24 | }
25 | String getName() {
26 | return m_name;
27 | }
28 | String getValue() {
29 | return m_value;
30 | }
31 | private:
32 | String m_name; // The name of the header
33 | String m_value; // The value of the header
34 | };
35 |
36 | /**
37 | * Set the REST/HTTP command type.
38 | */
39 | void RESTProcessor::setCommandType(HTTPCommand commandType) {
40 | m_commandType = commandType;
41 | } // End of setCommandType
42 |
43 |
44 | String RESTProcessor::getHostname() {
45 | return m_hostname;
46 | } // End of getHostname
47 |
48 | int RESTProcessor::getPort() {
49 | return m_port;
50 | } // End of getPort
51 |
52 | String RESTProcessor::getPath() {
53 | return m_path;
54 | } // End of getPath
55 |
56 | bool RESTProcessor::isUseSSL() {
57 | return m_useSSL;
58 | } // End of isUseSSL
59 |
60 |
61 | /**
62 | * Build a REST request.
63 | * The REST request to be sent over the network is returned in the
64 | * String returned by this function.
65 | */
66 | String RESTProcessor::buildRequest(String payload) {
67 | String data;
68 | switch (m_commandType) {
69 | case GET:
70 | data += "GET";
71 | break;
72 | case PUT:
73 | data += "PUT";
74 | break;
75 | case POST:
76 | data += "POST";
77 | break;
78 | }
79 | // Build the start line of the request.
80 | data += " " + String(m_path) + String(" HTTP/1.1\r\n");
81 |
82 | // Add the headers to the HTTP request
83 | int size = m_headers.size();
84 | for (int i = 0; i < size; i++) {
85 | Header *pHeader = (Header *) m_headers.get(i);
86 | data += pHeader->getName() + String(": ") + pHeader->getValue()
87 | + String("\r\n");
88 | }
89 |
90 | // Add the payload length (Content-Length) if the type is PUT or POST.
91 | if (m_commandType == PUT || m_commandType == POST) {
92 | data += "Content-Length: " + payload + String("\r\n");
93 | }
94 |
95 | // Add the data separator line.
96 | data += String("\r\n");
97 |
98 | // Add the payload if we have any.
99 | if (payload != NULL) {
100 | data += payload;
101 | }
102 |
103 | return data;
104 | } // End of buildRequest
105 |
106 | void RESTProcessor::addHeader(const char *name, const char *value) {
107 | Header *pHeader = new Header();
108 | pHeader->setHeader((char *) name, (char *) value);
109 | m_headers.add((void *) pHeader);
110 | } // End of addHeader
111 |
112 |
113 | /**
114 | * Parse a URL to extract the host, port, path and whether or not SSL is required.
115 | */
116 | bool RESTProcessor::parseURL(String url) {
117 |
118 | int i;
119 | if (url.startsWith("http://")) {
120 | m_useSSL = false;
121 | i = 7; // Skip 7 characters
122 | } else if (url.startsWith("https://")) {
123 | m_useSSL = true;
124 | i = 8; // Skip 8 characters
125 | } else {
126 | return false;
127 | }
128 | // Look for either an end of string, a ':' or a '/'
129 | // i
130 | // |
131 | // V
132 | // http://[:]/path
133 | //
134 | int j = i;
135 | char c = url.charAt(j);
136 | while (c != 0 && c != ':' && c != '/') {
137 | j++;
138 | c = url.charAt(j);
139 | }
140 | // url[i] - url[j] = hostname
141 | m_hostname = url.substring(i, j);
142 |
143 | // Let us now see if we have a port or just a path
144 | if (c == 0 || c == '/') {
145 | m_port = 80;
146 | } else {
147 | j++;
148 | int s = j;
149 | char c = url.charAt(j);
150 | while (c != 0 && c != '/') {
151 | j++;
152 | c = url.charAt(j);
153 | }
154 | String portStr = url.substring(s, j);
155 | m_port = portStr.toInt();
156 | }
157 | m_path = url.substring(j);
158 | // Now we have ssl, host, port and path
159 | return true;
160 | } // End of parseURL
161 | // End of file
162 |
--------------------------------------------------------------------------------
/ArduinoLibs/ESP_RESTClient/RESTProcessor.h:
--------------------------------------------------------------------------------
1 | #ifndef RESTProcessor_h
2 | #define RESTProcessor_h
3 | #include
4 | #include
5 | enum HTTPCommand {
6 | GET, POST, PUT
7 | };
8 |
9 | class RESTProcessor {
10 | public:
11 | bool parseURL(String url);
12 | String getHostname();
13 | int getPort();
14 | String getPath();
15 | bool isUseSSL();
16 | void setCommandType(HTTPCommand commandType);
17 | void addHeader(const char *name, const char *value);
18 | String buildRequest(String payload);
19 | private:
20 | String m_hostname;
21 | HTTPCommand m_commandType;
22 | int m_port;
23 | String m_path;
24 | bool m_useSSL;
25 | List m_headers;
26 | };
27 | #endif
28 |
--------------------------------------------------------------------------------
/CPPLibs/ESPString/ESPString.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | ESPString::ESPString(char *data) {
4 | m_pData = (char *)malloc(strlen(data) + 1);
5 | strcpy(m_pData, data);
6 | }
7 |
--------------------------------------------------------------------------------
/CPPLibs/ESPString/ESPString.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPString_h
2 | #define ESPString_h
3 | class ESPString {
4 | public:
5 | ESPString(char *data);
6 | private:
7 | char *m_pData;
8 | };
9 | #endif
10 |
--------------------------------------------------------------------------------
/CPPLibs/List/List.cpp:
--------------------------------------------------------------------------------
1 | #include "List.h"
2 | /**
3 | * Constructor
4 | */
5 | List::List() {
6 | m_pPrev = 0;
7 | m_pNext = 0;
8 | }
9 |
10 | /**
11 | * Return the number of items in the list.
12 | */
13 | int List::size() {
14 | if (m_pPrev != 0) {
15 | return -1;
16 | }
17 | int size = 0;
18 | List *pCurrent = this->m_pNext;
19 | while (pCurrent != 0) {
20 | size++;
21 | pCurrent = pCurrent->m_pNext;
22 | }
23 | return size;
24 | } // End of size
25 |
26 |
27 | /**
28 | * Add a new item to the end of the list.
29 | */
30 | void List::add(void *data) {
31 | List *pCurrent = this;
32 | while (pCurrent->m_pNext != 0) {
33 | pCurrent = pCurrent->m_pNext;
34 | }
35 | List *pNew = new List();
36 | pNew->m_pPrev = pCurrent;
37 | pNew->m_pNext = 0;
38 | pNew->m_pData = data;
39 | pCurrent->m_pNext = pNew;
40 | } // End of add
41 |
42 |
43 | /**
44 | * Get a specific item in the list.
45 | */
46 | void *List::get(int index) {
47 | if (m_pPrev != 0) {
48 | return 0;
49 | }
50 | List *pCurrent = m_pNext;
51 | while (pCurrent != 0 && index != 0) {
52 | pCurrent = pCurrent->m_pNext;
53 | index--;
54 | }
55 | if (pCurrent == 0) {
56 | return 0;
57 | }
58 | return pCurrent->m_pData;
59 | } // End of get
60 |
61 |
62 | /**
63 | * Remove an item from the list by index.
64 | */
65 | void *List::remove(int index) {
66 | if (m_pPrev != 0) {
67 | return 0;
68 | }
69 | List *pCurrent = m_pNext;
70 | while (pCurrent != 0 && index != 0) {
71 | pCurrent = pCurrent->m_pNext;
72 | index--;
73 | }
74 | if (pCurrent == 0) {
75 | return 0;
76 | }
77 | List *pPrev = pCurrent->m_pPrev;
78 | List *pNext = pCurrent->m_pNext;
79 | pPrev->m_pNext = pNext;
80 | pNext->m_pPrev = pPrev;
81 | void *pData = pCurrent->m_pData;
82 | delete pCurrent;
83 | return pData;
84 | } //End of remove
85 | // End of file
86 |
--------------------------------------------------------------------------------
/CPPLibs/List/List.h:
--------------------------------------------------------------------------------
1 | #ifndef List_h
2 | #define List_h
3 | class List {
4 | public:
5 | /**
6 | * Constructor
7 | */
8 | List();
9 |
10 | /**
11 | * Return the number of items in the list.
12 | */
13 | int size();
14 | /**
15 | * Add a new item to the end of the list.
16 | */
17 | void add(void *data);
18 | /**
19 | * Get a specific item in the list.
20 | */
21 | void *get(int index);
22 | /**
23 | * Remove an item from the list by index.
24 | */
25 | void *remove(int index);
26 | private:
27 | /**
28 | * Pointer to the next item.
29 | */
30 | List *m_pNext;
31 | /**
32 | * Pointer to the previous item.
33 | */
34 | List *m_pPrev;
35 | /**
36 | * The data contained in this list item.
37 | */
38 | void *m_pData;
39 | };
40 | #endif
41 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # ESPLibs
2 | Libraries and utilities for working with the ESP8266.
3 |
4 | ##See also
5 | * [Kolban's book on the ESP8266](http://neilkolban.com/tech/esp8266/)
6 |
--------------------------------------------------------------------------------
/SDKLibs/CommonSDK/CommonSDK.h:
--------------------------------------------------------------------------------
1 | #ifndef INCLUDE_COMMON_H_
2 | #define INCLUDE_COMMON_H_
3 | #include
4 | #include
5 | #include
6 | #include
7 | const char *authModeToString(AUTH_MODE mode);
8 | void checkError(sint8 err);
9 | void delayMilliseconds(uint32 milliseconds);
10 | void dumpBSSINFO(struct bss_info *bssInfo);
11 | void dumpEspConn(struct espconn *pEspConn);
12 | void dumpRestart();
13 | void dumpState();
14 | const char *errorToString(sint8 err);
15 | void eventLogger(System_Event_t *event);
16 | const char *eventReasonToString(int reason);
17 | const char *flashSizeAndMapToString(enum flash_size_map sizeMap);
18 | const char *opModeToString(int opMode);
19 | void setAsGpio(uint8 pin);
20 | void setupBlink(uint8 blinkPin);
21 | uint8 *toHex(uint8 *ptr, int size, uint8 *buffer);
22 | #endif /* INCLUDE_COMMON_H_ */
23 |
--------------------------------------------------------------------------------
/SDKLibs/CommonSDK/commonSDK.c:
--------------------------------------------------------------------------------
1 | /*
2 | * CommonSDK.c
3 | *
4 | * Created on: Jul 6, 2015
5 | * Author: kolban
6 | */
7 | #include
8 | #include "ESP_Log.h"
9 |
10 | extern "C" {
11 | #include
12 | #include
13 | #include
14 | #include
15 | #include
16 | #include
17 | #include
18 | #include "CommonSDK.h"
19 |
20 | void ets_delay_us(unsigned int del);
21 |
22 | #define DELAY 1000
23 | LOCAL os_timer_t blink_timer;
24 | LOCAL uint8_t led_state=0;
25 | LOCAL int _blinkPin;
26 | LOCAL int pinMapping[] = {
27 | PERIPHS_IO_MUX_GPIO0_U, // GPIO0
28 | PERIPHS_IO_MUX_U0TXD_U, // GPIO1
29 | PERIPHS_IO_MUX_GPIO2_U, // GPIO2
30 | PERIPHS_IO_MUX_U0RXD_U, // GPIO3
31 | PERIPHS_IO_MUX_GPIO4_U, // GPIO4
32 | PERIPHS_IO_MUX_GPIO5_U, // GPIO5
33 | PERIPHS_IO_MUX_SD_CLK_U, // GPIO6
34 | PERIPHS_IO_MUX_SD_DATA0_U, // GPIO7
35 | PERIPHS_IO_MUX_SD_DATA1_U, // GPIO8
36 | PERIPHS_IO_MUX_SD_DATA2_U, // GPIO9
37 | PERIPHS_IO_MUX_SD_DATA3_U, // GPIO10
38 | PERIPHS_IO_MUX_SD_CMD_U, // GPIO11
39 | PERIPHS_IO_MUX_MTDI_U, // GPIO12
40 | PERIPHS_IO_MUX_MTCK_U, // GPIO13
41 | PERIPHS_IO_MUX_MTMS_U, // GPIO14
42 | PERIPHS_IO_MUX_MTDO_U // GPIO15
43 | };
44 |
45 | LOCAL int funcMapping[] = {
46 | FUNC_GPIO0,
47 | FUNC_GPIO1,
48 | FUNC_GPIO2,
49 | FUNC_GPIO3,
50 | FUNC_GPIO4,
51 | FUNC_GPIO5,
52 | -1,
53 | -1,
54 | -1,
55 | FUNC_GPIO9,
56 | FUNC_GPIO10,
57 | -1,
58 | FUNC_GPIO12,
59 | FUNC_GPIO13,
60 | FUNC_GPIO14,
61 | FUNC_GPIO15
62 | };
63 |
64 | void setAsGpio(uint8 pin) {
65 | if (pin < 0 || pin > 15) {
66 | os_printf("bad pin: %d\n", pin);
67 | return;
68 | }
69 | PIN_FUNC_SELECT(pinMapping[pin], funcMapping[pin]);
70 | } // End of setAsGpio
71 |
72 | LOCAL void ICACHE_FLASH_ATTR blink_cb(void *arg)
73 | {
74 | GPIO_OUTPUT_SET(_blinkPin, led_state);
75 | led_state = !led_state;
76 | }
77 |
78 | const char *stringFromIP(uint8_t ip[4]) {
79 | static char str[20];
80 | sprintf(str, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
81 | return str;
82 | }
83 |
84 | const char *eventReasonToString(int reason) {
85 | switch(reason) {
86 | case REASON_UNSPECIFIED:
87 | return "REASON_UNSPECIFIED";
88 | case REASON_AUTH_EXPIRE:
89 | return "REASON_AUTH_EXPIRE";
90 | case REASON_AUTH_LEAVE:
91 | return "REASON_AUTH_LEAVE";
92 | case REASON_ASSOC_EXPIRE:
93 | return "REASON_ASSOC_EXPIRE";
94 | case REASON_ASSOC_TOOMANY:
95 | return "REASON_ASSOC_TOOMANY";
96 | case REASON_NOT_AUTHED:
97 | return "REASON_NOT_AUTHED";
98 | case REASON_NOT_ASSOCED:
99 | return "REASON_NOT_ASSOCED";
100 | case REASON_ASSOC_LEAVE:
101 | return "REASON_ASSOC_LEAVE";
102 | case REASON_ASSOC_NOT_AUTHED:
103 | return "REASON_ASSOC_NOT_AUTHED";
104 | case REASON_DISASSOC_PWRCAP_BAD:
105 | return "REASON_DISASSOC_PWRCAP_BAD";
106 | case REASON_DISASSOC_SUPCHAN_BAD:
107 | return "REASON_DISASSOC_SUPCHAN_BAD";
108 | case REASON_IE_INVALID:
109 | return "REASON_IE_INVALID";
110 | case REASON_MIC_FAILURE:
111 | return "REASON_MIC_FAILURE";
112 | case REASON_4WAY_HANDSHAKE_TIMEOUT:
113 | return "REASON_4WAY_HANDSHAKE_TIMEOUT";
114 | case REASON_GROUP_KEY_UPDATE_TIMEOUT:
115 | return "REASON_GROUP_KEY_UPDATE_TIMEOUT";
116 | case REASON_IE_IN_4WAY_DIFFERS:
117 | return "REASON_IE_IN_4WAY_DIFFERS";
118 | case REASON_GROUP_CIPHER_INVALID:
119 | return "REASON_GROUP_CIPHER_INVALID";
120 | case REASON_PAIRWISE_CIPHER_INVALID:
121 | return "REASON_PAIRWISE_CIPHER_INVALID";
122 | case REASON_AKMP_INVALID:
123 | return "REASON_AKMP_INVALID";
124 | case REASON_UNSUPP_RSN_IE_VERSION:
125 | return "REASON_UNSUPP_RSN_IE_VERSION";
126 | case REASON_INVALID_RSN_IE_CAP:
127 | return "REASON_INVALID_RSN_IE_CAP";
128 | case REASON_802_1X_AUTH_FAILED:
129 | return "REASON_802_1X_AUTH_FAILED";
130 | case REASON_CIPHER_SUITE_REJECTED:
131 | return "REASON_CIPHER_SUITE_REJECTED";
132 | case REASON_BEACON_TIMEOUT:
133 | return "REASON_BEACON_TIMEOUT";
134 | case REASON_NO_AP_FOUND:
135 | return "REASON_NO_AP_FOUND";
136 | default:
137 | return "*** Unknown reason ***";
138 | }
139 | }
140 |
141 | void eventLogger(System_Event_t *event) {
142 | switch(event->event) {
143 | case EVENT_STAMODE_CONNECTED:
144 | os_printf("Event: EVENT_STAMODE_CONNECTED\n");
145 | break;
146 | case EVENT_STAMODE_DISCONNECTED:
147 | os_printf("Event: EVENT_STAMODE_DISCONNECTED - reason: %d (%s)\n", event->event_info.disconnected.reason, eventReasonToString(event->event_info.disconnected.reason));
148 | break;
149 | case EVENT_STAMODE_AUTHMODE_CHANGE:
150 | os_printf("Event: EVENT_STAMODE_AUTHMODE_CHANGE\n");
151 | break;
152 | case EVENT_STAMODE_GOT_IP:
153 | os_printf("Event: EVENT_STAMODE_GOT_IP\n");
154 | break;
155 | case EVENT_SOFTAPMODE_STACONNECTED:
156 | os_printf("Event: EVENT_SOFTAPMODE_STACONNECTED - channel: %d\n", event->event_info.connected.channel);
157 | break;
158 | case EVENT_SOFTAPMODE_STADISCONNECTED:
159 | os_printf("Event: EVENT_SOFTAPMODE_STADISCONNECTED\n");
160 | break;
161 | default:
162 | os_printf("Unexpected event: %d\n", event->event);
163 | break;
164 | }
165 | }
166 |
167 | void setupBlink(uint8 blinkPin) {
168 | _blinkPin = blinkPin;
169 | setAsGpio(blinkPin);
170 | os_timer_disarm(&blink_timer);
171 | os_timer_setfn(&blink_timer, (os_timer_func_t *)blink_cb, (void *)0);
172 | os_timer_arm(&blink_timer, DELAY, 1);
173 | } // End of setupBlink
174 |
175 | void dumpRestart() {
176 | struct rst_info *rstInfo = system_get_rst_info();
177 | os_printf("Restart info:\n");
178 | os_printf(" reason: %d\n", rstInfo->reason);
179 | os_printf(" exccause: %x\n", rstInfo->exccause);
180 | os_printf(" epc1: %x\n", rstInfo->epc1);
181 | os_printf(" epc2: %x\n", rstInfo->epc2);
182 | os_printf(" epc3: %x\n", rstInfo->epc3);
183 | os_printf(" excvaddr: %x\n", rstInfo->excvaddr);
184 | os_printf(" depc: %x\n", rstInfo->depc);
185 | } // End of dump_restart
186 |
187 | const char *flashSizeAndMapToString(enum flash_size_map sizeMap) {
188 | switch(sizeMap) {
189 | case FLASH_SIZE_4M_MAP_256_256:
190 | return "Size: 4M, Map: 256 256";
191 | case FLASH_SIZE_2M:
192 | return "Size: 2M";
193 | case FLASH_SIZE_8M_MAP_512_512:
194 | return "Size: 8M, Map: 512 512";
195 | case FLASH_SIZE_16M_MAP_512_512:
196 | return "Size: 16M, Map: 512 512";
197 | case FLASH_SIZE_32M_MAP_512_512:
198 | return "Size: 32M, Map: 512 512";
199 | case FLASH_SIZE_16M_MAP_1024_1024:
200 | return "Size: 16M, Map: 1024 1024";
201 | case FLASH_SIZE_32M_MAP_1024_1024:
202 | return "Size: 32M, Map: 1024 1024";
203 | }
204 | return "Unknown size and map";
205 | } // End of flashSizeAndMapToString
206 |
207 | const char *opModeToString(int opMode) {
208 | switch(opMode) {
209 | case STATION_MODE:
210 | return "STATION_MODE";
211 | case SOFTAP_MODE:
212 | return "SOFTAP_MODE";
213 | case STATIONAP_MODE:
214 | return "STATIONAP_MODE";
215 | default:
216 | return "*** Unknown Mode ***";
217 | }
218 | } // End of opModeToString
219 |
220 | void dumpState() {
221 | uint8 opMode = wifi_get_opmode();
222 | os_printf("Op Mode: %s\n", opModeToString(opMode));
223 | struct station_config stationConfig;
224 | wifi_station_get_config(&stationConfig);
225 | os_printf("Current Station Config\r\n");
226 | os_printf("SSID: %s, password: %s\r\n",
227 | stationConfig.ssid,
228 | stationConfig.password);
229 | struct ip_info info;
230 | wifi_get_ip_info(SOFTAP_IF, &info);
231 | //os_printf("AP: ip: " IPSTR ", gw: " IPSTR "\n", IP2STR(&info.ip), IP2STR(&info.gw));
232 | wifi_get_ip_info(STATION_IF, &info);
233 | //os_printf("STA: ip: " IPSTR ", gw: " IPSTR "\n", IP2STR(&info.ip), IP2STR(&info.gw));
234 | uint8 macaddr[6];
235 | wifi_get_macaddr(SOFTAP_IF, macaddr);
236 | os_printf("AP MAC Addr: " MACSTR "\n", MAC2STR(macaddr));
237 | wifi_get_macaddr(STATION_IF, macaddr);
238 | os_printf("STA MAC Addr: " MACSTR "\n", MAC2STR(macaddr));
239 | os_printf("Free heap size: %d bytes\n", system_get_free_heap_size());
240 | os_printf("Boot version: %d\n", system_get_boot_version());
241 | os_printf("Boot mode: %d\n", system_get_boot_mode());
242 | os_printf("Chip Id: %x\n", system_get_chip_id());
243 | os_printf("UserBin addr: %x\n", system_get_userbin_addr());
244 | os_printf("CPU frequency: %dMHz\n", system_get_cpu_freq());
245 | os_printf("Flash size map: %s\n", flashSizeAndMapToString(system_get_flash_size_map()));
246 | os_printf("SDK version: %s\n", system_get_sdk_version());
247 | system_print_meminfo();
248 | } // End of dumpState
249 |
250 | const char *authModeToString(AUTH_MODE mode) {
251 | switch (mode) {
252 | case AUTH_OPEN:
253 | return "OPEN";
254 | case AUTH_WEP:
255 | return "WEP";
256 | case AUTH_WPA_PSK:
257 | return "WPA PSK";
258 | case AUTH_WPA2_PSK:
259 | return "WPA2 PSK";
260 | case AUTH_WPA_WPA2_PSK:
261 | return "WPA/WPA2 PSK";
262 | case AUTH_MAX:
263 | break;
264 | }
265 | return "Unknown auth mode!!";
266 | } // End of authModeToString
267 |
268 | const char *errorToString(sint8 err) {
269 | switch(err) {
270 | case ESPCONN_MEM:
271 | return "ESPCONN_MEM";
272 | case ESPCONN_TIMEOUT:
273 | return "ESPCONN_TIMEOUT";
274 | case ESPCONN_RTE:
275 | return "ESPCONN_RTE";
276 | case ESPCONN_INPROGRESS:
277 | return "ESPCONN_INPROGRESS";
278 | case ESPCONN_ABRT:
279 | return "ESPCONN_ABRT";
280 | case ESPCONN_RST:
281 | return "ESPCONN_RST";
282 | case ESPCONN_CLSD:
283 | return "ESPCONN_CLSD";
284 | case ESPCONN_CONN:
285 | return "ESPCONN_CONN";
286 | case ESPCONN_ARG:
287 | return "ESPCONN_ARG";
288 | case ESPCONN_ISCONN:
289 | return "ESPCONN_ISCONN";
290 | case ESPCONN_HANDSHAKE:
291 | return "ESPCONN_HANDSHAKE";
292 | case ESPCONN_PROTO_MSG:
293 | return "ESPCONN_PROTO_MSG";
294 | }
295 | return "Unknown error";
296 | } // End of errorToString
297 |
298 | void scanCB(void *arg, STATUS status) {
299 | if (status == OK) {
300 | struct bss_info *bssInfo = (struct bss_info *) arg;
301 | bssInfo = STAILQ_NEXT(bssInfo, next);
302 | while (bssInfo != NULL) {
303 | dumpBSSINFO(bssInfo);
304 | bssInfo = STAILQ_NEXT(bssInfo, next);
305 | }
306 | } else {
307 | os_printf("Scan CB reported error: %d\n", status);
308 | }
309 | } // End of scanCB
310 |
311 | void dumpBSSINFO(struct bss_info *bssInfo) {
312 | uint8 bssidString[13];
313 | os_printf("ssid: %s, bssid: %s, rssi: %d, authMode: %s\n",
314 | bssInfo->ssid,
315 | toHex(bssInfo->bssid, 6, bssidString),
316 | bssInfo->rssi,
317 | authModeToString(bssInfo->authmode));
318 | }
319 |
320 | void dumpHex(uint8 *data, unsigned int length) {
321 | uint32_t i;
322 | for (i=0; itype) {
333 | case ESPCONN_TCP:
334 | LOG("type = TCP\n");
335 | LOG(" - local_port = %d\n", pEspConn->proto.tcp->local_port);
336 | LOG(" - local_ip = %s\n", stringFromIP(pEspConn->proto.tcp->local_ip));
337 | LOG(" - remote_port = %d\n", pEspConn->proto.tcp->remote_port);
338 | LOG(" - remote_ip = %s\n", stringFromIP(pEspConn->proto.tcp->remote_ip));
339 | break;
340 | case ESPCONN_UDP:
341 | os_printf("type = UDP\n");
342 | os_printf(" - local_port = %d\n", pEspConn->proto.udp->local_port);
343 | os_printf(" - local_ip = %s\n", stringFromIP(pEspConn->proto.udp->local_ip));
344 | os_printf(" - remote_port = %d\n", pEspConn->proto.udp->remote_port);
345 | os_printf(" - remote_ip = %s\n", stringFromIP(pEspConn->proto.udp->remote_ip));
346 | break;
347 | default:
348 | LOG("type = Unknown!! 0x%x\n", pEspConn->type);
349 | }
350 | switch(pEspConn->state) {
351 | case ESPCONN_NONE:
352 | LOG("state = NONE\n");
353 | break;
354 | case ESPCONN_WAIT:
355 | LOG("state = WAIT\n");
356 | break;
357 | case ESPCONN_LISTEN:
358 | LOG("state = LISTEN\n");
359 | break;
360 | case ESPCONN_CONNECT:
361 | LOG("state = CONNECT\n");
362 | break;
363 | case ESPCONN_WRITE:
364 | LOG("state = WRITE\n");
365 | break;
366 | case ESPCONN_READ:
367 | LOG("state = READ\n");
368 | break;
369 | case ESPCONN_CLOSE:
370 | LOG("state = CLOSE\n");
371 | break;
372 | default:
373 | LOG("state = unknown!!\n");
374 | break;
375 | }
376 | LOG("link_cnt = %d\n", pEspConn->link_cnt);
377 | LOG("reverse = %x\n", (unsigned int)pEspConn->reverse);
378 | } // End of dumpEspConn
379 |
380 | void delayMilliseconds(uint32 milliseconds) {
381 | while(milliseconds > 0) {
382 | os_delay_us(1000);
383 | milliseconds--;
384 | }
385 | } // End of delayMilliseconds
386 |
387 | void checkError(sint8 err) {
388 | if (err == ESPCONN_OK ) {
389 | return;
390 | }
391 | os_printf("Error: %s\n", errorToString(err));
392 | } // End of checkError
393 |
394 | LOCAL char nibbleToHex(int n) {
395 | if (n < 10) {
396 | return '0' + n;
397 | }
398 | return 'A' + n - 10;
399 | } // End of nibbleToHex
400 |
401 | /**
402 | * Format an array of bytes pointed to by 'ptr' that is 'size' bytes long.
403 | * Convert them to hex characters and store them in buffer. Buffer will then
404 | * be NULL terminated. Buffer should be at least 2 * size + 1 bytes in length.
405 | * Return a pointer to the start of the buffer.
406 | */
407 | uint8 *toHex(uint8 *ptr, int size, uint8 *buffer) {
408 | uint8 *output = buffer;
409 | int i;
410 | for (i=0; i> 4);
412 | output++;
413 | *output = nibbleToHex((*ptr) & 0x0f);
414 | output++;
415 | ptr++;
416 | }
417 | *output = 0;
418 | return buffer;
419 | }
420 | }
421 | // End of file
422 |
--------------------------------------------------------------------------------
/SDKLibs/CommonSDK/espmissingincludes.h:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of Espruino, a JavaScript interpreter for Microcontrollers
3 | *
4 | * Copyright (C) 2015 Gordon Williams
5 | *
6 | * This Source Code Form is subject to the terms of the Mozilla Public
7 | * License, v. 2.0. If a copy of the MPL was not distributed with this
8 | * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 | *
10 | * ----------------------------------------------------------------------------
11 | * This file is designed to be parsed during the build process
12 | *
13 | * Contains ESP8266 board specific functions.
14 | * ----------------------------------------------------------------------------
15 | */
16 | #ifndef ESPMISSINGINCLUDES_H
17 | #define ESPMISSINGINCLUDES_H
18 |
19 | #include
20 | #include
21 |
22 | // The linking is arranged to put all functions into IROM, so we need a special define to put
23 | // a function into IRAM
24 | #define ICACHE_RAM_ATTR __attribute__((section(".iram1.text")))
25 |
26 | //Missing function prototypes in include folders. Gcc will warn on these if we don't define 'em anywhere.
27 | //MOST OF THESE ARE GUESSED! but they seem to work and shut up the compiler.
28 | typedef struct espconn espconn;
29 |
30 | bool wifi_station_set_hostname(char *);
31 | char *wifi_station_get_hostname(void);
32 |
33 | int atoi(const char *nptr);
34 |
35 | void ets_install_putc1(void *routine); // necessary for #define os_xxx -> ets_xxx
36 | void ets_isr_attach(int intr, void *handler, void *arg);
37 | void ets_isr_mask(unsigned intr);
38 | void ets_isr_unmask(unsigned intr);
39 | void ets_intr_lock(void);
40 | void ets_intr_unlock(void);
41 | void NmiTimSetFunc(void (*func)(void));
42 |
43 | int ets_memcmp(const void *s1, const void *s2, size_t n);
44 | void *ets_memcpy(void *dest, const void *src, size_t n);
45 | void *ets_memmove(void *dest, const void *src, size_t n);
46 | void *ets_memset(void *s, int c, size_t n);
47 | int ets_sprintf(char *str, const char *format, ...) __attribute__ ((format (printf, 2, 3)));
48 | int ets_str2macaddr(void *, void *);
49 | int ets_strcmp(const char *s1, const char *s2);
50 | char *ets_strcpy(char *dest, const char *src);
51 | size_t ets_strlen(const char *s);
52 | int ets_strncmp(const char *s1, const char *s2, int len);
53 | char *ets_strncpy(char *dest, const char *src, size_t n);
54 | char *ets_strstr(const char *haystack, const char *needle);
55 |
56 | void ets_timer_arm_new(ETSTimer *a, int b, int c, int isMstimer);
57 | void ets_timer_disarm(ETSTimer *a);
58 | void ets_timer_setfn(ETSTimer *t, ETSTimerFunc *fn, void *parg);
59 |
60 | void ets_update_cpu_frequency(int freqmhz);
61 |
62 | int os_snprintf(char *str, size_t size, const char *format, ...) __attribute__((format(printf, 3, 4)));
63 | int os_printf_plus(const char *format, ...) __attribute__((format(printf, 1, 2)));
64 |
65 | // memory allocation functions are "different" due to memory debugging functionality
66 | // added in SDK 1.4.0
67 | #ifndef ESPSDK_1_3_0
68 | void vPortFree(void *ptr, char * file, int line);
69 | void *pvPortMalloc(size_t xWantedSize, char * file, int line);
70 | void *pvPortZalloc(size_t, char * file, int line);
71 | void *vPortMalloc(size_t xWantedSize);
72 | void pvPortFree(void *ptr);
73 | void *pvPortRealloc(void *pv, size_t size, char * file, int line);
74 | #else
75 | void vPortFree(void *ptr);
76 | void *pvPortMalloc(size_t xWantedSize);
77 | void *pvPortZalloc(size_t);
78 | void *vPortMalloc(size_t xWantedSize);
79 | void pvPortFree(void *ptr);
80 | void *pvPortRealloc(void *pv, size_t size);
81 | #define os_realloc pvPortRealloc
82 | void *pvPortRealloc(void* ptr, size_t size);
83 | #endif
84 |
85 | void uart_div_modify(int no, unsigned int freq);
86 | uint32 system_get_time();
87 | int rand(void);
88 | void ets_bzero(void *s, size_t n);
89 | void ets_delay_us(int ms);
90 |
91 | // disappeared in SDK 1.1.0:
92 | #define os_timer_done ets_timer_done
93 | #define os_timer_handler_isr ets_timer_handler_isr
94 | #define os_timer_init ets_timer_init
95 |
96 | // This is not missing in SDK 1.1.0 but causes a parens error
97 | #undef PIN_FUNC_SELECT
98 | #define PIN_FUNC_SELECT(PIN_NAME, FUNC) do { \
99 | WRITE_PERI_REG(PIN_NAME, \
100 | (READ_PERI_REG(PIN_NAME) & ~(PERIPHS_IO_MUX_FUNC<
2 | #include "user_interface.h"
3 |
4 | #include "espmissingincludes.h"
5 | #include "startListening.h"
6 |
7 | static void (*g_callback)(struct espconn *pEspconn);
8 | static struct espconn conn1;
9 | static esp_tcp tcp1;
10 |
11 | static void connectCB(void *arg) {
12 | g_callback((struct espconn *)arg);
13 | }
14 |
15 | void startListening(uint16 port, void (*callback)(struct espconn *pEspconn)) {
16 | g_callback = callback;
17 | conn1.proto.tcp = &tcp1;
18 | conn1.type = ESPCONN_TCP;
19 | conn1.state = ESPCONN_NONE;
20 | tcp1.local_port = port;
21 | espconn_regist_connectcb(&conn1, connectCB);
22 | sint8 rc = espconn_accept(&conn1);
23 | if (rc != 0) {
24 | os_printf("Error from espconn_accept: %d\n", rc);
25 | return;
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/SDKLibs/CommonSDK/startListening.h:
--------------------------------------------------------------------------------
1 | /*
2 | * startListening.h
3 | *
4 | * Created on: Nov 16, 2015
5 | * Author: kolban
6 | */
7 |
8 | #ifndef STARTLISTENING_H_
9 | #define STARTLISTENING_H_
10 |
11 | #include "espconn.h"
12 | void startListening(uint16 port, void (*callback)(struct espconn *pEspconn));
13 |
14 | #endif /* STARTLISTENING_H_ */
15 |
--------------------------------------------------------------------------------
/SDKLibs/CommonSDK/wificonnect.c:
--------------------------------------------------------------------------------
1 | /*
2 | * wificonnect.c
3 | *
4 | * Created on: Nov 16, 2015
5 | * Author: kolban
6 | */
7 |
8 | #include
9 | #include "user_interface.h"
10 | #include "espmissingincludes.h"
11 | #include "wificonnect.h"
12 |
13 | static void (*g_callback)();
14 |
15 | static void eventHandler(System_Event_t *event) {
16 | switch(event->event) {
17 | case EVENT_STAMODE_CONNECTED:
18 | os_printf("Event: EVENT_STAMODE_CONNECTED\n");
19 | break;
20 | case EVENT_STAMODE_DISCONNECTED:
21 | os_printf("Event: EVENT_STAMODE_DISCONNECTED\n");
22 | break;
23 | case EVENT_STAMODE_AUTHMODE_CHANGE:
24 | os_printf("Event: EVENT_STAMODE_AUTHMODE_CHANGE\n");
25 | break;
26 | case EVENT_STAMODE_GOT_IP:
27 | os_printf("Event: EVENT_STAMODE_GOT_IP\n");
28 | if (g_callback != NULL) {
29 | g_callback();
30 | }
31 | break;
32 | case EVENT_SOFTAPMODE_STACONNECTED:
33 | os_printf("Event: EVENT_SOFTAPMODE_STACONNECTED\n");
34 | break;
35 | case EVENT_SOFTAPMODE_STADISCONNECTED:
36 | os_printf("Event: EVENT_SOFTAPMODE_STADISCONNECTED\n");
37 | break;
38 | default:
39 | os_printf("Unexpected event: %d\n", event->event);
40 | break;
41 | }
42 | }
43 |
44 | void wifiConnect(char *ssid, char *password, void (*callback)()) {
45 | /*
46 | assert(ssid != NULL);
47 | assert(password != NULL);
48 | assert(callback != NULL);
49 | */
50 |
51 | g_callback = callback;
52 | wifi_set_opmode_current(STATION_MODE);
53 | struct station_config stationConfig;
54 | os_strncpy(stationConfig.ssid, ssid, 32);
55 | os_strncpy(stationConfig.password, password, 64);
56 | wifi_station_set_config(&stationConfig);
57 | wifi_set_event_handler_cb(eventHandler);
58 | wifi_station_connect();
59 | }
60 |
--------------------------------------------------------------------------------
/SDKLibs/CommonSDK/wificonnect.h:
--------------------------------------------------------------------------------
1 | /*
2 | * wificonnect.h
3 | *
4 | * Created on: Nov 16, 2015
5 | * Author: kolban
6 | */
7 |
8 | #ifndef WIFICONNECT_H_
9 | #define WIFICONNECT_H_
10 |
11 | void wifiConnect(char *ssid, char *password, void (*callback)());
12 |
13 | #endif /* WIFICONNECT_H_ */
14 |
--------------------------------------------------------------------------------