├── LICENSE ├── README ├── config.cpp ├── config.h ├── debug.h ├── logger.cpp ├── logger.h ├── low_power.cpp ├── low_power.h ├── main.ino ├── tool.c ├── tool.h ├── tracker.cpp ├── tracker.h ├── wifiscan.cpp ├── wifiscan.h ├── wisol.cpp └── wisol.h /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | ESP8266-Sigofx-trackr is a ESP8266 / Arduino based WiFi tracking solution using Sigfox Atlas WiFi geoloction service. 2 | 3 | This project uses an ESP8266 as main MCU and for WiFi scanning on regular basis. The 2 best Access Points MAC address are reported over Sigfox network using a Wisol WSSFM10R1 module. 4 | 5 | This project is implementing Low Power feature on ESP8266 and Wisol module to reach a couple of uA most of the time. It includes different sub-library for managing: 6 | - ESP low power switch with context backup on RTC memory 7 | - ESP logging feature with flash storage and level filtering 8 | - Wisol module access library 9 | 10 | You will find more information about this sketch on my website https://www.disk91.com 11 | And the related blog post is https://www.disk91.com/2018/technology/sigfox/create-a-5-autonomous-tracker-with-esp8266-and-sigfox/ 12 | 13 | Specific dependency : 14 | * SoftwareSerial - https://github.com/plerup/espsoftwareserial - **/!\ version > 3.4.1 is recommended** 15 | 16 | -------------------------------------------------------------------------------- /config.cpp: -------------------------------------------------------------------------------- 1 | /* ====================================================================== 2 | This file is part of disk91_config. 3 | 4 | disk91_config is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Foobar is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with Foobar. If not, see . 16 | ======================================================================= 17 | */ 18 | /* ====================================================================== 19 | * ESP8266 config module 20 | * ---------------------------------------------------------------------- 21 | * (c) Disk91 - 2018 22 | * Author : Paul Pinault aka disk91.com 23 | * ---------------------------------------------------------------------- 24 | */ 25 | 26 | #include "config.h" 27 | extern "C" { 28 | #include "tool.h" 29 | } 30 | #include "debug.h" 31 | #include "wisol.h" 32 | #include 33 | 34 | ConfigClass configService; 35 | 36 | /** 37 | * Set the config structure with the factory default information 38 | */ 39 | void ConfigClass::setDefaultConfig() { 40 | // Mandtory init 41 | config.magic = EEPROM_MAGIC; 42 | config.size = sizeof(t_config); 43 | config.crc32 = 0x00; 44 | config.firmwareVersion = FIRMWARE_VERSION; 45 | 46 | // Project specific configuration 47 | config.sigfoxId = wisolService.getSigfoxIdWithRetry(3); 48 | config.logConfig = CONFIG_LOGGEUR; 49 | 50 | // -- end of project specific code 51 | config.crc32 = calculateCRC32((uint8_t*) &config, sizeof(t_config)); 52 | } 53 | 54 | 55 | void ConfigClass::printConfig() { 56 | TTRACE(("-------- Config --------\r\n")); 57 | TTRACE((" Build %s %s\r\n",__DATE__,__TIME__)); 58 | TTRACE((" Magic : %04X\r\n",config.magic)); 59 | TTRACE((" HW version : %02X\r\n",HARDWARE_VERSION)); 60 | TTRACE((" FW version : %02X\r\n",config.firmwareVersion)); 61 | 62 | // Project specific configuration 63 | TTRACE((" SigfoxId : %08X\r\n",config.sigfoxId)); 64 | TTRACE((" logConfig : %04X\r\n",config.logConfig)); 65 | 66 | } 67 | 68 | 69 | /** 70 | * Force Init the device config 71 | * return true if the default config has been flashed. 72 | * Rq : No trace - at this point the trace configuration is not activated. 73 | */ 74 | bool ConfigClass::init(bool forceReset) { 75 | // Try to load the config from EEPROM 76 | if ( ! loadConfig() || forceReset ) { 77 | // Flash the default configuration 78 | TTRACE1(("Flash the default configuration\r\n")); 79 | setDefaultConfig(); 80 | storeConfig(); 81 | return true; 82 | } 83 | return false; 84 | } 85 | 86 | /** 87 | * Store the config struture into the EEPROM 88 | */ 89 | void ConfigClass::storeConfig() { 90 | EEPROM.begin(EPROM_MAX_SZ); 91 | EEPROM.put(0,config); 92 | EEPROM.commit(); 93 | EEPROM.end(); 94 | } 95 | 96 | 97 | bool ConfigClass::loadConfig() { 98 | EEPROM.begin(EPROM_MAX_SZ); 99 | EEPROM.get(0,config); 100 | EEPROM.end(); 101 | 102 | if ( config.magic == EEPROM_MAGIC ) { 103 | if ( config.size == sizeof(t_config) ) { 104 | uint32_t crc32 = config.crc32; 105 | config.crc32 = 0; 106 | if ( calculateCRC32((uint8_t*) &config, sizeof(t_config)) == crc32 ) { 107 | if ( config.firmwareVersion == FIRMWARE_VERSION) { 108 | // Load sucess 109 | return true; 110 | } else { 111 | TTRACE1(("Config Load Error - FW version\r\n")); 112 | } 113 | } else { 114 | TTRACE1(("Config Load Error - CRC\r\n")); 115 | } 116 | } else { 117 | TTRACE1(("Config Load Error - Size\r\n")); 118 | } 119 | } else { 120 | TTRACE1(("Config Load Error - Magic\r\n")); 121 | } 122 | return false; 123 | } 124 | 125 | 126 | -------------------------------------------------------------------------------- /config.h: -------------------------------------------------------------------------------- 1 | /* ====================================================================== 2 | This file is part of disk91_config. 3 | 4 | disk91_config is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Foobar is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with Foobar. If not, see . 16 | ======================================================================= 17 | */ 18 | /* ====================================================================== 19 | * ESP8266 config module 20 | * ---------------------------------------------------------------------- 21 | * (c) Disk91 - 2018 22 | * Author : Paul Pinault aka disk91.com 23 | * ---------------------------------------------------------------------- 24 | */ 25 | 26 | #ifndef CONFIG_H_ 27 | #define CONFIG_H_ 28 | 29 | #include 30 | 31 | // ------------------------------------------------- 32 | // Version 33 | #define HARDWARE_VERSION 0x01 34 | #define FIRMWARE_VERSION 0x01 35 | 36 | // ------------------------------------------------- 37 | // Scheduling 38 | //#define SCHEDULER_PERIOD_MS (30*1000) 39 | #define SCHEDULER_PERIOD_MS (15*60*1000) // 15 minutes scan and transmission 40 | 41 | 42 | // ------------------------------------------------- 43 | // Trace & Debug 44 | #define DEBUG 3 45 | #define CONFIG_LOGGEUR 0xF0FF // all level in file and Serial 46 | 47 | // ------------------------------------------------- 48 | // HARDWARE PINOUT 49 | #define WISOL_RX_PIN D6 50 | #define WISOL_TX_PIN D7 51 | #define DEBUG_SOFTSERIAL_RX_PIN 0 52 | #define DEBUG_SOFTSERIAL_TX_PIN 0 53 | 54 | 55 | // ------------------------------------------------- 56 | // HARDWARE ESP DEFINES 57 | #define EPROM_MAX_SZ 512 58 | #define RTC_MAX_SZ 512 59 | #define EEPROM_MAGIC 0xA5FC 60 | 61 | 62 | 63 | typedef struct s_config { 64 | // mandatory fields for config management 65 | uint16_t magic; 66 | uint16_t size; 67 | uint32_t crc32; 68 | 69 | // here are the project specific settings 70 | uint8_t firmwareVersion; 71 | uint16_t logConfig; // see logger.cpp to get the format 72 | uint32_t sigfoxId; 73 | 74 | } t_config; 75 | 76 | class ConfigClass { 77 | public: 78 | t_config config; 79 | 80 | bool init(bool forceReset); 81 | bool defaultConfig(); 82 | bool loadConfig(); 83 | void storeConfig(); 84 | void printConfig(); 85 | 86 | protected: 87 | void setDefaultConfig(); 88 | 89 | 90 | }; 91 | extern ConfigClass configService; 92 | 93 | 94 | #endif 95 | -------------------------------------------------------------------------------- /debug.h: -------------------------------------------------------------------------------- 1 | /* ====================================================================== 2 | This file is part of disk91_tools. 3 | 4 | disk91_tools is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Foobar is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with Foobar. If not, see . 16 | ======================================================================= 17 | */ 18 | /* ====================================================================== 19 | * Debug settings 20 | * ---------------------------------------------------------------------- 21 | * (c) Disk91 - 2018 22 | * Author : Paul Pinault aka disk91.com 23 | * ---------------------------------------------------------------------- 24 | */ 25 | 26 | #ifndef DEBUG_H_ 27 | #define DEBUG_H_ 28 | 29 | #include 30 | 31 | #ifdef DEBUG 32 | #define TTRACE(x) Serial.printf x 33 | #define DTRACE(x) Serial.printf(x) 34 | #else 35 | #define TTRACE(x) 36 | #define DTRACE(x) 37 | #endif 38 | 39 | #if DEBUG > 1 40 | #define TTRACE1(x) Serial.printf x 41 | #else 42 | #define TTRACE1(x) 43 | #endif 44 | 45 | #if DEBUG > 2 46 | #define TTRACE2(x) Serial.printf x 47 | #else 48 | #define TTRACE2(x) 49 | #endif 50 | 51 | #if DEBUG > 3 52 | #define TTRACE3(x) Serial.printf x 53 | #else 54 | #define TTRACE3(x) 55 | #endif 56 | 57 | #define FLUSH(x) { Serial.flush(); delay(2); } 58 | 59 | #define STOP_PIN D5 60 | 61 | 62 | #endif 63 | -------------------------------------------------------------------------------- /logger.cpp: -------------------------------------------------------------------------------- 1 | /* ====================================================================== 2 | This file is part of disk91_logger. 3 | 4 | disk91_logger is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Foobar is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with Foobar. If not, see . 16 | ======================================================================= 17 | */ 18 | 19 | /* ====================================================================== 20 | * LOGGER / ESP8266 / logging module 21 | * ---------------------------------------------------------------------- 22 | * (c) Disk91 - 2018 23 | * Author : Paul Pinault aka disk91.com 24 | * ---------------------------------------------------------------------- 25 | */ 26 | 27 | 28 | #include "logger.h" 29 | #include "config.h" 30 | 31 | LoggerClass _log; 32 | 33 | /** 34 | * Init the logger structure from a given configuration 35 | * The configuration is a 32bits field for each of the 36 | * 4 possible logging option with the 4 level of trace. 37 | * This configuration is optimize to be stored in the application 38 | * configuration and context to be easily saved & restore when 39 | * going deep sleep. 40 | * Format is: 41 | * +-------------------------------------------------------------------+ 42 | * | FILE output | SoftSerial output | Serial1 output | Serial output | 43 | * +-------------------------------------------------------------------+ 44 | * | D I W E | D I W E | D I W E | D I W E | 45 | * +-------------------------------------------------------------------+ 46 | * D : DEBUG Level - activated when 1 / Mask when 0 47 | * I : INFO Level - activated when 1 / Mask when 0 48 | * W : WARN Level - activated when 1 / Mask when 0 49 | * E : ERROR Level - activated when 1 / Mask when 0 50 | */ 51 | bool LoggerClass::init(uint16_t config) { 52 | 53 | this->logError = ( config & LOGGER_CONFIG_ERROR_LVL_MASK ); 54 | this->logWarn = ( config & LOGGER_CONFIG_WARN_LVL_MASK ); 55 | this->logInfo = ( config & LOGGER_CONFIG_INFO_LVL_MASK ); 56 | this->logDebug = ( config & LOGGER_CONFIG_DEBUG_LVL_MASK ); 57 | this->onSerial = ( config & LOGGER_CONFIG_SERIAL_MASK ); 58 | this->onSerial1 = ( config & LOGGER_CONFIG_SERIAL1_MASK ); 59 | this->onSSerial = ( config & LOGGER_CONFIG_SSERIAL_MASK ); 60 | this->onFile = ( config & LOGGER_CONFIG_FILE_MASK ); 61 | 62 | // Init the loggers 63 | if (this->onSerial) { 64 | if ( !Serial ) { 65 | Serial.begin(LOGGER_SERIAL_DEFAULT_SPEED); 66 | } 67 | if ( Serial.baudRate() != LOGGER_SERIAL_DEFAULT_SPEED) { 68 | Serial.begin(LOGGER_SERIAL_DEFAULT_SPEED); 69 | } 70 | } 71 | 72 | if (this->onSerial1) { 73 | Serial1.begin(LOGGER_SERIAL1_DEFAULT_SPEED); 74 | } 75 | 76 | if (this->onSSerial) { 77 | if ( SSerial == NULL ) SSerial = new SoftwareSerial(DEBUG_SOFTSERIAL_RX_PIN, DEBUG_SOFTSERIAL_TX_PIN, false, 256); // RX, TX, Invert, Buffer Size 78 | SSerial->begin(9600); 79 | } 80 | 81 | if (this->onFile) { 82 | if ( !SPIFFS.begin() || !SPIFFS.exists("/formatComplete.txt") ) { 83 | // format fs 84 | SPIFFS.format(); 85 | File f = SPIFFS.open("/formatComplete.txt", "w"); 86 | f.close(); 87 | } 88 | 89 | this->logFile = SPIFFS.open("/log.txt", "a"); 90 | if ( this->logFile ) { 91 | if ( this->logFile.size() >= LOGGER_FILE_MAX_SIZE ) { 92 | this->logFile.close(); 93 | SPIFFS.remove("/log.txt"); 94 | this->logFile = SPIFFS.open("/log.txt", "a"); 95 | } 96 | } 97 | if ( !this->logFile ) { 98 | // problem, disable file logging 99 | this->onFile = false; 100 | } 101 | } 102 | this->logConf = config; 103 | this->ready = true; 104 | return true; 105 | } 106 | 107 | /** 108 | * Terminate the logging. This should be called before going deep-sleep to flush 109 | * Current log processing. 110 | */ 111 | uint16_t LoggerClass::close() { 112 | if ( this->onFile) { 113 | this->logFile.close(); 114 | SPIFFS.end(); 115 | } 116 | if ( this->onSerial) { 117 | Serial.flush(); 118 | } 119 | if ( this->onSerial1) { 120 | Serial1.flush(); 121 | } 122 | if ( this->onSSerial) { 123 | SSerial->flush(); 124 | } 125 | this->ready=false; 126 | return this->logConf; 127 | } 128 | 129 | /** 130 | * Print the log file over the serial line. As this is usually called during the 131 | * sleeping loop the SPIFF is supposed to be closed. The function try to manage this and 132 | * restore the initial state. 133 | */ 134 | void LoggerClass::cat() { 135 | if ( this->ready && this->onFile) { 136 | this->logFile.close(); 137 | } 138 | if ( !this->ready ) { 139 | SPIFFS.begin(); 140 | } 141 | this->logFile = SPIFFS.open("/log.txt", "r"); 142 | if (this->logFile) { 143 | Serial.printf("====== Read Log File (%db)=======\n",this->logFile.size()); 144 | while ( this->logFile.available() ) { 145 | Serial.print((char)this->logFile.read()); 146 | } 147 | Serial.println("====== end of Log File =======\n"); 148 | this->logFile.close(); 149 | } 150 | if ( this->ready ) { 151 | this->logFile = SPIFFS.open("/log.txt", "a"); 152 | } else { 153 | SPIFFS.end(); 154 | } 155 | } 156 | 157 | /** 158 | * Purge the log file 159 | * sleeping loop the SPIFF is supposed to be closed. The function try to manage this and 160 | * restore the initial state. 161 | */ 162 | void LoggerClass::clean() { 163 | if ( this->ready && this->onFile) { 164 | this->logFile.close(); 165 | } 166 | if ( !this->ready ) { 167 | SPIFFS.begin(); 168 | } 169 | SPIFFS.remove("/log.txt"); 170 | if ( this->ready ) { 171 | this->logFile = SPIFFS.open("/log.txt", "a"); 172 | } else { 173 | this->logFile.close(); 174 | SPIFFS.end(); 175 | } 176 | } 177 | 178 | /** 179 | * Log an error according to the configuration on the different 180 | * possible logger 181 | */ 182 | void LoggerClass::error(char *format, ...) { 183 | va_list args; 184 | if ( this->logError && this->ready ) { 185 | va_start(args,format); 186 | vsnprintf(fmtBuffer,LOGGER_MAX_BUF_SZ,format,args); 187 | va_end(args); 188 | 189 | if ( this->logConf & LOGGER_CONFIG_SERIAL_MASK & LOGGER_CONFIG_ERROR_LVL_MASK ) { 190 | Serial.print(fmtBuffer); 191 | } 192 | 193 | if ( this->logConf & LOGGER_CONFIG_SERIAL1_MASK & LOGGER_CONFIG_ERROR_LVL_MASK ) { 194 | Serial1.print(fmtBuffer); 195 | } 196 | 197 | if ( this->logConf & LOGGER_CONFIG_SSERIAL_MASK & LOGGER_CONFIG_ERROR_LVL_MASK ) { 198 | SSerial->print(fmtBuffer); 199 | } 200 | 201 | if ( this->logConf & LOGGER_CONFIG_FILE_MASK & LOGGER_CONFIG_ERROR_LVL_MASK ) { 202 | this->logFile.printf("%lu [error] ",millis()); 203 | this->logFile.print(fmtBuffer); 204 | } 205 | 206 | } 207 | } 208 | 209 | /** 210 | * Log a warning according to the configuration on the different 211 | * possible logger 212 | */ 213 | void LoggerClass::warn(char *format, ...) { 214 | va_list args; 215 | if ( this->logWarn && this->ready ) { 216 | va_start(args,format); 217 | vsnprintf(fmtBuffer,LOGGER_MAX_BUF_SZ,format,args); 218 | va_end(args); 219 | 220 | if ( this->logConf & LOGGER_CONFIG_SERIAL_MASK & LOGGER_CONFIG_WARN_LVL_MASK ) { 221 | Serial.print(fmtBuffer); 222 | } 223 | 224 | if ( this->logConf & LOGGER_CONFIG_SERIAL1_MASK & LOGGER_CONFIG_WARN_LVL_MASK ) { 225 | Serial1.print(fmtBuffer); 226 | } 227 | 228 | if ( this->logConf & LOGGER_CONFIG_SSERIAL_MASK & LOGGER_CONFIG_WARN_LVL_MASK ) { 229 | SSerial->print(fmtBuffer); 230 | } 231 | 232 | if ( this->logConf & LOGGER_CONFIG_FILE_MASK & LOGGER_CONFIG_WARN_LVL_MASK ) { 233 | this->logFile.printf("%lu [warn ] ",millis()); 234 | this->logFile.print(fmtBuffer); 235 | } 236 | 237 | } 238 | } 239 | 240 | 241 | /** 242 | * Log a info according to the configuration on the different 243 | * possible logger 244 | */ 245 | void LoggerClass::info(char *format, ...) { 246 | va_list args; 247 | if ( this->logInfo && this->ready ) { 248 | va_start(args,format); 249 | vsnprintf(fmtBuffer,LOGGER_MAX_BUF_SZ,format,args); 250 | va_end(args); 251 | 252 | if ( this->logConf & LOGGER_CONFIG_SERIAL_MASK & LOGGER_CONFIG_INFO_LVL_MASK ) { 253 | Serial.print(fmtBuffer); 254 | } 255 | 256 | if ( this->logConf & LOGGER_CONFIG_SERIAL1_MASK & LOGGER_CONFIG_INFO_LVL_MASK ) { 257 | Serial1.print(fmtBuffer); 258 | } 259 | 260 | if ( this->logConf & LOGGER_CONFIG_SSERIAL_MASK & LOGGER_CONFIG_INFO_LVL_MASK ) { 261 | SSerial->print(fmtBuffer); 262 | } 263 | 264 | if ( this->logConf & LOGGER_CONFIG_FILE_MASK & LOGGER_CONFIG_INFO_LVL_MASK ) { 265 | this->logFile.printf("%lu [info ] ",millis()); 266 | this->logFile.print(fmtBuffer); 267 | } 268 | 269 | } 270 | } 271 | 272 | /** 273 | * Log a debug according to the configuration on the different 274 | * possible logger 275 | */ 276 | void LoggerClass::debug(char *format, ...) { 277 | va_list args; 278 | if ( this->logDebug && this->ready ) { 279 | va_start(args,format); 280 | vsnprintf(fmtBuffer,LOGGER_MAX_BUF_SZ,format,args); 281 | va_end(args); 282 | 283 | if ( this->logConf & LOGGER_CONFIG_SERIAL_MASK & LOGGER_CONFIG_DEBUG_LVL_MASK ) { 284 | Serial.print(fmtBuffer); 285 | } 286 | 287 | if ( this->logConf & LOGGER_CONFIG_SERIAL1_MASK & LOGGER_CONFIG_DEBUG_LVL_MASK ) { 288 | Serial1.print(fmtBuffer); 289 | } 290 | 291 | if ( this->logConf & LOGGER_CONFIG_SSERIAL_MASK & LOGGER_CONFIG_DEBUG_LVL_MASK ) { 292 | SSerial->print(fmtBuffer); 293 | } 294 | 295 | if ( this->logConf & LOGGER_CONFIG_FILE_MASK & LOGGER_CONFIG_DEBUG_LVL_MASK ) { 296 | this->logFile.printf("%lu [debug] ",millis()); 297 | this->logFile.print(fmtBuffer); 298 | } 299 | 300 | } 301 | } 302 | 303 | /** 304 | * Log a debug according to the configuration on the different 305 | * possible logger 306 | */ 307 | void LoggerClass::any(char *format, ...) { 308 | va_list args; 309 | 310 | va_start(args,format); 311 | vsnprintf(fmtBuffer,LOGGER_MAX_BUF_SZ,format,args); 312 | va_end(args); 313 | 314 | if ( this->logConf & LOGGER_CONFIG_SERIAL_MASK ) { 315 | Serial.print(fmtBuffer); 316 | } 317 | 318 | if ( this->logConf & LOGGER_CONFIG_SERIAL1_MASK ) { 319 | Serial1.print(fmtBuffer); 320 | } 321 | 322 | if ( this->logConf & LOGGER_CONFIG_SSERIAL_MASK ) { 323 | SSerial->print(fmtBuffer); 324 | } 325 | 326 | if ( this->logConf & LOGGER_CONFIG_FILE_MASK ) { 327 | this->logFile.printf("%lu [any ] ",millis()); 328 | this->logFile.print(fmtBuffer); 329 | } 330 | } 331 | 332 | -------------------------------------------------------------------------------- /logger.h: -------------------------------------------------------------------------------- 1 | /* ====================================================================== 2 | This file is part of disk91_logger. 3 | 4 | disk91_logger is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Foobar is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with Foobar. If not, see . 16 | ======================================================================= 17 | */ 18 | 19 | /* ====================================================================== 20 | * LOGGER / ESP8266 / logging module 21 | * ---------------------------------------------------------------------- 22 | * (c) Disk91 - 2018 23 | * Author : Paul Pinault aka disk91.com 24 | * ---------------------------------------------------------------------- 25 | */ 26 | 27 | #ifndef LOGGER_H_ 28 | #define LOGGER_H_ 29 | 30 | #include 31 | #include 32 | #include 33 | 34 | #define LOGGER_SERIAL_DEFAULT_SPEED 74880 35 | #define LOGGER_SERIAL1_DEFAULT_SPEED 115200 36 | #define LOGGER_MAX_BUF_SZ 256 37 | 38 | #define LOGGER_CONFIG_FILE_MASK 0xF000 39 | #define LOGGER_CONFIG_SSERIAL_MASK 0x0F00 40 | #define LOGGER_CONFIG_SERIAL1_MASK 0x00F0 41 | #define LOGGER_CONFIG_SERIAL_MASK 0x000F 42 | #define LOGGER_CONFIG_DEBUG_LVL_MASK 0x8888 43 | #define LOGGER_CONFIG_INFO_LVL_MASK 0x4444 44 | #define LOGGER_CONFIG_WARN_LVL_MASK 0x2222 45 | #define LOGGER_CONFIG_ERROR_LVL_MASK 0x1111 46 | 47 | #define LOGGER_FILE_MAX_SIZE 200000 // 200k - Max log file size - after this size the log file is deleted 48 | 49 | class LoggerClass { 50 | public: 51 | bool init(uint16_t config); 52 | uint16_t close(); 53 | void error(char *format, ...); 54 | void warn(char *format, ...); 55 | void info(char *format, ...); 56 | void debug(char *format, ...); 57 | void any(char *format, ...); 58 | 59 | void cat(); 60 | void clean(); 61 | 62 | 63 | protected: 64 | bool ready; // Initialization has been done 65 | bool logError; // Error log level reported somewhere 66 | bool logWarn; // Warn log level reported somewhere 67 | bool logInfo; // Info log level reported somewhere 68 | bool logDebug; // Debug log level reported somewhere 69 | bool onSerial; // Some logs are reported on Serial Line 70 | bool onSerial1; // Some logs are reported on Serial1 Line 71 | bool onSSerial; // Some logs are reported on SoftwareSerial Line 72 | bool onFile; // Some logs are reported on Falsh file 73 | uint16_t logConf; // Detailed log level 74 | 75 | File logFile; 76 | SoftwareSerial * SSerial = NULL; 77 | 78 | char fmtBuffer[LOGGER_MAX_BUF_SZ]; // buffer for log line formating before printing 79 | 80 | }; 81 | 82 | extern LoggerClass _log; 83 | 84 | #endif 85 | -------------------------------------------------------------------------------- /low_power.cpp: -------------------------------------------------------------------------------- 1 | /* ====================================================================== 2 | This file is part of disk91_lowpower. 3 | 4 | disk91_lowpower is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Foobar is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with Foobar. If not, see . 16 | ======================================================================= 17 | */ 18 | 19 | /* ====================================================================== 20 | * ESP8266 Low power management module 21 | * ---------------------------------------------------------------------- 22 | * (c) Disk91 - 2018 23 | * Author : Paul Pinault aka disk91.com 24 | * ---------------------------------------------------------------------- 25 | */ 26 | 27 | extern "C" { 28 | #include 29 | #include "tool.h" 30 | } 31 | #include 32 | #include "config.h" 33 | #include "debug.h" 34 | #include "low_power.h" 35 | 36 | 37 | 38 | 39 | LowPowerClass lowPowerService; 40 | 41 | 42 | /** 43 | * When the device is restarting this function is called at first. 44 | * In case of reset startup the function just return false. 45 | * In case of restart after deep sleep the function restore context and return true 46 | * A byte buffer where to restaure the data is provided as context. 47 | * In this byte buffer, a specific area contains the location for crc32 to ensure a correct restore. 48 | */ 49 | bool LowPowerClass::wakeUp(uint8_t * context, uint32_t * crc32area, unsigned int sz) { 50 | 51 | if ( sz > RTC_MAX_SZ ) { 52 | TTRACE(("** Invalid context size !\r\n")); 53 | while(true); 54 | } 55 | 56 | rst_info * rstInfo = ESP.getResetInfoPtr(); 57 | if ( rstInfo->reason == REASON_DEEP_SLEEP_AWAKE ) { 58 | TTRACE1(("Wake Up from deep-sleep\r\n")); 59 | // Restoring context from the RTC Memory 60 | if ( ESP.rtcUserMemoryRead(0, (uint32_t*) context, sz) ) { 61 | uint32_t crc = *crc32area; 62 | *crc32area = 0; 63 | if ( crc != calculateCRC32((uint8_t*) context, sz) ) { 64 | TTRACE(("CRC32 Error during RTC Context restoration\r\n")); 65 | ESP.reset(); 66 | } 67 | } else { 68 | TTRACE(("Error during RTC Context restoration\r\n")); 69 | ESP.reset(); 70 | } 71 | return true; 72 | } else { 73 | TTRACE1(("Wake-up : Reset detected \r\n")); 74 | return false; 75 | } 76 | 77 | } 78 | 79 | /** 80 | * The device is entering in deepSleep Mode for the given time in Ms. After this time 81 | * the GPIO16 (D0 on D1-mini) will go low. Connected to RST pin it will restart the device 82 | * During restart the cause will indicate what to do on restart. 83 | * Context of execution is stored in RTC memory and restore. Max size is 512 Bytes 84 | */ 85 | void LowPowerClass::deepSleep(uint32_t durationMs, uint8_t * context, uint32_t * crc32area, unsigned int sz) { 86 | *crc32area = 0; 87 | *crc32area = calculateCRC32((uint8_t*) context, sz); 88 | if ( ! ESP.rtcUserMemoryWrite(0, (uint32_t*) context, sz) ) { 89 | TTRACE(("Error when writting RTC Memory\r\n")); 90 | } 91 | ESP.deepSleep( durationMs * 1000L, WAKE_RF_DISABLED ); 92 | } 93 | 94 | -------------------------------------------------------------------------------- /low_power.h: -------------------------------------------------------------------------------- 1 | /* ====================================================================== 2 | This file is part of disk91_lowpower. 3 | 4 | disk91_lowpower is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Foobar is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with Foobar. If not, see . 16 | ======================================================================= 17 | */ 18 | 19 | /* ====================================================================== 20 | * ESP8266 Low power management module 21 | * ---------------------------------------------------------------------- 22 | * (c) Disk91 - 2018 23 | * Author : Paul Pinault aka disk91.com 24 | * ---------------------------------------------------------------------- 25 | */ 26 | 27 | #ifndef LOWPOWER_H_ 28 | #define LOWPOWER_H_ 29 | 30 | class LowPowerClass { 31 | public: 32 | bool wakeUp(uint8_t * context, uint32_t * crc32area, unsigned int sz); 33 | void deepSleep(uint32_t durationMs, uint8_t * context, uint32_t * crc32area, unsigned int sz); 34 | 35 | }; 36 | 37 | extern LowPowerClass lowPowerService; 38 | #endif 39 | -------------------------------------------------------------------------------- /main.ino: -------------------------------------------------------------------------------- 1 | /* ====================================================================== 2 | This file is part of disk91_tracker. 3 | 4 | disk91_tracker is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Foobar is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with Foobar. If not, see . 16 | ======================================================================= 17 | */ 18 | /* ====================================================================== 19 | * ESP8266 Main module 20 | * ---------------------------------------------------------------------- 21 | * (c) Disk91.com - 2018 22 | * Author : Paul Pinault aka disk91.com 23 | * ---------------------------------------------------------------------- 24 | */ 25 | #include 26 | 27 | #include "config.h" 28 | #include "debug.h" 29 | #include "logger.h" 30 | #include "low_power.h" 31 | #include "tracker.h" 32 | 33 | int bootTime,bootCycle; 34 | bool debugMode = false; 35 | bool debugModeLoop = false; 36 | bool inCommandMode = false; 37 | 38 | void setup() { 39 | bootTime = millis(); 40 | 41 | // Enable WatchDog 42 | ESP.wdtEnable(32000); // 32s watchdog 43 | Serial.begin(LOGGER_SERIAL_DEFAULT_SPEED); // needed for config load & lowpower trace on Serial 44 | 45 | // Disable Wifi 46 | WiFi.disconnect(); 47 | WiFi.mode(WIFI_OFF); 48 | WiFi.forceSleepBegin(); 49 | delay(1); 50 | 51 | // On start, check the STOP PIN, in zero => stop execution 52 | // This is protecting against programmation issue when running deep sleep continuously 53 | pinMode(D5,INPUT); 54 | if ( digitalRead(D5) == LOW ) { 55 | debugMode = true; 56 | } 57 | 58 | TTRACE1(("BootTime %d \r\n", bootTime)); 59 | 60 | 61 | } 62 | 63 | void manageCommand() { 64 | char c; 65 | if ( Serial.available() ) { 66 | char c = Serial.read(); 67 | if ( c == '!' ) { 68 | inCommandMode = true; 69 | } else if ( inCommandMode ) { 70 | if ( c == 'd' ) { TTRACE(("switch to debug Mode\r\n")); debugMode = true; } 71 | trackrService.processCommands(c); 72 | 73 | inCommandMode = false; 74 | } 75 | } 76 | } 77 | 78 | 79 | void loop() { 80 | 81 | // ---- 82 | // For real this loop will be executed only one time after every deep sleep wake up 83 | uint32_t elapsed = ( debugModeLoop )? 0 : millis(); 84 | 85 | if ( debugModeLoop || lowPowerService.wakeUp((uint8_t*)&trackrService.state, &trackrService.state.crc32, sizeof(trackrService.state)) ) { 86 | 87 | // This is a standard loop from a device wake up signal or after an internal wait loop 88 | // We execute all what we have to do on regular basis 89 | 90 | trackrService.execute(SCHEDULER_PERIOD_MS+elapsed); // Load the context from RTC memory & execute actions 91 | 92 | } else { 93 | 94 | // This is the first loop after powering ON the device 95 | // We can call boot method to execute all the first time settings 96 | 97 | trackrService.boot(elapsed+5000); // What have to be done just one time on startup from a reset 98 | // wait for potential debug mode switch for 5 seconds 99 | uint32_t start = millis(); 100 | while ( (millis() - start) < 5000 ) { 101 | manageCommand(); 102 | } 103 | } 104 | 105 | if ( ! debugMode ) { 106 | // In the normal mod the ESP8266 is going deep sleep 107 | // going deep sleep... 108 | lowPowerService.deepSleep( SCHEDULER_PERIOD_MS,(uint8_t*)&trackrService.state, &trackrService.state.crc32, sizeof(trackrService.state) ); 109 | 110 | } else { 111 | // debug mode, no sleep, always run so we can listen for command on the 112 | // serial line 113 | uint32_t start = millis(); 114 | while ( (millis() - start) < SCHEDULER_PERIOD_MS ) { 115 | manageCommand(); 116 | delay(1); 117 | yield(); 118 | ESP.wdtFeed(); 119 | } 120 | } 121 | if ( debugMode ) debugModeLoop = true; 122 | } 123 | -------------------------------------------------------------------------------- /tool.c: -------------------------------------------------------------------------------- 1 | /* ====================================================================== 2 | This file is part of disk91_tools. 3 | 4 | disk91_tools is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Foobar is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with Foobar. If not, see . 16 | ======================================================================= 17 | */ 18 | /* ====================================================================== 19 | * ESP8266 Misc tooling functions 20 | * ---------------------------------------------------------------------- 21 | * (c) Disk91 - 2018 22 | * Author : Paul Pinault aka disk91.com 23 | * ---------------------------------------------------------------------- 24 | */ 25 | 26 | #include 27 | 28 | /** 29 | * Calculate a CRC32 on a memory zone 30 | */ 31 | uint32_t calculateCRC32(const uint8_t *data, size_t length) { 32 | uint32_t crc = 0xffffffff; 33 | while (length--) { 34 | uint8_t c = *data++; 35 | for (uint32_t i = 0x80; i > 0; i >>= 1) { 36 | bool bit = crc & 0x80000000; 37 | if (c & i) { 38 | bit = !bit; 39 | } 40 | crc <<= 1; 41 | if (bit) { 42 | crc ^= 0x04c11db7; 43 | } 44 | } 45 | } 46 | return crc; 47 | } 48 | 49 | // ======================================================================================= 50 | // Converters 51 | // ======================================================================================= 52 | 53 | /* ----------------------------------------------------------- 54 | * Convert a 0-16 value to a upper/lower Char 55 | */ 56 | char dsk_convertHalfInt2HexChar(uint8_t v,bool upper) { 57 | if ( v >= 0 && v <= 9 ) return '0'+v; 58 | if ( v >= 10 && v <= 15 ) return (upper)?'A'+(v-10):'a'+(v+10); 59 | return 0; 60 | } 61 | 62 | /* ----------------------------------------------------------- 63 | * Convert a 0-F char to a 0-16 value 64 | */ 65 | uint8_t dsk_convertHexChar2HalfInt(char c) { 66 | if ( c >= '0' && c <= '9' ) return c-'0'; 67 | if ( c >= 'a' && c <= 'f' ) return 10+c-'a'; 68 | if ( c >= 'A' && c <= 'F' ) return 10+c-'A'; 69 | return 0; 70 | } 71 | 72 | 73 | /* ----------------------------------------------------------- 74 | * Convert a 0-256 value to a 2 byte upper/lower case string 75 | */ 76 | void dsk_convertInt2HexChar(uint8_t v, char * dest, bool upper) { 77 | uint8_t q0 = (v & 0xF0) >> 4; 78 | uint8_t q1 = (v & 0x0F); 79 | dest[0] = dsk_convertHalfInt2HexChar(q0,upper); 80 | dest[1] = dsk_convertHalfInt2HexChar(q1,upper); 81 | } 82 | 83 | /* ----------------------------------------------------------- 84 | * Convert a "0"-"FF" value to 0-255 uint8_t value 85 | */ 86 | uint8_t dsk_convertHexChar2Int(char * v) { 87 | uint8_t q0 = dsk_convertHexChar2HalfInt(v[0]); 88 | uint8_t q1 = dsk_convertHexChar2HalfInt(v[1]); 89 | return (q0*16)+q1; 90 | } 91 | 92 | /* ----------------------------------------------------------- 93 | * Convert a 32bit hex string value into uint32_t value 94 | */ 95 | uint32_t dsk_convertHexChar8Int(char * v) { 96 | uint32_t ret = dsk_convertHexChar2Int(&v[0]); 97 | ret <<= 8; 98 | ret += dsk_convertHexChar2Int(&v[2]); 99 | ret <<= 8; 100 | ret += dsk_convertHexChar2Int(&v[4]); 101 | ret <<= 8; 102 | ret += dsk_convertHexChar2Int(&v[6]); 103 | return ret; 104 | } 105 | 106 | /* ----------------------------------------------------------- 107 | * Convert a 4 char decimal (+ sign) string value into uint16_t value 108 | */ 109 | int16_t dsk_convertDecChar4Int(char * v) { 110 | int sign = 1; 111 | if ( *v == '-' ) { 112 | sign = -1; 113 | v++; 114 | } 115 | uint16_t ret = dsk_convertHexChar2HalfInt(*v);v++; 116 | ret *= 10; 117 | ret += dsk_convertHexChar2HalfInt(*v);v++; 118 | ret *= 10; 119 | ret += dsk_convertHexChar2HalfInt(*v);v++; 120 | ret *= 10; 121 | ret += dsk_convertHexChar2HalfInt(*v);v++; 122 | ret *=sign; 123 | return ret; 124 | } 125 | 126 | /* ----------------------------------------------------------- 127 | * Convert a 8bytes table to Upper Hex upper/lower string 128 | */ 129 | void dsk_convertIntTab2Hex(char * dest, uint8_t * tab, int len, bool upper) { 130 | int i; 131 | for ( i = 0; i < len ; i++ ) { 132 | dsk_convertInt2HexChar(tab[i],&dest[2*i],upper); 133 | } 134 | dest[2*len]='\0'; 135 | } 136 | 137 | /* ---------------------------------------------------------- 138 | * Convert a Char String to a hex value tab entries 139 | */ 140 | void dsk_convertHexStr2IntTab(char * hexstr,uint8_t * tab, int len) { 141 | 142 | int i; 143 | for ( i = 0; i < len ; i++ ) { 144 | tab[i] = dsk_convertHexChar2Int(&hexstr[2*i]); 145 | } 146 | 147 | } 148 | 149 | /* ---------------------------------------------------------- 150 | * Verify a char is an Hex Char 151 | */ 152 | bool dsk_isHexChar(char c, bool upper) { 153 | if ( (c >= '0' && c <= '9' ) 154 | || (c >= 'A' && c <= 'F' ) 155 | || (!upper && c >= 'a' && c <= 'f') 156 | ) { 157 | return true; 158 | } 159 | return false; 160 | } 161 | 162 | /* ---------------------------------------------------------- 163 | * Verify a string is a valid Hex string with given size 164 | */ 165 | bool dsk_isHexString(char * str,int n,bool upper) { 166 | int i = 0; 167 | while ( i < n && str[i] != 0 ) { 168 | if ( (str[i] >= '0' && str[i] <= '9' ) 169 | || (str[i] >= 'A' && str[i] <= 'F' ) 170 | || (!upper && str[i] >= 'a' && str[i] <= 'f') 171 | ) { 172 | i++; 173 | } else { 174 | return false; 175 | } 176 | } 177 | return ( i == n )?true:false; 178 | } 179 | 180 | /* ---------------------------------------------------- 181 | * Convert a 6 x uint8_t mac data into a String 182 | * XX:XX:XX:XX:XX:XX into the str buffer. The str 183 | * buffer size must be 18 bytes long or more 184 | */ 185 | void dsk_macToString(char * str, uint8_t * mac) { 186 | for ( int j = 0 ; j < 6 ; j++ ) { 187 | dsk_convertInt2HexChar(mac[j],&str[3*j],true); 188 | if ( j < 5 ) str[3*j+2]=':'; 189 | } 190 | str[17]='\0'; 191 | } 192 | 193 | -------------------------------------------------------------------------------- /tool.h: -------------------------------------------------------------------------------- 1 | /* ====================================================================== 2 | This file is part of disk91_tools. 3 | 4 | disk91_tools is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Foobar is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with Foobar. If not, see . 16 | ======================================================================= 17 | */ 18 | /* ====================================================================== 19 | * ESP8266 Misc tooling functions 20 | * ---------------------------------------------------------------------- 21 | * (c) Disk91 - 2018 22 | * Author : Paul Pinault aka disk91.com 23 | * ---------------------------------------------------------------------- 24 | */ 25 | 26 | #ifndef TOOL_H_ 27 | #define TOOL_H_ 28 | 29 | 30 | uint32_t calculateCRC32(const uint8_t *data, size_t length); 31 | 32 | // ------------------------------------------------------------------------ 33 | // Converters 34 | char dsk_convertHalfInt2HexChar(uint8_t v,bool upper); 35 | void dsk_convertInt2HexChar(uint8_t v, char * dest, bool upper); 36 | void dsk_convertIntTab2Hex(char * dest, uint8_t * tab, int len, bool upper); 37 | bool dsk_isHexChar(char c, bool upper); 38 | bool dsk_isHexString(char * str,int n,bool upper); 39 | uint8_t dsk_convertHexChar2HalfInt(char c); 40 | uint8_t dsk_convertHexChar2Int(char * v); 41 | uint32_t dsk_convertHexChar8Int(char * v); 42 | uint16_t dsk_convertDecChar4Int(char * v); 43 | void dsk_convertHexStr2IntTab(char * hexstr,uint8_t * tab, int len); 44 | void dsk_macToString(char * str, uint8_t * mac); 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /tracker.cpp: -------------------------------------------------------------------------------- 1 | /* ====================================================================== 2 | This file is part of disk91_tracker. 3 | 4 | disk91_tracker is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Foobar is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with Foobar. If not, see . 16 | ======================================================================= 17 | */ 18 | 19 | /* ====================================================================== 20 | * Tracker main module 21 | * ---------------------------------------------------------------------- 22 | * (c) Disk91.com - 2018 23 | * Author : Paul Pinault aka disk91.com 24 | * ---------------------------------------------------------------------- 25 | */ 26 | #include "tracker.h" 27 | #include "config.h" 28 | #include "debug.h" 29 | #include "wisol.h" 30 | #include "wifiscan.h" 31 | #include "logger.h" 32 | #include "wisol.h" 33 | extern "C" { 34 | #include "tool.h" 35 | } 36 | 37 | 38 | TrackrClass trackrService; 39 | 40 | 41 | 42 | /** 43 | * Function call on every boot to diplay informations, run init ... 44 | * elapsedTime = time elapsed in Ms since power on. 45 | */ 46 | void TrackrClass::boot(uint32_t elapsedTime) { 47 | char buf[128]; 48 | uint32_t start = millis(); 49 | 50 | // Wisol Hardware reset 51 | wisolService.wakeUp(); 52 | wisolService.reset(); 53 | 54 | // Init software components 55 | configService.init(true); // Load the configuration from flash or create it - @TODO : remove forcReset with false 56 | _log.init(configService.config.logConfig); // init logging engine 57 | this->init(); 58 | 59 | // Boot messages 60 | _log.any("*** boot - TrackR - version %02X \r\n",FIRMWARE_VERSION); 61 | _log.debug("Sdk version: %s\r\n", ESP.getSdkVersion()); 62 | _log.debug("Core Version: %s\r\n", ESP.getCoreVersion().c_str()); 63 | _log.debug("Boot Version: %u\r\n", ESP.getBootVersion()); 64 | _log.debug("Boot Mode: %u\r\n", ESP.getBootMode()); 65 | _log.debug("CPU Frequency: %u MHz\r\n", ESP.getCpuFreqMHz()); 66 | _log.debug("Flash Size: %u \r\n", ESP.getFlashChipSize()); 67 | 68 | 69 | 70 | // Terminate boot 71 | wisolService.sleepMode(); 72 | delay(2000); 73 | _log.close(); 74 | state.totalMs = elapsedTime + (millis() - start); 75 | } 76 | 77 | /** 78 | * This function is called on every wake-up and manage the other one 79 | * The context has already been restored in Main 80 | * Elapsed Time = time in Ms elapsed since Last call 81 | */ 82 | void TrackrClass::execute(uint32_t elapsedTime) { 83 | uint32_t start = millis(); 84 | uint8_t mac1[6]; 85 | uint8_t mac2[6]; 86 | 87 | // Reinit hardware - reload config 88 | configService.init(false); // load the configuratin from flash 89 | _log.init(configService.config.logConfig); // init logging engine 90 | 91 | // What we want to do on every wakeup 92 | this->printTime(); 93 | 94 | // Scan for Wifi 95 | uint8_t msg[12]; 96 | wifiscanService.startScan(6000,4,true); 97 | if ( wifiscanService.getFirstAndSecondBestWiFi(mac1, mac2) == 2 ) { 98 | char macStr[20]; 99 | dsk_macToString(macStr,mac1); 100 | _log.info("1. %s\r\n",macStr); 101 | dsk_macToString(macStr,mac2); 102 | _log.info("2. %s\r\n",macStr); 103 | 104 | // Prepare the frame ! 105 | for ( int i = 0 ; i < 6 ; i++ ) { 106 | msg[i]=mac1[i]; 107 | msg[i+6]=mac2[i]; 108 | } 109 | } else { 110 | // send a frame with MAC 00:00:00:00:00:00 to get a network position 111 | for ( int i = 0 ; i < 12 ; i++ ) { 112 | msg[i]=0; 113 | } 114 | } 115 | wisolService.wakeUp(); 116 | wisolService.sendRaw(msg,12,false,NULL); 117 | wisolService.sleepMode(); 118 | 119 | // Prepare to sleep 120 | _log.close(); 121 | state.totalMs += elapsedTime + (millis() - start); 122 | } 123 | 124 | 125 | /** 126 | * Init the device state after a cold restart. 127 | */ 128 | bool TrackrClass::init() { 129 | _log.info("State Init\r\n"); 130 | state.totalMs = 0; 131 | 132 | } 133 | 134 | /** 135 | * Update some timer on every call 136 | */ 137 | void TrackrClass::printTime() { 138 | int hour = state.totalMs / (3600*1000); 139 | int min = (state.totalMs - (hour*(3600*1000))) / (60*1000); 140 | int sec = (state.totalMs - (hour*(3600*1000)) - (min*(60*1000))) / 1000; 141 | int ms = (state.totalMs % 1000); 142 | _log.info("Time is : %d:%02d:%02d.%03d\n",hour,min,sec,ms); 143 | } 144 | 145 | 146 | void TrackrClass::processCommands(char c) { 147 | if ( c == '?' ) { _log.any("(c) 2018 Disk91.com\r\n"); } 148 | if ( c == 'l' ) { _log.cat(); } 149 | if ( c == 'C' ) { _log.any("Clean log file\n");_log.clean(); } 150 | if ( c == 'P' ) { char buf[64]; wisolService.wakeUp(); wisolService.getSigfoxPakWithRetry(buf,64,3); _log.any("Sigfox PAK : %s\n",buf); wisolService.sleepMode(); } 151 | if ( c == 'c' ) { configService.printConfig(); } 152 | 153 | } 154 | 155 | -------------------------------------------------------------------------------- /tracker.h: -------------------------------------------------------------------------------- 1 | /* ====================================================================== 2 | This file is part of disk91_tracker. 3 | 4 | disk91_tracker is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Foobar is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with Foobar. If not, see . 16 | ======================================================================= 17 | */ 18 | /* ====================================================================== 19 | * Tracker main module 20 | * ---------------------------------------------------------------------- 21 | * (c) Disk91.com - 2018 22 | * Author : Paul Pinault aka disk91.com 23 | * ---------------------------------------------------------------------- 24 | */ 25 | #ifndef TRACKR_H_ 26 | #define TRACKR_H_ 27 | 28 | #include 29 | #include "config.h" 30 | 31 | typedef struct s_state { 32 | uint64_t totalMs; 33 | uint8_t tab[128]; 34 | 35 | uint32_t crc32; // zone to store RTC crc32 36 | } t_state; 37 | 38 | 39 | class TrackrClass { 40 | public: 41 | t_state state; 42 | 43 | bool init(); 44 | void boot(uint32_t elapsedTime); 45 | void execute(uint32_t elapsedTime); 46 | 47 | void processCommands(char c); 48 | 49 | protected: 50 | void printTime(); 51 | 52 | }; 53 | 54 | extern TrackrClass trackrService; 55 | 56 | 57 | 58 | 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /wifiscan.cpp: -------------------------------------------------------------------------------- 1 | /* ====================================================================== 2 | This file is part of disk91_wifi. 3 | 4 | disk91_wifi is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Foobar is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with Foobar. If not, see . 16 | ======================================================================= 17 | */ 18 | /* ====================================================================== 19 | * ESP8266 / WiFi Scanner / 20 | * ---------------------------------------------------------------------- 21 | * (c) Disk91 - 2018 22 | * Author : Paul Pinault aka disk91.com 23 | * ---------------------------------------------------------------------- 24 | */ 25 | #include "wifiscan.h" 26 | #include "ESP8266WiFi.h" 27 | 28 | WifiScanClass wifiscanService; 29 | 30 | #define WIFISCAN_SSIDFILTERLEN 5 31 | static const char ssidFiltered[WIFISCAN_SSIDFILTERLEN][16]= { 32 | "android", 33 | "phone", 34 | "samsung", 35 | "huawei", 36 | "tp-dis" 37 | }; 38 | 39 | /** 40 | * Start a WiFi scan sequence for the given timeoutMs duration (step of 2180 Ms) 41 | * The scan also stop if the maxAp number of Access Point have been discovered 42 | * With filtered option the MAC list will be filtered using some basic rules to 43 | * ensure a better sucess for geolocation 44 | * - Only unicast 45 | * - Public SSID not containg keyworks like ( android, phone, samsung, huawei ) for ermoving mobile hotspot 46 | * - Full 00 47 | * - Locally administred ( byte 0, bit 1) = 1 48 | */ 49 | void WifiScanClass::startScan(uint32_t timeoutMs, uint8_t maxAp, boolean filtered) { 50 | 51 | // Init WiFi from sleep mode 52 | WiFi.forceSleepWake(); 53 | WiFi.mode(WIFI_STA); 54 | 55 | bool scanHidden=(filtered)?false:true; 56 | 57 | WIFISCAN_LOG_DEBUG(("WiFi start scanning\r\n")); 58 | uint32_t start = millis(); 59 | 60 | if ( maxAp > WIFISCAN_MAX_AP ) maxAp = WIFISCAN_MAX_AP; 61 | this->wifiFound = 0; 62 | while ( (millis() - start) < timeoutMs && this->wifiFound < maxAp ) { 63 | int n = WiFi.scanNetworks(false,scanHidden); 64 | for(int i=0; i < n; i++){ 65 | if ( filtered && filtering(i) ) continue; 66 | this->addWiFi(WiFi.BSSID(i),WiFi.RSSI(i),false); 67 | } 68 | } 69 | WIFISCAN_LOG_DEBUG(("WiFi scanning duration %d ms, found %d WiFi\r\n",millis()-start,this->wifiFound)); 70 | 71 | WiFi.mode(WIFI_OFF); 72 | WiFi.forceSleepBegin(); 73 | delay(1); 74 | } 75 | 76 | /** 77 | * Indicate if the given index entry have to be filtered or not (true if it have to be filtered) 78 | * Filter conditions 79 | * - Multicast (byte0, bit 0) = 1 80 | * - Locally administred ( byte 0, bit 1) = 1 81 | * - Public SSID not containg keyworks like ( android, phone, samsung, huawei ) for ermoving mobile hotspot 82 | * - Full of 00 83 | * - Full of FF 84 | */ 85 | bool WifiScanClass::filtering(int index) { 86 | 87 | // Test for Multicast, Locally Administred and Full of FF 88 | uint8_t * mac = WiFi.BSSID(index); 89 | uint8_t firstMacByte = mac[0]; 90 | if ( (firstMacByte & 0x3) != 0 ) return true; 91 | 92 | // Test for full of 0 93 | if ( firstMacByte == 0 ) { 94 | int i=0; 95 | while ( i < 6 ) { 96 | if ( mac[i] != 0 ) break; 97 | i++; 98 | } 99 | if ( i == 6 ) return true; 100 | } 101 | 102 | // Test Hidden 103 | if ( WiFi.isHidden(index) ) return true; 104 | 105 | // Test SSID 106 | String ssid = WiFi.SSID(index); 107 | ssid.toLowerCase(); 108 | for (int i=0 ; i < WIFISCAN_SSIDFILTERLEN ; i++) { 109 | if ( ssid.indexOf(ssidFiltered[i]) > -1 ) return true; 110 | } 111 | 112 | return false; 113 | } 114 | 115 | /** 116 | * Print in the log file the Wifi Found during the last scan 117 | */ 118 | void WifiScanClass::printWiFi() { 119 | WIFISCAN_LOG_ANY(("+-------- WiFi Found -----+\r\n")); 120 | WIFISCAN_LOG_ANY(("| MAC | RSSI |\r\n")); 121 | // |00:00:00:00:00:00| -125 | 122 | for ( int i = 0 ; i < this->wifiFound ; i++ ) { 123 | 124 | WIFISCAN_LOG_ANY(("|")); 125 | for ( int j = 0 ; j < 6 ; j++ ) { 126 | WIFISCAN_LOG_ANY(("%02X",this->wifi[i].mac[j])); 127 | if ( j < 5 ) WIFISCAN_LOG_ANY((":")); 128 | } 129 | WIFISCAN_LOG_ANY(("| ")); 130 | WIFISCAN_LOG_ANY(("%4d |\r\n",this->wifi[i].rssi)); 131 | 132 | } 133 | WIFISCAN_LOG_ANY(("+-------------------------+\r\n")); 134 | } 135 | 136 | 137 | /** 138 | * Search in the WiFi list the two offering the best RSSI and copy the MAC 139 | * address into the given mac1 and mac2 buffer. each have to be a uint8_t[6] 140 | * buffer. Returns the number of Wifi information returned 0 / 1 / 2 141 | */ 142 | int WifiScanClass::getFirstAndSecondBestWiFi(uint8_t * mac1, uint8_t * mac2) { 143 | switch(this->wifiFound){ 144 | case 0: 145 | return 0; 146 | case 1: 147 | for (int k=0; k< 6 ; k++) mac1[k]=this->wifi[0].mac[k]; 148 | return 1; 149 | default: 150 | int best1=0; int8_t rss1=-128; 151 | int best2=0; int8_t rss2=-128; 152 | for (int i=0 ; iwifiFound ; i++) { 153 | if ( this->wifi[i].rssi >= rss1 ) { 154 | best2 = best1; 155 | rss2 = rss1; 156 | best1 = i; 157 | rss1 = this->wifi[i].rssi; 158 | } else if ( this->wifi[i].rssi >= rss2 ) { 159 | best2 = i; 160 | rss2 = this->wifi[i].rssi; 161 | } 162 | for (int k=0; k< 6 ; k++) mac1[k]=this->wifi[best1].mac[k]; 163 | for (int k=0; k< 6 ; k++) mac2[k]=this->wifi[best2].mac[k]; 164 | } 165 | return 2; 166 | } 167 | } 168 | 169 | /** 170 | * Add a Wifi entry in the table list if not already existing 171 | * Update the rssi when better if exists 172 | * Rssi is modified to fit -128 to +127 range 173 | * When unicastOnly is true, only the MAC type unicast are added 174 | * unicast is indicated by higher byte lower bit is 0 175 | */ 176 | void WifiScanClass::addWiFi(uint8_t * _mac, int32_t _rssi, bool unicastOnly) 177 | { 178 | // filter the multicast addresses 179 | if ( unicastOnly && (_mac[0] & 0x01) == 0x01 ) return; 180 | 181 | t_wifiAp * entry = this->searchForWiFi(_mac); 182 | if ( entry == NULL && this->wifiFound < WIFISCAN_MAX_AP ) { 183 | // create a new WiFi entry 184 | for ( int j = 0 ; j < 6 ; j++ ) { 185 | this->wifi[this->wifiFound].mac[j] = _mac[j]; 186 | } 187 | this->wifi[this->wifiFound].rssi = (int8_t)((_rssi < -128)?-128:_rssi); 188 | this->wifiFound++; 189 | 190 | } else { 191 | // update the Rssi if better 192 | if ( _rssi > entry->rssi ) { 193 | entry->rssi = (int8_t)((_rssi < -128)?-128:_rssi); 194 | } 195 | } 196 | 197 | } 198 | 199 | /** 200 | * Serach in the wifi list a corresponding entry with the same 201 | * MAC adress. Return this entry when found, NULL otherwise. 202 | */ 203 | t_wifiAp * WifiScanClass::searchForWiFi(uint8_t * _mac) { 204 | 205 | for ( int i = 0 ; i < this->wifiFound ; i++ ) { 206 | uint8_t c = 0; 207 | while ( c < 6 && this->wifi[i].mac[c] == _mac[c] ) c++; 208 | if ( c == 6 ) return &this->wifi[i]; 209 | } 210 | return NULL; 211 | 212 | } 213 | 214 | 215 | -------------------------------------------------------------------------------- /wifiscan.h: -------------------------------------------------------------------------------- 1 | /* ====================================================================== 2 | This file is part of disk91_wifi. 3 | 4 | disk91_wifi is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Foobar is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with Foobar. If not, see . 16 | ======================================================================= 17 | */ 18 | /* ====================================================================== 19 | * ESP8266 / WiFi Scanner / 20 | * ---------------------------------------------------------------------- 21 | * (c) Disk91 - 2018 22 | * Author : Paul Pinault aka disk91.com 23 | * ---------------------------------------------------------------------- 24 | */ 25 | 26 | #ifndef WIFISCANNER_H_ 27 | #define WIFISCANNER_H_ 28 | 29 | #include 30 | #include "logger.h" 31 | 32 | #define WIFISCAN_LOG_LEVEL 5 // 5 - Debug | 4 - Info | 3 - Warn | 2 - Error | 1 - Any | 0 - None 33 | #define WIFISCAN_MAX_AP 32 34 | 35 | typedef struct s_wifiAp { 36 | uint8_t mac[6]; 37 | int8_t rssi; 38 | } t_wifiAp; 39 | 40 | 41 | class WifiScanClass { 42 | public: 43 | void startScan(uint32_t timeoutMs, uint8_t maxAp, boolean filtered); 44 | void printWiFi(); 45 | int getFirstAndSecondBestWiFi(uint8_t * mac1, uint8_t * mac2); 46 | 47 | protected: 48 | uint8_t wifiFound; 49 | t_wifiAp wifi[WIFISCAN_MAX_AP]; 50 | 51 | void addWiFi(uint8_t * _mac, int32_t _rssi, bool unicastOnly); 52 | bool filtering(int index); 53 | t_wifiAp * searchForWiFi(uint8_t * _mac); 54 | }; 55 | 56 | extern WifiScanClass wifiscanService; 57 | 58 | // Logger wrapper 59 | #if WIFISCAN_LOG_LEVEL >= 5 60 | #define WIFISCAN_LOG_DEBUG(x) _log.debug x 61 | #else 62 | #define WIFISCAN_LOG_DEBUG(x) 63 | #endif 64 | 65 | #if WIFISCAN_LOG_LEVEL >= 4 66 | #define WIFISCAN_LOG_INFO(x) _log.info x 67 | #else 68 | #define WIFISCAN_LOG_INFO(x) 69 | #endif 70 | 71 | #if WIFISCAN_LOG_LEVEL >= 3 72 | #define WIFISCAN_LOG_WARN(x) _log.warn x 73 | #else 74 | #define WIFISCAN_LOG_WARN(x) 75 | #endif 76 | 77 | #if WIFISCAN_LOG_LEVEL >= 2 78 | #define WIFISCAN_LOG_ERROR(x) _log.error x 79 | #else 80 | #define WIFISCAN_LOG_ERROR(x) 81 | #endif 82 | 83 | #if WIFISCAN_LOG_LEVEL >= 1 84 | #define WIFISCAN_LOG_ANY(x) _log.any x 85 | #else 86 | #define WIFISCAN_LOG_ANY(x) 87 | #endif 88 | 89 | #endif 90 | -------------------------------------------------------------------------------- /wisol.cpp: -------------------------------------------------------------------------------- 1 | /* ====================================================================== 2 | This file is part of disk91_sigfox. 3 | 4 | disk91_sigfox is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Foobar is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with Foobar. If not, see . 16 | ======================================================================= 17 | */ 18 | /* ====================================================================== 19 | * WISOL / Sigfox module / management module 20 | * ---------------------------------------------------------------------- 21 | * (c) Disk91 - 2018 22 | * Author : Paul Pinault aka disk91.com 23 | * ---------------------------------------------------------------------- 24 | * Requires : SoftwareSerial - https://github.com/plerup/espsoftwareserial 25 | * /!\ version > 3.4.1 is recommanded 26 | */ 27 | 28 | #include "wisol.h" 29 | extern "C" { 30 | #include "tool.h" 31 | } 32 | #include "config.h" 33 | #include "SoftwareSerial.h" 34 | 35 | WisolClass wisolService; 36 | SoftwareSerial swSer1(WISOL_RX_PIN, WISOL_TX_PIN, false, 64); // RX, TX, Invert, Buffer Size 37 | 38 | /** 39 | * Init the communication with Wisol. 40 | * This must be called before any Wisol access 41 | */ 42 | bool WisolClass::init() { 43 | if ( ! ready ) { 44 | WISOL_LOG_INFO(("Init Wisol\r\n")); 45 | swSer1.begin(9600); 46 | delay(10); 47 | flushRxLine(); 48 | ready = true; 49 | } 50 | return true; 51 | } 52 | 53 | /** 54 | * Reset the wisol chip 55 | */ 56 | bool WisolClass::reset() { 57 | sendLine("AT$P=0\r"); 58 | delay(1000); 59 | flushRxLine(); 60 | return true; 61 | } 62 | 63 | 64 | 65 | /** 66 | * Verify id the Wisol is sleeping 67 | * Should not respond to AT message 68 | * Rq : It is not possible to know if the device is sleeping or 69 | * not as any communication will wake it up ... sound not normal 70 | * as definition of line break is a Low level LARGER than a normal char 71 | * but real life ! 72 | */ 73 | /* 74 | bool WisolClass::isSleeping() { 75 | char buf[100]; 76 | 77 | sendLine("AT\r\n"); 78 | if ( readLine(buf,100,WISOL_WAIT_STD_TIME_MS,false) ) { 79 | //TTRACE(("%s\r\n",buf)); 80 | return false; 81 | } else { 82 | return true; 83 | } 84 | } 85 | */ 86 | 87 | 88 | /** 89 | * Switch the Wisol device to sleep mode 90 | * Normal standby mode is 500uA vs 2uA in sleep mode 91 | * Wake up with a UART Break 92 | */ 93 | bool WisolClass::sleepMode() { 94 | char buf[100]; 95 | 96 | WISOL_LOG_INFO(("Wisol - request sleeping\r\n")); 97 | sendLine("AT$P=1\r"); // /!\ Note = if you add a LF (\n) at end of this command the redive returns OK but don't switch to sleep mode 98 | if ( readLine(buf,100,WISOL_WAIT_STD_TIME_MS,false) ) { 99 | if ( strcmp(buf,"OK") == 0 ) { 100 | WISOL_LOG_WARN(("wisol sleep request applied\r\n")); 101 | return true; 102 | } else { 103 | WISOL_LOG_DEBUG(("wisol sleep request returned : %s\r\n",buf)); 104 | return false; 105 | } 106 | } else { 107 | // If not responding, looks strange 108 | return false; 109 | } 110 | } 111 | 112 | 113 | /** 114 | * Wake up wisol by sending a break on UART-TX 115 | * Break is LOW on tx durring ??? 116 | */ 117 | void WisolClass::wakeUp() { 118 | WISOL_LOG_INFO(("Wisol - waking up\r\n")); 119 | init(); 120 | digitalWrite(WISOL_TX_PIN,LOW); 121 | delay(5); 122 | digitalWrite(WISOL_TX_PIN,HIGH); 123 | delay(20); 124 | flushRxLine(); 125 | } 126 | 127 | 128 | /** 129 | * Send a message to Sigfox of the indicated len. 130 | * When withDownlink is true, a downlink response is expected. The response will be stored in the downlink 8bytes given buffer 131 | * The status returns is the following 132 | * Uplink : 133 | * WISOL_STATUS_SEND_KO => Fame not transmitted 134 | * WISOL_STATUS_SEND_OK => Frame transmitted 135 | * WISOL_STATUS_NO_DONWLINK => Frame transmitted, no downlink response 136 | * WISOL_STATUS_DOWNLINK => Frame trasnmitted, downlink response received 137 | * 138 | * Rq : Downlink feature are not yet implemented 139 | */ 140 | int WisolClass::sendRaw(uint8_t * frame, int len, bool withDownlink, uint8_t * downlink) { 141 | 142 | if ( len > 12 ) return WISOL_STATUS_SEND_KO; 143 | 144 | char msg[25]; 145 | dsk_convertIntTab2Hex(msg,frame,len,true); 146 | if ( !withDownlink ) { 147 | char cmd[128]; 148 | sprintf(cmd,"AT$SF=%s\r",msg); 149 | WISOL_LOG_DEBUG(("Wisol is sending the following command : [%s]\r\n",cmd)); 150 | sendLine(cmd); 151 | if ( readLine(cmd,128,WISOL_WAIT_UPLINK_MS,false) ) { 152 | if ( strcmp(cmd,"OK") == 0 ) { 153 | return WISOL_STATUS_SEND_OK; 154 | } else { 155 | WISOL_LOG_ERROR(("Wisol uplink returned an invalid response (%s)\r\n",cmd)); 156 | return WISOL_STATUS_SEND_KO; 157 | } 158 | } else { 159 | WISOL_LOG_ERROR(("Wisol uplink did not return response\r\n")); 160 | return WISOL_STATUS_SEND_KO; 161 | } 162 | 163 | } else { 164 | WISOL_LOG_ERROR(("Wisol downlink support not yet implemented\r\n")); 165 | } 166 | 167 | } 168 | 169 | /** 170 | * Get the sigfox PAK stored in Wisol module. The string is stored 171 | * in the given buffer with a maxium of sz char (-1) 172 | * Return true when success 173 | */ 174 | bool WisolClass::getSigfoxPak(char * buf, int sz) { 175 | bool ret = false; 176 | sendLine("AT$I=11\r"); 177 | if ( readLine(buf,sz,WISOL_WAIT_STD_TIME_MS,false) ) { 178 | if ( strlen(buf) == 16 && dsk_isHexString(buf,strlen(buf),true) ) { 179 | ret = true; 180 | } else { 181 | WISOL_LOG_WARN(("Invalid response (PAK) from Wisol\r\n")); 182 | } 183 | } else { 184 | WISOL_LOG_WARN(("No response (PAK) from Wisol\r\n")); 185 | ret = false; 186 | } 187 | 188 | return ret; 189 | } 190 | 191 | /** 192 | * Same as getSigfoxPak but make retry in case of communication error 193 | */ 194 | bool WisolClass::getSigfoxPakWithRetry(char * buf, int sz,int retry) { 195 | bool ret; 196 | while (retry>0 && !(ret=getSigfoxPak(buf,sz)) ) { retry-- ; delay(10); } 197 | return ret; 198 | } 199 | 200 | 201 | /** 202 | * Get the sigfoxId from the Wisol device 203 | * Return 0 in case of error 204 | */ 205 | uint32_t WisolClass::getSigfoxId() { 206 | char buf[32]; 207 | uint32_t ret; 208 | 209 | sendLine("AT$I=10\r"); 210 | if ( readLine(buf,32,WISOL_WAIT_STD_TIME_MS,false) ) { 211 | if ( strlen(buf) == 8 && dsk_isHexString(buf,8,false) ){ 212 | ret = dsk_convertHexChar8Int(buf); 213 | } else { 214 | WISOL_LOG_WARN(("Invalid response (ID) from Wisol\r\n")); 215 | ret = 0; 216 | } 217 | } else { 218 | WISOL_LOG_WARN(("No response (ID) from Wisol\r\n")); 219 | ret = 0; 220 | } 221 | 222 | return ret; 223 | } 224 | 225 | /** 226 | * Same as getSigfoxId but make retry in case of communication error 227 | */ 228 | uint32_t WisolClass::getSigfoxIdWithRetry(int retry) { 229 | uint32_t ret; 230 | while (retry>0 && !(ret=getSigfoxId()) ) { retry-- ; delay(10); } 231 | return ret; 232 | } 233 | 234 | 235 | /** 236 | * Return the temperature as measured by the chip 237 | * The result is by 1/10 of Celcius degrees. 238 | * In case of error the result is -300 (INVALID_TEMPERATURE) 239 | */ 240 | int16_t WisolClass::getTemperature() { 241 | char buf[100]; 242 | sendLine("AT$T?\r"); 243 | if ( readLine(buf,100,WISOL_WAIT_STD_TIME_MS,false) ) { 244 | int16_t temp = -300; 245 | if ( strlen(buf) >= 4 ) { 246 | temp = dsk_convertDecChar4Int(buf); 247 | } 248 | WISOL_LOG_DEBUG(("Wisol Temperature : %s -- %d\r\n",buf,temp)); 249 | return temp; 250 | } else { 251 | return WISOL_INVALID_TEMPERATURE; 252 | } 253 | } 254 | 255 | /** 256 | * Same as getTemperature but make retry in case of communication error 257 | */ 258 | uint16_t WisolClass::getTemperatureWithRetry(int retry) { 259 | uint16_t ret; 260 | while (retry>0 && (ret=getTemperature()) == WISOL_INVALID_TEMPERATURE ) { retry-- ; delay(10); } 261 | return ret; 262 | } 263 | 264 | 265 | /** 266 | * Return the Wisol voltage in mV. In case of error 0 is returned 267 | */ 268 | uint16_t WisolClass::getVoltage() { 269 | char buf[100]; 270 | sendLine("AT$V?\r"); 271 | if ( readLine(buf,100,WISOL_WAIT_STD_TIME_MS,false) ) { 272 | uint16_t volt = 0; 273 | if ( strlen(buf) >= 4 ) { 274 | volt = dsk_convertDecChar4Int(buf); 275 | } 276 | WISOL_LOG_DEBUG(("Wisol Volt : %s -- %d\r\n",buf,volt)); 277 | return volt; 278 | } else { 279 | return WISOL_INVALID_VOLTAGE; 280 | } 281 | } 282 | /** 283 | * Same as getTemperature but make retry in case of communication error 284 | */ 285 | uint16_t WisolClass::getVoltageWithRetry(int retry) { 286 | uint16_t ret; 287 | while (retry>0 && (ret=getVoltage()) == WISOL_INVALID_VOLTAGE ) { retry-- ; delay(10); } 288 | return ret; 289 | } 290 | 291 | 292 | // ========================================================================== 293 | // Internal functions 294 | 295 | void WisolClass::flushRxLine() { 296 | while ( swSer1.available() ) swSer1.read(); 297 | } 298 | 299 | void WisolClass::sendLine(const char * str) { 300 | init(); 301 | int len = strlen(str); 302 | if ( len > 1 ) { 303 | for ( int i = 0 ; i < len-1 ; i++ ) { 304 | swSer1.print(str[i]); 305 | swSer1.flush(); 306 | delay(100); 307 | } 308 | swSer1.print(str[len-1]); 309 | } else { 310 | swSer1.print(str); 311 | } 312 | } 313 | 314 | /** 315 | * Read a line from the SerialLine 316 | * Blocking util read terminated or timeout in Ms 317 | * when withEol is true \r and \n are also copied to buffer 318 | * Check the content of the received line, when starting with "ERROR:" it returns false. 319 | * Return true when getting in time a string chain not an error 320 | */ 321 | bool WisolClass::readLine(char * _buf,int sz, int maxMs, bool withEol) { 322 | 323 | char * buf =_buf; 324 | bool end = false; 325 | char c; 326 | while ( true ) { 327 | while ( !swSer1.available() && maxMs > 0 ) { delay(1); maxMs--; } 328 | if ( maxMs == 0 ) return false; 329 | while ( swSer1.available() > 0 && sz > 1) { 330 | c=swSer1.read(); 331 | if ( withEol || (c != '\r' && c != '\n') ) { 332 | *buf=c; 333 | buf++; 334 | sz--; 335 | } 336 | } 337 | if ( c == '\n' ) { 338 | *buf='\0'; 339 | //TTRACE2(("rTime : %d\r\n",maxMs )); 340 | if ( strncmp(_buf,"ERROR:",6) == 0 ) { 341 | WISOL_LOG_DEBUG(("Wisol serial err\r\n")); 342 | return false; 343 | } 344 | return true; 345 | } 346 | if ( sz <= 1 ) return false; 347 | } 348 | } 349 | 350 | -------------------------------------------------------------------------------- /wisol.h: -------------------------------------------------------------------------------- 1 | /* ====================================================================== 2 | This file is part of disk91_sigfox. 3 | 4 | disk91_sigfox is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | Foobar is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with Foobar. If not, see . 16 | ======================================================================= 17 | */ 18 | /* ====================================================================== 19 | * WISOL / Sigfox module / management module 20 | * ---------------------------------------------------------------------- 21 | * (c) Disk91 - 2018 22 | * Author : Paul Pinault aka disk91.com 23 | * ---------------------------------------------------------------------- 24 | * Requires : SoftwareSerial - https://github.com/plerup/espsoftwareserial 25 | * /!\ version > 3.4.1 is recommanded 26 | */ 27 | 28 | 29 | #ifndef WISOL_H_ 30 | #define WISOL_H_ 31 | 32 | #include 33 | #include "logger.h" 34 | 35 | #define WISOL_LOG_LEVEL 5 // 5 - Debug | 4 - Info | 3 - Warn | 2 - Error | 1 - Any | 0 - None 36 | #define WISOL_WAIT_STD_TIME_MS 50 // Wait time in MS for wisol responding on standard short operation like config access 37 | #define WISOL_WAIT_UPLINK_MS 12000 // Wait time in MS for wisol responding on a frame transmition 38 | 39 | 40 | #define WISOL_INVALID_TEMPERATURE -300 41 | #define WISOL_INVALID_VOLTAGE 0 42 | 43 | #define WISOL_STATUS_SEND_KO 0 44 | #define WISOL_STATUS_SEND_OK 1 45 | #define WISOL_STATUS_NO_DONWLINK 2 46 | #define WISOL_STATUS_DOWNLINK 3 47 | 48 | class WisolClass { 49 | public: 50 | bool reset(); 51 | 52 | int sendRaw(uint8_t * frame, int len, bool withDownlink, uint8_t * downlink); 53 | 54 | uint32_t getSigfoxId(); 55 | uint32_t getSigfoxIdWithRetry(int retry); 56 | bool getSigfoxPak(char * buf, int sz); 57 | bool getSigfoxPakWithRetry(char * buf, int sz, int retry); 58 | 59 | int16_t getTemperature(); 60 | uint16_t getTemperatureWithRetry(int retry); 61 | uint16_t getVoltage(); 62 | uint16_t getVoltageWithRetry(int retry); 63 | 64 | // Power Management 65 | void wakeUp(); 66 | bool sleepMode(); 67 | //bool isSleeping(); 68 | 69 | 70 | protected: 71 | bool init(); 72 | bool readLine(char * buf,int sz, int maxMs,bool withEol); 73 | void sendLine(const char * str); 74 | void flushRxLine(); 75 | bool ready = 0; 76 | 77 | }; 78 | 79 | extern WisolClass wisolService; 80 | 81 | // Logger wrapper 82 | #if WISOL_LOG_LEVEL >= 5 83 | #define WISOL_LOG_DEBUG(x) _log.debug x 84 | #else 85 | #define WISOL_LOG_DEBUG(x) 86 | #endif 87 | 88 | #if WISOL_LOG_LEVEL >= 4 89 | #define WISOL_LOG_INFO(x) _log.info x 90 | #else 91 | #define WISOL_LOG_INFO(x) 92 | #endif 93 | 94 | #if WISOL_LOG_LEVEL >= 3 95 | #define WISOL_LOG_WARN(x) _log.warn x 96 | #else 97 | #define WISOL_LOG_WARN(x) 98 | #endif 99 | 100 | #if WISOL_LOG_LEVEL >= 2 101 | #define WISOL_LOG_ERROR(x) _log.error x 102 | #else 103 | #define WISOL_LOG_ERROR(x) 104 | #endif 105 | 106 | #if WISOL_LOG_LEVEL >= 1 107 | #define WISOL_LOG_ANY(x) _log.any x 108 | #else 109 | #define WISOL_LOG_ANY(x) 110 | #endif 111 | 112 | #endif 113 | --------------------------------------------------------------------------------