├── LICENSE ├── README.md ├── examples ├── Advanced │ ├── AdvancedChatServer │ │ └── AdvancedChatServer.ino │ ├── ConfigurationAP │ │ └── ConfigurationAP.ino │ ├── DeepSleepAndHwReset │ │ └── DeepSleepAndHwReset.ino │ ├── HelloServer │ │ └── HelloServer.ino │ ├── SDWebServer │ │ └── SDWebServer.ino │ ├── TelnetConsole │ │ └── TelnetConsole.ino │ ├── TemporaryWiFiConnect │ │ ├── TemporaryWiFiConnect.ino │ │ └── arduino_secrets.h │ ├── WebServerSSL │ │ └── WebServerSSL.ino │ └── WiFiEspAT2UDP │ │ └── WiFiEspAT2UDP.ino ├── Basic │ ├── ScanNetworksAdvanced │ │ └── ScanNetworksAdvanced.ino │ ├── UdpSendReceiveString │ │ └── UdpSendReceiveString.ino │ ├── UdpSender │ │ └── UdpSender.ino │ ├── WebClient │ │ └── WebClient.ino │ ├── WebServer │ │ └── WebServer.ino │ └── WiFiSSLClient │ │ └── WiFiSSLClient.ino ├── Special │ ├── PagerServer │ │ └── PagerServer.ino │ ├── SNTPTime │ │ └── SNTPTime.ino │ └── WiFiEspAtBlynk │ │ └── WiFiEspAtBlynk.ino └── Tools │ ├── ChangeATBaudRate │ └── ChangeATBaudRate.ino │ ├── CheckFirmware │ └── CheckFirmware.ino │ ├── PrintPersistentSettings │ └── PrintPersistentSettings.ino │ ├── SerialPassthrough │ └── SerialPassthrough.ino │ ├── SetupPersistentAP │ ├── SetupPersistentAP.ino │ └── arduino_secrets.h │ └── SetupPersistentWiFiConnection │ ├── SetupPersistentWiFiConnection.ino │ └── arduino_secrets.h ├── library.properties ├── mkrzero-esp-01s.jpg └── src ├── WiFi.cpp ├── WiFi.h ├── WiFiClient.cpp ├── WiFiClient.h ├── WiFiEspAT.h ├── WiFiEspAtBuffManager.cpp ├── WiFiEspAtBuffManager.h ├── WiFiEspAtBuffStream.cpp ├── WiFiEspAtBuffStream.h ├── WiFiEspAtConfig.h ├── WiFiEspAtSharedBuffStreamPtr.h ├── WiFiSSLClient.cpp ├── WiFiSSLClient.h ├── WiFiServer.cpp ├── WiFiServer.h ├── WiFiUdp.cpp ├── WiFiUdp.h └── utility ├── EspAtDrv.cpp ├── EspAtDrv.h ├── EspAtDrvLogging.cpp ├── EspAtDrvLogging.h └── EspAtDrvTypes.h /LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 2.1, February 1999 3 | 4 | Copyright (C) 1991, 1999 Free Software Foundation, Inc. 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | [This is the first released version of the Lesser GPL. It also counts 10 | as the successor of the GNU Library Public License, version 2, hence 11 | the version number 2.1.] 12 | 13 | Preamble 14 | 15 | The licenses for most software are designed to take away your 16 | freedom to share and change it. By contrast, the GNU General Public 17 | Licenses are intended to guarantee your freedom to share and change 18 | free software--to make sure the software is free for all its users. 19 | 20 | This license, the Lesser General Public License, applies to some 21 | specially designated software packages--typically libraries--of the 22 | Free Software Foundation and other authors who decide to use it. You 23 | can use it too, but we suggest you first think carefully about whether 24 | this license or the ordinary General Public License is the better 25 | strategy to use in any particular case, based on the explanations below. 26 | 27 | When we speak of free software, we are referring to freedom of use, 28 | not price. Our General Public Licenses are designed to make sure that 29 | you have the freedom to distribute copies of free software (and charge 30 | for this service if you wish); that you receive source code or can get 31 | it if you want it; that you can change the software and use pieces of 32 | it in new free programs; and that you are informed that you can do 33 | these things. 34 | 35 | To protect your rights, we need to make restrictions that forbid 36 | distributors to deny you these rights or to ask you to surrender these 37 | rights. These restrictions translate to certain responsibilities for 38 | you if you distribute copies of the library or if you modify it. 39 | 40 | For example, if you distribute copies of the library, whether gratis 41 | or for a fee, you must give the recipients all the rights that we gave 42 | you. You must make sure that they, too, receive or can get the source 43 | code. If you link other code with the library, you must provide 44 | complete object files to the recipients, so that they can relink them 45 | with the library after making changes to the library and recompiling 46 | it. And you must show them these terms so they know their rights. 47 | 48 | We protect your rights with a two-step method: (1) we copyright the 49 | library, and (2) we offer you this license, which gives you legal 50 | permission to copy, distribute and/or modify the library. 51 | 52 | To protect each distributor, we want to make it very clear that 53 | there is no warranty for the free library. Also, if the library is 54 | modified by someone else and passed on, the recipients should know 55 | that what they have is not the original version, so that the original 56 | author's reputation will not be affected by problems that might be 57 | introduced by others. 58 | 59 | Finally, software patents pose a constant threat to the existence of 60 | any free program. We wish to make sure that a company cannot 61 | effectively restrict the users of a free program by obtaining a 62 | restrictive license from a patent holder. Therefore, we insist that 63 | any patent license obtained for a version of the library must be 64 | consistent with the full freedom of use specified in this license. 65 | 66 | Most GNU software, including some libraries, is covered by the 67 | ordinary GNU General Public License. This license, the GNU Lesser 68 | General Public License, applies to certain designated libraries, and 69 | is quite different from the ordinary General Public License. We use 70 | this license for certain libraries in order to permit linking those 71 | libraries into non-free programs. 72 | 73 | When a program is linked with a library, whether statically or using 74 | a shared library, the combination of the two is legally speaking a 75 | combined work, a derivative of the original library. The ordinary 76 | General Public License therefore permits such linking only if the 77 | entire combination fits its criteria of freedom. The Lesser General 78 | Public License permits more lax criteria for linking other code with 79 | the library. 80 | 81 | We call this license the "Lesser" General Public License because it 82 | does Less to protect the user's freedom than the ordinary General 83 | Public License. It also provides other free software developers Less 84 | of an advantage over competing non-free programs. These disadvantages 85 | are the reason we use the ordinary General Public License for many 86 | libraries. However, the Lesser license provides advantages in certain 87 | special circumstances. 88 | 89 | For example, on rare occasions, there may be a special need to 90 | encourage the widest possible use of a certain library, so that it becomes 91 | a de-facto standard. To achieve this, non-free programs must be 92 | allowed to use the library. A more frequent case is that a free 93 | library does the same job as widely used non-free libraries. In this 94 | case, there is little to gain by limiting the free library to free 95 | software only, so we use the Lesser General Public License. 96 | 97 | In other cases, permission to use a particular library in non-free 98 | programs enables a greater number of people to use a large body of 99 | free software. For example, permission to use the GNU C Library in 100 | non-free programs enables many more people to use the whole GNU 101 | operating system, as well as its variant, the GNU/Linux operating 102 | system. 103 | 104 | Although the Lesser General Public License is Less protective of the 105 | users' freedom, it does ensure that the user of a program that is 106 | linked with the Library has the freedom and the wherewithal to run 107 | that program using a modified version of the Library. 108 | 109 | The precise terms and conditions for copying, distribution and 110 | modification follow. Pay close attention to the difference between a 111 | "work based on the library" and a "work that uses the library". The 112 | former contains code derived from the library, whereas the latter must 113 | be combined with the library in order to run. 114 | 115 | GNU LESSER GENERAL PUBLIC LICENSE 116 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 117 | 118 | 0. This License Agreement applies to any software library or other 119 | program which contains a notice placed by the copyright holder or 120 | other authorized party saying it may be distributed under the terms of 121 | this Lesser General Public License (also called "this License"). 122 | Each licensee is addressed as "you". 123 | 124 | A "library" means a collection of software functions and/or data 125 | prepared so as to be conveniently linked with application programs 126 | (which use some of those functions and data) to form executables. 127 | 128 | The "Library", below, refers to any such software library or work 129 | which has been distributed under these terms. A "work based on the 130 | Library" means either the Library or any derivative work under 131 | copyright law: that is to say, a work containing the Library or a 132 | portion of it, either verbatim or with modifications and/or translated 133 | straightforwardly into another language. (Hereinafter, translation is 134 | included without limitation in the term "modification".) 135 | 136 | "Source code" for a work means the preferred form of the work for 137 | making modifications to it. For a library, complete source code means 138 | all the source code for all modules it contains, plus any associated 139 | interface definition files, plus the scripts used to control compilation 140 | and installation of the library. 141 | 142 | Activities other than copying, distribution and modification are not 143 | covered by this License; they are outside its scope. The act of 144 | running a program using the Library is not restricted, and output from 145 | such a program is covered only if its contents constitute a work based 146 | on the Library (independent of the use of the Library in a tool for 147 | writing it). Whether that is true depends on what the Library does 148 | and what the program that uses the Library does. 149 | 150 | 1. You may copy and distribute verbatim copies of the Library's 151 | complete source code as you receive it, in any medium, provided that 152 | you conspicuously and appropriately publish on each copy an 153 | appropriate copyright notice and disclaimer of warranty; keep intact 154 | all the notices that refer to this License and to the absence of any 155 | warranty; and distribute a copy of this License along with the 156 | Library. 157 | 158 | You may charge a fee for the physical act of transferring a copy, 159 | and you may at your option offer warranty protection in exchange for a 160 | fee. 161 | 162 | 2. You may modify your copy or copies of the Library or any portion 163 | of it, thus forming a work based on the Library, and copy and 164 | distribute such modifications or work under the terms of Section 1 165 | above, provided that you also meet all of these conditions: 166 | 167 | a) The modified work must itself be a software library. 168 | 169 | b) You must cause the files modified to carry prominent notices 170 | stating that you changed the files and the date of any change. 171 | 172 | c) You must cause the whole of the work to be licensed at no 173 | charge to all third parties under the terms of this License. 174 | 175 | d) If a facility in the modified Library refers to a function or a 176 | table of data to be supplied by an application program that uses 177 | the facility, other than as an argument passed when the facility 178 | is invoked, then you must make a good faith effort to ensure that, 179 | in the event an application does not supply such function or 180 | table, the facility still operates, and performs whatever part of 181 | its purpose remains meaningful. 182 | 183 | (For example, a function in a library to compute square roots has 184 | a purpose that is entirely well-defined independent of the 185 | application. Therefore, Subsection 2d requires that any 186 | application-supplied function or table used by this function must 187 | be optional: if the application does not supply it, the square 188 | root function must still compute square roots.) 189 | 190 | These requirements apply to the modified work as a whole. If 191 | identifiable sections of that work are not derived from the Library, 192 | and can be reasonably considered independent and separate works in 193 | themselves, then this License, and its terms, do not apply to those 194 | sections when you distribute them as separate works. But when you 195 | distribute the same sections as part of a whole which is a work based 196 | on the Library, the distribution of the whole must be on the terms of 197 | this License, whose permissions for other licensees extend to the 198 | entire whole, and thus to each and every part regardless of who wrote 199 | it. 200 | 201 | Thus, it is not the intent of this section to claim rights or contest 202 | your rights to work written entirely by you; rather, the intent is to 203 | exercise the right to control the distribution of derivative or 204 | collective works based on the Library. 205 | 206 | In addition, mere aggregation of another work not based on the Library 207 | with the Library (or with a work based on the Library) on a volume of 208 | a storage or distribution medium does not bring the other work under 209 | the scope of this License. 210 | 211 | 3. You may opt to apply the terms of the ordinary GNU General Public 212 | License instead of this License to a given copy of the Library. To do 213 | this, you must alter all the notices that refer to this License, so 214 | that they refer to the ordinary GNU General Public License, version 2, 215 | instead of to this License. (If a newer version than version 2 of the 216 | ordinary GNU General Public License has appeared, then you can specify 217 | that version instead if you wish.) Do not make any other change in 218 | these notices. 219 | 220 | Once this change is made in a given copy, it is irreversible for 221 | that copy, so the ordinary GNU General Public License applies to all 222 | subsequent copies and derivative works made from that copy. 223 | 224 | This option is useful when you wish to copy part of the code of 225 | the Library into a program that is not a library. 226 | 227 | 4. You may copy and distribute the Library (or a portion or 228 | derivative of it, under Section 2) in object code or executable form 229 | under the terms of Sections 1 and 2 above provided that you accompany 230 | it with the complete corresponding machine-readable source code, which 231 | must be distributed under the terms of Sections 1 and 2 above on a 232 | medium customarily used for software interchange. 233 | 234 | If distribution of object code is made by offering access to copy 235 | from a designated place, then offering equivalent access to copy the 236 | source code from the same place satisfies the requirement to 237 | distribute the source code, even though third parties are not 238 | compelled to copy the source along with the object code. 239 | 240 | 5. A program that contains no derivative of any portion of the 241 | Library, but is designed to work with the Library by being compiled or 242 | linked with it, is called a "work that uses the Library". Such a 243 | work, in isolation, is not a derivative work of the Library, and 244 | therefore falls outside the scope of this License. 245 | 246 | However, linking a "work that uses the Library" with the Library 247 | creates an executable that is a derivative of the Library (because it 248 | contains portions of the Library), rather than a "work that uses the 249 | library". The executable is therefore covered by this License. 250 | Section 6 states terms for distribution of such executables. 251 | 252 | When a "work that uses the Library" uses material from a header file 253 | that is part of the Library, the object code for the work may be a 254 | derivative work of the Library even though the source code is not. 255 | Whether this is true is especially significant if the work can be 256 | linked without the Library, or if the work is itself a library. The 257 | threshold for this to be true is not precisely defined by law. 258 | 259 | If such an object file uses only numerical parameters, data 260 | structure layouts and accessors, and small macros and small inline 261 | functions (ten lines or less in length), then the use of the object 262 | file is unrestricted, regardless of whether it is legally a derivative 263 | work. (Executables containing this object code plus portions of the 264 | Library will still fall under Section 6.) 265 | 266 | Otherwise, if the work is a derivative of the Library, you may 267 | distribute the object code for the work under the terms of Section 6. 268 | Any executables containing that work also fall under Section 6, 269 | whether or not they are linked directly with the Library itself. 270 | 271 | 6. As an exception to the Sections above, you may also combine or 272 | link a "work that uses the Library" with the Library to produce a 273 | work containing portions of the Library, and distribute that work 274 | under terms of your choice, provided that the terms permit 275 | modification of the work for the customer's own use and reverse 276 | engineering for debugging such modifications. 277 | 278 | You must give prominent notice with each copy of the work that the 279 | Library is used in it and that the Library and its use are covered by 280 | this License. You must supply a copy of this License. If the work 281 | during execution displays copyright notices, you must include the 282 | copyright notice for the Library among them, as well as a reference 283 | directing the user to the copy of this License. Also, you must do one 284 | of these things: 285 | 286 | a) Accompany the work with the complete corresponding 287 | machine-readable source code for the Library including whatever 288 | changes were used in the work (which must be distributed under 289 | Sections 1 and 2 above); and, if the work is an executable linked 290 | with the Library, with the complete machine-readable "work that 291 | uses the Library", as object code and/or source code, so that the 292 | user can modify the Library and then relink to produce a modified 293 | executable containing the modified Library. (It is understood 294 | that the user who changes the contents of definitions files in the 295 | Library will not necessarily be able to recompile the application 296 | to use the modified definitions.) 297 | 298 | b) Use a suitable shared library mechanism for linking with the 299 | Library. A suitable mechanism is one that (1) uses at run time a 300 | copy of the library already present on the user's computer system, 301 | rather than copying library functions into the executable, and (2) 302 | will operate properly with a modified version of the library, if 303 | the user installs one, as long as the modified version is 304 | interface-compatible with the version that the work was made with. 305 | 306 | c) Accompany the work with a written offer, valid for at 307 | least three years, to give the same user the materials 308 | specified in Subsection 6a, above, for a charge no more 309 | than the cost of performing this distribution. 310 | 311 | d) If distribution of the work is made by offering access to copy 312 | from a designated place, offer equivalent access to copy the above 313 | specified materials from the same place. 314 | 315 | e) Verify that the user has already received a copy of these 316 | materials or that you have already sent this user a copy. 317 | 318 | For an executable, the required form of the "work that uses the 319 | Library" must include any data and utility programs needed for 320 | reproducing the executable from it. However, as a special exception, 321 | the materials to be distributed need not include anything that is 322 | normally distributed (in either source or binary form) with the major 323 | components (compiler, kernel, and so on) of the operating system on 324 | which the executable runs, unless that component itself accompanies 325 | the executable. 326 | 327 | It may happen that this requirement contradicts the license 328 | restrictions of other proprietary libraries that do not normally 329 | accompany the operating system. Such a contradiction means you cannot 330 | use both them and the Library together in an executable that you 331 | distribute. 332 | 333 | 7. You may place library facilities that are a work based on the 334 | Library side-by-side in a single library together with other library 335 | facilities not covered by this License, and distribute such a combined 336 | library, provided that the separate distribution of the work based on 337 | the Library and of the other library facilities is otherwise 338 | permitted, and provided that you do these two things: 339 | 340 | a) Accompany the combined library with a copy of the same work 341 | based on the Library, uncombined with any other library 342 | facilities. This must be distributed under the terms of the 343 | Sections above. 344 | 345 | b) Give prominent notice with the combined library of the fact 346 | that part of it is a work based on the Library, and explaining 347 | where to find the accompanying uncombined form of the same work. 348 | 349 | 8. You may not copy, modify, sublicense, link with, or distribute 350 | the Library except as expressly provided under this License. Any 351 | attempt otherwise to copy, modify, sublicense, link with, or 352 | distribute the Library is void, and will automatically terminate your 353 | rights under this License. However, parties who have received copies, 354 | or rights, from you under this License will not have their licenses 355 | terminated so long as such parties remain in full compliance. 356 | 357 | 9. You are not required to accept this License, since you have not 358 | signed it. However, nothing else grants you permission to modify or 359 | distribute the Library or its derivative works. These actions are 360 | prohibited by law if you do not accept this License. Therefore, by 361 | modifying or distributing the Library (or any work based on the 362 | Library), you indicate your acceptance of this License to do so, and 363 | all its terms and conditions for copying, distributing or modifying 364 | the Library or works based on it. 365 | 366 | 10. Each time you redistribute the Library (or any work based on the 367 | Library), the recipient automatically receives a license from the 368 | original licensor to copy, distribute, link with or modify the Library 369 | subject to these terms and conditions. You may not impose any further 370 | restrictions on the recipients' exercise of the rights granted herein. 371 | You are not responsible for enforcing compliance by third parties with 372 | this License. 373 | 374 | 11. If, as a consequence of a court judgment or allegation of patent 375 | infringement or for any other reason (not limited to patent issues), 376 | conditions are imposed on you (whether by court order, agreement or 377 | otherwise) that contradict the conditions of this License, they do not 378 | excuse you from the conditions of this License. If you cannot 379 | distribute so as to satisfy simultaneously your obligations under this 380 | License and any other pertinent obligations, then as a consequence you 381 | may not distribute the Library at all. For example, if a patent 382 | license would not permit royalty-free redistribution of the Library by 383 | all those who receive copies directly or indirectly through you, then 384 | the only way you could satisfy both it and this License would be to 385 | refrain entirely from distribution of the Library. 386 | 387 | If any portion of this section is held invalid or unenforceable under any 388 | particular circumstance, the balance of the section is intended to apply, 389 | and the section as a whole is intended to apply in other circumstances. 390 | 391 | It is not the purpose of this section to induce you to infringe any 392 | patents or other property right claims or to contest validity of any 393 | such claims; this section has the sole purpose of protecting the 394 | integrity of the free software distribution system which is 395 | implemented by public license practices. Many people have made 396 | generous contributions to the wide range of software distributed 397 | through that system in reliance on consistent application of that 398 | system; it is up to the author/donor to decide if he or she is willing 399 | to distribute software through any other system and a licensee cannot 400 | impose that choice. 401 | 402 | This section is intended to make thoroughly clear what is believed to 403 | be a consequence of the rest of this License. 404 | 405 | 12. If the distribution and/or use of the Library is restricted in 406 | certain countries either by patents or by copyrighted interfaces, the 407 | original copyright holder who places the Library under this License may add 408 | an explicit geographical distribution limitation excluding those countries, 409 | so that distribution is permitted only in or among countries not thus 410 | excluded. In such case, this License incorporates the limitation as if 411 | written in the body of this License. 412 | 413 | 13. The Free Software Foundation may publish revised and/or new 414 | versions of the Lesser General Public License from time to time. 415 | Such new versions will be similar in spirit to the present version, 416 | but may differ in detail to address new problems or concerns. 417 | 418 | Each version is given a distinguishing version number. If the Library 419 | specifies a version number of this License which applies to it and 420 | "any later version", you have the option of following the terms and 421 | conditions either of that version or of any later version published by 422 | the Free Software Foundation. If the Library does not specify a 423 | license version number, you may choose any version ever published by 424 | the Free Software Foundation. 425 | 426 | 14. If you wish to incorporate parts of the Library into other free 427 | programs whose distribution conditions are incompatible with these, 428 | write to the author to ask for permission. For software which is 429 | copyrighted by the Free Software Foundation, write to the Free 430 | Software Foundation; we sometimes make exceptions for this. Our 431 | decision will be guided by the two goals of preserving the free status 432 | of all derivatives of our free software and of promoting the sharing 433 | and reuse of software generally. 434 | 435 | NO WARRANTY 436 | 437 | 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO 438 | WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. 439 | EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR 440 | OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY 441 | KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE 442 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 443 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE 444 | LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME 445 | THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 446 | 447 | 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN 448 | WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY 449 | AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU 450 | FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR 451 | CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE 452 | LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING 453 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A 454 | FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF 455 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 456 | DAMAGES. 457 | 458 | END OF TERMS AND CONDITIONS 459 | 460 | How to Apply These Terms to Your New Libraries 461 | 462 | If you develop a new library, and you want it to be of the greatest 463 | possible use to the public, we recommend making it free software that 464 | everyone can redistribute and change. You can do so by permitting 465 | redistribution under these terms (or, alternatively, under the terms of the 466 | ordinary General Public License). 467 | 468 | To apply these terms, attach the following notices to the library. It is 469 | safest to attach them to the start of each source file to most effectively 470 | convey the exclusion of warranty; and each file should have at least the 471 | "copyright" line and a pointer to where the full notice is found. 472 | 473 | 474 | Copyright (C) 475 | 476 | This library is free software; you can redistribute it and/or 477 | modify it under the terms of the GNU Lesser General Public 478 | License as published by the Free Software Foundation; either 479 | version 2.1 of the License, or (at your option) any later version. 480 | 481 | This library is distributed in the hope that it will be useful, 482 | but WITHOUT ANY WARRANTY; without even the implied warranty of 483 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 484 | Lesser General Public License for more details. 485 | 486 | You should have received a copy of the GNU Lesser General Public 487 | License along with this library; if not, write to the Free Software 488 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 489 | USA 490 | 491 | Also add information on how to contact you by electronic and paper mail. 492 | 493 | You should also get your employer (if you work as a programmer) or your 494 | school, if any, to sign a "copyright disclaimer" for the library, if 495 | necessary. Here is a sample; alter the names: 496 | 497 | Yoyodyne, Inc., hereby disclaims all copyright interest in the 498 | library `Frob' (a library for tweaking knobs) written by James Random 499 | Hacker. 500 | 501 | , 1 April 1990 502 | Ty Coon, President of Vice 503 | 504 | That's all there is to it! 505 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
2 | **Version 2 Upgrade Notes** (click to expand) 3 | 4 | Version 2 has major improvements for WiFiClient: 5 | 6 | * it closes the connection as soon as no WiFiClient object references it (when last copy is cleared or goes out of scope) 7 | * it detects that connection was closed by peer and releases the link. As soon as all data are read by the sketch it makes the internal buffer available for next connection. It is not necessary to call stop() on connection disconnected by the remote side. 8 | * in version 1 an old WiFiClient copy could interfere on a new connection on the same AT link. This is very unlikely to happen in version 2. 9 | 10 | Version 2 has some breaking changes: 11 | 12 | * Version 1 returned MAC address and BSSID in reversed ordering because 5 years ago all Arduino WiFi libraries had it that way. Version 2 returns MAC address and BSSID in normal ordering. 13 | 14 | * Version 2 removes `server.available()` function from the library. The functions is still there but only as alias for `server.accept()`. It will work for most use cases. For proper `server.available()`with help of the NetApiHelpers library see the PagerServer example. 15 | 16 | * Version 1 had optimized UDP classes WiFiUdpSender and WiFiEspAtUDP. These are removed in version 2. WiFiUDP has the functionality of both of them. 17 | 18 | * In version 1 `WiFi.disconnect()` cleared the static IP configuration. In version 2 the static IP configuration is preserved for next `WiFi.begin()`. To return to DHCP clear the static IP configuration with `WiFi.config(INADDR_NONE)`. All Arduino WiFi libraries have it this way. 19 |
20 | 21 | # WiFiEspAT library 22 | 23 | This library creates standard Arduino WiFi networking API over ESP8266 or ESP32 AT commands. The Arduino WiFi networking API was established by Arduino WiFi library and enhanced by Arduino WiFi101 and Arduino WiFiNINA library. 24 | 25 | This library is fast and reliable. It can communicate with AT firmware at high baud rates without flow control, limited only by reliability of UART at chosen speed. 26 | 27 | WiFiEspAT supports wired Ethernet with esp8266 or esp32 with an Ethernet module, AT firmware with Ethernet support and the [EthernetEspAT](https://github.com/Networking-for-Arduino/EthernetEspAT) library. 28 | 29 | The library is for all Arduino MCU architectures. 30 | 31 | ![MKR Zero with esp-01S](mkrzero-esp-01s.jpg) 32 | 33 | ## Contents 34 | 35 | * [Getting started](#getting-started) 36 | * [Why a new WiFiEsp library?](#Why-a-new-wifiesp-library) 37 | * [Limitations](#limitations) 38 | * [AT firmware versions](#at-firmware-versions) 39 | * [Wiring of ESP module](#wiring-of-esp-module) 40 | * [Baud rate for communication with AT firmware](#baud-rate-for-communication-with-AT-firmware) 41 | * [Persistent WiFi connection](#persistent-wifi-connection) 42 | * [Persistent Access Point](#persistent-access-point) 43 | * [Enhanced WiFi API](#enhanced-wifi-api) 44 | * [Logging](#logging) 45 | * [Sketch size optimization](#sketch-size-optimization) 46 | * [Advanced use](#advanced-use) 47 | 48 | ## Getting started 49 | 50 | * Put a supported version of the AT firmware into the ESP you want to use with the WiFiEspAT library. Make sure the firmware is working and returning OK to test command "AT". 51 | 52 | 53 | * Wire the ESP module to Serial1 of your Arduino. Wire RX to TX. If your Arduino doesn't have Serial1, wire the ESP module to pins 6 as RX and 7 as TX for SoftwareSerial. 54 | 55 | 56 | * For AT firmware version 2.4.0 or higher open in the folder of the library the file src/utility/EspAtDrvTypes.h in a text editor and comment out the line `#define WIFIESPAT1` like this `//#define WIFIESPAT1` 57 | 58 | 59 | * If you use SoftwareSerial run the ChangeATBaudRate sketch from WiFiEspAT/Tools section in IDE Examples menu. If the sketch ends with "Communication with WiFi module failed!" check the wiring. 60 | 61 | 62 | * Run the CheckFirmware sketch from WiFiEspAT/Tools section in IDE Examples menu. If the sketch ends with "Communication with WiFi module failed!" check the wiring. 63 | 64 | 65 | * Open the SetupPersistentWiFiConnection sketch from WiFiEspAT/Tools section in IDE Examples menu. Set the SSID and password on arduino_secrets tab. Run the sketch to make a persistent connection to your WiFi network. 66 | 67 | 68 | * Try the Basic examples. Warning: The example sketches are designed to present library functions in a simplest way. They are not examples of a good sketch structure and efficiency. Don't use delays and String in your projects. 69 | 70 | 71 | ## Why a new WiFiEsp library? 72 | 73 | This library uses the passive receive mode implemented in AT firmware 1.7 (Non OS SDK 3) and AT firmware 2.4+ (IDF). The [older WiFiEsp library](https://github.com/bportaluri/WiFiEsp) can't do much with larger data received. Without the passive receive mode, the AT firmware sends all the data at once and the serial RX buffer overflows. It is hard to receive more data over network with AT firmware without UART hardware flow control and Arduino AVR boards don't have flow control and simple esp8266 modules don't have the flow control pins exposed. 74 | 75 | Note: The older WiFiEsp library referenced the AT firmware version by SDK version. This library reports AT commands version. 76 | 77 | 78 | ## Limitations 79 | 80 | The official AT firmwares are limited to one TCP server. 81 | 82 | ### AT 1.7 83 | 84 | AT 1.7 is only for esp8266. 85 | 86 | The passive receive mode of the AT firmware is not supported for UDP and secure connection (SSL). For this reason UDP received message size is limited to configured buffer size and secure connection (SSL, https) is not supported. 87 | 88 | ### AT 2 89 | 90 | The AT 2 has a small problem with UDP messages in passive receive mode. The received message must be read at once so received message size is limited to configured buffer size with this library. 91 | 92 | AT 2 is for esp32. Use at least version 2.4. Versions 2.1 and 2.2 were for esp8266 too, but are not reliable. 93 | 94 | Note to 2.0.0 versions: ESP8266 version can't be used with this library. It doesn't support passive receive mode. In ESP32 AT 2.0.0 version UDP doesn't work in passive receive mode. 95 | 96 | ### Capabilities comparison 97 | 98 | The table focuses on limits of AT firmwares in passive receive mode. 99 | 100 | |Feature|AT 1.7|AT 2.4+|ESP_ATMod(1)| 101 | |---| :---: | :---: | :---: | 102 | |more than one TCP server|✗|✗|✓(2)| 103 | |SSL server|✗|✓|✗| 104 | |TCP client|✓|✓|✓| 105 | |SSL client|✗|✓|✓| 106 | |SSL client TLS 1.2|✗(3)|✓|✓| 107 | |UDP (4)|✓|✓|✗| 108 | |UDP backlog|✗|✓|n/a| 109 | |UDP multicast|✗|✓|n/a| 110 | |SoftAP|✓|✓|✓| 111 | |WPA2 Enterprise|✗|✓|✗| 112 | |epoch time|Lobo|✓|✓| 113 | 114 | * (1) [Jiri Bilek's firmware](https://github.com/JiriBilek/ESP_ATMod#description) 115 | * (2) uncomment `#define WIFIESPAT_MULTISERVER` in src/utility/EspAtDrv.h 116 | * (3) it is possible to use the [SSLClient library](https://github.com/OPEnSLab-OSU/SSLClient) for TLS 1.2 on 32bit MCU 117 | * (4) it is not possible to receive UDP message larger than the configured buffer 118 | 119 | ## AT firmware versions 120 | 121 | You can use the CheckFirmware sketch from examples Tools to check the version of the AT firmware. 122 | 123 | ### AT 1.7 124 | 125 | AT firmware 1.7.x is build on Espressif NonOS SDK 3. 126 | 127 | The Espressif AT binary is built for 2MB-c1 flash partitioning. Flash Download Tool corrects it for 4 MB flash, but for esptool you should use the `--flash_size 2MB-c1` option and the corresponding addresses. 128 | 129 | ``` 130 | esptool.py write_flash --flash_size 2MB-c1 0x0 boot_v1.7.bin 0x01000 at/1024+1024/user1.2048.new.5.bin 0x1fb000 blank.bin 0x1fc000 esp_init_data_default_v08.bin 0xfe000 blank.bin 0x1fe000 blank.bin 131 | ``` 132 | 133 | The AT 1.7.4 version has bin file for flashing to 1 MB flash. 134 | 135 | ``` 136 | esptool.py write_flash --flash_size 1MB 0x0 boot_v1.7.bin 0x01000 at/512+512/user1.1024.new.2.bin 0xfb000 blank.bin 0xfc000 esp_init_data_default_v08.bin 0xfe000 blank.bin 0x7e000 blank.bin 137 | ``` 138 | 139 | For some esp8266 modules you will have to add `--flash_mode dout` before `--flash_size`. 140 | 141 | GitHub user loboris (Boris Lovosevic) builds customized versions of AT firmware with SDK 3 for all flash sizes. You can download the files from his [ESP8266_AT_LoBo GitHub repository](https://github.com/loboris/ESP8266_AT_LoBo). Run his flash.sh utility to flash the correct binary. 142 | 143 | Jiri Bilek created [an alternative AT 1.7 firmware implementation](https://github.com/JiriBilek/ESP_ATMod) over esp8266 Arduino core and WiFi library. This supports SSL TLS1.2 connection in passive mode with this library. This firmware doesn't yet support UDP. 144 | 145 | Resources: 146 | * [the Espressif binaries](https://github.com/espressif/ESP8266_NONOS_SDK/tree/master/bin) - version 1.7.4 in NONOS SDK 3.0.4 147 | * [AT 1.7 reference](https://www.espressif.com/en/support/documents/technical-documents?keys=Non-OS+AT&field_type_tid%5B%5D=14) - it contains firmware flashing instructions too 148 | * [AT LoBo flashing instructions](https://github.com/loboris/ESP8266_AT_LoBo/#flashing). First install esptool.py. If you download the AT LoBo repository as zip, it contains the firmware binaries and a script to flash them. 149 | * [Jiri Bilek's ESP_ATMod](https://github.com/JiriBilek/ESP_ATMod) 150 | 151 | ### AT 2 152 | 153 | This library can work with AT firmware version 2.4 or higher. AT firmware 2 is for ESP32 and is build on Espressif's IDF framework. 154 | 155 | The flashing command of AT2 with esptool is `esptool.py write_flash @download.config`. I recommend to change in download.config file flash_mode to qio if your ESP module supports it and flash frequency to 40MHz if your module doesn't support 80MHz. 156 | 157 | Resources: 158 | * [the Espressif AT2 binaries](https://docs.espressif.com/projects/esp-at/en/latest/AT_Binary_Lists/index.html) 159 | * [the Espressif AT2 releases on GitHub](https://github.com/espressif/esp-at/releases) 160 | 161 | ### Flashing tools 162 | 163 | * [Flash Download Tool](https://www.espressif.com/en/support/download/other-tools?keys=&field_type_tid%5B%5D=14) It is Windows only, but can detect settings of your ESP and apply them to the flashed settings binary 164 | * [esptool.py installation instructions](https://github.com/espressif/esptool#installation--dependencies) 165 | 166 | ## Wiring of the ESP module 167 | 168 | The examples of this library expect the AT firmware's RX/TX wired to hardware Serial1 for boards with Serial1. For AVR boards without Serial1 the examples setup SoftwareSerial on pins 6 (RX) and 7 (TX). Wire RX to TX. 169 | 170 | To use hardware reset instead of software reset, wire selected pin of Arduino to reset pin of the ESP and set the pin as second parameter in WiFi.init(Serial1, ESP_RESET_PIN);. No logic level conversion is required for this connection. The library only sets the pin LOW or floating. 171 | 172 | You can use the SerialPassthrough sketch from WiFiEspAT/Tools in IDE Example menu to bridge the ESP to computer over Arduino with the above wiring. You can then send AT commands to the AT firmware from the Serial Monitor. It is even possible to upload AT firmware if the ESP is on hardware Serial. Only put the ESP in flashing mode and set the upload baud rate to 115200 baud. 173 | 174 | 175 | ## Baud rate for communication with AT firmware 176 | 177 | Over hardware Serial this library works reliably with AT firmware's default 115200 baud. With SoftwareSerial, 9600 baud should be used because of limitations of SoftwareSerial. You can try alternative software Serial libraries with their recommended baud rates. The ChangeBaudRate sketch in examples Tools section can change over SoftwareSerial the default baud rate of AT firmware from 115200 to 9600 baud. 178 | 179 | For hardware serial high baud rates can be used. For Arduino Mega 500000 baud works good. The ChangeBaudRate sketch can be used to change the default baud rate of the AT firmware to a higher one for hardware Serial. For example a WebServer serving web files from SD card requires high baud rate to be able to serve multiple files one after other fast enough. 180 | 181 | 182 | ## Persistent WiFi connection 183 | 184 | The ESP can remember the WiFi network settings to connect automatically to WiFi network after power-up or reset. This library supports this with the SetupWiFiConnection example tool sketch. The sketch uses the WiFi.setPersistent() setting. After the connection is successful, it is remembered by the ESP and set for autoconnect at start. Other sketches don't need to call WiFi.begin(), only wait until WiFi.status() returns WL_CONNECTED. 185 | 186 | Note: The ESP SDK remembers the SSID and password by default and uses them to autoconnect at start. Your ESP may have remembered setting from previous use with any firmware or Arduino sketch. 187 | 188 | Using persistent connections has two benefits. The sketch size is smaller without WiFi.begin() and the connection to network is asynchronous, it happens while other devices are setup in setup(). (Synchronous WiFi.begin() with DHCP waits 5 seconds for the OK from AT firmware.) 189 | 190 | WiFi.disconnect() in 'persistent mode' or WiFi.disconnect(true) clear the remembered connection and disables automatic connection to network. Don't use persistent WiFi.begin(ssid, pass) and persistent WiFi.disconnect() in the same sketch. The settings would be written to flash and then cleared repeatedly, which would lead to faster ESP flash memory wearing. 191 | 192 | With WiFi.setAutoConnect(false) the remembered network settings are not used, but stay remembered. If ESP auto starts the connection, it can collide with WiFi.begin(). Clear the persistent connection with WiFi.disconnect() before using a temporary not persistent connection. 193 | 194 | 195 | ## Persistent Access Point 196 | 197 | This library supports ESP SoftAP control over AT firmware. With WiFi.beginAP() without parameters the SoftAP is started with default settings. Default settings are factory settings or remembered persistent settings. The SetupPeristentAP example tool can be used to setup SoftAP with custom SSID and optionally setup encryption/password and IP address for the SoftAP. 198 | 199 | Use WiFi.endAP(true) to disable start of SoftAP at ESP startup. It will not clear the persistent AP settings. 200 | 201 | 202 | ## Enhanced WiFi API 203 | 204 | This library implements Arduino WiFi networking API. The last version of this API is defined by the [Arduino WiFiNINA library](https://www.arduino.cc/en/Reference/WiFiNINA). The WiFiEspAT library has some differences. 205 | 206 | ### the WiFi object differences 207 | 208 | * `init` command to set the Serial interface used for communication 209 | * `begin` for AT 1.7 begin() without parameters (joining remembered WiFi) is not available 210 | * `beginEnterprise` AT 2 only. to connect to WPA2 Enterprise network (sorry, it is not tested) 211 | * `setPersistent` to set the remembering of the following WiFi connection (see the SetupPersistentWiFiConnection.ino tool example) 212 | * `setAutoConnect` to set the automatic connection to remembered WiFi AP 213 | * `scanNetworks` optionally can be called with array of type `WiFiApData[]` to fill 214 | * `hostname` to get the hostname. can be called with char array to fill (see PrintPersistentSettings.ino tool example) 215 | * `SSID` optionally can be called with char array to fill (see PrintPersistentSettings.ino tool example) 216 | * `channel` getter 217 | * `dhcpIsEnabled` to determine if DHCP or static IP is used (see PrintPersistentSettings.ino tool example) 218 | * `beginAP` can be called without parameters to start the persistent AP (see ConfigurationAP.ino example) 219 | * `endAP` - stops the AP. `endAP(true)` stops the AP and disables start of persistent AP at startup 220 | * `configureAP` - to configure AP. see the SetupPersistentAP.ino tool example 221 | * AP parameters getters - apMacAddress, apSSID, apPassphrase, apEncryptionType, apMaxConnections, apIsHidden, apDhcpIsEnabled, apIP, apGatewayIP, apSubnetMask (see PrintPersistentSettings.ino tool example) 222 | * `startMDNS` to execute AT+MDNS. refer to AT reference for parameters 223 | * `sntp` - to enable and configure SNTP servers. see SNTPTime.ino example 224 | * `reset` - to reset or wake-up the ESP. see DeepSleepAndHwReset.ino example 225 | * `sleepMode`- to set the level of automatic sleep mode. possible modes are WIFI_NONE_SLEEP, WIFI_LIGHT_SLEEP and WIFI_MODEM_SLEEP 226 | * `deepSleep`- to turn-off the ESP. see DeepSleepAndHwReset.ino example 227 | * `ping` doesn't have the ttl parameter and returns only true or false 228 | 229 | ### the WiFiClient class differences 230 | 231 | * `connectSSL` is not supported with standard AT 1 firmware 232 | * `write(file)` variant of write function for efficient sending of SD card file. see SDWebServer.ino example 233 | * `write(callback)` variant of write function for efficient sending with a callback function. see SDWebServer.ino example 234 | * `abort` AT1 only. closes the TCP connection without waiting for the remote side 235 | 236 | ### the WiFiServer class differences 237 | 238 | The standard AT firmwares support only one TCP server. The ESP_ATMod firmware supports multiple servers (uncomment `#define WIFIESPAT_MULTISERVER` in src/utility/EspAtDrv.h). 239 | 240 | * `begin` has optional parameters maxConnCount (default 1) and serverTimeout in seconds (default 60) 241 | * `beginSSL` ESP32 only. starts the server for secure connections. 242 | * `begin(port)` and `beginSSL(port)` and constructor without parameters 243 | * `end` to stop the server and the clients managed by the server for available() 244 | * `accept` like in new [Ethernet library](https://www.arduino.cc/en/Reference/EthernetServerAccept). see the AdvancedChatServer 245 | * available - WiFiEspAT version 2 doesn't implement server.available(). See the PagerServer example on how to use the NetApiHelpers library for a WiFiServer with `available()` 246 | 247 | The WiFiServer class in this library doesn't derive from the Arduino Server class. It doesn't implement the never used 'send to all clients' functionality with Print class methods (print, write). For 'send to all clients' see the PagerServer example. 248 | 249 | ### the UDP differences 250 | 251 | The Arduino UDP API requires to start a listening port to send an UDP message. This library doesn't require udp.begin() before udp.beginPacket(). See the UdpSender.ino example. 252 | 253 | * `beginMulticast` works only with AT2 254 | * `availableForParse` for AT2 only. returns the size of the available message. see the WiFiEspAT2UDP example 255 | * `parsePacket(buffer, size, ip, port)` for AT2 only. to read the message into provided buffer. see the WiFiEspAT2UDP example 256 | * `write(callback)` variant of write function for efficient sending with a callback function 257 | 258 | With AT2 You can receive messages with the `parsePacket(buffer, size, ip, port)` function. This doesn't use internal buffer to receive the message with `parsePacket()` so it saves memory. See the WiFiEspAT2UDP example. 259 | 260 | ## Logging 261 | 262 | The EspAtDrv in center of the library has four logging levels to troubleshoot communication with AT firmware or help with development of new functions. At default logging is off, set to SILENT level. 263 | 264 | Logging level and logging Serial instance, can be changed in src/utils/EspAtDrvLogging.h. Level DEBUG will print all AT commands and responses from AT firmware. Level ERROR will print only errors in communication with AT firmware. Higher logging level result in larger sketch size. 265 | 266 | 267 | ## Sketch size optimization 268 | 269 | For small MCU like the ATmega328p on Uno, the sketch size matters. With modern compiler and linker almost all not used code is removed. So any additional function used in sketch results in larger sketch. Any function called to get information about the WiFi connection only to print a fancy welcome message adds to sketch size. 270 | 271 | With persistent WiFi connection WiFi.begin() and underlying functions are not used, resulting in smaller sketch size. 272 | 273 | Use WiFiUdpSender class if you only send UDP messages. You can use WiFiEspAtUDP with AT2. WiFiUDP always allocates the receive buffer required for AT1 UDP receive without passive mode and for AT2 to fulfill the Arduino UDP API. 274 | 275 | 276 | ## Advanced use 277 | 278 | ### Buffering and buffers 279 | 280 | This library has small buffers in WiFiClient to achieve better performance over UART and over network. The size of the buffers is configured in WiFiEspAtConfig.h file in library's src folder. There is a special set of settings for AVR MCU with small SRAM. For small SRAM, the RX and TX buffer are 32 bytes, for all other it is 64 bytes. 281 | 282 | For UDP to hold the composed message and the received message 64 bytes buffers are used for small AVR MCU and 256 bytes buffers for other MCU. 283 | 284 | It is recommended to use WiFiClient.flush() after completing the output. WiFiClient.stop() and WiFiUdp.endPacket() both call flush(). The library calls flush() if TX buffer is not empty and available() is called. 285 | 286 | The buffers size can be changed in WiFiEspAtConfig.h or set on build command line. The TCP TX buffer can be set to 0 and the RX buffer must be at least 1 (for peek()), but then please use buffers in sketch for example with [StreamLib's](https://github.com/jandrassy/StreamLib) wrapper class BufferedPrint. 287 | 288 | The size of the UDP TX buffer can be set to zero in WiFiEspAtConfig.h if the complete message is sent with one print(msg), one write(msg, length) or with write(callback). Otherwise the size of the UDP buffers limits the size of the message. If the composed message is larger than the buffer it will be send as partial UDP messages. If the size of received message with AT1 is larger than the UDP TX buffer, the message will be dropped (with WiFi.getLastDriverError() set to EspAtDrvError::UDP_LARGE). 289 | 290 | To set different custom sizes of buffers for different boards, you can create a file boards.local.txt next to boards.txt file in hardware package. Set build.extra_flags for individual boards. For example for Mega you can add to boards.local.txt a line with -D options to define the macros. 291 | 292 | mega.build.extra_flags=-DWIFIESPAT_TCP_RX_BUFFER_SIZE=128 -DWIFIESPAT_TCP_TX_BUFFER_SIZE=128 293 | 294 | With WiFiEspAT library the incoming data are buffered at two levels. First level is in the AT firmware. After it received all the data to buffer and closed the connection, the yet unread data are still available to read. Second buffering is in library's BuffStream. Here still can be data available even the firmware and EspAtDrv are done with the link and it can be used for a new connection. 295 | 296 | ### the `write(callback)` function 297 | 298 | While the internal buffering of the library and the use of Nagle's algorithm by AT firmware prevents sending client.prints in many very very small TCP packets, with the write(callback) function all prints executed in the callback function are send to AT firmware with one AT+CIPSENDEX command resulting in efficient TCP or UDP packet size. AT+CIPSENDEX is limited to 2 kbytes and `\\0` terminates the command so it can't occur in data. 299 | 300 | The SDWebServer example shows the use of the `write(callback)` function with C++ anonymous lambda functions as callbacks. 301 | 302 | ### EspAtDrv Errors 303 | 304 | The library functions with bool as return type return false in case of fail. The functions which return a value return 0 or - 1 in case of error, depending on the semantic of the function. To get the reason of the error the sketch can test the WiFi.getLastDriverError(). The error codes are enumerated in util/EspAtDrvTypes.h. 305 | 306 | For some functions 0 or false as returned value can have a meaning of error or be a valid return value. For example if you call `readBytes`, which has unsigned return type, without previously testing if bytes are available, then returned 0 can have the meaning of no bytes available or error occurred. 307 | 308 | ### UART Flow Control 309 | 310 | The AT firmware sends notifications about different events without the host requesting them. Examples of these events are if data arrive for a connection or connection is closed. Without flow control of Serial interface some of these events can get lost in buffer overflow if they arrive in bulk. As compensation for lost events, the WiFiEspAT library polls the state of the connections. This adds to time spent in library functions. 311 | 312 | If you can setup for the WiFiEspAT library a Serial connection with flow control, you can uncomment `#define ESPATDRV_ASSUME_FLOW_CONTROL` in EspAtDrv.cpp or define ESPATDRV_ASSUME_FLOW_CONTROL in boards.local.txt for the board. This will remove the polling of connections state. 313 | 314 | The SerialPassthrough sketch from WiFiEspAT/Tools in IDE Example menu has optional configuration of SAMD SERCOM3 to create 'Serial' interface with flow control. The esp8266 CTS pin is pin 13. The example has pin 2 of MKRZERO as RTS pin. To activate flow control on the AT firmware side, use the AT+UART command with last parameter 2 or 3. 315 | 316 | ### Create a copy for AT2 317 | 318 | If you want to use the library in projects with AT1 and AT2, create for AT2 a copy of the library. Copy the folder of the library as WiFiEspAT2, rename the file WiFiEspAT.h to WiFiEspAT2.h and change in library.properties `includes=` to `WiFiEspAT2.h`. -------------------------------------------------------------------------------- /examples/Advanced/AdvancedChatServer/AdvancedChatServer.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Advanced Chat Server 3 | 4 | A more advanced server that distributes any incoming messages 5 | to all connected clients but the client the message comes from. 6 | To use, telnet to your device's IP address and type. 7 | You can see the client's input in the serial monitor as well. 8 | 9 | created 18 Dec 2009 10 | by David A. Mellis 11 | modified 9 Apr 2012 12 | by Tom Igoe 13 | redesigned to make use of operator== 25 Nov 2013 14 | by Norbert Truchsess 15 | modified in Jul 2018 to use EthernetServer accept() 16 | by Paul Stoffregen 17 | */ 18 | 19 | #include 20 | 21 | // Emulate Serial1 on pins 6/7 if not present 22 | #if defined(ARDUINO_ARCH_AVR) && !defined(HAVE_HWSERIAL1) 23 | #include "SoftwareSerial.h" 24 | SoftwareSerial Serial1(6, 7); // RX, TX 25 | #define AT_BAUD_RATE 9600 26 | #else 27 | #define AT_BAUD_RATE 115200 28 | #endif 29 | 30 | #define MAX_CLIENTS WIFIESPAT_LINKS_COUNT 31 | const int CLIENT_CONN_TIMEOUT = 3600; // seconds. 1 hour 32 | 33 | WiFiServer server(2323); 34 | 35 | WiFiClient clients[MAX_CLIENTS]; 36 | 37 | void setup() { 38 | Serial.begin(115200); 39 | while (!Serial); 40 | 41 | Serial1.begin(AT_BAUD_RATE); 42 | WiFi.init(Serial1); 43 | 44 | if (WiFi.status() == WL_NO_MODULE) { 45 | Serial.println(); 46 | Serial.println("Communication with WiFi module failed!"); 47 | // don't continue 48 | while (true); 49 | } 50 | 51 | // waiting for connection to Wifi network set with the SetupWiFiConnection sketch 52 | Serial.println("Waiting for connection to WiFi"); 53 | while (WiFi.status() != WL_CONNECTED) { 54 | delay(1000); 55 | Serial.print('.'); 56 | } 57 | Serial.println(); 58 | 59 | // start listening for clients 60 | server.begin(MAX_CLIENTS, CLIENT_CONN_TIMEOUT); 61 | 62 | Serial.print("Chat server address:"); 63 | Serial.println(WiFi.localIP()); 64 | } 65 | 66 | void loop() { 67 | // check for any new client connecting, and say hello (before any incoming data) 68 | WiFiClient newClient = server.accept(); 69 | if (newClient) { 70 | for (byte i=0; i < MAX_CLIENTS; i++) { 71 | if (!clients[i]) { 72 | Serial.print("We have a new client #"); 73 | Serial.println(i); 74 | newClient.print("Hello, client number: "); 75 | newClient.println(i); 76 | // Once we "accept", the client is no longer tracked by EthernetServer 77 | // so we must store it into our list of clients 78 | clients[i] = newClient; 79 | break; 80 | } 81 | } 82 | } 83 | 84 | // check for incoming data from all clients 85 | for (byte i=0; i < MAX_CLIENTS; i++) { 86 | if (clients[i] && clients[i].available() > 0) { 87 | // read bytes from a client 88 | byte buffer[80]; 89 | int count = clients[i].read(buffer, 80); 90 | // write the bytes to all other connected clients 91 | for (byte j=0; j < MAX_CLIENTS; j++) { 92 | if (j != i && clients[j].connected()) { 93 | clients[j].write(buffer, count); 94 | } 95 | } 96 | } 97 | } 98 | 99 | // stop any clients which disconnect 100 | for (byte i=0; i < MAX_CLIENTS; i++) { 101 | if (clients[i] && !clients[i].connected()) { 102 | Serial.print("disconnect client #"); 103 | Serial.println(i); 104 | clients[i].stop(); 105 | } 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /examples/Advanced/ConfigurationAP/ConfigurationAP.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Configuration AP example 3 | The example shows a very simple Configuration Access Point. 4 | 5 | created in August 2019 for WiFiEspAT library 6 | by Juraj Andrassy https://github.com/jandrassy 7 | 8 | */ 9 | #include 10 | 11 | // Emulate Serial1 on pins 6/7 if not present 12 | #if defined(ARDUINO_ARCH_AVR) && !defined(HAVE_HWSERIAL1) 13 | #include "SoftwareSerial.h" 14 | SoftwareSerial Serial1(6, 7); // RX, TX 15 | #define AT_BAUD_RATE 9600 16 | #else 17 | #define AT_BAUD_RATE 115200 18 | #endif 19 | 20 | void setup() { 21 | 22 | Serial.begin(115200); 23 | while (!Serial); 24 | 25 | Serial1.begin(AT_BAUD_RATE); 26 | WiFi.init(Serial1); 27 | 28 | if (WiFi.status() == WL_NO_MODULE) { 29 | Serial.println(); 30 | Serial.println("Communication with WiFi module failed!"); 31 | // don't continue 32 | while (true); 33 | } 34 | 35 | WiFi.disconnect(); // stop the persistent connection to test the Configuration AP 36 | 37 | // waiting for connection to Wifi network set with the SetupWiFiConnection sketch 38 | Serial.println(); 39 | Serial.println("Waiting for connection to WiFi"); 40 | for (int i = 0; i < 5; i++) { 41 | if (WiFi.status() == WL_CONNECTED) 42 | break; 43 | delay(1000); 44 | Serial.print('.'); 45 | } 46 | Serial.println(); 47 | 48 | if (WiFi.status() != WL_CONNECTED) { 49 | Serial.println(); 50 | Serial.println("Could not connect to WiFi. Starting configuration AP..."); 51 | configAP(); 52 | } else { 53 | Serial.println("You're connected to the network"); 54 | } 55 | } 56 | 57 | void loop() { 58 | } 59 | 60 | void configAP() { 61 | 62 | WiFiServer server(80); 63 | 64 | WiFi.beginAP(); // starts the default AP (factory default or setup as persistent) 65 | 66 | char ssid[33]; 67 | WiFi.apSSID(ssid); 68 | Serial.print("Connect your computer to the WiFi network "); 69 | Serial.print(ssid); 70 | Serial.println(); 71 | IPAddress ip = WiFi.apIP(); 72 | Serial.print("and enter http://"); 73 | Serial.print(ip); 74 | Serial.println(" in a Web browser"); 75 | 76 | server.begin(); 77 | 78 | while (true) { 79 | 80 | WiFiClient client = server.accept(); 81 | if (client && client.available()) { // if !available yet, we return to this client in next loop 82 | char line[64]; 83 | int l = client.readBytesUntil('\n', line, sizeof(line)); 84 | line[l] = 0; 85 | client.find((char*) "\r\n\r\n"); 86 | 87 | if (strncmp_P(line, PSTR("POST"), strlen("POST")) == 0) { 88 | l = client.readBytes(line, sizeof(line)); 89 | line[l] = 0; 90 | 91 | // parse the parameters sent by the html form 92 | const char* delims = "=&"; 93 | strtok(line, delims); 94 | const char* ssid = strtok(NULL, delims); 95 | strtok(NULL, delims); 96 | const char* pass = strtok(NULL, delims); 97 | 98 | // send a response before attemting to connect to the WiFi network 99 | // because it will reset the SoftAP and disconnect the client station 100 | client.println(F("HTTP/1.1 200 OK")); 101 | client.println(F("Connection: close")); 102 | client.println(F("Refresh: 10")); 103 | client.println(); 104 | client.println(F("

Configuration AP


connecting...")); 105 | client.flush(); 106 | delay(1000); 107 | client.stop(); 108 | 109 | Serial.println(); 110 | Serial.print("Attempting to connect to WPA SSID: "); 111 | Serial.println(ssid); 112 | // WiFi.setPersistent(); // to make a successful connection persistent 113 | WiFi.begin(ssid, pass); 114 | 115 | // configuration continues with the next request 116 | 117 | } else { 118 | 119 | client.println(F("HTTP/1.1 200 OK")); 120 | client.println(F("Connection: close")); 121 | client.println(); 122 | client.println(F("

Configuration AP


")); 123 | 124 | int status = WiFi.status(); 125 | if (status == WL_CONNECTED) { 126 | client.println(F("Connection successful. Ending AP.")); 127 | } else { 128 | client.println(F("
WiFi connection failed. Enter valid parameters, please.

")); 129 | client.println(F("SSID:

")); 130 | client.println(F("Password:


")); 131 | client.println(F("
")); 132 | } 133 | client.println(F("")); 134 | client.stop(); 135 | 136 | if (status == WL_CONNECTED) { 137 | delay(1000); // to let the AT firmware finish the communication 138 | Serial.println("Connection successful. Ending AP."); 139 | server.end(); 140 | WiFi.endAP(true); 141 | return; 142 | } 143 | } 144 | } 145 | } 146 | } 147 | -------------------------------------------------------------------------------- /examples/Advanced/DeepSleepAndHwReset/DeepSleepAndHwReset.ino: -------------------------------------------------------------------------------- 1 | /* 2 | This example demonstrates hardware reset option 3 | of the WiFiEspAT library and the deepSleep command. 4 | Hw reset is required to wakeup the esp from deep sleep. 5 | 6 | Circuit: additionally to RX, TX and ground connection, 7 | wire the reset pin of the esp8266 to selected pin of your Arduino. 8 | 9 | created in August 2019 for WiFiEspAT library 10 | by Juraj Andrassy https://github.com/jandrassy 11 | */ 12 | 13 | #include 14 | 15 | // Emulate Serial1 on pins 6/7 if not present 16 | #if defined(ARDUINO_ARCH_AVR) && !defined(HAVE_HWSERIAL1) 17 | #include 18 | SoftwareSerial Serial1(6, 7); // RX, TX 19 | #define AT_BAUD_RATE 9600 20 | #else 21 | #define AT_BAUD_RATE 115200 22 | #endif 23 | 24 | const char* server = "arduino.tips"; 25 | 26 | const byte ESP_RESET_PIN = 12; 27 | const long SLEEP_INTERVAL = 30000; 28 | 29 | WiFiClient client; 30 | 31 | void setup() { 32 | 33 | Serial.begin(115200); 34 | while (!Serial); 35 | 36 | Serial1.begin(AT_BAUD_RATE); 37 | WiFi.init(Serial1, ESP_RESET_PIN); 38 | 39 | if (WiFi.status() == WL_NO_MODULE) { 40 | Serial.println("Communication with WiFi module failed!"); 41 | // don't continue 42 | while (true); 43 | } 44 | } 45 | 46 | void loop() { 47 | 48 | Serial.println("Waiting for connection to WiFi"); 49 | while (WiFi.status() != WL_CONNECTED) { 50 | delay(1000); 51 | Serial.print('.'); 52 | } 53 | Serial.println(); 54 | Serial.println("Connected to WiFi network."); 55 | 56 | doSomething(); 57 | 58 | Serial.println(); 59 | Serial.println("putting ESP to deep sleep..."); 60 | WiFi.deepSleep(); 61 | 62 | delay(SLEEP_INTERVAL); // here the main MCU could be set to sleep too 63 | 64 | WiFi.reset(ESP_RESET_PIN); // wake-up the esp from deep slepp 65 | 66 | Serial.println(); 67 | Serial.println("wakeup"); 68 | } 69 | 70 | void doSomething() { 71 | Serial.println(); 72 | Serial.println("Starting connection to server..."); 73 | if (client.connect(server, 80)) { 74 | Serial.println("connected to server"); 75 | 76 | client.println("GET /asciilogo.txt HTTP/1.1"); 77 | client.print("Host: "); 78 | client.println(server); 79 | client.println("Connection: close"); 80 | client.println(); 81 | client.flush(); 82 | while (client.connected()) { 83 | while (client.available()) { 84 | char c = client.read(); 85 | Serial.write(c); 86 | } 87 | } 88 | Serial.println(); 89 | Serial.println("disconnecting from server."); 90 | client.stop(); 91 | } 92 | } 93 | 94 | -------------------------------------------------------------------------------- /examples/Advanced/HelloServer/HelloServer.ino: -------------------------------------------------------------------------------- 1 | /* 2 | HelloServer example sketch for WiFiEspAT 3 | using the WiFiWebServer library by Khoi Hoang 4 | https://github.com/khoih-prog/WiFiWebServer 5 | 6 | This example doesn't fit into 2 kB SRAM of ATmega328p, 7 | so it can't be run on Uno, classic Nano or Mini 8 | It works perfect on Mega, Nano Every, Nano 33 BLE, MKR, Zero, M0, ... 9 | 10 | created in 2015 for ESP8266WebServer library 11 | by Ivan Grokhotkov 12 | 13 | adopted in March 2020 for WiFiEspAT library 14 | by Juraj Andrassy https://github.com/jandrassy 15 | 16 | */ 17 | 18 | #include 19 | #define USE_WIFI_NINA 0 20 | #include // library by Khoi Hoang available in Library Manager 21 | 22 | WiFiWebServer server(80); 23 | 24 | const int led = LED_BUILTIN; 25 | 26 | void handleRoot() { 27 | digitalWrite(led, 1); 28 | server.send(200, "text/plain", "hello from Arduino" ); 29 | digitalWrite(led, 0); 30 | } 31 | 32 | void handleNotFound() { 33 | digitalWrite(led, 1); 34 | String message = "File Not Found\n\n"; 35 | message += "URI: "; 36 | message += server.uri(); 37 | message += "\nMethod: "; 38 | message += (server.method() == HTTP_GET) ? "GET" : "POST"; 39 | message += "\nArguments: "; 40 | message += server.args(); 41 | message += "\n"; 42 | for (uint8_t i = 0; i < server.args(); i++) { 43 | message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; 44 | } 45 | server.send(404, "text/plain", message); 46 | digitalWrite(led, 0); 47 | } 48 | 49 | void setup(void) { 50 | pinMode(led, OUTPUT); 51 | digitalWrite(led, 0); 52 | Serial.begin(115200); 53 | while (!Serial); 54 | 55 | Serial1.begin(115200); 56 | WiFi.init(Serial1); 57 | 58 | if (WiFi.status() == WL_NO_MODULE) { 59 | Serial.println("Communication with WiFi module failed!"); 60 | // don't continue 61 | while (true); 62 | } 63 | Serial.println(""); 64 | 65 | // Wait for connection to Wifi network set with the SetupWiFiConnection sketch 66 | while (WiFi.status() != WL_CONNECTED) { 67 | delay(500); 68 | Serial.print("."); 69 | } 70 | Serial.println(""); 71 | Serial.print("Connected to "); 72 | Serial.println(WiFi.SSID()); 73 | Serial.print("IP address: "); 74 | Serial.println(WiFi.localIP()); 75 | 76 | server.on("/", handleRoot); 77 | 78 | server.on("/inline", []() { 79 | server.send(200, "text/plain", "this works as well"); 80 | }); 81 | 82 | server.onNotFound(handleNotFound); 83 | 84 | server.begin(); 85 | Serial.println("HTTP server started"); 86 | } 87 | 88 | void loop(void) { 89 | server.handleClient(); 90 | } 91 | -------------------------------------------------------------------------------- /examples/Advanced/SDWebServer/SDWebServer.ino: -------------------------------------------------------------------------------- 1 | /* 2 | SD Web Server example sketch for WiFiEspAT 3 | 4 | the example demonstrates the write(file) and write() with callback functions 5 | 6 | This example doesn't fit into 2 kB SRAM of ATmega328p, 7 | so it can't be run on Uno, classic Nano or Mini 8 | It works perfect on Mega, Nano Every, Nano 33 BLE, MKR, Zero, M0, ... 9 | 10 | created in December 2019 for WiFiEspAT library 11 | by Juraj Andrassy https://github.com/jandrassy 12 | 13 | */ 14 | 15 | #include 16 | #include 17 | #include // install in Library Manager. Used to generate HTML of directory listing 18 | 19 | // Emulate Serial1 on pins 6/7 if not present 20 | #if defined(ARDUINO_ARCH_AVR) && !defined(HAVE_HWSERIAL1) 21 | #include 22 | SoftwareSerial Serial1(6, 7); // RX, TX 23 | #define AT_BAUD_RATE 9600 24 | #else 25 | #define AT_BAUD_RATE 115200 26 | #endif 27 | 28 | const int SDCARD_CS = 4; 29 | 30 | WiFiServer server(80); 31 | 32 | void setup() { 33 | 34 | Serial.begin(115200); 35 | while (!Serial); 36 | 37 | if (!SD.begin(SDCARD_CS)) { 38 | Serial.println(F("SD card initialization failed!")); 39 | // don't continue 40 | while (true); 41 | } 42 | 43 | Serial1.begin(AT_BAUD_RATE); 44 | WiFi.init(Serial1); 45 | 46 | 47 | if (WiFi.status() == WL_NO_MODULE) { 48 | Serial.println(F("Communication with WiFi module failed!")); 49 | // don't continue 50 | while (true); 51 | } 52 | 53 | // waiting for connection to Wifi network set with the SetupWiFiConnection sketch 54 | Serial.println(F("Waiting for connection to WiFi")); 55 | while (WiFi.status() != WL_CONNECTED) { 56 | delay(1000); 57 | Serial.print('.'); 58 | } 59 | Serial.println(); 60 | 61 | server.begin(3, 60); 62 | 63 | IPAddress ip = WiFi.localIP(); 64 | Serial.println(); 65 | Serial.println(F("Connected to WiFi network.")); 66 | Serial.print(F("To access the server, enter \"http://")); 67 | Serial.print(ip); 68 | Serial.println(F("/\" in web browser.")); 69 | } 70 | 71 | void loop() { 72 | 73 | static File file; // must by static to be accessible by the lambda functions 74 | static char fn[32]; 75 | 76 | WiFiClient client = server.accept(); 77 | 78 | if (client && client.connected()) { 79 | if (client.find(' ')) { // GET /fn HTTP/1.1 80 | int l = client.readBytesUntil(' ', fn, sizeof(fn) - 1); // read the filename from URL 81 | fn[l] = 0; 82 | while (client.read() != -1); // discard the rest of the request 83 | file = SD.open(fn); 84 | if (!file) { // file was not found 85 | client.write([](Print& p) { // anonymous lambda function to be called by the library 86 | p.println(F("HTTP/1.1 404 Not Found")); 87 | p.println(F("Connection: close")); 88 | p.print(F("Content-Length: ")); 89 | p.println(strlen(" not found") + strlen(fn)); 90 | p.println(); 91 | p.print(fn); 92 | p.print(F(" not found")); 93 | }); 94 | } else if (file.isDirectory()) { 95 | client.write([](Print& p) { // anonymous lambda function to be called by the library 96 | p.println(F("HTTP/1.1 200 OK")); 97 | p.println(F("Connection: close")); 98 | p.println(F("Content-Type: text/html")); 99 | p.println(F("Transfer-Encoding: chunked")); 100 | p.println(); 101 | char buff[64]; // buffer for chunks 102 | ChunkedPrint chunked(p, buff, sizeof(buff)); 103 | chunked.begin(); 104 | chunked.printf(F("\r\n\r\n\r\n

Folder '%s'

\r\n"), fn); 105 | while (true) { 106 | File entry = file.openNextFile(); 107 | if (!entry) 108 | break; 109 | if (strcmp(fn, "/") == 0) { 110 | chunked.printf(F(""), entry.name()); 111 | } else { 112 | chunked.printf(F(""), fn, entry.name()); 113 | } 114 | chunked.print(entry.name()); 115 | if (entry.isDirectory()) { 116 | chunked.println(F("/
")); 117 | } else { 118 | chunked.printf(F(" (%ld B)
\r\n"), entry.size()); 119 | } 120 | entry.close(); 121 | } 122 | chunked.println(F("\r\n")); 123 | chunked.end(); 124 | file.close(); 125 | }); 126 | } else { 127 | client.write([](Print& p) { // anonymous lambda function to be called by the library 128 | p.println(F("HTTP/1.1 200 OK")); 129 | p.println(F("Connection: close")); 130 | p.print(F("Content-Length: ")); 131 | p.println(file.size()); 132 | p.print(F("Content-Type: ")); 133 | const char* ext = strchr(file.name(), '.'); 134 | p.println(getContentType(ext)); 135 | p.println(); 136 | }); 137 | client.write(file); // send the file as body of the response 138 | file.close(); 139 | } 140 | } 141 | client.stop(); 142 | } 143 | } 144 | 145 | const char* getContentType(const char* ext){ 146 | if (!strcmp(ext, ".HTM")) 147 | return "text/html"; 148 | if (!strcmp(ext, ".CSS")) 149 | return "text/css"; 150 | if (!strcmp(ext, ".JS")) 151 | return "application/javascript"; 152 | if (!strcmp(ext, ".PNG")) 153 | return "image/png"; 154 | if (!strcmp(ext, ".GIF")) 155 | return "image/gif"; 156 | if (!strcmp(ext, ".JPG")) 157 | return "image/jpeg"; 158 | if (!strcmp(ext, ".XML")) 159 | return "text/xml"; 160 | return "text/plain"; 161 | } 162 | -------------------------------------------------------------------------------- /examples/Advanced/TelnetConsole/TelnetConsole.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Telnet Console 3 | 4 | This sketch expects esp8266 on Serial and doesn't use Serial for debug output. 5 | It uses the built in LED to indicate WiFi connection error 6 | and Telnet server for debug prints. 7 | Make sure that logging is set to SILENT in EspAtDrvLogging.h 8 | 9 | Configure a static IP address or write down the DHCP assigned IP address 10 | printed by the SetupWiFiConnection sketch. 11 | 12 | After the LED stops blinking start Telnet client application on IP address 13 | and port 2323. On Windows or Linux command prompt you would write 14 | for example 15 | telnet 192.168.1.104 2323 16 | 17 | only one client can connect at the time 18 | 19 | created in August 2019 for WiFiEspAT library 20 | by Juraj Andrassy https://github.com/jandrassy 21 | */ 22 | 23 | #include 24 | 25 | #define AT_BAUD_RATE 115200 26 | 27 | WiFiServer telnetServer(2323); 28 | WiFiClient telneClient; 29 | bool newClient = false; 30 | 31 | void setup() { 32 | 33 | Serial.begin(AT_BAUD_RATE); 34 | WiFi.init(Serial); 35 | 36 | pinMode(LED_BUILTIN, OUTPUT); 37 | 38 | if (WiFi.status() == WL_NO_MODULE) { 39 | while (true) { 40 | blink(100); 41 | } 42 | } 43 | 44 | // waiting for connection to Wifi network set with the SetupWiFiConnection sketch 45 | while (WiFi.status() != WL_CONNECTED) { 46 | blink(500); 47 | } 48 | 49 | telnetServer.begin(); 50 | 51 | } 52 | 53 | void loop() { 54 | 55 | if (!telneClient) { // client is not connected 56 | telneClient = telnetServer.accept(); // returns active or 'empty' client 57 | } 58 | 59 | if (telneClient.connected()) { 60 | if (!newClient) { 61 | newClient = true; 62 | telneClient.println(); 63 | telneClient.println("Welcome"); 64 | telneClient.println("You can send C to close the connection."); 65 | delay(5000); 66 | } 67 | telneClient.println(); 68 | for (int analogChannel = 0; analogChannel < 4; analogChannel++) { 69 | int sensorReading = analogRead(analogChannel); 70 | telneClient.print("analog input A"); 71 | telneClient.print(analogChannel); 72 | telneClient.print(" is "); 73 | telneClient.println(sensorReading); 74 | } 75 | telneClient.flush(); 76 | if (telneClient.read() == 'C') { // close command from client 77 | telneClient.stop(); 78 | newClient = false; 79 | } 80 | } else if (telneClient) { // stop disconnected client 81 | telneClient.stop(); 82 | newClient = false; 83 | } 84 | delay(5000); 85 | } 86 | 87 | void blink(int interval) { 88 | digitalWrite(LED_BUILTIN, HIGH); 89 | delay(interval); 90 | digitalWrite(LED_BUILTIN, LOW); 91 | delay(interval); 92 | } 93 | -------------------------------------------------------------------------------- /examples/Advanced/TemporaryWiFiConnect/TemporaryWiFiConnect.ino: -------------------------------------------------------------------------------- 1 | /* 2 | This example connects to a Wifi network if it is accessible. 3 | Then it prints the MAC address of the Wifi module, 4 | the IP address obtained, and other network details. 5 | 6 | based on the ConnectWithWPA.ino 7 | created 13 July 2010 8 | by dlf (Metodo2 srl) 9 | modified 31 May 2012 10 | by Tom Igoe 11 | 12 | created in Jul 2019 for WiFiEspAT library 13 | by Juraj Andrassy https://github.com/jandrassy 14 | */ 15 | 16 | #include 17 | 18 | // Emulate Serial1 on pins 6/7 if not present 19 | #if defined(ARDUINO_ARCH_AVR) && !defined(HAVE_HWSERIAL1) 20 | #include 21 | SoftwareSerial Serial1(6, 7); // RX, TX 22 | #define AT_BAUD_RATE 9600 23 | #else 24 | #define AT_BAUD_RATE 115200 25 | #endif 26 | 27 | #include "arduino_secrets.h" 28 | ///////please enter your sensitive data in the Secret tab/arduino_secrets.h 29 | const char ssid[] = SECRET_SSID; // your network SSID (name) 30 | const char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) 31 | 32 | void setup() { 33 | 34 | Serial.begin(115200); 35 | while (!Serial); // wait for serial port to connect. Needed for native USB port only 36 | 37 | Serial1.begin(AT_BAUD_RATE); 38 | WiFi.init(Serial1); 39 | 40 | if (WiFi.status() == WL_NO_MODULE) { 41 | Serial.println(); 42 | Serial.println("Communication with WiFi module failed!"); 43 | // don't continue 44 | while (true); 45 | } 46 | 47 | // disconnect persistent connection (not persistent) 48 | WiFi.disconnect(); 49 | 50 | } 51 | 52 | void loop() { 53 | 54 | if (WiFi.status() != WL_CONNECTED) { 55 | 56 | Serial.println(); 57 | Serial.print("Attempting to connect to WPA SSID: "); 58 | Serial.println(ssid); 59 | 60 | int status = WiFi.begin(ssid, pass); 61 | Serial.println(); 62 | 63 | if (status != WL_CONNECTED) { 64 | Serial.println("Failed to connect to AP"); 65 | } else { 66 | Serial.println("You're connected to the network"); 67 | printWifiData(); 68 | } 69 | } 70 | if (WiFi.status() == WL_CONNECTED) { 71 | printCurrentNet(); 72 | } 73 | delay(10000); 74 | } 75 | 76 | void printWifiData() { 77 | // print your board's IP address: 78 | IPAddress ip = WiFi.localIP(); 79 | Serial.print("IP Address: "); 80 | Serial.println(ip); 81 | 82 | // print your MAC address: 83 | byte mac[6]; 84 | WiFi.macAddress(mac); 85 | Serial.print("MAC address: "); 86 | printMacAddress(mac); 87 | } 88 | 89 | void printCurrentNet() { 90 | // print the SSID of the network you're attached to: 91 | Serial.print("SSID: "); 92 | Serial.println(WiFi.SSID()); 93 | 94 | // print the MAC address of the router you're attached to: 95 | byte bssid[6]; 96 | WiFi.BSSID(bssid); 97 | Serial.print("BSSID: "); 98 | printMacAddress(bssid); 99 | 100 | // print the received signal strength: 101 | long rssi = WiFi.RSSI(); 102 | Serial.print("signal strength (RSSI):"); 103 | Serial.println(rssi); 104 | 105 | } 106 | 107 | void printMacAddress(byte mac[]) { 108 | for (int i = 5; i >= 0; i--) { 109 | if (mac[i] < 16) { 110 | Serial.print("0"); 111 | } 112 | Serial.print(mac[i], HEX); 113 | if (i > 0) { 114 | Serial.print(":"); 115 | } 116 | } 117 | Serial.println(); 118 | } 119 | -------------------------------------------------------------------------------- /examples/Advanced/TemporaryWiFiConnect/arduino_secrets.h: -------------------------------------------------------------------------------- 1 | #define SECRET_SSID "" 2 | #define SECRET_PASS "" 3 | -------------------------------------------------------------------------------- /examples/Advanced/WebServerSSL/WebServerSSL.ino: -------------------------------------------------------------------------------- 1 | /* 2 | WiFi SSL Web Server only for AT 2 on ESP32 3 | 4 | A SSL web server that shows the value of the analog input pins. 5 | 6 | created 13 July 2010 7 | by dlf (Metodo2 srl) 8 | modified 31 May 2012 9 | by Tom Igoe 10 | modified in August 2020 for WiFiEspAT library 11 | by Juraj Andrassy https://github.com/jandrassy 12 | */ 13 | 14 | #include 15 | 16 | // Emulate Serial1 on pins 6/7 if not present 17 | #if defined(ARDUINO_ARCH_AVR) && !defined(HAVE_HWSERIAL1) 18 | #include 19 | SoftwareSerial Serial1(6, 7); // RX, TX 20 | #define AT_BAUD_RATE 9600 21 | #else 22 | #define AT_BAUD_RATE 115200 23 | #endif 24 | 25 | WiFiServer server(443); 26 | 27 | void setup() { 28 | 29 | Serial.begin(115200); 30 | while (!Serial); 31 | 32 | Serial1.begin(AT_BAUD_RATE); 33 | WiFi.init(Serial1); 34 | 35 | if (WiFi.status() == WL_NO_MODULE) { 36 | Serial.println("Communication with WiFi module failed!"); 37 | // don't continue 38 | while (true); 39 | } 40 | 41 | // waiting for connection to Wifi network set with the SetupWiFiConnection sketch 42 | Serial.println("Waiting for connection to WiFi"); 43 | while (WiFi.status() != WL_CONNECTED) { 44 | delay(1000); 45 | Serial.print('.'); 46 | } 47 | Serial.println(); 48 | 49 | server.beginSSL(); 50 | 51 | IPAddress ip = WiFi.localIP(); 52 | Serial.println(); 53 | Serial.println("Connected to WiFi network."); 54 | Serial.print("To access the server, enter \"https://"); 55 | Serial.print(ip); 56 | Serial.println("/\" in web browser."); 57 | } 58 | 59 | void loop() { 60 | 61 | WiFiClient client = server.accept(); 62 | if (client) { // true only for a connected client with data available 63 | IPAddress ip = client.remoteIP(); 64 | Serial.print("new client "); 65 | Serial.println(ip); 66 | 67 | while (client.connected()) { 68 | if (client.available()) { 69 | char line[128]; 70 | size_t l = client.readBytesUntil('\n', (uint8_t*) line, sizeof(line)); 71 | if (l == 0) { 72 | Serial.print("error reading request"); 73 | break; 74 | } 75 | line[l - 1] = 0; // overwrite \r with a 0 76 | Serial.println(line); 77 | 78 | if (strlen(line) == 0) { // http headers end with an empty line 79 | // send a standard http response header 80 | client.println("HTTP/1.1 200 OK"); 81 | client.println("Content-Type: text/html"); 82 | client.println("Connection: close"); // the connection will be closed after completion of the response 83 | client.println("Refresh: 5"); // refresh the page automatically every 5 sec 84 | client.println(); 85 | client.println(""); 86 | client.println(""); 87 | 88 | // output the value of analog input pins 89 | for (int analogChannel = 0; analogChannel < 4; analogChannel++) { 90 | int sensorReading = analogRead(analogChannel); 91 | client.print("analog input "); 92 | client.print(analogChannel); 93 | client.print(" is "); 94 | client.print(sensorReading); 95 | client.println("
"); 96 | } 97 | client.println(""); 98 | break; 99 | } 100 | } 101 | } 102 | 103 | // close the connection: 104 | client.stop(); 105 | Serial.println("client disconnected"); 106 | } 107 | } 108 | 109 | -------------------------------------------------------------------------------- /examples/Advanced/WiFiEspAT2UDP/WiFiEspAT2UDP.ino: -------------------------------------------------------------------------------- 1 | /* 2 | WiFi UDP Receive String with provided buffer 3 | 4 | This sketch wait an UDP packet on localPort using the WiFi module. 5 | When a packet is received an Acknowledge packet is sent to the client on port remotePort 6 | 7 | This example requires WiFiEspAT library with ESP AT 2 firmware 8 | and uses WiFiEspAT specific UDP receiving functions. 9 | 10 | created 30 December 2012 11 | by dlf (Metodo2 srl) 12 | modified in August 2020 for WiFiEspAT library 13 | by Juraj Andrassy https://github.com/jandrassy 14 | */ 15 | 16 | #include 17 | 18 | // Emulate Serial1 on pins 6/7 if not present 19 | #if defined(ARDUINO_ARCH_AVR) && !defined(HAVE_HWSERIAL1) 20 | #include 21 | SoftwareSerial Serial1(6, 7); // RX, TX 22 | #define AT_BAUD_RATE 9600 23 | #else 24 | #define AT_BAUD_RATE 115200 25 | #endif 26 | 27 | unsigned int localPort = 2390; // local port to listen on 28 | 29 | char reply[] = "acknowledged"; // a string to send back 30 | 31 | WiFiUDP udp; 32 | 33 | void setup() { 34 | //Initialize serial and wait for port to open: 35 | Serial.begin(115200); 36 | while (!Serial) { 37 | ; // wait for serial port to connect. Needed for native USB port only 38 | } 39 | 40 | Serial1.begin(AT_BAUD_RATE); 41 | WiFi.init(Serial1); 42 | 43 | // check for the WiFi module: 44 | if (WiFi.status() == WL_NO_MODULE) { 45 | Serial.println("Communication with WiFi module failed!"); 46 | // don't continue 47 | while (true); 48 | } 49 | 50 | // waiting for connection to Wifi network set with the SetupWiFiConnection sketch 51 | Serial.println("Waiting for connection to WiFi"); 52 | while (WiFi.status() != WL_CONNECTED) { 53 | delay(1000); 54 | Serial.print('.'); 55 | } 56 | Serial.println(); 57 | Serial.println("Connected to wifi"); 58 | printWifiStatus(); 59 | 60 | 61 | Serial.println("\nStarting connection to server..."); 62 | // if you get a connection, report back via serial: 63 | udp.begin(localPort); 64 | } 65 | 66 | void loop() { 67 | 68 | // if there's data available, read a packet 69 | size_t packetSize = udp.availableForParse(); 70 | if (packetSize) { 71 | char packetBuffer[packetSize + 1]; // +1 for the terminating 0 72 | IPAddress remoteIp; 73 | uint16_t remotePort = 0; 74 | size_t len = udp.parsePacket((uint8_t*) packetBuffer, packetSize, remoteIp, remotePort); 75 | Serial.print("Received packet of size "); 76 | Serial.println(packetSize); 77 | Serial.print("From "); 78 | Serial.print(remoteIp); 79 | Serial.print(", port "); 80 | Serial.println(remotePort); 81 | if (len > 0) { 82 | packetBuffer[len] = 0; 83 | } 84 | Serial.println("Contents:"); 85 | Serial.println(packetBuffer); 86 | 87 | // send a reply, to the IP address and port that sent us the packet we received 88 | udp.beginPacket(remoteIp, remotePort); 89 | udp.write(reply); 90 | udp.endPacket(); 91 | } 92 | } 93 | 94 | void printWifiStatus() { 95 | // print the SSID of the network you're attached to: 96 | Serial.print("SSID: "); 97 | Serial.println(WiFi.SSID()); 98 | 99 | // print your board's IP address: 100 | IPAddress ip = WiFi.localIP(); 101 | Serial.print("IP Address: "); 102 | Serial.println(ip); 103 | 104 | // print the received signal strength: 105 | long rssi = WiFi.RSSI(); 106 | Serial.print("signal strength (RSSI):"); 107 | Serial.print(rssi); 108 | Serial.println(" dBm"); 109 | } 110 | 111 | 112 | 113 | 114 | -------------------------------------------------------------------------------- /examples/Basic/ScanNetworksAdvanced/ScanNetworksAdvanced.ino: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | This example prints the board's MAC address, and 4 | scans for available WiFi networks using a esp8266 with AT firmware. 5 | Every ten seconds, it scans again. It doesn't actually 6 | connect to any network, so no encryption scheme is specified. 7 | BSSID and WiFi channel are printed 8 | 9 | This example is based on ScanNetworks 10 | created 13 July 2010 11 | by dlf (Metodo2 srl) 12 | modified 21 Junn 2012 13 | by Tom Igoe and Jaymes Dec 14 | 15 | created 1 Mar 2017 16 | by Arturo Guadalupi 17 | 18 | */ 19 | 20 | #include 21 | 22 | // Emulate Serial1 on pins 6/7 if not present 23 | #if defined(ARDUINO_ARCH_AVR) && !defined(HAVE_HWSERIAL1) 24 | #include 25 | SoftwareSerial Serial1(6, 7); // RX, TX 26 | #define AT_BAUD_RATE 9600 27 | #else 28 | #define AT_BAUD_RATE 115200 29 | #endif 30 | 31 | void setup() { 32 | 33 | Serial.begin(115200); 34 | while (!Serial); 35 | 36 | Serial1.begin(AT_BAUD_RATE); 37 | WiFi.init(Serial1); 38 | 39 | // check for the WiFi module: 40 | if (WiFi.status() == WL_NO_MODULE) { 41 | Serial.println(); 42 | Serial.println("Communication with WiFi module failed!"); 43 | // don't continue 44 | while (true); 45 | } 46 | 47 | // print your MAC address: 48 | byte mac[6]; 49 | WiFi.macAddress(mac); 50 | Serial.print("MAC: "); 51 | printMacAddress(mac); 52 | 53 | // scan for existing networks: 54 | Serial.println(); 55 | Serial.println("Scanning available networks..."); 56 | listNetworks(); 57 | } 58 | 59 | void loop() { 60 | delay(10000); 61 | // scan for existing networks: 62 | Serial.println("Scanning available networks..."); 63 | listNetworks(); 64 | } 65 | 66 | void listNetworks() { 67 | // scan for nearby networks: 68 | Serial.println("** Scan Networks **"); 69 | int numSsid = WiFi.scanNetworks(); 70 | if (numSsid == -1) 71 | { 72 | Serial.println("Couldn't get a WiFi connection"); 73 | while (true); 74 | } 75 | 76 | // print the list of networks seen: 77 | Serial.print("number of available networks: "); 78 | Serial.println(numSsid); 79 | 80 | // print the network number and name for each network found: 81 | for (int thisNet = 0; thisNet < numSsid; thisNet++) { 82 | Serial.print(thisNet + 1); 83 | Serial.print(") "); 84 | Serial.print("Signal: "); 85 | Serial.print(WiFi.RSSI(thisNet)); 86 | Serial.print(" dBm"); 87 | Serial.print("\tChannel: "); 88 | Serial.print(WiFi.channel(thisNet)); 89 | byte bssid[6]; 90 | Serial.print("\t\tBSSID: "); 91 | printMacAddress(WiFi.BSSID(thisNet, bssid)); 92 | Serial.print("\tEncryption: "); 93 | printEncryptionType(WiFi.encryptionType(thisNet)); 94 | Serial.print("\t\tSSID: "); 95 | Serial.println(WiFi.SSID(thisNet)); 96 | Serial.flush(); 97 | } 98 | Serial.println(); 99 | } 100 | 101 | void printEncryptionType(int thisType) { 102 | // read the encryption type and print out the name: 103 | switch (thisType) { 104 | case ENC_TYPE_WEP: 105 | Serial.print("WEP"); 106 | break; 107 | case ENC_TYPE_TKIP: 108 | Serial.print("WPA"); 109 | break; 110 | case ENC_TYPE_CCMP: 111 | Serial.print("WPA2"); 112 | break; 113 | case ENC_TYPE_NONE: 114 | Serial.print("None"); 115 | break; 116 | case ENC_TYPE_AUTO: 117 | Serial.print("Auto"); 118 | break; 119 | case ENC_TYPE_UNKNOWN: 120 | default: 121 | Serial.print("Unknown"); 122 | break; 123 | } 124 | } 125 | 126 | void print2Digits(byte thisByte) { 127 | if (thisByte < 0xF) { 128 | Serial.print("0"); 129 | } 130 | Serial.print(thisByte, HEX); 131 | } 132 | 133 | void printMacAddress(byte mac[]) { 134 | for (int i = 0; i < 6; i++) { 135 | if (i > 0) { 136 | Serial.print(":"); 137 | } 138 | if (mac[i] < 16) { 139 | Serial.print("0"); 140 | } 141 | Serial.print(mac[i], HEX); 142 | } 143 | Serial.println(); 144 | } 145 | -------------------------------------------------------------------------------- /examples/Basic/UdpSendReceiveString/UdpSendReceiveString.ino: -------------------------------------------------------------------------------- 1 | /* 2 | WiFi UDP Send and Receive String 3 | 4 | This sketch wait an UDP packet on localPort using the WiFi module. 5 | When a packet is received an Acknowledge packet is sent to the client on port remotePort 6 | 7 | created 30 December 2012 8 | by dlf (Metodo2 srl) 9 | 10 | */ 11 | 12 | #include 13 | 14 | // Emulate Serial1 on pins 6/7 if not present 15 | #if defined(ARDUINO_ARCH_AVR) && !defined(HAVE_HWSERIAL1) 16 | #include 17 | SoftwareSerial Serial1(6, 7); // RX, TX 18 | #define AT_BAUD_RATE 9600 19 | #else 20 | #define AT_BAUD_RATE 115200 21 | #endif 22 | 23 | unsigned int localPort = 2390; // local port to listen on 24 | 25 | char packetBuffer[255]; //buffer to hold incoming packet 26 | char ReplyBuffer[] = "acknowledged"; // a string to send back 27 | 28 | WiFiUDP Udp; 29 | 30 | void setup() { 31 | //Initialize serial and wait for port to open: 32 | Serial.begin(115200); 33 | while (!Serial) { 34 | ; // wait for serial port to connect. Needed for native USB port only 35 | } 36 | 37 | Serial1.begin(AT_BAUD_RATE); 38 | WiFi.init(Serial1); 39 | 40 | // check for the WiFi module: 41 | if (WiFi.status() == WL_NO_MODULE) { 42 | Serial.println("Communication with WiFi module failed!"); 43 | // don't continue 44 | while (true); 45 | } 46 | 47 | // waiting for connection to Wifi network set with the SetupWiFiConnection sketch 48 | Serial.println("Waiting for connection to WiFi"); 49 | while (WiFi.status() != WL_CONNECTED) { 50 | delay(1000); 51 | Serial.print('.'); 52 | } 53 | Serial.println(); 54 | Serial.println("Connected to wifi"); 55 | printWifiStatus(); 56 | 57 | 58 | Serial.println("\nStarting connection to server..."); 59 | // if you get a connection, report back via serial: 60 | Udp.begin(localPort); 61 | } 62 | 63 | void loop() { 64 | 65 | // if there's data available, read a packet 66 | int packetSize = Udp.parsePacket(); 67 | if (packetSize) { 68 | Serial.print("Received packet of size "); 69 | Serial.println(packetSize); 70 | Serial.print("From "); 71 | IPAddress remoteIp = Udp.remoteIP(); 72 | Serial.print(remoteIp); 73 | Serial.print(", port "); 74 | Serial.println(Udp.remotePort()); 75 | 76 | // read the packet into packetBufffer 77 | int len = Udp.read(packetBuffer, 255); 78 | if (len > 0) { 79 | packetBuffer[len] = 0; 80 | } 81 | Serial.println("Contents:"); 82 | Serial.println(packetBuffer); 83 | 84 | // send a reply, to the IP address and port that sent us the packet we received 85 | Udp.beginPacket(Udp.remoteIP(), Udp.remotePort()); 86 | Udp.write(ReplyBuffer); 87 | Udp.endPacket(); 88 | } 89 | } 90 | 91 | void printWifiStatus() { 92 | // print the SSID of the network you're attached to: 93 | Serial.print("SSID: "); 94 | Serial.println(WiFi.SSID()); 95 | 96 | // print your board's IP address: 97 | IPAddress ip = WiFi.localIP(); 98 | Serial.print("IP Address: "); 99 | Serial.println(ip); 100 | 101 | // print the received signal strength: 102 | long rssi = WiFi.RSSI(); 103 | Serial.print("signal strength (RSSI):"); 104 | Serial.print(rssi); 105 | Serial.println(" dBm"); 106 | } 107 | 108 | 109 | 110 | 111 | -------------------------------------------------------------------------------- /examples/Basic/UdpSender/UdpSender.ino: -------------------------------------------------------------------------------- 1 | /* 2 | WiFi UDP Send String 3 | 4 | This sketch sends an UDP packet using an Arduino board with WiFiEspAT library. 5 | 6 | created in Jul 2019 for WiFiEspAT library 7 | by Juraj Andrassy https://github.com/jandrassy 8 | 9 | */ 10 | #include 11 | 12 | // Emulate Serial1 on pins 6/7 if not present 13 | #if defined(ARDUINO_ARCH_AVR) && !defined(HAVE_HWSERIAL1) 14 | #include 15 | SoftwareSerial Serial1(6, 7); // RX, TX 16 | #define AT_BAUD_RATE 9600 17 | #else 18 | #define AT_BAUD_RATE 115200 19 | #endif 20 | 21 | WiFiUDP Udp; 22 | IPAddress reciverIP(192, 168, 1, 108); 23 | unsigned int receiverPort = 9876; 24 | 25 | void setup() { 26 | Serial.begin(115200); 27 | while (!Serial); 28 | 29 | Serial1.begin(AT_BAUD_RATE); 30 | WiFi.init(Serial1); 31 | 32 | // check for the WiFi module: 33 | if (WiFi.status() == WL_NO_MODULE) { 34 | Serial.println(); 35 | Serial.println("Communication with WiFi module failed!"); 36 | // don't continue 37 | while (true); 38 | } 39 | 40 | // waiting for connection to Wifi network set with the SetupWiFiConnection sketch 41 | Serial.println("Waiting for connection to WiFi"); 42 | while (WiFi.status() != WL_CONNECTED) { 43 | delay(1000); 44 | Serial.print('.'); 45 | } 46 | Serial.println(); 47 | Serial.println("Connected to WiFi network."); 48 | } 49 | 50 | void loop() { 51 | 52 | Udp.beginPacket(reciverIP, receiverPort); 53 | Udp.print("Arduino millis "); 54 | Udp.print(millis()); 55 | Udp.endPacket(); 56 | 57 | delay(5000); 58 | } 59 | -------------------------------------------------------------------------------- /examples/Basic/WebClient/WebClient.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Web client 3 | 4 | This sketch connects to a website (http://arduino.cc) 5 | using the WiFi module. 6 | 7 | created 13 July 2010 8 | by dlf (Metodo2 srl) 9 | modified 31 May 2012 10 | by Tom Igoe 11 | modified in Jul 2019 for WiFiEspAT library 12 | by Juraj Andrassy https://github.com/jandrassy 13 | */ 14 | 15 | #include 16 | 17 | // Emulate Serial1 on pins 6/7 if not present 18 | #if defined(ARDUINO_ARCH_AVR) && !defined(HAVE_HWSERIAL1) 19 | #include 20 | SoftwareSerial Serial1(6, 7); // RX, TX 21 | #define AT_BAUD_RATE 9600 22 | #else 23 | #define AT_BAUD_RATE 115200 24 | #endif 25 | 26 | const char* server = "arduino.tips"; 27 | 28 | WiFiClient client; 29 | 30 | void setup() { 31 | Serial.begin(115200); 32 | while (!Serial); 33 | 34 | Serial1.begin(AT_BAUD_RATE); 35 | WiFi.init(Serial1); 36 | 37 | if (WiFi.status() == WL_NO_MODULE) { 38 | Serial.println(); 39 | Serial.println("Communication with WiFi module failed!"); 40 | // don't continue 41 | while (true); 42 | } 43 | 44 | // waiting for connection to Wifi network set with the SetupWiFiConnection sketch 45 | Serial.println("Waiting for connection to WiFi"); 46 | while (WiFi.status() != WL_CONNECTED) { 47 | delay(1000); 48 | Serial.print('.'); 49 | } 50 | Serial.println(); 51 | Serial.println("Connected to WiFi network."); 52 | 53 | Serial.println("Starting connection to server..."); 54 | if (client.connect(server, 80)) { 55 | Serial.println("connected to server"); 56 | 57 | client.println("GET /asciilogo.txt HTTP/1.1"); 58 | client.print("Host: "); 59 | client.println(server); 60 | client.println("Connection: close"); 61 | client.println(); 62 | client.flush(); 63 | } 64 | } 65 | 66 | void loop() { 67 | 68 | // if there are incoming bytes available 69 | // from the server, read them and print them 70 | while (client.available()) { 71 | char c = client.read(); 72 | Serial.write(c); 73 | } 74 | 75 | // if the server's disconnected, stop the client 76 | if (!client.connected()) { 77 | Serial.println(); 78 | Serial.println("disconnecting from server."); 79 | client.stop(); 80 | 81 | // do nothing forevermore 82 | while (true); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /examples/Basic/WebServer/WebServer.ino: -------------------------------------------------------------------------------- 1 | /* 2 | WiFi Web Server 3 | 4 | A simple web server that shows the value of the analog input pins. 5 | 6 | created 13 July 2010 7 | by dlf (Metodo2 srl) 8 | modified 31 May 2012 9 | by Tom Igoe 10 | modified in Jul 2019 for WiFiEspAT library 11 | by Juraj Andrassy https://github.com/jandrassy 12 | */ 13 | 14 | #include 15 | 16 | // Emulate Serial1 on pins 6/7 if not present 17 | #if defined(ARDUINO_ARCH_AVR) && !defined(HAVE_HWSERIAL1) 18 | #include 19 | SoftwareSerial Serial1(6, 7); // RX, TX 20 | #define AT_BAUD_RATE 9600 21 | #else 22 | #define AT_BAUD_RATE 115200 23 | #endif 24 | 25 | WiFiServer server(80); 26 | 27 | void setup() { 28 | 29 | Serial.begin(115200); 30 | while (!Serial); 31 | 32 | Serial1.begin(AT_BAUD_RATE); 33 | WiFi.init(Serial1); 34 | 35 | if (WiFi.status() == WL_NO_MODULE) { 36 | Serial.println("Communication with WiFi module failed!"); 37 | // don't continue 38 | while (true); 39 | } 40 | 41 | // waiting for connection to Wifi network set with the SetupWiFiConnection sketch 42 | Serial.println("Waiting for connection to WiFi"); 43 | while (WiFi.status() != WL_CONNECTED) { 44 | delay(1000); 45 | Serial.print('.'); 46 | } 47 | Serial.println(); 48 | 49 | server.begin(); 50 | 51 | IPAddress ip = WiFi.localIP(); 52 | Serial.println(); 53 | Serial.println("Connected to WiFi network."); 54 | Serial.print("To access the server, enter \"http://"); 55 | Serial.print(ip); 56 | Serial.println("/\" in web browser."); 57 | } 58 | 59 | void loop() { 60 | 61 | WiFiClient client = server.accept(); 62 | if (client) { 63 | IPAddress ip = client.remoteIP(); 64 | Serial.print("new client "); 65 | Serial.println(ip); 66 | 67 | while (client.connected()) { 68 | if (client.available()) { 69 | String line = client.readStringUntil('\n'); 70 | line.trim(); 71 | Serial.println(line); 72 | 73 | // if you've gotten to the end of the HTTP header (the line is blank), 74 | // the http request has ended, so you can send a reply 75 | if (line.length() == 0) { 76 | // send a standard http response header 77 | client.println("HTTP/1.1 200 OK"); 78 | client.println("Content-Type: text/html"); 79 | client.println("Connection: close"); // the connection will be closed after completion of the response 80 | client.println("Refresh: 5"); // refresh the page automatically every 5 sec 81 | client.println(); 82 | client.println(""); 83 | client.println(""); 84 | // output the value of analog input pins 85 | for (int analogChannel = 0; analogChannel < 4; analogChannel++) { 86 | int sensorReading = analogRead(analogChannel); 87 | client.print("analog input "); 88 | client.print(analogChannel); 89 | client.print(" is "); 90 | client.print(sensorReading); 91 | client.println("
"); 92 | } 93 | client.println(""); 94 | client.flush(); 95 | break; 96 | } 97 | } 98 | } 99 | 100 | // close the connection: 101 | client.stop(); 102 | Serial.println("client disconnected"); 103 | } 104 | } 105 | 106 | -------------------------------------------------------------------------------- /examples/Basic/WiFiSSLClient/WiFiSSLClient.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Web client 3 | 4 | This sketch connects to https 5 | using the WiFi module. 6 | 7 | created 13 July 2010 8 | by dlf (Metodo2 srl) 9 | modified 31 May 2012 10 | by Tom Igoe 11 | modified in Jul 2019 for WiFiEspAT library 12 | by Juraj Andrassy https://github.com/jandrassy 13 | */ 14 | 15 | #include 16 | 17 | const char* server = "api.github.com"; 18 | 19 | WiFiSSLClient client; 20 | 21 | void setup() { 22 | Serial.begin(115200); 23 | while (!Serial); 24 | 25 | Serial1.begin(115200); 26 | WiFi.init(Serial1); 27 | 28 | if (WiFi.status() == WL_NO_MODULE) { 29 | Serial.println(); 30 | Serial.println("Communication with WiFi module failed!"); 31 | // don't continue 32 | while (true); 33 | } 34 | 35 | // waiting for connection to Wifi network set with the SetupWiFiConnection sketch 36 | Serial.println("Waiting for connection to WiFi"); 37 | while (WiFi.status() != WL_CONNECTED) { 38 | delay(1000); 39 | Serial.print('.'); 40 | } 41 | Serial.println(); 42 | Serial.println("Connected to WiFi network."); 43 | 44 | Serial.println("Starting connection to server..."); 45 | if (client.connect(server, 443)) { // port 443 is the default https port 46 | Serial.println("connected to server"); 47 | 48 | client.println("GET /repos/jandrassy/WiFiEspAT/commits/master/status HTTP/1.1"); 49 | client.print("Host: "); 50 | client.println(server); 51 | client.println("User-Agent: Arduino"); 52 | client.println("Connection: close"); 53 | client.println(); 54 | client.flush(); 55 | } 56 | } 57 | 58 | void loop() { 59 | 60 | // if there are incoming bytes available 61 | // from the server, read them and print them 62 | while (client.available()) { 63 | char c = client.read(); 64 | Serial.write(c); 65 | } 66 | 67 | // if the server's disconnected, stop the client 68 | if (!client.connected()) { 69 | Serial.println(); 70 | Serial.println("disconnecting from server."); 71 | client.stop(); 72 | 73 | // do nothing forevermore 74 | while (true); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /examples/Special/PagerServer/PagerServer.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Pager Server 3 | 4 | A simple server that echoes any incoming messages to all 5 | connected clients. Connect two or more telnet sessions 6 | to see how server.available() and server.print() works. 7 | 8 | For version 2 of the WiFiEspAT library this example uses 9 | ArduinoWiFiServer from the NetApiHelpers library 10 | because the WiFiEspAT library doesn't directly support 11 | server.available() and print-to-all-clients anymore. 12 | 13 | created in September 2020 for WiFiEspAT library 14 | modified in July 2024 for version 2 of the WiFiEspAT 15 | by Juraj Andrassy https://github.com/jandrassy 16 | 17 | */ 18 | #include 19 | #include 20 | 21 | #define SERVER_MAX_MONITORED_CLIENTS 3 // applied in ArduinoWiFiServer.h 22 | #include 23 | 24 | // Emulate Serial1 on pins 6/7 if not present 25 | #if defined(ARDUINO_ARCH_AVR) && !defined(HAVE_HWSERIAL1) 26 | #include 27 | SoftwareSerial Serial1(6, 7); // RX, TX 28 | #define AT_BAUD_RATE 9600 29 | #else 30 | #define AT_BAUD_RATE 115200 31 | #endif 32 | 33 | const int CLIENT_CONN_TIMEOUT = 3600; // seconds. 1 hour 34 | 35 | ArduinoWiFiServer server(2323); 36 | 37 | void setup() { 38 | 39 | Serial.begin(115200); 40 | while (!Serial); 41 | 42 | Serial1.begin(AT_BAUD_RATE); 43 | WiFi.init(Serial1); 44 | 45 | if (WiFi.status() == WL_NO_MODULE) { 46 | Serial.println(); 47 | Serial.println("Communication with WiFi module failed!"); 48 | // don't continue 49 | while (true); 50 | } 51 | 52 | // waiting for connection to Wifi network set with the SetupWiFiConnection sketch 53 | Serial.println("Waiting for connection to WiFi"); 54 | while (WiFi.status() != WL_CONNECTED) { 55 | delay(1000); 56 | Serial.print('.'); 57 | } 58 | Serial.println(); 59 | 60 | server.begin(SERVER_MAX_MONITORED_CLIENTS, CLIENT_CONN_TIMEOUT); 61 | 62 | IPAddress ip = WiFi.localIP(); 63 | Serial.println(); 64 | Serial.println("Connected to WiFi network."); 65 | Serial.print("To access the server, connect with Telnet client to "); 66 | Serial.print(ip); 67 | Serial.println(" 2323"); 68 | } 69 | 70 | void loop() { 71 | 72 | WiFiClient client = server.available(); // returns first client which has data to read or a 'false' client 73 | if (client) { // client is true only if it is connected and has data to read 74 | String s = client.readStringUntil('\n'); // read the message incoming from one of the clients 75 | s.trim(); // trim eventual \r 76 | Serial.println(s); // print the message to Serial Monitor 77 | client.print("echo: "); // this is only for the sending client 78 | server.println(s); // send the message to all connected clients 79 | server.flush(); // flush the buffers 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /examples/Special/SNTPTime/SNTPTime.ino: -------------------------------------------------------------------------------- 1 | /* 2 | SNTP Time with customized AT firmware 3 | It doesn't work with standard Espressif AT firmware 4 | 5 | created in Jul 2019 for WiFiEspAT library 6 | by Juraj Andrassy https://github.com/jandrassy 7 | */ 8 | 9 | #include 10 | #include // in LibraryManager as "Time" 11 | 12 | // Emulate Serial1 on pins 6/7 if not present 13 | #if defined(ARDUINO_ARCH_AVR) && !defined(HAVE_HWSERIAL1) 14 | #include 15 | SoftwareSerial Serial1(6, 7); // RX, TX 16 | #define AT_BAUD_RATE 9600 17 | #else 18 | #define AT_BAUD_RATE 115200 19 | #endif 20 | 21 | const int8_t TIME_ZONE = 2; // UTC + 2 22 | 23 | void setup() { 24 | 25 | Serial.begin(115200); 26 | while (!Serial); 27 | 28 | Serial1.begin(AT_BAUD_RATE); 29 | WiFi.init(Serial1); 30 | 31 | if (WiFi.status() == WL_NO_MODULE) { 32 | Serial.println("Communication with WiFi module failed!"); 33 | // don't continue 34 | while (true); 35 | } 36 | 37 | // waiting for connection to Wifi network set with the SetupWiFiConnection sketch 38 | Serial.println("Waiting for connection to WiFi"); 39 | while (WiFi.status() != WL_CONNECTED) { 40 | delay(1000); 41 | Serial.print('.'); 42 | } 43 | Serial.println(); 44 | 45 | WiFi.sntp("us.pool.ntp.org"); 46 | 47 | Serial.println("Waiting for SNTP"); 48 | while (WiFi.getTime() < SECS_YR_2000) { 49 | delay(1000); 50 | Serial.print('.'); 51 | } 52 | Serial.println(); 53 | 54 | setTime(WiFi.getTime() + (SECS_PER_HOUR * TIME_ZONE)); 55 | 56 | } 57 | 58 | void loop() { 59 | 60 | char buff[20]; 61 | sprintf(buff, "%02d-%02d-%02d %02d:%02d:%02d", year(), month(), day(), hour(), minute(), second()); 62 | Serial.println(buff); 63 | 64 | delay(1000); 65 | } 66 | 67 | -------------------------------------------------------------------------------- /examples/Special/WiFiEspAtBlynk/WiFiEspAtBlynk.ino: -------------------------------------------------------------------------------- 1 | /************************************************************* 2 | Download latest Blynk library here: 3 | https://github.com/blynkkk/blynk-library/releases/latest 4 | 5 | Blynk is a platform with iOS and Android apps to control 6 | Arduino, Raspberry Pi and the likes over the Internet. 7 | You can easily build graphic interfaces for all your 8 | projects by simply dragging and dropping widgets. 9 | 10 | Downloads, docs, tutorials: http://www.blynk.cc 11 | Sketch generator: http://examples.blynk.cc 12 | Blynk community: http://community.blynk.cc 13 | Follow us: http://www.fb.com/blynkapp 14 | http://twitter.com/blynk_app 15 | 16 | Blynk library is licensed under MIT license 17 | This example code is in public domain. 18 | 19 | modified in Jul 2019 for WiFiEspAT library 20 | by Juraj Andrassy https://github.com/jandrassy 21 | 22 | *************************************************************/ 23 | 24 | /* Comment this out to disable prints and save space */ 25 | #define BLYNK_PRINT Serial 26 | 27 | /* Fill in information from Blynk Device Info here */ 28 | //#define BLYNK_TEMPLATE_ID "TMPxxxxxx" 29 | //#define BLYNK_TEMPLATE_NAME "Device" 30 | //#define BLYNK_AUTH_TOKEN "YourAuthToken" 31 | 32 | #include 33 | #include 34 | 35 | // Emulate Serial1 on pins 6/7 if not present 36 | #if defined(ARDUINO_ARCH_AVR) && !defined(HAVE_HWSERIAL1) 37 | #include 38 | SoftwareSerial Serial1(6, 7); // RX, TX 39 | #define AT_BAUD_RATE 9600 40 | #else 41 | #define AT_BAUD_RATE 115200 42 | #endif 43 | 44 | void setup() { 45 | Serial.begin(115200); 46 | while (!Serial); 47 | 48 | Serial1.begin(AT_BAUD_RATE); 49 | WiFi.init(Serial1); 50 | 51 | if (WiFi.status() == WL_NO_MODULE) { 52 | Serial.println("Communication with WiFi module failed!"); 53 | // don't continue 54 | while (true); 55 | } 56 | 57 | // waiting for connection to Wifi network set with the SetupWiFiConnection sketch 58 | Serial.println("Waiting for connection to WiFi"); 59 | while (WiFi.status() != WL_CONNECTED) { 60 | delay(1000); 61 | Serial.print('.'); 62 | } 63 | Serial.println("You're connected to the network"); 64 | 65 | Blynk.config(BLYNK_AUTH_TOKEN, BLYNK_DEFAULT_DOMAIN, BLYNK_DEFAULT_PORT); 66 | while(Blynk.connect() != true) {} 67 | } 68 | 69 | void loop() 70 | { 71 | Blynk.run(); 72 | } 73 | 74 | -------------------------------------------------------------------------------- /examples/Tools/ChangeATBaudRate/ChangeATBaudRate.ino: -------------------------------------------------------------------------------- 1 | /* 2 | The purpose of this sketch is to switch the default baud rate 3 | of the AT firmware to SoftwareSerial friendly 9600 baud. 4 | SoftwareSerial has problems to receive at 115200 baud, but is 5 | able to send a short string at 115200 baud. So we send 6 | at 115200 baud over SoftwareSerial the AT command to change 7 | the baud rate to 9600 baud. 8 | 9 | created in Jul 2019 for WiFiEspAT library 10 | by Juraj Andrassy https://github.com/jandrassy 11 | */ 12 | 13 | #include 14 | 15 | // Emulate Serial1 on pins 6/7 if not present 16 | #if defined(ARDUINO_ARCH_AVR) && !defined(HAVE_HWSERIAL1) 17 | #include 18 | SoftwareSerial Serial1(6, 7); // RX, TX 19 | #define AT_BAUD_RATE 9600 20 | #else 21 | #define AT_BAUD_RATE 115200 22 | #endif 23 | 24 | #define OLD_BAUD_RATE 115200 25 | #define NEW_BAUD_RATE 9600 26 | 27 | void setup() { 28 | 29 | Serial.begin(115200); 30 | while (!Serial); 31 | 32 | Serial1.begin(OLD_BAUD_RATE); 33 | 34 | Serial.println("Sending baud rate change..."); 35 | Serial1.print("AT+UART_DEF="); 36 | Serial1.print(NEW_BAUD_RATE); 37 | Serial1.println(",8,1,0,0"); 38 | delay(100); 39 | // we can't expect a readable answer over SoftwareSerial at 115200 40 | 41 | Serial1.begin(NEW_BAUD_RATE); 42 | WiFi.init(Serial1); 43 | const char* ver = WiFi.firmwareVersion(); 44 | if (ver[0] != 0) { 45 | Serial.print("Baud rate "); 46 | Serial.print(NEW_BAUD_RATE); 47 | Serial.println(" baud is working."); 48 | Serial.print("Firmware version is "); 49 | Serial.println(ver); 50 | } else { 51 | Serial.print("Error communicating at "); 52 | Serial.print(NEW_BAUD_RATE); 53 | Serial.println(" baud."); 54 | } 55 | } 56 | 57 | void loop() { 58 | } 59 | -------------------------------------------------------------------------------- /examples/Tools/CheckFirmware/CheckFirmware.ino: -------------------------------------------------------------------------------- 1 | /* 2 | This sketch checks the version of the AT firmware in attached esp8266. 3 | 4 | created in Jul 2019 for WiFiEspAT library 5 | by Juraj Andrassy https://github.com/jandrassy 6 | 7 | */ 8 | 9 | #include 10 | 11 | // Emulate Serial1 on pins 6/7 if not present 12 | #if defined(ARDUINO_ARCH_AVR) && !defined(HAVE_HWSERIAL1) 13 | #include 14 | SoftwareSerial Serial1(6, 7); // RX, TX 15 | #define AT_BAUD_RATE 9600 16 | #else 17 | #define AT_BAUD_RATE 115200 18 | #endif 19 | 20 | void setup() { 21 | 22 | Serial.begin(115200); 23 | while (!Serial); 24 | 25 | Serial1.begin(AT_BAUD_RATE); 26 | WiFi.init(Serial1); 27 | 28 | if (WiFi.status() == WL_NO_MODULE) { 29 | Serial.println("Communication with WiFi module failed!"); 30 | // don't continue 31 | while (true); 32 | } 33 | 34 | char ver[10]; 35 | int major = 0; 36 | int minor = 0; 37 | if (WiFi.firmwareVersion(ver)) { 38 | Serial.print("AT firmware version "); 39 | Serial.println(ver); 40 | char* tok = strtok(ver, "."); 41 | major = atoi(tok); 42 | tok = strtok(NULL, "."); 43 | minor = atoi(tok); 44 | if (major == 2 && minor == 0) { 45 | Serial.println("AT firmware version 2.0 doesn't support passive receive mode and can't be used with the WiFiEspAt library"); 46 | } else if (major < 1 || (major == 1 && minor < 7)) { 47 | Serial.println("WiWiEspAT library requires at least version 1.7.0 of AT firmware (but not 2.0)"); 48 | } else { 49 | Serial.println("AT firmware is OK for the WiFiEspAT library."); 50 | #ifdef WIFIESPAT1 51 | if (major > 1) { 52 | Serial.println("For AT firmware version 2 comment out #define WIFIESPAT1 in EspAtDrvTypes.h"); 53 | } 54 | #else 55 | if (major == 1) { 56 | Serial.println("For AT firmware version 1 add #define WIFIESPAT1 in EspAtDrvTypes.h"); 57 | } 58 | #endif 59 | } 60 | } else { 61 | Serial.println("Error getting AT firmware version"); 62 | } 63 | 64 | 65 | } 66 | 67 | void loop() { 68 | } 69 | -------------------------------------------------------------------------------- /examples/Tools/PrintPersistentSettings/PrintPersistentSettings.ino: -------------------------------------------------------------------------------- 1 | /* 2 | This sketch prints the configuration remembered in the esp8266. 3 | 4 | created in August 2019 for WiFiEspAT library 5 | by Juraj Andrassy https://github.com/jandrassy 6 | 7 | */ 8 | #include 9 | 10 | // Emulate Serial1 on pins 6/7 if not present 11 | #if defined(ARDUINO_ARCH_AVR) && !defined(HAVE_HWSERIAL1) 12 | #include "SoftwareSerial.h" 13 | SoftwareSerial Serial1(6, 7); // RX, TX 14 | #define AT_BAUD_RATE 9600 15 | #else 16 | #define AT_BAUD_RATE 115200 17 | #endif 18 | 19 | void setup() { 20 | 21 | Serial.begin(115200); 22 | while (!Serial); 23 | 24 | Serial1.begin(AT_BAUD_RATE); 25 | WiFi.init(Serial1); 26 | 27 | if (WiFi.status() == WL_NO_MODULE) { 28 | Serial.println(); 29 | Serial.println("Communication with WiFi module failed!"); 30 | // don't continue 31 | while (true); 32 | } 33 | 34 | int status = WiFi.beginAP(); 35 | 36 | if (status == WL_AP_LISTENING) { 37 | Serial.println(); 38 | printApStatus(); 39 | WiFi.endAP(); 40 | } else { 41 | Serial.println(); 42 | Serial.println("AP failed to start."); 43 | } 44 | 45 | // waiting for connection to Wifi network set with the SetupWiFiConnection sketch 46 | Serial.println(); 47 | Serial.println("Waiting for connection to WiFi"); 48 | for (int i = 0; i < 10; i++) { 49 | if (WiFi.status() == WL_CONNECTED) 50 | break; 51 | delay(1000); 52 | Serial.print('.'); 53 | } 54 | Serial.println(); 55 | 56 | printWifiStatus(); 57 | } 58 | 59 | void loop() { 60 | } 61 | 62 | void printWifiStatus() { 63 | 64 | uint8_t mac[6]; 65 | WiFi.macAddress(mac); 66 | Serial.print("Station MAC: "); 67 | printMacAddress(mac); 68 | 69 | char ssid[33]; 70 | WiFi.SSID(ssid); 71 | if (strlen(ssid) == 0) { 72 | Serial.println("Station is not connected"); 73 | return; 74 | } 75 | char hostname[33]; 76 | WiFi.hostname(hostname); 77 | Serial.print("hostname "); 78 | Serial.println(hostname); 79 | 80 | Serial.print("SSID: "); 81 | Serial.println(ssid); 82 | 83 | uint8_t bssid[6]; 84 | WiFi.BSSID(bssid); 85 | Serial.print("BSSID: "); 86 | printMacAddress(bssid); 87 | 88 | long rssi = WiFi.RSSI(); 89 | Serial.print("RSSI:"); 90 | Serial.print(rssi); 91 | Serial.println(" dBm"); 92 | 93 | bool dhcp = WiFi.dhcpIsEnabled(); 94 | Serial.print("DHCP: "); 95 | Serial.println(dhcp ? "enabled" : "disabled"); 96 | 97 | IPAddress ip = WiFi.localIP(); 98 | Serial.print("IP Address: "); 99 | Serial.println(ip); 100 | 101 | IPAddress gw = WiFi.gatewayIP(); 102 | Serial.print("gateway IP Address: "); 103 | Serial.println(gw); 104 | 105 | IPAddress mask = WiFi.subnetMask(); 106 | Serial.print("subnet IP mask: "); 107 | Serial.println(mask); 108 | 109 | Serial.print("DNS server: "); 110 | IPAddress dns1 = WiFi.dnsIP(); 111 | if (dns1 == INADDR_NONE) { 112 | Serial.println("not set"); 113 | } else { 114 | dns1.printTo(Serial); 115 | Serial.println(); 116 | IPAddress dns2 = WiFi.dnsIP(1); 117 | if (dns2 != INADDR_NONE) { 118 | Serial.print("DNS server2: "); 119 | dns2.printTo(Serial); 120 | Serial.println(); 121 | } 122 | } 123 | } 124 | 125 | void printApStatus() { 126 | 127 | uint8_t mac[6]; 128 | WiFi.apMacAddress(mac); 129 | Serial.print("AP MAC: "); 130 | printMacAddress(mac); 131 | 132 | char ssid[33]; 133 | WiFi.apSSID(ssid); 134 | Serial.print("AP SSID: "); 135 | Serial.print(ssid); 136 | if (WiFi.apIsHidden()) { 137 | Serial.print(" (is hidden)"); 138 | } 139 | Serial.println(); 140 | 141 | Serial.print("AP max stations "); 142 | Serial.println(WiFi.apMaxConnections()); 143 | 144 | Serial.print("AP encryption: "); 145 | switch (WiFi.apEncryptionType()) { 146 | case ENC_TYPE_NONE: 147 | Serial.println("open (no encryption)"); 148 | break; 149 | case ENC_TYPE_TKIP: 150 | Serial.println("WPA"); 151 | break; 152 | case ENC_TYPE_CCMP: 153 | Serial.println("WPA2"); 154 | break; 155 | } 156 | 157 | if (WiFi.apEncryptionType() != ENC_TYPE_NONE) { 158 | char pass[65]; 159 | WiFi.apPassphrase(pass); 160 | Serial.print("AP passphrase: "); 161 | Serial.println(pass); 162 | } 163 | 164 | IPAddress ip = WiFi.apIP(); 165 | Serial.print("AP IP Address: "); 166 | Serial.println(ip); 167 | 168 | IPAddress gw = WiFi.apGatewayIP(); 169 | Serial.print("AP gateway IP Address: "); 170 | Serial.println(gw); 171 | 172 | IPAddress mask = WiFi.apSubnetMask(); 173 | Serial.print("AP subnet mask: "); 174 | Serial.println(mask); 175 | 176 | Serial.print("AP DHCP: "); 177 | Serial.println(WiFi.apDhcpIsEnabled() ? "enabled" : "disabled"); 178 | } 179 | 180 | void printMacAddress(byte mac[]) { 181 | for (int i = 0; i < 6; i++) { 182 | if (i > 0) { 183 | Serial.print(":"); 184 | } 185 | if (mac[i] < 16) { 186 | Serial.print("0"); 187 | } 188 | Serial.print(mac[i], HEX); 189 | } 190 | Serial.println(); 191 | } 192 | 193 | -------------------------------------------------------------------------------- /examples/Tools/SerialPassthrough/SerialPassthrough.ino: -------------------------------------------------------------------------------- 1 | /* 2 | SerialPassthrough sketch 3 | with SoftwareSeral option 4 | 5 | created in August 2019 for WiFiEspAT library 6 | by Juraj Andrassy https://github.com/jandrassy 7 | */ 8 | 9 | //#define SAMD_FLOW_CONTROL 10 | 11 | // Emulate Serial1 on pins 6/7 if not present 12 | #if defined(ARDUINO_ARCH_AVR) && !defined(HAVE_HWSERIAL1) 13 | #include "SoftwareSerial.h" 14 | SoftwareSerial SerialAT(6, 7); // RX, TX 15 | #define AT_BAUD_RATE 9600 16 | #elif defined(ARDUINO_ARCH_SAMD) && defined(SAMD_FLOW_CONTROL) 17 | #include "wiring_private.h" 18 | Uart SerialAT(&sercom3, 0, 1, SERCOM_RX_PAD_1, UART_TX_PAD_0, 2, 255); 19 | #define AT_BAUD_RATE 115200 20 | #else 21 | #define SerialAT Serial1 22 | #define AT_BAUD_RATE 115200 23 | #endif 24 | 25 | void setup() { 26 | 27 | Serial.begin(115200); 28 | while (!Serial); 29 | 30 | SerialAT.begin(AT_BAUD_RATE); 31 | #if defined(ARDUINO_ARCH_SAMD) && defined(SAMD_FLOW_CONTROL) 32 | pinPeripheral(0, PIO_SERCOM); 33 | pinPeripheral(1, PIO_SERCOM); 34 | #endif 35 | } 36 | 37 | void loop() { 38 | while (Serial.available()) { 39 | SerialAT.write(Serial.read()); 40 | } 41 | while (SerialAT.available()) { 42 | Serial.write(SerialAT.read()); 43 | } 44 | } 45 | 46 | #if defined(ARDUINO_ARCH_SAMD) && defined(SAMD_FLOW_CONTROL) 47 | void SERCOM3_Handler() { 48 | SerialAT.IrqHandler(); 49 | } 50 | #endif 51 | -------------------------------------------------------------------------------- /examples/Tools/SetupPersistentAP/SetupPersistentAP.ino: -------------------------------------------------------------------------------- 1 | /* 2 | This sketch configures SoftAP and sets the AT firmware to remember it. 3 | You can start the remembered AP with WiFi.beginAP() without parameters. 4 | 5 | created in Jul 2019 for WiFiEspAT library 6 | by Juraj Andrassy https://github.com/jandrassy 7 | 8 | */ 9 | #include 10 | 11 | #include "arduino_secrets.h" 12 | ///////please enter your sensitive data in the Secret tab/arduino_secrets.h 13 | const char ssid[] = SECRET_SSID; // your network SSID (name) 14 | const char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) 15 | 16 | // Emulate Serial1 on pins 6/7 if not present 17 | #if defined(ARDUINO_ARCH_AVR) && !defined(HAVE_HWSERIAL1) 18 | #include "SoftwareSerial.h" 19 | SoftwareSerial Serial1(6, 7); // RX, TX 20 | #define AT_BAUD_RATE 9600 21 | #else 22 | #define AT_BAUD_RATE 115200 23 | #endif 24 | 25 | void setup() { 26 | 27 | Serial.begin(115200); 28 | while (!Serial); 29 | 30 | Serial1.begin(AT_BAUD_RATE); 31 | WiFi.init(Serial1); 32 | 33 | if (WiFi.status() == WL_NO_MODULE) { 34 | Serial.println(); 35 | Serial.println("Communication with WiFi module failed!"); 36 | // don't continue 37 | while (true); 38 | } 39 | 40 | WiFi.setPersistent(); // set the following settings as persistent 41 | 42 | WiFi.endAP(); // to disable default automatic start of persistent AP at startup 43 | 44 | // use this lines for custom SoftAP IP. it determines the IP of stations too 45 | // IPAddress ip(192, 168, 2, 1); 46 | // WiFi.configureAP(ip); 47 | 48 | Serial.println(); 49 | Serial.print("Start AP with SSID: "); 50 | Serial.println(ssid); 51 | 52 | // int status = WiFi.beginAP(ssid); // for AP open without passphrase/encryption 53 | int status = WiFi.beginAP(ssid, pass); 54 | 55 | if (status == WL_AP_LISTENING) { 56 | Serial.println(); 57 | delay(1000); // startup of AP 58 | Serial.println("AP started"); 59 | printApStatus(); 60 | } else { 61 | Serial.println(); 62 | Serial.println("AP failed to start."); 63 | } 64 | 65 | } 66 | 67 | void loop() { 68 | } 69 | 70 | void printApStatus() { 71 | 72 | char ssid[33]; 73 | WiFi.apSSID(ssid); 74 | Serial.print("AP SSID: "); 75 | Serial.println(ssid); 76 | 77 | Serial.print("AP can handle "); 78 | Serial.print(WiFi.apMaxConnections()); 79 | Serial.println(" stations."); 80 | 81 | if (WiFi.apEncryptionType() == ENC_TYPE_NONE) { 82 | Serial.println("AP is open (no encryption)"); 83 | } else { 84 | Serial.print("Encryption of AP is "); 85 | switch (WiFi.apEncryptionType()) { 86 | case ENC_TYPE_TKIP: 87 | Serial.println("WPA."); 88 | break; 89 | case ENC_TYPE_CCMP: 90 | Serial.println("WPA2."); 91 | break; 92 | } 93 | char pass[65]; 94 | WiFi.apPassphrase(pass); 95 | Serial.print("AP passphrase: "); 96 | Serial.println(pass); 97 | } 98 | 99 | uint8_t mac[6]; 100 | WiFi.apMacAddress(mac); 101 | Serial.print("AP MAC: "); 102 | printMacAddress(mac); 103 | 104 | IPAddress ip = WiFi.apIP(); 105 | Serial.print("AP IP Address: "); 106 | Serial.println(ip); 107 | } 108 | 109 | void printMacAddress(byte mac[]) { 110 | for (int i = 0; i < 6; i++) { 111 | if (i > 0) { 112 | Serial.print(":"); 113 | } 114 | if (mac[i] < 16) { 115 | Serial.print("0"); 116 | } 117 | Serial.print(mac[i], HEX); 118 | } 119 | Serial.println(); 120 | } 121 | 122 | -------------------------------------------------------------------------------- /examples/Tools/SetupPersistentAP/arduino_secrets.h: -------------------------------------------------------------------------------- 1 | #define SECRET_SSID "WiFiEspAT" 2 | #define SECRET_PASS "esp-8266" 3 | -------------------------------------------------------------------------------- /examples/Tools/SetupPersistentWiFiConnection/SetupPersistentWiFiConnection.ino: -------------------------------------------------------------------------------- 1 | /* 2 | This sketch connects to a WiFi network and sets the AT firmware 3 | to remember this WiFi network and auto-connect to it at startup. 4 | 5 | based on the ConnectWithWPA.ino 6 | created 13 July 2010 7 | by dlf (Metodo2 srl) 8 | modified 31 May 2012 9 | by Tom Igoe 10 | 11 | created in Jul 2019 for WiFiEspAT library 12 | by Juraj Andrassy https://github.com/jandrassy 13 | 14 | */ 15 | #include 16 | 17 | #include "arduino_secrets.h" 18 | ///////please enter your sensitive data in the Secret tab/arduino_secrets.h 19 | const char ssid[] = SECRET_SSID; // your network SSID (name) 20 | const char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) 21 | 22 | // Emulate Serial1 on pins 6/7 if not present 23 | #if defined(ARDUINO_ARCH_AVR) && !defined(HAVE_HWSERIAL1) 24 | #include "SoftwareSerial.h" 25 | SoftwareSerial Serial1(6, 7); // RX, TX 26 | #define AT_BAUD_RATE 9600 27 | #else 28 | #define AT_BAUD_RATE 115200 29 | #endif 30 | 31 | void setup() { 32 | 33 | Serial.begin(115200); 34 | while (!Serial); 35 | 36 | Serial1.begin(AT_BAUD_RATE); 37 | WiFi.init(Serial1); 38 | 39 | if (WiFi.status() == WL_NO_MODULE) { 40 | Serial.println(); 41 | Serial.println("Communication with WiFi module failed!"); 42 | // don't continue 43 | while (true); 44 | } 45 | 46 | WiFi.disconnect(); // to clear the way. not persistent 47 | 48 | WiFi.setPersistent(); // set the following WiFi connection as persistent 49 | 50 | WiFi.endAP(); // to disable default automatic start of persistent AP at startup 51 | 52 | // uncomment this lines for persistent static IP. set addresses valid for your network 53 | // IPAddress ip(192, 168, 1, 9); 54 | // IPAddress gw(192, 168, 1, 1); 55 | // IPAddress nm(255, 255, 255, 0); 56 | // WiFi.config(ip, gw, gw, nm); 57 | 58 | Serial.println(); 59 | Serial.print("Attempting to connect to SSID: "); 60 | Serial.println(ssid); 61 | 62 | // use following lines if you want to connect with bssid 63 | // const byte bssid[] = {0x8A, 0x2F, 0xC3, 0xE9, 0x25, 0xC0}; 64 | // int status = WiFi.begin(ssid, pass, bssid); 65 | 66 | int status = WiFi.begin(ssid, pass); 67 | 68 | if (status == WL_CONNECTED) { 69 | Serial.println(); 70 | Serial.println("Connected to WiFi network."); 71 | printWifiStatus(); 72 | } else { 73 | WiFi.disconnect(); // remove the WiFi connection 74 | Serial.println(); 75 | Serial.println("Connection to WiFi network failed."); 76 | } 77 | } 78 | 79 | void loop() { 80 | } 81 | 82 | void printWifiStatus() { 83 | 84 | // print the SSID of the network you're attached to: 85 | char ssid[33]; 86 | WiFi.SSID(ssid); 87 | Serial.print("SSID: "); 88 | Serial.println(ssid); 89 | 90 | // print the BSSID of the network you're attached to: 91 | uint8_t bssid[6]; 92 | WiFi.BSSID(bssid); 93 | Serial.print("BSSID: "); 94 | printMacAddress(bssid); 95 | 96 | uint8_t mac[6]; 97 | WiFi.macAddress(mac); 98 | Serial.print("MAC: "); 99 | printMacAddress(mac); 100 | 101 | // print your board's IP address: 102 | IPAddress ip = WiFi.localIP(); 103 | Serial.print("IP Address: "); 104 | Serial.println(ip); 105 | 106 | // print the received signal strength: 107 | long rssi = WiFi.RSSI(); 108 | Serial.print("signal strength (RSSI):"); 109 | Serial.print(rssi); 110 | Serial.println(" dBm"); 111 | } 112 | 113 | void printMacAddress(byte mac[]) { 114 | for (int i = 0; i < 6; i++) { 115 | if (i > 0) { 116 | Serial.print(":"); 117 | } 118 | if (mac[i] < 16) { 119 | Serial.print("0"); 120 | } 121 | Serial.print(mac[i], HEX); 122 | } 123 | Serial.println(); 124 | } 125 | 126 | -------------------------------------------------------------------------------- /examples/Tools/SetupPersistentWiFiConnection/arduino_secrets.h: -------------------------------------------------------------------------------- 1 | #define SECRET_SSID "" 2 | #define SECRET_PASS "" 3 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=WiFiEspAT 2 | version=2.0.0 3 | author=Juraj Andrassy 4 | maintainer=Juraj Andrassy 5 | sentence=Enables network connection with esp8266 or esp32 as network adapter. 6 | paragraph=This library creates standard Arduino WiFi networking API over ESP AT commands. 7 | category=Communication 8 | url=https://github.com/jandrassy/WiFiEspAT 9 | architectures=* 10 | includes=WiFiEspAT.h 11 | dot_a_linkage=true 12 | -------------------------------------------------------------------------------- /mkrzero-esp-01s.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JAndrassy/WiFiEspAT/93f504bceb9a50a271fccc017d6e7bc525606b08/mkrzero-esp-01s.jpg -------------------------------------------------------------------------------- /src/WiFi.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the WiFiEspAT library for Arduino 3 | https://github.com/jandrassy/WiFiEspAT 4 | Copyright 2019 Juraj Andrassy 5 | 6 | This library is free software; you can redistribute it and/or 7 | modify it under the terms of the GNU Lesser General Public 8 | License as published by the Free Software Foundation; either 9 | version 2.1 of the License, or (at your option) any later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | Lesser General Public License for more details. 15 | 16 | You should have received a copy of the GNU Lesser General Public License 17 | along with this library. If not, see . 18 | */ 19 | 20 | #include "WiFi.h" 21 | #include "utility/EspAtDrv.h" 22 | 23 | // these statics are removed by compiler, if not used 24 | char WiFiClass::fwVersion[15] = {0}; 25 | char WiFiClass::ssid[33] = {0}; 26 | char WiFiClass::name[33] = {0}; // hostname 27 | WiFiApData WiFiClass::apDataInternal[WIFIESPAT_INTERNAL_AP_LIST_SIZE]; 28 | 29 | bool WiFiClass::init(Stream& serial, int8_t resetPin) { 30 | return init(&serial, resetPin); 31 | } 32 | 33 | bool WiFiClass::init(Stream* serial, int8_t resetPin) { 34 | bool ok = EspAtDrv.init(serial, resetPin); 35 | state = ok ? WL_IDLE_STATUS : WL_NO_MODULE; 36 | return ok; 37 | } 38 | 39 | bool WiFiClass::setPersistent(bool persistent) { 40 | return EspAtDrv.sysPersistent(persistent); 41 | } 42 | 43 | uint8_t WiFiClass::status() { 44 | if (state == WL_NO_MODULE) 45 | return state; 46 | int res = EspAtDrv.staStatus(); 47 | switch (res) { 48 | case -1: 49 | switch (EspAtDrv.getLastErrorCode()) { 50 | case EspAtDrvError::NOT_INITIALIZED: 51 | case EspAtDrvError::AT_NOT_RESPONDIG: 52 | state = WL_NO_MODULE; 53 | break; 54 | default: // some temporary error? 55 | break; // no change 56 | } 57 | break; 58 | case 2: 59 | case 3: 60 | case 4: 61 | state = WL_CONNECTED; 62 | break; 63 | case 0: // inactive 64 | case 1: // idle 65 | case 5: // STA disconnected 66 | switch (state) { 67 | case WL_CONNECT_FAILED: 68 | break; // no change 69 | case WL_CONNECTED: 70 | state = WL_CONNECTION_LOST; 71 | break; 72 | default: 73 | state = WL_DISCONNECTED; 74 | } 75 | } 76 | return state; 77 | } 78 | 79 | bool WiFiClass::setAutoConnect(bool autoConnect) { 80 | return EspAtDrv.staAutoConnect(autoConnect); 81 | } 82 | 83 | int WiFiClass::begin(const char* ssid, const char* passphrase, const uint8_t* bssid) { 84 | bool ok = EspAtDrv.joinAP(ssid, passphrase, bssid); 85 | state = ok ? WL_CONNECTED : WL_CONNECT_FAILED; 86 | return state; 87 | } 88 | 89 | int WiFiClass::beginEnterprise(const char* ssid, uint8_t method, const char* username, const char* passphrase, const char* identity, uint8_t security) { 90 | bool ok = EspAtDrv.joinEAP(ssid, method, identity, passphrase, username, security); 91 | state = ok ? WL_CONNECTED : WL_CONNECT_FAILED; 92 | return state; 93 | } 94 | 95 | int WiFiClass::disconnect(bool persistent) { 96 | if (EspAtDrv.quitAP(persistent)) { 97 | state = WL_DISCONNECTED; 98 | } 99 | return state; 100 | } 101 | 102 | bool WiFiClass::config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet) { 103 | 104 | if (local_ip == INADDR_NONE) 105 | return EspAtDrv.staEnableDHCP(); 106 | 107 | if (dns_server == INADDR_NONE) { 108 | dns_server = local_ip; 109 | dns_server[3] = 1; 110 | } 111 | return EspAtDrv.staStaticIp(local_ip, gateway, subnet) && setDNS(dns_server); 112 | } 113 | 114 | bool WiFiClass::setDNS(IPAddress dns_server1, IPAddress dns_server2) { 115 | return EspAtDrv.setDNS(dns_server1, dns_server2); 116 | } 117 | 118 | bool WiFiClass::setHostname(const char* name) { 119 | return EspAtDrv.setHostname(name); 120 | } 121 | 122 | const char* WiFiClass::hostname(char* buffer) { 123 | if (!EspAtDrv.hostnameQuery(buffer)) { 124 | buffer[0] = 0; 125 | } 126 | return buffer; 127 | } 128 | 129 | uint8_t* WiFiClass::macAddress(uint8_t* mac) { 130 | if (!EspAtDrv.staMacQuery(mac)) 131 | return nullptr; 132 | return mac; 133 | } 134 | 135 | IPAddress WiFiClass::localIP() { 136 | IPAddress ip; 137 | IPAddress gw; 138 | IPAddress mask; 139 | EspAtDrv.staIpQuery(ip, gw, mask); 140 | return ip; 141 | } 142 | 143 | IPAddress WiFiClass::gatewayIP() { 144 | IPAddress ip; 145 | IPAddress gw; 146 | IPAddress mask; 147 | EspAtDrv.staIpQuery(ip, gw, mask); 148 | return gw; 149 | } 150 | 151 | IPAddress WiFiClass::subnetMask() { 152 | IPAddress ip; 153 | IPAddress gw; 154 | IPAddress mask; 155 | EspAtDrv.staIpQuery(ip, gw, mask); 156 | return mask; 157 | } 158 | 159 | IPAddress WiFiClass::dnsIP(int n) { 160 | IPAddress dns1; 161 | IPAddress dns2; 162 | EspAtDrv.dnsQuery(dns1, dns2); 163 | switch (n) { 164 | case 0: 165 | return dns1; 166 | case 1: 167 | return dns2; 168 | } 169 | return IPAddress(0, 0, 0, 0); 170 | } 171 | 172 | bool WiFiClass::dhcpIsEnabled() { 173 | bool sta; 174 | bool ap; 175 | bool eth; 176 | if (!EspAtDrv.dhcpStateQuery(sta, ap, eth)) 177 | return false; 178 | return sta; 179 | } 180 | 181 | const char* WiFiClass::SSID(char* ssid) { 182 | uint8_t bssid[6] = {0}; 183 | uint8_t ch = 0; 184 | int8_t rssi = 0; 185 | EspAtDrv.apQuery(ssid, bssid, ch, rssi); 186 | return ssid; 187 | } 188 | 189 | uint8_t* WiFiClass::BSSID(uint8_t* bssid) { 190 | uint8_t ch = 0; 191 | int8_t rssi = 0; 192 | EspAtDrv.apQuery(nullptr, bssid, ch, rssi); 193 | return bssid; 194 | } 195 | 196 | uint8_t WiFiClass::channel() { 197 | uint8_t bssid[6] = {0}; 198 | uint8_t ch = 0; 199 | int8_t rssi = 0; 200 | EspAtDrv.apQuery(nullptr, bssid, ch, rssi); 201 | return ch; 202 | } 203 | 204 | int8_t WiFiClass::RSSI() { 205 | uint8_t bssid[6] = {0}; 206 | uint8_t ch = 0; 207 | int8_t rssi = 0; 208 | EspAtDrv.apQuery(nullptr, bssid, ch, rssi); 209 | return rssi; 210 | } 211 | 212 | int8_t WiFiClass::scanNetworks() { 213 | return scanNetworks(apDataInternal, WIFIESPAT_INTERNAL_AP_LIST_SIZE); 214 | } 215 | 216 | int8_t WiFiClass::scanNetworks(WiFiApData* _apData, uint8_t _apDataSize) { 217 | apData = _apData; 218 | apDataSize = _apDataSize; 219 | apDataLength = EspAtDrv.listAP(apData, apDataSize); 220 | return apDataLength; 221 | } 222 | 223 | const char* WiFiClass::SSID(uint8_t index) { 224 | if (index >= apDataLength) 225 | return nullptr; 226 | return apData[index].ssid; 227 | 228 | } 229 | 230 | uint8_t WiFiClass::encryptionType(uint8_t index) { 231 | if (index >= apDataLength) 232 | return ENC_TYPE_UNKNOWN; 233 | return mapAtEnc2ArduinoEnc(apData[index].enc); 234 | } 235 | 236 | uint8_t* WiFiClass::BSSID(uint8_t index, uint8_t* bssid) { 237 | if (index >= apDataLength) 238 | return nullptr; 239 | memcpy(bssid, apData[index].bssid, 6); 240 | return bssid; 241 | } 242 | 243 | uint8_t WiFiClass::channel(uint8_t index) { 244 | if (index >= apDataLength) 245 | return 0; 246 | return apData[index].channel; 247 | } 248 | 249 | int32_t WiFiClass::RSSI(uint8_t index) { 250 | if (index >= apDataLength) 251 | return 0; 252 | return apData[index].rssi; 253 | } 254 | 255 | bool WiFiClass::startMDNS(const char* hostname, const char* serverName, uint16_t serverPort) { 256 | return EspAtDrv.mDNS(hostname, serverName, serverPort); 257 | } 258 | 259 | bool WiFiClass::hostByName(const char* hostname, IPAddress& result) { 260 | return EspAtDrv.resolve(hostname, result); 261 | } 262 | 263 | bool WiFiClass::ping(const char* hostname) { 264 | return EspAtDrv.ping(hostname); 265 | } 266 | 267 | bool WiFiClass::ping(IPAddress ip) { 268 | char s[16]; 269 | EspAtDrv.ip2str(ip, s); 270 | return ping(s); 271 | } 272 | 273 | bool WiFiClass::sntp(const char* server1, const char* server2) { 274 | return EspAtDrv.sntpCfg(server1, server2); 275 | } 276 | 277 | unsigned long WiFiClass::getTime() { 278 | return EspAtDrv.sntpTime(); 279 | } 280 | 281 | int WiFiClass::beginAP(const char *ssid, const char* passphrase, uint8_t channel, uint8_t encryptionType, uint8_t maxConnetions, bool hidden) { 282 | uint8_t encoding = 0; // OPEN 283 | switch (encryptionType) { 284 | case ENC_TYPE_WEP: // WEP is not supported 285 | return WL_AP_FAILED; 286 | case ENC_TYPE_TKIP: 287 | encoding = 2; // WPA_PSK 288 | break; 289 | case ENC_TYPE_CCMP: 290 | encoding = 4; // WPA_WPA2_PSK 291 | break; 292 | } 293 | apMaxConn = 0; // clear the ap params info cahche 294 | return EspAtDrv.beginSoftAP(ssid, passphrase, channel, encoding, maxConnetions, hidden) ? WL_AP_LISTENING : WL_AP_FAILED; 295 | } 296 | 297 | bool WiFiClass::endAP(bool pers) { 298 | return EspAtDrv.endSoftAP(pers); 299 | } 300 | 301 | bool WiFiClass::configureAP(IPAddress ip, IPAddress gateway, IPAddress subnet) { 302 | return EspAtDrv.softApIp(ip, gateway, subnet); 303 | } 304 | 305 | uint8_t* WiFiClass::apMacAddress(uint8_t* mac) { 306 | if (!EspAtDrv.softApMacQuery(mac)) { 307 | for (int i = 0; i < 5; i++) { 308 | mac[i] = 0; 309 | } 310 | } 311 | return mac; 312 | } 313 | 314 | const char* WiFiClass::apSSID(char* buffer) { 315 | uint8_t ch = 0; 316 | if (!EspAtDrv.softApQuery(buffer, nullptr, ch, apEnc, apMaxConn, apHidden)) { 317 | buffer[0] = 0; 318 | } 319 | return buffer; 320 | } 321 | 322 | const char* WiFiClass::apPassphrase(char* buffer) { 323 | uint8_t ch = 0; 324 | if (!EspAtDrv.softApQuery(nullptr, buffer, ch, apEnc, apMaxConn, apHidden)) { 325 | buffer[0] = 0; 326 | } 327 | return buffer; 328 | } 329 | 330 | uint8_t WiFiClass::apEncryptionType() { 331 | if (apMaxConn == 0) { 332 | apSSID(nullptr); 333 | } 334 | return mapAtEnc2ArduinoEnc(apEnc); 335 | } 336 | 337 | uint8_t WiFiClass::apMaxConnections() { 338 | if (apMaxConn == 0) { 339 | apSSID(nullptr); 340 | } 341 | return apMaxConn; 342 | } 343 | 344 | bool WiFiClass::apIsHidden() { 345 | if (apMaxConn == 0) { 346 | apSSID(nullptr); 347 | } 348 | return apHidden; 349 | } 350 | 351 | bool WiFiClass::apDhcpIsEnabled() { 352 | bool sta; 353 | bool ap; 354 | bool eth; 355 | EspAtDrv.dhcpStateQuery(sta, ap, eth); 356 | return ap; 357 | } 358 | 359 | IPAddress WiFiClass::apIP() { 360 | IPAddress ip; 361 | IPAddress gw; 362 | IPAddress mask; 363 | EspAtDrv.softApIpQuery(ip, gw, mask); 364 | return ip; 365 | } 366 | 367 | IPAddress WiFiClass::apGatewayIP() { 368 | IPAddress ip; 369 | IPAddress gw; 370 | IPAddress mask; 371 | EspAtDrv.softApIpQuery(ip, gw, mask); 372 | return gw; 373 | } 374 | 375 | IPAddress WiFiClass::apSubnetMask() { 376 | IPAddress ip; 377 | IPAddress gw; 378 | IPAddress mask; 379 | EspAtDrv.softApIpQuery(ip, gw, mask); 380 | return mask; 381 | } 382 | 383 | bool WiFiClass::softAP(const char* ssid, const char* psk, int channel, int ssid_hidden, int max_connection) { 384 | return beginAP(ssid, psk, channel, 0, max_connection, ssid_hidden); 385 | } 386 | 387 | bool WiFiClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet) { 388 | return configureAP(local_ip, gateway, subnet); 389 | } 390 | 391 | bool WiFiClass::softAPdisconnect() { 392 | return endAP(false); 393 | } 394 | 395 | IPAddress WiFiClass::softAPIP() { 396 | return apIP(); 397 | } 398 | 399 | uint8_t* WiFiClass::softAPmacAddress(uint8_t* mac) { 400 | return apMacAddress(mac); 401 | } 402 | 403 | String WiFiClass::softAPSSID() { 404 | char ssid[33]; 405 | return apSSID(ssid); 406 | } 407 | 408 | String WiFiClass::softAPPSK() { 409 | char pass[64]; 410 | return apPassphrase(pass); 411 | } 412 | 413 | const char* WiFiClass::firmwareVersion(char* buffer) { 414 | EspAtDrv.firmwareVersion(buffer); 415 | return buffer; 416 | } 417 | 418 | EspAtDrvError WiFiClass::getLastDriverError() { 419 | return EspAtDrv.getLastErrorCode(); 420 | } 421 | 422 | bool WiFiClass::sleepMode(EspAtSleepMode mode) { 423 | return EspAtDrv.sleepMode(mode); 424 | } 425 | 426 | bool WiFiClass::deepSleep() { 427 | return EspAtDrv.deepSleep(); 428 | } 429 | 430 | bool WiFiClass::reset(uint8_t resetPin) { 431 | return EspAtDrv.reset(resetPin); 432 | } 433 | 434 | uint8_t WiFiClass::mapAtEnc2ArduinoEnc(uint8_t encryptionType) { 435 | if (encryptionType == 0) { // WIFI_AUTH_OPEN 436 | encryptionType = ENC_TYPE_NONE; 437 | } else if (encryptionType == 1) { // WIFI_AUTH_WEP 438 | encryptionType = ENC_TYPE_WEP; 439 | } else if (encryptionType == 2) { // WIFI_AUTH_WPA_PSK 440 | encryptionType = ENC_TYPE_TKIP; 441 | } else if (encryptionType == 3 || encryptionType == 4) { // WIFI_AUTH_WPA2_PSK || WIFI_AUTH_WPA_WPA2_PSK 442 | encryptionType = ENC_TYPE_CCMP; 443 | } else { 444 | // unknown? 445 | encryptionType = ENC_TYPE_UNKNOWN; 446 | } 447 | return encryptionType; 448 | } 449 | 450 | WiFiClass WiFi; 451 | -------------------------------------------------------------------------------- /src/WiFi.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the WiFiEspAT library for Arduino 3 | https://github.com/jandrassy/WiFiEspAT 4 | Copyright 2019 Juraj Andrassy 5 | 6 | This library is free software; you can redistribute it and/or 7 | modify it under the terms of the GNU Lesser General Public 8 | License as published by the Free Software Foundation; either 9 | version 2.1 of the License, or (at your option) any later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | Lesser General Public License for more details. 15 | 16 | You should have received a copy of the GNU Lesser General Public License 17 | along with this library. If not, see . 18 | */ 19 | 20 | #ifndef _WIFI_ESP_AT_H_ 21 | #define _WIFI_ESP_AT_H_ 22 | 23 | #include 24 | #include 25 | #include "WiFiEspAtConfig.h" 26 | #include "utility/EspAtDrvTypes.h" 27 | #include "WiFiClient.h" 28 | #include "WiFiServer.h" 29 | #include "WiFiUdp.h" 30 | #include "WiFiSSLClient.h" 31 | 32 | #define WIFIESPAT_LIB_VERSION 2 33 | 34 | enum { 35 | WL_NO_SHIELD = 255, 36 | WL_NO_MODULE = WL_NO_SHIELD, 37 | WL_IDLE_STATUS = 0, 38 | WL_NO_SSID_AVAIL, 39 | WL_CONNECTED, 40 | WL_CONNECT_FAILED, 41 | WL_CONNECTION_LOST, 42 | WL_DISCONNECTED, 43 | WL_AP_LISTENING, 44 | WL_AP_CONNECTED, 45 | WL_AP_FAILED 46 | }; 47 | 48 | /* Encryption modes */ 49 | enum wl_enc_type { /* Values map to 802.11 Cipher Algorithm Identifier */ 50 | ENC_TYPE_WEP = 5, 51 | ENC_TYPE_TKIP = 2, 52 | ENC_TYPE_WPA = ENC_TYPE_TKIP, 53 | ENC_TYPE_CCMP = 4, 54 | ENC_TYPE_WPA2 = ENC_TYPE_CCMP, 55 | ENC_TYPE_GCMP = 6, 56 | ENC_TYPE_WPA3 = ENC_TYPE_GCMP, 57 | /* ... except these two, 7 and 8 are reserved in 802.11-2007 */ 58 | ENC_TYPE_NONE = 7, 59 | ENC_TYPE_AUTO = 8, 60 | 61 | ENC_TYPE_UNKNOWN = 255 62 | }; 63 | 64 | class WiFiClass { 65 | public: 66 | 67 | bool init(Stream& serial, int8_t resetPin = -1); 68 | bool init(Stream* serial, int8_t resetPin = -1); // old WiFiEsp lib compatibility 69 | 70 | uint8_t status(); 71 | 72 | bool setPersistent(bool persistent = true); 73 | 74 | bool setAutoConnect(bool autoConnect); 75 | 76 | #ifdef WIFIESPAT1 77 | int begin(const char* ssid, const char *passphrase = nullptr, const uint8_t* bssid = nullptr); 78 | #else 79 | int begin(const char* ssid = nullptr, const char *passphrase = nullptr, const uint8_t* bssid = nullptr); 80 | #endif 81 | int beginEnterprise(const char* ssid, uint8_t method, const char* username, const char* passphrase, const char* identity, uint8_t security); 82 | int disconnect(bool persistent = false); 83 | 84 | bool config(IPAddress local_ip, IPAddress dns_server = INADDR_NONE, IPAddress gateway = INADDR_NONE, IPAddress subnet = INADDR_NONE); 85 | 86 | bool setDNS(IPAddress dns_server1, IPAddress dns_server2 = INADDR_NONE); 87 | bool setHostname(const char* name); 88 | const char* hostname(char* buffer = name); 89 | 90 | uint8_t* macAddress(uint8_t* mac); 91 | IPAddress localIP(); 92 | IPAddress gatewayIP(); 93 | IPAddress subnetMask(); 94 | IPAddress dnsIP(int n = 0); 95 | bool dhcpIsEnabled(); 96 | 97 | // WiFi network parameters 98 | const char* SSID(char* _ssid = ssid); // using the default parameter will take 33 bytes of SRAM 99 | uint8_t* BSSID(uint8_t* bssid); 100 | uint8_t channel(); 101 | int8_t RSSI(); 102 | 103 | // enumerate WiFi access points 104 | int8_t scanNetworks(); // using internal array will occupy a lot of SRAM 105 | int8_t scanNetworks(WiFiApData* _apData, uint8_t apDataSize); // optional version 106 | const char* SSID(uint8_t index); 107 | uint8_t encryptionType(uint8_t index); 108 | uint8_t* BSSID(uint8_t index, uint8_t* bssid); 109 | uint8_t channel(uint8_t index); 110 | int32_t RSSI(uint8_t index); 111 | 112 | // additional station functions: 113 | 114 | bool startMDNS(const char* hostname, const char* serverName, uint16_t serverPort); 115 | 116 | bool hostByName(const char* hostname, IPAddress& result); 117 | 118 | bool ping(const char* hostname); 119 | bool ping(IPAddress ip); 120 | 121 | bool sntp(const char* server1, const char* server2 = nullptr); 122 | unsigned long getTime(); 123 | 124 | // AP related functions: 125 | 126 | int beginAP(const char *ssid = nullptr, const char* passphrase = nullptr, uint8_t channel = 1, 127 | uint8_t encryptionType = ENC_TYPE_CCMP, uint8_t maxConnections = 0, bool hidden = false); 128 | bool endAP(bool persistent = false); 129 | 130 | bool configureAP(IPAddress ip, IPAddress gateway = INADDR_NONE, IPAddress subnet = INADDR_NONE); 131 | 132 | uint8_t* apMacAddress(uint8_t* mac); 133 | const char* apSSID(char*); 134 | const char* apPassphrase(char*); 135 | uint8_t apEncryptionType(); 136 | uint8_t apMaxConnections(); 137 | bool apIsHidden(); 138 | bool apDhcpIsEnabled(); 139 | 140 | IPAddress apIP(); 141 | IPAddress apGatewayIP(); 142 | IPAddress apSubnetMask(); 143 | 144 | // esp8266 and esp32 compatible softAP functions 145 | bool softAP(const char* ssid, const char* psk = NULL, int channel = 1, int ssid_hidden = 0, int max_connection = 4); 146 | bool softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet); 147 | bool softAPdisconnect(); 148 | IPAddress softAPIP(); 149 | uint8_t* softAPmacAddress(uint8_t* mac); 150 | String softAPSSID(); 151 | String softAPPSK(); 152 | 153 | // 154 | const char* firmwareVersion(char* buffer = fwVersion); 155 | EspAtDrvError getLastDriverError(); 156 | 157 | bool sleepMode(EspAtSleepMode mode); 158 | bool deepSleep(); 159 | bool reset(uint8_t resetPin = -1); 160 | 161 | private: 162 | uint8_t mapAtEnc2ArduinoEnc(uint8_t encryptionType); 163 | 164 | uint8_t state = WL_NO_MODULE; 165 | 166 | // this members are removed by compiler if the corresponding function is not used 167 | static char fwVersion[]; // static is for use as default parameter value of function 168 | static char ssid[]; 169 | static char name[]; // hostname 170 | 171 | uint8_t apEnc; 172 | uint8_t apMaxConn; 173 | bool apHidden; 174 | 175 | static WiFiApData apDataInternal[]; 176 | WiFiApData* apData; 177 | uint8_t apDataSize; 178 | uint8_t apDataLength; // count of AP 179 | }; 180 | 181 | extern WiFiClass WiFi; 182 | 183 | #endif 184 | -------------------------------------------------------------------------------- /src/WiFiClient.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the WiFiEspAT library for Arduino 3 | https://github.com/jandrassy/WiFiEspAT 4 | Copyright 2019 Juraj Andrassy 5 | 6 | This library is free software; you can redistribute it and/or 7 | modify it under the terms of the GNU Lesser General Public 8 | License as published by the Free Software Foundation; either 9 | version 2.1 of the License, or (at your option) any later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | Lesser General Public License for more details. 15 | 16 | You should have received a copy of the GNU Lesser General Public License 17 | along with this library. If not, see . 18 | */ 19 | 20 | #include "utility/EspAtDrv.h" 21 | #include "WiFiClient.h" 22 | #include "WiFiEspAtBuffManager.h" 23 | 24 | WiFiClient::WiFiClient() { 25 | } 26 | 27 | WiFiClient::WiFiClient(uint8_t linkId) { 28 | stream = WiFiEspAtBuffManager.getBuffStream(linkId, WIFIESPAT_CLIENT_RX_BUFFER_SIZE, WIFIESPAT_CLIENT_TX_BUFFER_SIZE); 29 | if (!stream) { 30 | EspAtDrv.close(linkId); 31 | } 32 | } 33 | 34 | int WiFiClient::connect(bool ssl, const char* host, uint16_t port) { 35 | if (stream) { 36 | stop(); 37 | } 38 | uint8_t linkId = EspAtDrv.connect(ssl ? "SSL" : "TCP", host, port); 39 | if (linkId == NO_LINK) 40 | return false; 41 | stream = WiFiEspAtBuffManager.getBuffStream(linkId, WIFIESPAT_CLIENT_RX_BUFFER_SIZE, WIFIESPAT_CLIENT_TX_BUFFER_SIZE); 42 | if (!stream) { 43 | EspAtDrv.close(linkId); 44 | return false; 45 | } 46 | return true; 47 | } 48 | 49 | int WiFiClient::connect(bool ssl, IPAddress ip, uint16_t port) { 50 | char s[16]; 51 | EspAtDrv.ip2str(ip, s); 52 | return connect(ssl, s, port); 53 | } 54 | 55 | int WiFiClient::connect(const char* host, uint16_t port) { 56 | return connect(false, host, port); 57 | } 58 | 59 | int WiFiClient::connect(IPAddress ip, uint16_t port) { 60 | return connect(false, ip, port); 61 | } 62 | 63 | int WiFiClient::connectSSL(const char* host, uint16_t port) { 64 | return connect(true, host, port); 65 | } 66 | 67 | int WiFiClient::connectSSL(IPAddress ip, uint16_t port) { 68 | return connect(true, ip, port); 69 | } 70 | 71 | void WiFiClient::stop() { 72 | if (!stream) 73 | return; 74 | flush(); 75 | stream->close(); 76 | stream = nullptr; 77 | } 78 | 79 | void WiFiClient::abort() { 80 | if (!stream) 81 | return; 82 | stream->close(true); 83 | stream = nullptr; 84 | } 85 | 86 | size_t WiFiClient::write(uint8_t b) { 87 | if (!stream) 88 | return 0; 89 | return stream->write(b); 90 | } 91 | 92 | size_t WiFiClient::write(const uint8_t *data, size_t length) { 93 | if (!stream) 94 | return 0; 95 | return stream->write(data, length); 96 | } 97 | 98 | void WiFiClient::flush() { 99 | if (!stream) 100 | return; 101 | stream->flush(); 102 | } 103 | 104 | size_t WiFiClient::write(Stream& file) { 105 | if (!stream) 106 | return 0; 107 | return stream->write(file); 108 | } 109 | 110 | size_t WiFiClient::write(SendCallbackFnc callback) { 111 | if (!stream) 112 | return 0; 113 | return stream->write(callback); 114 | } 115 | 116 | int WiFiClient::available() { 117 | if (!stream) 118 | return 0; 119 | return stream->available(); 120 | } 121 | 122 | int WiFiClient::read() { 123 | if (!stream) 124 | return -1; 125 | return stream->read(); 126 | } 127 | 128 | int WiFiClient::read(uint8_t* data, size_t size) { 129 | if (!stream) 130 | return 0; 131 | return stream->read(data, size); 132 | } 133 | 134 | int WiFiClient::peek() { 135 | if (!stream) 136 | return -1; 137 | return stream->peek(); 138 | } 139 | 140 | WiFiClient::operator bool() { 141 | return !!stream; 142 | } 143 | 144 | uint8_t WiFiClient::connected() { 145 | if (!stream) 146 | return false; 147 | if (stream->connected() || available()) // Arduino WiFi library examples expect connected true while data are available 148 | return true; 149 | // link is closed and all data from stream are read 150 | stream->free(); 151 | stream = nullptr; 152 | return false; 153 | } 154 | 155 | uint8_t WiFiClient::status() { 156 | if (!stream) 157 | return CLOSED; 158 | if (stream->connected()) 159 | return ESTABLISHED; 160 | return CLOSED; 161 | } 162 | 163 | IPAddress WiFiClient::remoteIP() { 164 | IPAddress ip; 165 | uint16_t port = 0; 166 | uint16_t lport = 0; 167 | if (stream && stream->getLinkId() != NO_LINK) { 168 | EspAtDrv.remoteParamsQuery(stream->getLinkId(), ip, port, lport); 169 | } 170 | return ip; 171 | } 172 | 173 | uint16_t WiFiClient::remotePort() { 174 | IPAddress ip; 175 | uint16_t port = 0; 176 | uint16_t lport = 0; 177 | if (stream && stream->getLinkId() != NO_LINK) { 178 | EspAtDrv.remoteParamsQuery(stream->getLinkId(), ip, port, lport); 179 | } 180 | return port; 181 | } 182 | 183 | uint16_t WiFiClient::localPort() { 184 | if (stream && stream->getLinkId() == NO_LINK) 185 | return 0; 186 | return EspAtDrv.localPortQuery(stream->getLinkId()); 187 | } 188 | -------------------------------------------------------------------------------- /src/WiFiClient.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the WiFiEspAT library for Arduino 3 | https://github.com/jandrassy/WiFiEspAT 4 | Copyright 2019 Juraj Andrassy 5 | 6 | This library is free software; you can redistribute it and/or 7 | modify it under the terms of the GNU Lesser General Public 8 | License as published by the Free Software Foundation; either 9 | version 2.1 of the License, or (at your option) any later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | Lesser General Public License for more details. 15 | 16 | You should have received a copy of the GNU Lesser General Public License 17 | along with this library. If not, see . 18 | */ 19 | 20 | #ifndef _WIFICLIENT_H_ 21 | #define _WIFICLIENT_H_ 22 | 23 | #include 24 | #include "WiFiEspAtSharedBuffStreamPtr.h" 25 | #include "WiFiEspAtConfig.h" 26 | 27 | enum WiFiTcpState { 28 | CLOSED = 0, 29 | LISTEN = 1, 30 | SYN_SENT = 2, 31 | SYN_RCVD = 3, 32 | ESTABLISHED = 4, 33 | FIN_WAIT_1 = 5, 34 | FIN_WAIT_2 = 6, 35 | CLOSE_WAIT = 7, 36 | CLOSING = 8, 37 | LAST_ACK = 9, 38 | TIME_WAIT = 10 39 | }; 40 | 41 | class WiFiServer; 42 | 43 | class WiFiClient : public Client { 44 | 45 | friend WiFiServer; 46 | WiFiClient(uint8_t linkId); 47 | 48 | public: 49 | WiFiClient(); 50 | 51 | virtual int connect(IPAddress ip, uint16_t port); 52 | virtual int connect(const char *host, uint16_t port); 53 | int connectSSL(IPAddress ip, uint16_t port); 54 | int connectSSL(const char *host, uint16_t port); 55 | virtual void stop(); 56 | void abort(); 57 | 58 | virtual size_t write(uint8_t); 59 | virtual size_t write(const uint8_t *buf, size_t size); 60 | virtual void flush(); 61 | 62 | size_t write(Stream& file); 63 | size_t write(SendCallbackFnc callback); 64 | 65 | virtual int available(); 66 | virtual int read(); 67 | virtual int read(uint8_t *buf, size_t size); 68 | virtual int peek(); 69 | 70 | virtual operator bool(); 71 | virtual uint8_t connected(); 72 | uint8_t status(); 73 | 74 | // remoteIP and the ports are not stored 75 | // every call to these functions retrieves the value from AT firmware 76 | IPAddress remoteIP(); 77 | uint16_t remotePort(); 78 | uint16_t localPort(); 79 | 80 | using Print::write; 81 | 82 | private: 83 | int connect(bool ssl, IPAddress ip, uint16_t port); 84 | int connect(bool ssl, const char *host, uint16_t port); 85 | 86 | WiFiEspAtSharedBuffStreamPtr stream; 87 | 88 | }; 89 | 90 | #endif 91 | -------------------------------------------------------------------------------- /src/WiFiEspAT.h: -------------------------------------------------------------------------------- 1 | #include "WiFi.h" 2 | -------------------------------------------------------------------------------- /src/WiFiEspAtBuffManager.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the WiFiEspAT library for Arduino 3 | https://github.com/jandrassy/WiFiEspAT 4 | Copyright 2020 Juraj Andrassy 5 | 6 | This library is free software; you can redistribute it and/or 7 | modify it under the terms of the GNU Lesser General Public 8 | License as published by the Free Software Foundation; either 9 | version 2.1 of the License, or (at your option) any later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | Lesser General Public License for more details. 15 | 16 | You should have received a copy of the GNU Lesser General Public License 17 | along with this library. If not, see . 18 | */ 19 | 20 | #include "WiFiEspAtBuffManager.h" 21 | #include "utility/EspAtDrvLogging.h" 22 | #include "utility/EspAtDrv.h" 23 | 24 | WiFiEspAtBuffManagerClass::WiFiEspAtBuffManagerClass() { 25 | for (int i = 0; i < WIFIESPAT_LINKS_COUNT; i++) { 26 | pool[i] = nullptr; 27 | } 28 | } 29 | 30 | WiFiEspAtBuffStream* WiFiEspAtBuffManagerClass::getBuffStream(uint8_t linkId, size_t rxBufferSize, size_t txBufferSize) { 31 | 32 | int freePos = -1; 33 | 34 | for (int i = 0; i < WIFIESPAT_LINKS_COUNT; i++) { 35 | if (pool[i] == nullptr) { 36 | freePos = i; 37 | break; 38 | } 39 | if (pool[i]->serialId) 40 | continue; 41 | if (pool[i]->rxBufferSize == rxBufferSize && pool[i]->txBufferSize == txBufferSize) { 42 | pool[i]->linkId = linkId; 43 | pool[i]->serialId = nextSerialId(); 44 | LOG_INFO_PRINT_PREFIX(); 45 | LOG_INFO_PRINT(F("BuffManager returned buff.stream id ")); 46 | LOG_INFO_PRINT(serialId); 47 | LOG_INFO_PRINT(F(" at index ")); 48 | LOG_INFO_PRINT(i); 49 | if (linkId != WIFIESPAT_NO_LINK) { 50 | LOG_INFO_PRINT(F(" for linkId ")); 51 | LOG_INFO_PRINT(linkId & INDEX_MASK); 52 | } 53 | LOG_INFO_PRINTLN(); 54 | return pool[i]; 55 | } 56 | } 57 | if (freePos == -1) { 58 | LOG_WARN_PRINT_PREFIX(); 59 | LOG_WARN_PRINTLN(F("getBuffStream no free position")); 60 | return nullptr; 61 | } 62 | WiFiEspAtBuffStream *res = new WiFiEspAtBuffStream(); 63 | if (rxBufferSize) { 64 | res->rxBuffer = new uint8_t[rxBufferSize]; 65 | } 66 | if (txBufferSize) { 67 | res->txBuffer = new uint8_t[txBufferSize]; 68 | } 69 | res->rxBufferSize = rxBufferSize; 70 | res->txBufferSize = txBufferSize; 71 | res->linkId = linkId; 72 | res->serialId = nextSerialId(); 73 | pool[freePos] = res; 74 | LOG_INFO_PRINT_PREFIX(); 75 | LOG_INFO_PRINT(F("BuffManager new buff.stream id ")); 76 | LOG_INFO_PRINT(serialId); 77 | LOG_INFO_PRINT(F(" at index ")); 78 | LOG_INFO_PRINT(freePos); 79 | if (linkId != WIFIESPAT_NO_LINK) { 80 | LOG_INFO_PRINT(F(" for linkId ")); 81 | LOG_INFO_PRINT(linkId & INDEX_MASK); 82 | } 83 | LOG_INFO_PRINT(F(" rx ")); 84 | LOG_INFO_PRINT(rxBufferSize); 85 | LOG_INFO_PRINT(F(" tx ")); 86 | LOG_INFO_PRINTLN(txBufferSize); 87 | return res; 88 | } 89 | 90 | void WiFiEspAtBuffManagerClass::freeUnused() { 91 | for (int i = 0; i < WIFIESPAT_LINKS_COUNT; i++) { 92 | if (pool[i] == nullptr) 93 | break; 94 | if (pool[i]->linkId == WIFIESPAT_NO_LINK) { 95 | LOG_INFO_PRINT_PREFIX(); 96 | LOG_INFO_PRINT(F("BuffManager free tx ")); 97 | LOG_INFO_PRINTLN(pool[i]->txBufferSize); 98 | if (pool[i]->rxBuffer != nullptr) { 99 | delete pool[i]->rxBuffer; 100 | } 101 | if (pool[i]->txBuffer != nullptr) { 102 | delete pool[i]->txBuffer; 103 | } 104 | delete pool[i]; 105 | pool[i] = nullptr; 106 | } 107 | } 108 | int i = 0; 109 | for (; i < WIFIESPAT_LINKS_COUNT && pool[i] != nullptr; i++); 110 | int j = i; 111 | for (; i < WIFIESPAT_LINKS_COUNT; i++) { 112 | if (pool[i] != nullptr) { 113 | pool[j++] = pool[i]; 114 | pool[i] = nullptr; 115 | } 116 | } 117 | } 118 | 119 | uint8_t WiFiEspAtBuffManagerClass::nextSerialId() { 120 | while (true) { 121 | serialId++; 122 | int i = 0; 123 | for (; i < WIFIESPAT_LINKS_COUNT; i++) { 124 | if (pool[i] == nullptr || pool[i]->serialId == serialId) 125 | break; 126 | } 127 | if (i == WIFIESPAT_LINKS_COUNT || pool[i] == nullptr) 128 | return serialId; 129 | } 130 | } 131 | 132 | WiFiEspAtBuffManagerClass WiFiEspAtBuffManager; 133 | -------------------------------------------------------------------------------- /src/WiFiEspAtBuffManager.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the WiFiEspAT library for Arduino 3 | https://github.com/jandrassy/WiFiEspAT 4 | Copyright 2020 Juraj Andrassy 5 | 6 | This library is free software; you can redistribute it and/or 7 | modify it under the terms of the GNU Lesser General Public 8 | License as published by the Free Software Foundation; either 9 | version 2.1 of the License, or (at your option) any later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | Lesser General Public License for more details. 15 | 16 | You should have received a copy of the GNU Lesser General Public License 17 | along with this library. If not, see . 18 | */ 19 | 20 | #ifndef _ESP_AT_BUFF_MAN_H_ 21 | #define _ESP_AT_BUFF_MAN_H_ 22 | 23 | #include "WiFiEspAtBuffStream.h" 24 | 25 | class WiFiEspAtBuffManagerClass { 26 | public: 27 | 28 | WiFiEspAtBuffManagerClass(); 29 | 30 | WiFiEspAtBuffStream* getBuffStream(uint8_t linkId, size_t rxBufferSize, size_t txBufferSize); 31 | 32 | void freeUnused(); 33 | 34 | private: 35 | 36 | WiFiEspAtBuffStream* pool[WIFIESPAT_LINKS_COUNT]; 37 | uint8_t serialId = 0; 38 | 39 | uint8_t nextSerialId(); 40 | }; 41 | 42 | extern WiFiEspAtBuffManagerClass WiFiEspAtBuffManager; 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /src/WiFiEspAtBuffStream.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the WiFiEspAT library for Arduino 3 | https://github.com/jandrassy/WiFiEspAT 4 | Copyright 2019 Juraj Andrassy 5 | 6 | This library is free software; you can redistribute it and/or 7 | modify it under the terms of the GNU Lesser General Public 8 | License as published by the Free Software Foundation; either 9 | version 2.1 of the License, or (at your option) any later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | Lesser General Public License for more details. 15 | 16 | You should have received a copy of the GNU Lesser General Public License 17 | along with this library. If not, see . 18 | */ 19 | 20 | #include 21 | #include "WiFiEspAtBuffStream.h" 22 | #include "utility/EspAtDrv.h" 23 | #include "utility/EspAtDrvLogging.h" 24 | 25 | void WiFiEspAtBuffStream::setUdpPort(const char* _udpHost, uint16_t _udpPort) { 26 | udpHost = _udpHost; 27 | udpPort = _udpPort; 28 | } 29 | 30 | bool WiFiEspAtBuffStream::checkLink() { 31 | if (linkId != NO_LINK && EspAtDrv.getLastErrorCode() == EspAtDrvError::LINK_NOT_ACTIVE) { 32 | linkId = NO_LINK; 33 | available(); // calls free() if receive buffer is empty 34 | } 35 | return linkId != NO_LINK; 36 | } 37 | 38 | bool WiFiEspAtBuffStream::connected() { 39 | if (linkId != NO_LINK && !EspAtDrv.connected(linkId)) { 40 | linkId = NO_LINK; 41 | available(); // calls free() if receive buffer is empty 42 | } 43 | return (linkId != NO_LINK); 44 | } 45 | 46 | void WiFiEspAtBuffStream::free() { 47 | if (!serialId) 48 | return; 49 | LOG_INFO_PRINT_PREFIX(); 50 | LOG_INFO_PRINT(F("free BuffStream ")); 51 | LOG_INFO_PRINTLN(serialId); 52 | serialId = 0; 53 | refCount = 0; 54 | linkId = NO_LINK; 55 | rxBufferLength = 0; 56 | rxBufferIndex = 0; 57 | txBufferLength = 0; 58 | udpPort = 0; 59 | } 60 | 61 | void WiFiEspAtBuffStream::close(bool abort) { 62 | if (linkId != NO_LINK) { 63 | EspAtDrv.close(linkId, abort); 64 | } 65 | free(); 66 | } 67 | 68 | size_t WiFiEspAtBuffStream::write(uint8_t b) { 69 | if (linkId == NO_LINK) { 70 | setWriteError(); 71 | return 0; 72 | } 73 | txBuffer[txBufferLength++] = b; 74 | if (txBufferLength == txBufferSize) { 75 | flush(); 76 | if (getWriteError()) 77 | return 0; 78 | } 79 | return 1; 80 | } 81 | 82 | size_t WiFiEspAtBuffStream::write(const uint8_t *data, size_t length) { 83 | if (linkId == NO_LINK) { 84 | setWriteError(); 85 | return 0; 86 | } 87 | if (length == 0) 88 | return 0; 89 | if (txBufferLength == 0 && length > txBufferSize) { // if internal buffer is empty and provided buffer is large 90 | size_t res = EspAtDrv.sendData(linkId, data, length, udpHost, udpPort); // send it right away 91 | if (res != length && !checkLink()) { 92 | setWriteError(); 93 | } 94 | return res; 95 | } 96 | 97 | size_t a = txBufferSize - txBufferLength; // available space in internal buffer 98 | for (size_t i = 0; i < a && i < length; i++) { // copy data to internal buffer 99 | txBuffer[txBufferLength++] = data[i]; 100 | } 101 | int d = length - a; // left to write 102 | if (d >= 0) { // internal buffer is full 103 | flush(); 104 | } 105 | if (d <= 0) // nothing more to write 106 | return length; 107 | return a + write(data + a, d); // handle the rest of the provided buffer 108 | } 109 | 110 | void WiFiEspAtBuffStream::flush() { 111 | if (txBufferLength == 0) 112 | return; 113 | size_t res = EspAtDrv.sendData(linkId, txBuffer, txBufferLength, udpHost, udpPort); 114 | if (res != txBufferLength) { 115 | setWriteError(1); 116 | checkLink(); 117 | } 118 | txBufferLength = 0; 119 | } 120 | 121 | int WiFiEspAtBuffStream::availableForWrite() { 122 | if (linkId == NO_LINK) { 123 | setWriteError(); 124 | return 0; 125 | } 126 | return txBufferSize - txBufferLength; 127 | } 128 | 129 | size_t WiFiEspAtBuffStream::write(Stream& file) { 130 | flush(); 131 | size_t res = EspAtDrv.sendData(linkId, file, udpHost, udpPort); 132 | checkLink(); 133 | return res; 134 | } 135 | 136 | size_t WiFiEspAtBuffStream::write(SendCallbackFnc callback) { 137 | flush(); 138 | size_t res = EspAtDrv.sendData(linkId, callback, udpHost, udpPort); 139 | checkLink(); 140 | return res; 141 | } 142 | 143 | int WiFiEspAtBuffStream::available() { 144 | size_t a = (rxBufferLength - rxBufferIndex); 145 | if (linkId == NO_LINK) { 146 | if (a == 0) { 147 | free(); 148 | } 149 | return a; 150 | } 151 | if (a == 0) { 152 | a = EspAtDrv.availData(linkId); 153 | } 154 | if (a == 0 && checkLink()) { 155 | flush(); // maybe sketch is waiting for response without flushing the request 156 | } 157 | return a; 158 | } 159 | 160 | void WiFiEspAtBuffStream::fillRXbuffer() { 161 | if (rxBufferIndex < rxBufferLength || !available()) 162 | return; 163 | rxBufferIndex = 0; 164 | rxBufferLength = EspAtDrv.recvData(linkId, rxBuffer, rxBufferSize); 165 | } 166 | 167 | int WiFiEspAtBuffStream::read() { 168 | if (!available()) 169 | return -1; 170 | uint8_t b; 171 | if (!read(&b, 1)) 172 | return -1; 173 | return b; 174 | } 175 | 176 | int WiFiEspAtBuffStream::read(uint8_t* data, size_t size) { 177 | if (size == 0 || !available()) 178 | return 0; 179 | 180 | size_t l = rxBufferLength - rxBufferIndex; 181 | if (l == 0 && size > rxBufferSize) // internal buffer is empty and provided buffer is large 182 | return EspAtDrv.recvData(linkId, data, size); // fill the large provided buffer directly 183 | 184 | // copy from internal buffer 185 | fillRXbuffer(); 186 | for (size_t i = 0; i < l && i < size; i++) { 187 | data[i] = rxBuffer[rxBufferIndex++]; 188 | } 189 | if (size <= l) // provided buffer was filled 190 | return size; 191 | return l + read(data + l, size - l); // handle the rest of provided buffer 192 | } 193 | 194 | int WiFiEspAtBuffStream::peek() { 195 | if (!available()) 196 | return -1; 197 | fillRXbuffer(); 198 | return rxBuffer[rxBufferIndex]; 199 | } 200 | -------------------------------------------------------------------------------- /src/WiFiEspAtBuffStream.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the WiFiEspAT library for Arduino 3 | https://github.com/jandrassy/WiFiEspAT 4 | Copyright 2019 Juraj Andrassy 5 | 6 | This library is free software; you can redistribute it and/or 7 | modify it under the terms of the GNU Lesser General Public 8 | License as published by the Free Software Foundation; either 9 | version 2.1 of the License, or (at your option) any later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | Lesser General Public License for more details. 15 | 16 | You should have received a copy of the GNU Lesser General Public License 17 | along with this library. If not, see . 18 | */ 19 | 20 | #ifndef _ESP_AT_BUFF_STREAM_H_ 21 | #define _ESP_AT_BUFF_STREAM_H_ 22 | 23 | #include 24 | #include 25 | #include "utility/EspAtDrvTypes.h" 26 | 27 | class WiFiEspAtBuffStream { 28 | 29 | public: 30 | 31 | void setUdpPort(const char* udpHost, uint16_t udpPort); 32 | bool connected(); 33 | void free(); 34 | void close(bool abort = false); 35 | 36 | uint8_t getLinkId() {return linkId;} 37 | 38 | size_t write(uint8_t); 39 | size_t write(const uint8_t *buf, size_t size); 40 | void flush(); 41 | int availableForWrite(); 42 | 43 | size_t write(Stream& file); 44 | size_t write(SendCallbackFnc callback); 45 | 46 | int8_t getWriteError() {return writeError;} 47 | 48 | int available(); 49 | int read(); 50 | int read(uint8_t *buf, size_t size); 51 | int peek(); 52 | 53 | private: 54 | friend class WiFiEspAtBuffManagerClass; 55 | friend class WiFiEspAtSharedBuffStreamPtr; 56 | friend class WiFiUDP; 57 | 58 | void fillRXbuffer(); 59 | void setWriteError(int8_t err = -1) {writeError = err;} 60 | bool checkLink(); 61 | 62 | uint8_t serialId = 0; 63 | uint8_t refCount = 0; 64 | uint8_t linkId = WIFIESPAT_NO_LINK; 65 | const char* udpHost = nullptr; 66 | uint16_t udpPort = 0; 67 | 68 | int8_t writeError = 0; 69 | 70 | uint8_t* rxBuffer = nullptr; 71 | size_t rxBufferSize = 0; 72 | size_t rxBufferIndex = 0; 73 | size_t rxBufferLength = 0; 74 | 75 | uint8_t* txBuffer = nullptr; 76 | size_t txBufferSize = 0; 77 | size_t txBufferLength = 0; 78 | 79 | }; 80 | 81 | #endif 82 | -------------------------------------------------------------------------------- /src/WiFiEspAtConfig.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the WiFiEspAT library for Arduino 3 | https://github.com/jandrassy/WiFiEspAT 4 | Copyright 2019 Juraj Andrassy 5 | 6 | This library is free software; you can redistribute it and/or 7 | modify it under the terms of the GNU Lesser General Public 8 | License as published by the Free Software Foundation; either 9 | version 2.1 of the License, or (at your option) any later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | Lesser General Public License for more details. 15 | 16 | You should have received a copy of the GNU Lesser General Public License 17 | along with this library. If not, see . 18 | */ 19 | 20 | #ifndef _WIFIESPAT_CONFIG_H_ 21 | #define _WIFIESPAT_CONFIG_H_ 22 | 23 | #ifndef WIFIESPAT_INTERNAL_AP_LIST_SIZE 24 | #if defined(__AVR__) && RAMEND <= 0x8FF 25 | #define WIFIESPAT_INTERNAL_AP_LIST_SIZE 3 26 | #else 27 | #define WIFIESPAT_INTERNAL_AP_LIST_SIZE 6 28 | #endif 29 | #endif 30 | 31 | #ifndef WIFIESPAT_CLIENT_RX_BUFFER_SIZE 32 | #if defined(__AVR__) && RAMEND <= 0x8FF 33 | #define WIFIESPAT_CLIENT_RX_BUFFER_SIZE 32 34 | #else 35 | #define WIFIESPAT_CLIENT_RX_BUFFER_SIZE 64 36 | #endif 37 | #endif 38 | 39 | #ifndef WIFIESPAT_CLIENT_TX_BUFFER_SIZE 40 | #if defined(__AVR__) && RAMEND <= 0x8FF 41 | #define WIFIESPAT_CLIENT_TX_BUFFER_SIZE 32 42 | #else 43 | #define WIFIESPAT_CLIENT_TX_BUFFER_SIZE 64 44 | #endif 45 | #endif 46 | 47 | #if WIFIESPAT_CLIENT_RX_BUFFER_SIZE == 0 48 | #define WIFIESPAT_CLIENT_RX_BUFFER_SIZE 1 49 | #warning WiFiClient RX buffer size must be at least 1 50 | #endif 51 | 52 | #ifndef WIFIESPAT_UDP_TX_BUFFER_SIZE 53 | #if defined(__AVR__) && RAMEND <= 0x8FF 54 | #define WIFIESPAT_UDP_TX_BUFFER_SIZE 64 55 | #else 56 | #define WIFIESPAT_UDP_TX_BUFFER_SIZE 256 57 | #endif 58 | #endif 59 | 60 | #ifndef WIFIESPAT_UDP_RX_BUFFER_SIZE 61 | #if defined(__AVR__) && RAMEND <= 0x8FF 62 | #define WIFIESPAT_UDP_RX_BUFFER_SIZE 64 63 | #else 64 | #define WIFIESPAT_UDP_RX_BUFFER_SIZE 256 65 | #endif 66 | #endif 67 | 68 | #endif 69 | -------------------------------------------------------------------------------- /src/WiFiEspAtSharedBuffStreamPtr.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the WiFiEspAT library for Arduino 3 | https://github.com/jandrassy/WiFiEspAT 4 | Copyright 2024 Juraj Andrassy 5 | 6 | This library is free software; you can redistribute it and/or 7 | modify it under the terms of the GNU Lesser General Public 8 | License as published by the Free Software Foundation; either 9 | version 2.1 of the License, or (at your option) any later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | Lesser General Public License for more details. 15 | 16 | You should have received a copy of the GNU Lesser General Public License 17 | along with this library. If not, see . 18 | */ 19 | 20 | #ifndef _ESP_AT_BUFF_PTR_H_ 21 | #define _ESP_AT_BUFF_PTR_H_ 22 | 23 | #include "WiFiEspAtBuffStream.h" 24 | 25 | class WiFiEspAtSharedBuffStreamPtr { 26 | public: 27 | 28 | WiFiEspAtSharedBuffStreamPtr() {} 29 | 30 | WiFiEspAtSharedBuffStreamPtr(WiFiEspAtBuffStream* ptr) { 31 | if (ptr != nullptr) { 32 | this->ptr = ptr; 33 | serialId = ptr->serialId; 34 | increaseRefCount(); 35 | } 36 | } 37 | 38 | WiFiEspAtSharedBuffStreamPtr(WiFiEspAtSharedBuffStreamPtr& other) { 39 | ptr = other.ptr; 40 | serialId = other.serialId; 41 | increaseRefCount(); 42 | } 43 | 44 | WiFiEspAtSharedBuffStreamPtr(WiFiEspAtSharedBuffStreamPtr&& other) { 45 | ptr = other.ptr; 46 | serialId = other.serialId; 47 | other.ptr = nullptr; 48 | } 49 | 50 | ~WiFiEspAtSharedBuffStreamPtr() { 51 | decreaseRefCount(); 52 | } 53 | 54 | WiFiEspAtSharedBuffStreamPtr& operator=(const WiFiEspAtSharedBuffStreamPtr& other) { 55 | checkValid(); 56 | if (ptr != other.ptr && serialId != other.serialId) { 57 | decreaseRefCount(); 58 | ptr = other.ptr; 59 | serialId = other.serialId; 60 | increaseRefCount(); 61 | } 62 | return *this; 63 | } 64 | 65 | WiFiEspAtSharedBuffStreamPtr& operator=(WiFiEspAtSharedBuffStreamPtr&& other) { 66 | checkValid(); 67 | if (ptr != other.ptr && serialId != other.serialId) { 68 | decreaseRefCount(); 69 | ptr = other.ptr; 70 | serialId = other.serialId; 71 | other.ptr = nullptr; 72 | } 73 | return *this; 74 | } 75 | 76 | WiFiEspAtBuffStream* operator->() const { 77 | return ptr; 78 | } 79 | 80 | explicit operator bool() const { 81 | return (ptr != nullptr && serialId == ptr->serialId); 82 | } 83 | 84 | private: 85 | WiFiEspAtBuffStream* ptr = nullptr; 86 | uint8_t serialId = 0; 87 | 88 | bool checkValid() { 89 | if (ptr != nullptr && serialId != ptr->serialId) { 90 | ptr = nullptr; 91 | } 92 | return (ptr != nullptr); 93 | } 94 | 95 | void increaseRefCount() { 96 | if (checkValid()) { 97 | ptr->refCount++; 98 | } 99 | } 100 | void decreaseRefCount() { 101 | if (checkValid()) { 102 | ptr->refCount--; 103 | if (ptr->refCount == 0) { 104 | ptr->close(); 105 | ptr = nullptr; 106 | } 107 | } 108 | } 109 | }; 110 | 111 | #endif 112 | -------------------------------------------------------------------------------- /src/WiFiSSLClient.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the WiFiEspAT library for Arduino 3 | https://github.com/jandrassy/WiFiEspAT 4 | Copyright 2019 Juraj Andrassy 5 | 6 | This library is free software; you can redistribute it and/or 7 | modify it under the terms of the GNU Lesser General Public 8 | License as published by the Free Software Foundation; either 9 | version 2.1 of the License, or (at your option) any later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | Lesser General Public License for more details. 15 | 16 | You should have received a copy of the GNU Lesser General Public License 17 | along with this library. If not, see . 18 | */ 19 | 20 | #include 21 | #include "WiFiSSLClient.h" 22 | 23 | 24 | int WiFiSSLClient::connect(const char* host, uint16_t port) { 25 | return WiFiClient::connectSSL(host, port); 26 | } 27 | 28 | int WiFiSSLClient::connect(IPAddress ip, uint16_t port) { 29 | return WiFiClient::connectSSL(ip, port); 30 | } 31 | 32 | -------------------------------------------------------------------------------- /src/WiFiSSLClient.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the WiFiEspAT library for Arduino 3 | https://github.com/jandrassy/WiFiEspAT 4 | Copyright 2019 Juraj Andrassy 5 | 6 | This library is free software; you can redistribute it and/or 7 | modify it under the terms of the GNU Lesser General Public 8 | License as published by the Free Software Foundation; either 9 | version 2.1 of the License, or (at your option) any later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | Lesser General Public License for more details. 15 | 16 | You should have received a copy of the GNU Lesser General Public License 17 | along with this library. If not, see . 18 | */ 19 | 20 | #ifndef _WIFISSLCLIENT_H_ 21 | #define _WIFISSLCLIENT_H_ 22 | 23 | #include "WiFiClient.h" 24 | 25 | class WiFiSSLClient: public WiFiClient { 26 | public: 27 | virtual int connect(const char* host, uint16_t port); 28 | virtual int connect(IPAddress ip, uint16_t port); 29 | }; 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /src/WiFiServer.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the WiFiEspAT library for Arduino 3 | https://github.com/jandrassy/WiFiEspAT 4 | Copyright 2019 Juraj Andrassy 5 | 6 | This library is free software; you can redistribute it and/or 7 | modify it under the terms of the GNU Lesser General Public 8 | License as published by the Free Software Foundation; either 9 | version 2.1 of the License, or (at your option) any later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | Lesser General Public License for more details. 15 | 16 | You should have received a copy of the GNU Lesser General Public License 17 | along with this library. If not, see . 18 | */ 19 | 20 | #include "utility/EspAtDrv.h" 21 | #include "WiFiServer.h" 22 | 23 | WiFiServer::WiFiServer(uint16_t _port) { 24 | port = _port; 25 | state = CLOSED; 26 | } 27 | 28 | void WiFiServer::begin(uint8_t maxConnCount, uint16_t serverTimeout) { 29 | end(); 30 | state = EspAtDrv.serverBegin(port, maxConnCount, serverTimeout) ? LISTEN : CLOSED; 31 | } 32 | 33 | void WiFiServer::begin(uint16_t _port, uint8_t maxConnCount, uint16_t serverTimeout) { 34 | end(); 35 | port = _port; 36 | begin(maxConnCount, serverTimeout); 37 | } 38 | 39 | void WiFiServer::beginSSL(bool ca, uint8_t maxConnCount, uint16_t serverTimeout) { 40 | end(); 41 | state = EspAtDrv.serverBegin(port, maxConnCount, serverTimeout, true, ca) ? LISTEN : CLOSED; 42 | } 43 | 44 | void WiFiServer::beginSSL(uint16_t _port, bool ca, uint8_t maxConnCount, uint16_t serverTimeout) { 45 | end(); 46 | port = _port; 47 | begin(ca, maxConnCount, serverTimeout); 48 | } 49 | 50 | void WiFiServer::end() { 51 | if (state != CLOSED) { 52 | if (EspAtDrv.serverEnd(port)) { 53 | state = CLOSED; 54 | } 55 | } 56 | } 57 | 58 | uint8_t WiFiServer::status() { 59 | return state; 60 | } 61 | 62 | WiFiClient WiFiServer::available() { 63 | return accept(); 64 | } 65 | 66 | WiFiClient WiFiServer::accept() { 67 | if (state != CLOSED) { 68 | uint8_t linkId = EspAtDrv.newClientLinkId(port); 69 | if (linkId != NO_LINK) 70 | return WiFiClient(linkId); 71 | } 72 | return WiFiClient(); 73 | } 74 | 75 | WiFiServer::operator bool() { 76 | return (state != CLOSED); 77 | } 78 | -------------------------------------------------------------------------------- /src/WiFiServer.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the WiFiEspAT library for Arduino 3 | https://github.com/jandrassy/WiFiEspAT 4 | Copyright 2019 Juraj Andrassy 5 | 6 | This library is free software; you can redistribute it and/or 7 | modify it under the terms of the GNU Lesser General Public 8 | License as published by the Free Software Foundation; either 9 | version 2.1 of the License, or (at your option) any later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | Lesser General Public License for more details. 15 | 16 | You should have received a copy of the GNU Lesser General Public License 17 | along with this library. If not, see . 18 | */ 19 | 20 | #ifndef _WIFISERVER_H_ 21 | #define _WIFISERVER_H_ 22 | 23 | #include "WiFiClient.h" 24 | 25 | #ifndef WIFIESPAT_SERVER_MAX_CLIENTS 26 | #define WIFIESPAT_SERVER_MAX_CLIENTS 1 27 | #endif 28 | 29 | #ifndef WIFIESPAT_SERVER_CLIENT_TIMEOUT 30 | #define WIFIESPAT_SERVER_CLIENT_TIMEOUT 60 // seconds 31 | #endif 32 | 33 | class WiFiServer { 34 | 35 | public: 36 | WiFiServer(uint16_t port = 80); 37 | void begin() { begin(WIFIESPAT_SERVER_MAX_CLIENTS, WIFIESPAT_SERVER_CLIENT_TIMEOUT); } 38 | void begin(uint8_t maxConnCount, uint16_t serverTimeout); 39 | void begin(uint16_t port) { begin(port, WIFIESPAT_SERVER_MAX_CLIENTS, WIFIESPAT_SERVER_CLIENT_TIMEOUT); } 40 | void begin(uint16_t port, uint8_t maxConnCount, uint16_t serverTimeout); 41 | void beginSSL() { beginSSL(false, WIFIESPAT_SERVER_MAX_CLIENTS, WIFIESPAT_SERVER_CLIENT_TIMEOUT); } 42 | void beginSSL(bool ca, uint8_t maxConnCount, uint16_t serverTimeout); 43 | void beginSSL(uint16_t port) { beginSSL(port, false, WIFIESPAT_SERVER_MAX_CLIENTS, WIFIESPAT_SERVER_CLIENT_TIMEOUT); } 44 | void beginSSL(uint16_t port, bool ca, uint8_t maxConnCount, uint16_t serverTimeout); 45 | void end(); 46 | uint8_t status(); 47 | WiFiClient available() __attribute__((deprecated("Use accept()."))); 48 | WiFiClient accept(); 49 | virtual operator bool(); 50 | 51 | private: 52 | uint16_t port; 53 | uint8_t state; 54 | }; 55 | 56 | #endif 57 | -------------------------------------------------------------------------------- /src/WiFiUdp.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the WiFiEspAT library for Arduino 3 | https://github.com/jandrassy/WiFiEspAT 4 | Copyright 2019, 2024 Juraj Andrassy 5 | 6 | This library is free software; you can redistribute it and/or 7 | modify it under the terms of the GNU Lesser General Public 8 | License as published by the Free Software Foundation; either 9 | version 2.1 of the License, or (at your option) any later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | Lesser General Public License for more details. 15 | 16 | You should have received a copy of the GNU Lesser General Public License 17 | along with this library. If not, see . 18 | */ 19 | 20 | #include "utility/EspAtDrv.h" 21 | #include "WiFiUdp.h" 22 | #include "WiFiEspAtBuffManager.h" 23 | 24 | int WiFiUDP::beginPacket(IPAddress ip, uint16_t port) { 25 | EspAtDrv.ip2str(ip, strIP); 26 | return beginPacket(strIP, port); 27 | } 28 | 29 | int WiFiUDP::beginPacket(const char *host, uint16_t port) { 30 | if (txStream) { 31 | endPacket(); 32 | } 33 | uint8_t linkId; 34 | if (listening) { 35 | linkId = this->linkId; // AT allows to use the listener's linkId for sending 36 | } else { 37 | linkId = EspAtDrv.connect("UDP", host, port); 38 | } 39 | if (linkId == NO_LINK) 40 | return false; 41 | txStream = WiFiEspAtBuffManager.getBuffStream(linkId, 0, WIFIESPAT_UDP_TX_BUFFER_SIZE); 42 | if (!txStream) { 43 | if (!listening) { 44 | EspAtDrv.close(linkId); 45 | } 46 | return false; 47 | } 48 | txStream->setUdpPort(host, port); 49 | return true; 50 | } 51 | 52 | int WiFiUDP::endPacket() { 53 | if (!txStream) 54 | return 0; 55 | flush(); 56 | if (listening) { 57 | txStream->free(); 58 | } else { 59 | txStream->close(); 60 | } 61 | txStream = nullptr; 62 | return !getWriteError(); 63 | } 64 | 65 | size_t WiFiUDP::write(uint8_t b) { 66 | if (!txStream) 67 | return 0; 68 | return txStream->write(b); 69 | } 70 | 71 | size_t WiFiUDP::write(const uint8_t *data, size_t length) { 72 | if (!txStream) 73 | return 0; 74 | return txStream->write(data, length); 75 | } 76 | 77 | void WiFiUDP::flush() { 78 | if (!txStream) 79 | return; 80 | txStream->flush(); 81 | } 82 | 83 | int WiFiUDP::availableForWrite() { 84 | if (!txStream) 85 | return 0; 86 | return txStream->availableForWrite(); 87 | } 88 | 89 | size_t WiFiUDP::write(SendCallbackFnc callback) { 90 | if (!txStream) 91 | return 0; 92 | return txStream->write(callback); 93 | } 94 | 95 | #ifdef WIFIESPAT1 96 | uint8_t WiFiUDP::begin(uint16_t port) { 97 | if (linkId != NO_LINK) { 98 | stop(); 99 | } 100 | linkId = EspAtDrv.connect("UDP", "0.0.0.0", port, this, port); 101 | listening = (linkId != NO_LINK); 102 | return listening; 103 | } 104 | 105 | IPAddress WiFiUDP::remoteIP() { 106 | IPAddress ip; 107 | uint16_t port = 0; 108 | uint16_t lport = 0; 109 | if (linkId != NO_LINK) { 110 | EspAtDrv.remoteParamsQuery(linkId, ip, port, lport); 111 | } 112 | return ip; 113 | } 114 | 115 | uint16_t WiFiUDP::remotePort() { 116 | IPAddress ip; 117 | uint16_t port = 0; 118 | uint16_t lport = 0; 119 | if (linkId != NO_LINK) { 120 | EspAtDrv.remoteParamsQuery(linkId, ip, port, lport); 121 | } 122 | return port; 123 | } 124 | 125 | #else 126 | 127 | uint8_t WiFiUDP::begin(const char* ip, uint16_t port) { 128 | if (linkId != NO_LINK) { 129 | stop(); 130 | } 131 | linkId = EspAtDrv.connect("UDP", ip, port, port); 132 | listening = (linkId != NO_LINK); 133 | return listening; 134 | } 135 | 136 | uint8_t WiFiUDP::begin(uint16_t port) { 137 | return begin("0.0.0.0", port); 138 | } 139 | 140 | uint8_t WiFiUDP::beginMulticast(IPAddress ip, uint16_t port) { 141 | char s[16]; 142 | EspAtDrv.ip2str(ip, s); 143 | return begin(s, port); 144 | } 145 | 146 | size_t WiFiUDP::availableForParse() { 147 | if (linkId == NO_LINK) 148 | return 0; 149 | return EspAtDrv.availData(linkId); 150 | } 151 | 152 | size_t WiFiUDP::parsePacket(uint8_t* buffer, size_t size, IPAddress& remoteIP, uint16_t& remotePort) { 153 | size_t len = availableForParse(); 154 | if (!len) 155 | return 0; 156 | if (len > size) { 157 | len = size; 158 | } 159 | return EspAtDrv.recvDataWithInfo(linkId, buffer, len, remoteIP, remotePort); 160 | } 161 | #endif 162 | 163 | void WiFiUDP::stop() { 164 | if (rxStream) { 165 | rxStream->free(); 166 | rxStream = nullptr; 167 | } 168 | if (txStream) { 169 | endPacket(); 170 | } 171 | if (linkId == NO_LINK) 172 | return; 173 | EspAtDrv.close(linkId); 174 | linkId = NO_LINK; 175 | listening = false; 176 | } 177 | 178 | int WiFiUDP::parsePacket() { 179 | if (linkId == NO_LINK) 180 | return 0; 181 | #ifdef WIFIESPAT1 182 | if (rxStream && rxStream->rxBufferIndex > 0) { // clear already read packet 183 | rxStream->free(); 184 | rxStream = nullptr; 185 | } 186 | EspAtDrv.maintain(); 187 | #else 188 | if (rxStream) { 189 | rxStream->free(); 190 | rxStream = nullptr; 191 | } 192 | if (!availableForParse()) 193 | return 0; 194 | rxStream = WiFiEspAtBuffManager.getBuffStream(NO_LINK, WIFIESPAT_UDP_RX_BUFFER_SIZE, 0); 195 | if (!rxStream) 196 | return 0; 197 | rxStream->rxBufferLength = WiFiUDP::parsePacket(rxStream->rxBuffer, WIFIESPAT_UDP_RX_BUFFER_SIZE, senderIP, senderPort); 198 | #endif 199 | return available(); 200 | } 201 | 202 | int WiFiUDP::available() { 203 | if (!rxStream) 204 | return 0; 205 | return rxStream->available(); 206 | } 207 | 208 | int WiFiUDP::read() { 209 | if (!available()) 210 | return -1; 211 | return rxStream->read(); 212 | } 213 | 214 | int WiFiUDP::read(uint8_t* data, size_t size) { 215 | if (size == 0 || !available()) 216 | return 0; 217 | return rxStream->read(data, size); 218 | } 219 | 220 | int WiFiUDP::peek() { 221 | if (!available()) 222 | return -1; 223 | return rxStream->peek(); 224 | } 225 | 226 | #ifdef WIFIESPAT1 227 | uint8_t WiFiUDP::readRxData(Stream* serial, size_t len) { 228 | if (available() > 0) // to avoid overwrite of previous packet 229 | return BUSY; 230 | if (len > WIFIESPAT_UDP_RX_BUFFER_SIZE) 231 | return LARGE; 232 | rxStream = WiFiEspAtBuffManager.getBuffStream(NO_LINK, WIFIESPAT_UDP_RX_BUFFER_SIZE, 0); 233 | if (!rxStream) 234 | return BUSY; 235 | size_t l = serial->readBytes(rxStream->rxBuffer, len); 236 | if (l != len) { // timeout 237 | rxStream->free(); 238 | rxStream = nullptr; 239 | return TIMEOUT; 240 | } 241 | rxStream->rxBufferLength = len; 242 | return OK; 243 | } 244 | #endif 245 | -------------------------------------------------------------------------------- /src/WiFiUdp.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the WiFiEspAT library for Arduino 3 | https://github.com/jandrassy/WiFiEspAT 4 | Copyright 2019, 2024 Juraj Andrassy 5 | 6 | This library is free software; you can redistribute it and/or 7 | modify it under the terms of the GNU Lesser General Public 8 | License as published by the Free Software Foundation; either 9 | version 2.1 of the License, or (at your option) any later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | Lesser General Public License for more details. 15 | 16 | You should have received a copy of the GNU Lesser General Public License 17 | along with this library. If not, see . 18 | */ 19 | 20 | #ifndef _WIFIUDP_H_ 21 | #define _WIFIUDP_H_ 22 | 23 | #include 24 | #include "WiFiEspAtConfig.h" 25 | #include "WiFiEspAtSharedBuffStreamPtr.h" 26 | #include "utility/EspAtDrvTypes.h" 27 | 28 | class WiFiUDP : public UDP 29 | #ifdef WIFIESPAT1 30 | , protected EspAtDrvUdpDataCallback 31 | #endif 32 | { 33 | public: 34 | 35 | // Sending UDP packets 36 | virtual int beginPacket(IPAddress ip, uint16_t port); 37 | virtual int beginPacket(const char *host, uint16_t port); 38 | virtual int endPacket(); 39 | 40 | virtual size_t write(uint8_t); 41 | virtual size_t write(const uint8_t *buffer, size_t size); 42 | virtual void flush(); 43 | virtual int availableForWrite(); 44 | 45 | size_t write(SendCallbackFnc callback); 46 | 47 | using Print::write; 48 | 49 | virtual uint8_t begin(uint16_t port); 50 | 51 | #ifndef WIFIESPAT1 // AT2 52 | virtual uint8_t beginMulticast(IPAddress ip, uint16_t port); 53 | 54 | // WiFiEspAT AT2 special functions for receive 55 | size_t availableForParse(); 56 | size_t parsePacket(uint8_t* buffer, size_t bufferSize, IPAddress& remoteIP, uint16_t& remotePort); 57 | #endif 58 | 59 | virtual void stop(); 60 | 61 | // Listening for UDP packets 62 | virtual int parsePacket(); 63 | virtual int available(); 64 | virtual int read(); 65 | virtual int read(uint8_t* buffer, size_t len); 66 | virtual int read(char* buffer, size_t len) {return read((uint8_t*) buffer, len);} 67 | virtual int peek(); 68 | 69 | #ifdef WIFIESPAT1 70 | virtual IPAddress remoteIP(); 71 | virtual uint16_t remotePort(); 72 | 73 | protected: 74 | virtual uint8_t readRxData(Stream* serial, size_t len); // EspAtDrvUdpDataCallback implementation 75 | 76 | #else 77 | virtual IPAddress remoteIP() {return senderIP;} 78 | virtual uint16_t remotePort() {return senderPort;} 79 | 80 | #endif 81 | 82 | private: 83 | uint8_t linkId = WIFIESPAT_NO_LINK; 84 | bool listening = false; 85 | WiFiEspAtSharedBuffStreamPtr txStream; 86 | WiFiEspAtSharedBuffStreamPtr rxStream; 87 | char strIP[16]; // to hold the string version of IP for beginPacket(ip, port); 88 | 89 | #ifndef WIFIESPAT1 //AT2 90 | IPAddress senderIP; 91 | uint16_t senderPort; 92 | 93 | uint8_t begin(const char* ip, uint16_t port); 94 | #endif 95 | }; 96 | 97 | #endif 98 | 99 | -------------------------------------------------------------------------------- /src/utility/EspAtDrv.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the WiFiEspAT library for Arduino 3 | https://github.com/jandrassy/WiFiEspAT 4 | Copyright 2019, 2024 Juraj Andrassy 5 | 6 | This library is free software; you can redistribute it and/or 7 | modify it under the terms of the GNU Lesser General Public 8 | License as published by the Free Software Foundation; either 9 | version 2.1 of the License, or (at your option) any later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | Lesser General Public License for more details. 15 | 16 | You should have received a copy of the GNU Lesser General Public License 17 | along with this library. If not, see . 18 | */ 19 | 20 | #ifndef _ESP_AT_DRV_H_ 21 | #define _ESP_AT_DRV_H_ 22 | 23 | #include 24 | #include 25 | #include "utility/EspAtDrvTypes.h" 26 | 27 | const uint8_t LINKS_COUNT = WIFIESPAT_LINKS_COUNT; 28 | const uint8_t NO_LINK = WIFIESPAT_NO_LINK; 29 | 30 | const uint8_t LINK_CONNECTED = (1 << 0); 31 | const uint8_t LINK_CLOSING = (1 << 1); 32 | const uint8_t LINK_IS_INCOMING = (1 << 2); 33 | const uint8_t LINK_IS_ACCEPTED = (1 << 3); 34 | const uint8_t LINK_IS_UDP_LISTNER = (1 << 4); 35 | 36 | const uint8_t INDEX_MASK = 0b111; 37 | const uint8_t SERIALID_MASK = ~INDEX_MASK; 38 | 39 | //#define WIFIESPAT_MULTISERVER 40 | 41 | struct LinkInfo { 42 | uint8_t serialId = 0; 43 | uint8_t flags = 0; 44 | size_t available = 0; 45 | #ifdef WIFIESPAT_MULTISERVER 46 | uint16_t localPort = 0; 47 | #endif 48 | 49 | #ifdef WIFIESPAT1 50 | EspAtDrvUdpDataCallback* udpDataCallback; 51 | #endif 52 | 53 | bool isConnected() { return flags & LINK_CONNECTED;} 54 | bool isClosing() { return flags & LINK_CLOSING;} 55 | bool isIncoming() { return flags & LINK_IS_INCOMING;} 56 | bool isUdpListener() { return flags & LINK_IS_UDP_LISTNER;} 57 | 58 | void incrementSerialId() { 59 | serialId += (INDEX_MASK + 1); 60 | } 61 | }; 62 | 63 | class EspAtDrvClass { 64 | public: 65 | 66 | bool init(Stream* serial, int8_t resetPin = -1); 67 | 68 | bool reset(int8_t resetPin = -1); 69 | void maintain(); 70 | EspAtDrvError getLastErrorCode() {return lastErrorCode;} 71 | bool firmwareVersion(char* buff); 72 | bool sysPersistent(bool persistent); 73 | 74 | int staStatus(); 75 | int ethStatus(); 76 | 77 | uint8_t listAP(WiFiApData apData[], uint8_t size); // returns count of filled records 78 | 79 | bool setDNS(const IPAddress& dns1, const IPAddress& dns2); 80 | bool dnsQuery(IPAddress& dns1, IPAddress& dns2); 81 | 82 | bool staStaticIp(const IPAddress& local_ip, const IPAddress& gateway, const IPAddress& subnet); 83 | bool staEnableDHCP(); 84 | bool staMacQuery(uint8_t* mac); 85 | bool staIpQuery(IPAddress& ip, IPAddress& gwip, IPAddress& mask); 86 | 87 | bool joinAP(const char* ssid, const char* password, const uint8_t* bssid); 88 | bool joinEAP(const char* ssid, uint8_t method, const char* identity, const char* username, const char* password, uint8_t security); 89 | bool quitAP(bool save); 90 | bool staAutoConnect(bool autoConnect); 91 | bool apQuery(char* ssid, uint8_t* bssid, uint8_t& channel, int8_t& rssi); 92 | 93 | bool softApIp(const IPAddress& local_ip, const IPAddress& gateway, const IPAddress& subnet); 94 | bool softApMacQuery(uint8_t* mac); 95 | bool softApIpQuery(IPAddress& ip, IPAddress& gwip, IPAddress& mask); 96 | 97 | bool beginSoftAP(const char *ssid = nullptr, const char* passphrase = nullptr, uint8_t channel = 1, 98 | uint8_t encoding = 4, uint8_t maxConnections = 0, bool hidden = false); 99 | bool endSoftAP(bool persistent = false); 100 | bool softApQuery(char* ssid, char* passphrase, uint8_t& channel, uint8_t& encoding, uint8_t& maxConnections, bool& hidden); 101 | 102 | bool ethSetMac(uint8_t* mac); 103 | bool ethStaticIp(const IPAddress& local_ip, const IPAddress& gateway, const IPAddress& subnet); 104 | bool ethEnableDHCP(); 105 | bool ethMacQuery(uint8_t* mac); 106 | bool ethIpQuery(IPAddress& ip, IPAddress& gwip, IPAddress& mask); 107 | bool setEthHostname(const char* hostname); 108 | bool ethHostnameQuery(char* hostname); 109 | 110 | bool serverBegin(uint16_t port, uint8_t maxConnCount = 1, uint16_t serverTimeout = 60, bool ssl = false, bool ca = false); 111 | bool serverEnd(uint16_t port); 112 | uint8_t newClientLinkId(uint16_t serverPort); 113 | 114 | uint8_t connect(const char* type, const char* host, uint16_t port, // 115 | #ifdef WIFIESPAT1 116 | EspAtDrvUdpDataCallback* udpDataCallback = nullptr, 117 | #endif 118 | uint16_t udpLocalPort = 0); 119 | bool close(uint8_t linkId, bool abort = false); 120 | 121 | uint16_t localPortQuery(uint8_t linkId); 122 | bool remoteParamsQuery(uint8_t linkId, IPAddress& remoteIP, uint16_t& remotePort, uint16_t& localPort); 123 | 124 | bool connected(uint8_t linkId); 125 | size_t availData(uint8_t linkId); 126 | 127 | size_t recvData(uint8_t linkId, uint8_t buff[], size_t buffSize); 128 | size_t recvDataWithInfo(uint8_t linkId, uint8_t buff[], size_t buffSize, IPAddress& remoteIP, uint16_t& remotePort); 129 | size_t sendData(uint8_t linkId, const uint8_t buff[], size_t dataLength, const char* udpHost, uint16_t udpPort); 130 | size_t sendData(uint8_t linkId, Stream& file, const char* udpHost, uint16_t udpPort); 131 | size_t sendData(uint8_t linkId, SendCallbackFnc callback, const char* udpHost, uint16_t udpPort); 132 | 133 | bool setHostname(const char* hostname); 134 | bool hostnameQuery(char* hostname); 135 | bool dhcpStateQuery(bool& staDHCP, bool& softApDHCP, bool& ethDHCP); 136 | bool mDNS(const char* hostname, const char* serverName, uint16_t serverPort); 137 | bool resolve(const char* hostname, IPAddress& result); 138 | bool sntpCfg(const char* server1, const char* server2); 139 | unsigned long sntpTime(); 140 | bool ping(const char* hostname); 141 | 142 | bool wifiOff(bool save = false); 143 | bool sleepMode(EspAtSleepMode mode); 144 | bool deepSleep(); 145 | 146 | void ip2str(const IPAddress& ip, char* s); 147 | 148 | private: 149 | Stream* serial; 150 | Print* cmd; // debug wrapper or serial 151 | char buffer[64]; 152 | bool persistent = false; 153 | uint8_t wifiMode = 0; 154 | int8_t wifiModeDef = -1; 155 | bool ethConnected = false; 156 | LinkInfo linkInfo[LINKS_COUNT]; 157 | EspAtDrvError lastErrorCode = EspAtDrvError::NOT_INITIALIZED; 158 | unsigned long lastSyncMillis; 159 | 160 | uint8_t freeLinkId(); 161 | uint8_t checkLinkId(uint8_t linkId); 162 | 163 | bool readRX(PGM_P expected, bool bufferData = true, bool listItem = false); 164 | bool readOK(); 165 | bool sendCommand(PGM_P expected = nullptr, bool bufferData = true, bool listItem = false); 166 | bool simpleCommand(PGM_P cmd); 167 | 168 | bool setWifiMode(uint8_t mode, bool persistent = false); 169 | bool syncLinkInfo(); 170 | bool recvLenQuery(); 171 | bool checkLinks(); 172 | 173 | bool sysStoreInternal(bool store); // AT 2 174 | 175 | void printMAC(Print* out, uint8_t* mac); 176 | }; 177 | 178 | extern EspAtDrvClass EspAtDrv; 179 | 180 | #endif 181 | -------------------------------------------------------------------------------- /src/utility/EspAtDrvLogging.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the WiFiEspAT library for Arduino 3 | https://github.com/jandrassy/WiFiEspAT 4 | Copyright 2019 Juraj Andrassy 5 | 6 | This library is free software; you can redistribute it and/or 7 | modify it under the terms of the GNU Lesser General Public 8 | License as published by the Free Software Foundation; either 9 | version 2.1 of the License, or (at your option) any later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | Lesser General Public License for more details. 15 | 16 | You should have received a copy of the GNU Lesser General Public License 17 | along with this library. If not, see . 18 | */ 19 | 20 | #include "EspAtDrvLogging.h" 21 | #include // to include PROGMEM in a compatible way 22 | 23 | #if WIFIESPAT_LOG_LEVEL >= LOG_LEVEL_ERROR 24 | const char LOG_ERROR_PREFIX_PROGMEM[] PROGMEM = LOG_ERROR_PREFIX; 25 | #endif 26 | #if WIFIESPAT_LOG_LEVEL >= LOG_LEVEL_WARN 27 | const char LOG_WARN_PREFIX_PROGMEM[] PROGMEM = LOG_WARN_PREFIX; 28 | #endif 29 | #if WIFIESPAT_LOG_LEVEL >= LOG_LEVEL_INFO 30 | const char LOG_INFO_PREFIX_PROGMEM[] PROGMEM = LOG_INFO_PREFIX; 31 | #endif 32 | #if WIFIESPAT_LOG_LEVEL >= LOG_LEVEL_DEBUG 33 | const char LOG_DEBUG_PREFIX_PROGMEM[] PROGMEM = LOG_DEBUG_PREFIX; 34 | #endif 35 | -------------------------------------------------------------------------------- /src/utility/EspAtDrvLogging.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the WiFiEspAT library for Arduino 3 | https://github.com/jandrassy/WiFiEspAT 4 | Copyright 2019 Juraj Andrassy 5 | 6 | This library is free software; you can redistribute it and/or 7 | modify it under the terms of the GNU Lesser General Public 8 | License as published by the Free Software Foundation; either 9 | version 2.1 of the License, or (at your option) any later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | Lesser General Public License for more details. 15 | 16 | You should have received a copy of the GNU Lesser General Public License 17 | along with this library. If not, see . 18 | */ 19 | 20 | #ifndef _WIFIESPAT_DEBUG_H_ 21 | #define _WIFIESPAT_DEBUG_H_ 22 | 23 | #include 24 | 25 | #define FSH_P const __FlashStringHelper* 26 | 27 | #define LOG_LEVEL_SILENT 0 28 | #define LOG_LEVEL_ERROR 1 29 | #define LOG_LEVEL_WARN 2 30 | #define LOG_LEVEL_INFO 3 31 | #define LOG_LEVEL_DEBUG 4 32 | 33 | #ifndef WIFIESPAT_LOG_LEVEL 34 | #define WIFIESPAT_LOG_LEVEL LOG_LEVEL_SILENT 35 | #endif 36 | 37 | #ifdef ARDUINO_SAM_ZERO // M0 38 | #define LOG_OUTPUT SerialUSB 39 | #else 40 | #define LOG_OUTPUT Serial 41 | #endif 42 | 43 | #define LOG_ERROR_PREFIX "esp ERROR: " 44 | #if WIFIESPAT_LOG_LEVEL >= LOG_LEVEL_ERROR 45 | extern const char LOG_ERROR_PREFIX_PROGMEM[]; 46 | #define LOG_ERROR_PRINT_PREFIX() LOG_OUTPUT.print((FSH_P) LOG_ERROR_PREFIX_PROGMEM) 47 | #define LOG_ERROR_PRINT(msg) LOG_OUTPUT.print(msg) 48 | #define LOG_ERROR_PRINTLN(msg) LOG_OUTPUT.println(msg) 49 | #else 50 | #define LOG_ERROR_PRINT_PREFIX() 51 | #define LOG_ERROR_PRINT(msg) 52 | #define LOG_ERROR_PRINTLN(msg) 53 | #endif 54 | 55 | #define LOG_WARN_PREFIX "esp WARN: " 56 | #if WIFIESPAT_LOG_LEVEL >= LOG_LEVEL_WARN 57 | extern const char LOG_WARN_PREFIX_PROGMEM[]; 58 | #define LOG_WARN_PRINT_PREFIX() LOG_OUTPUT.print((FSH_P) LOG_WARN_PREFIX_PROGMEM) 59 | #define LOG_WARN_PRINT(msg) LOG_OUTPUT.print(msg) 60 | #define LOG_WARN_PRINTLN(msg) LOG_OUTPUT.println(msg) 61 | #else 62 | #define LOG_WARN_PRINT_PREFIX() 63 | #define LOG_WARN_PRINT(msg) 64 | #define LOG_WARN_PRINTLN(msg) 65 | #endif 66 | 67 | #define LOG_INFO_PREFIX "esp INFO: " 68 | #if WIFIESPAT_LOG_LEVEL >= LOG_LEVEL_INFO 69 | extern const char LOG_INFO_PREFIX_PROGMEM[]; 70 | #define LOG_INFO_PRINT_PREFIX() LOG_OUTPUT.print((FSH_P) LOG_INFO_PREFIX_PROGMEM) 71 | #define LOG_INFO_PRINT(msg) LOG_OUTPUT.print(msg) 72 | #define LOG_INFO_PRINTLN(msg) LOG_OUTPUT.println(msg) 73 | #else 74 | #define LOG_INFO_PRINT_PREFIX() 75 | #define LOG_INFO_PRINT(msg) 76 | #define LOG_INFO_PRINTLN(msg) 77 | #endif 78 | 79 | #define LOG_DEBUG_PREFIX "esp> " 80 | #if WIFIESPAT_LOG_LEVEL >= LOG_LEVEL_DEBUG 81 | extern const char LOG_DEBUG_PREFIX_PROGMEM[]; 82 | #define LOG_DEBUG_PRINT_PREFIX() LOG_OUTPUT.print((FSH_P) LOG_DEBUG_PREFIX_PROGMEM) 83 | #define LOG_DEBUG_PRINT(msg) LOG_OUTPUT.print(msg) 84 | #define LOG_DEBUG_PRINTLN(msg) LOG_OUTPUT.println(msg) 85 | #else 86 | #define LOG_DEBUG_PRINT_PREFIX() 87 | #define LOG_DEBUG_PRINT(msg) 88 | #define LOG_DEBUG_PRINTLN(msg) 89 | #endif 90 | 91 | #endif 92 | -------------------------------------------------------------------------------- /src/utility/EspAtDrvTypes.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the WiFiEspAT library for Arduino 3 | https://github.com/jandrassy/WiFiEspAT 4 | Copyright 2019 Juraj Andrassy 5 | 6 | This library is free software; you can redistribute it and/or 7 | modify it under the terms of the GNU Lesser General Public 8 | License as published by the Free Software Foundation; either 9 | version 2.1 of the License, or (at your option) any later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | Lesser General Public License for more details. 15 | 16 | You should have received a copy of the GNU Lesser General Public License 17 | along with this library. If not, see . 18 | */ 19 | 20 | #ifndef _ESPATDRV_TYPES_H_ 21 | #define _ESPATDRV_TYPES_H_ 22 | 23 | #include 24 | 25 | #ifndef WIFIESPAT2 // for -D 26 | #define WIFIESPAT1 27 | #endif 28 | 29 | const uint8_t WIFIESPAT_LINKS_COUNT = 5; 30 | const uint8_t WIFIESPAT_NO_LINK = 255; 31 | 32 | class EspAtDrvClass; 33 | 34 | enum struct EspAtDrvError { 35 | NO_ERROR, 36 | NOT_INITIALIZED, 37 | AT_NOT_RESPONDIG, 38 | AT_ERROR, 39 | NO_AP, 40 | LINK_ALREADY_CONNECTED, 41 | LINK_NOT_ACTIVE, 42 | RECEIVE, 43 | SEND, 44 | UDP_BUSY, 45 | UDP_LARGE, 46 | UDP_TIMEOUT 47 | }; 48 | 49 | enum EspAtSleepMode { 50 | WIFI_NONE_SLEEP = 0, 51 | WIFI_LIGHT_SLEEP = 1, 52 | WIFI_MODEM_SLEEP = 2 53 | }; 54 | 55 | struct WiFiApData { 56 | char ssid[33]; 57 | uint8_t bssid[6]; 58 | int8_t rssi; 59 | uint8_t channel; 60 | uint8_t enc; 61 | }; 62 | 63 | #ifdef WIFIESPAT1 64 | class EspAtDrvUdpDataCallback { 65 | protected: 66 | 67 | static const uint8_t OK = 0; 68 | static const uint8_t BUSY = 1; 69 | static const uint8_t LARGE = 2; 70 | static const uint8_t TIMEOUT = 3; 71 | 72 | virtual uint8_t readRxData(Stream* serial, size_t len) = 0; 73 | friend EspAtDrvClass; 74 | }; 75 | #endif 76 | 77 | typedef void (*SendCallbackFnc)(Print& p); 78 | 79 | #endif 80 | --------------------------------------------------------------------------------