├── .gitignore ├── HashMap ├── Contributors.txt ├── HashMap.h └── License.txt ├── PusherClient.cpp ├── PusherClient.h ├── README.md ├── WebSocketClient.cpp ├── WebSocketClient.h └── examples └── RobotExample └── RobotExample.ino /.gitignore: -------------------------------------------------------------------------------- 1 | # osx noise 2 | .DS_Store 3 | profile -------------------------------------------------------------------------------- /HashMap/Contributors.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/krohling/ArduinoPusherClient/cd21cd65f2ab6f15aa28753e44d16405abf65bb6/HashMap/Contributors.txt -------------------------------------------------------------------------------- /HashMap/HashMap.h: -------------------------------------------------------------------------------- 1 | /* $Id$ 2 | || 3 | || @author Alexander Brevig 4 | || @url http://wiring.org.co/ 5 | || @url http://alexanderbrevig.com/ 6 | || @contribution Brett Hagman 7 | || 8 | || @description 9 | || | Implementation of a HashMap data structure. 10 | || | 11 | || | Wiring Cross-platform Library 12 | || # 13 | || 14 | || @license Please see cores/Common/License.txt. 15 | || 16 | */ 17 | 18 | #ifndef HASHMAP_H 19 | #define HASHMAP_H 20 | 21 | //for convenience 22 | #define CreateHashMap(hashM, ktype, vtype, capacity) HashMap hashM 23 | #define CreateComplexHashMap(hashM, ktype, vtype, capacity, comparator) HashMap hashM(comparator) 24 | 25 | template 26 | class HashMap 27 | { 28 | public: 29 | typedef bool (*comparator)(K, K); 30 | 31 | /* 32 | || @constructor 33 | || | Initialize this HashMap 34 | || # 35 | || 36 | || @parameter compare optional function for comparing a key against another (for complex types) 37 | */ 38 | HashMap(comparator compare = 0) 39 | { 40 | cb_comparator = compare; 41 | currentIndex = 0; 42 | } 43 | 44 | /* 45 | || @description 46 | || | Get the size of this HashMap 47 | || # 48 | || 49 | || @return The size of this HashMap 50 | */ 51 | unsigned int size() const 52 | { 53 | return currentIndex; 54 | } 55 | 56 | /* 57 | || @description 58 | || | Get a key at a specified index 59 | || # 60 | || 61 | || @parameter idx the index to get the key at 62 | || 63 | || @return The key at index idx 64 | */ 65 | K keyAt(unsigned int idx) 66 | { 67 | return keys[idx]; 68 | } 69 | 70 | /* 71 | || @description 72 | || | Get a value at a specified index 73 | || # 74 | || 75 | || @parameter idx the index to get the value at 76 | || 77 | || @return The value at index idx 78 | */ 79 | V valueAt(unsigned int idx) 80 | { 81 | return values[idx]; 82 | } 83 | 84 | /* 85 | || @description 86 | || | Check if a new assignment will overflow this HashMap 87 | || # 88 | || 89 | || @return true if next assignment will overflow this HashMap 90 | */ 91 | bool willOverflow() 92 | { 93 | return (currentIndex + 1 > capacity); 94 | } 95 | 96 | /* 97 | || @description 98 | || | An indexer for accessing and assigning a value to a key 99 | || | If a key is used that exists, it returns the value for that key 100 | || | If there exists no value for that key, the key is added 101 | || # 102 | || 103 | || @parameter key the key to get the value for 104 | || 105 | || @return The const value for key 106 | */ 107 | const V& operator[](const K key) const 108 | { 109 | return operator[](key); 110 | } 111 | 112 | /* 113 | || @description 114 | || | An indexer for accessing and assigning a value to a key 115 | || | If a key is used that exists, it returns the value for that key 116 | || | If there exists no value for that key, the key is added 117 | || # 118 | || 119 | || @parameter key the key to get the value for 120 | || 121 | || @return The value for key 122 | */ 123 | V& operator[](const K key) 124 | { 125 | if (contains(key)) 126 | { 127 | return values[indexOf(key)]; 128 | } 129 | else if (currentIndex < capacity) 130 | { 131 | keys[currentIndex] = key; 132 | values[currentIndex] = nil; 133 | currentIndex++; 134 | return values[currentIndex - 1]; 135 | } 136 | return nil; 137 | } 138 | 139 | /* 140 | || @description 141 | || | Get the index of a key 142 | || # 143 | || 144 | || @parameter key the key to get the index for 145 | || 146 | || @return The index of the key, or -1 if key does not exist 147 | */ 148 | unsigned int indexOf(K key) 149 | { 150 | for (int i = 0; i < currentIndex; i++) 151 | { 152 | if (cb_comparator) 153 | { 154 | if (cb_comparator(key, keys[i])) 155 | { 156 | return i; 157 | } 158 | } 159 | else 160 | { 161 | if (key == keys[i]) 162 | { 163 | return i; 164 | } 165 | } 166 | } 167 | return -1; 168 | } 169 | 170 | /* 171 | || @description 172 | || | Check if a key is contained within this HashMap 173 | || # 174 | || 175 | || @parameter key the key to check if is contained within this HashMap 176 | || 177 | || @return true if it is contained in this HashMap 178 | */ 179 | bool contains(K key) 180 | { 181 | for (int i = 0; i < currentIndex; i++) 182 | { 183 | if (cb_comparator) 184 | { 185 | if (cb_comparator(key, keys[i])) 186 | { 187 | return true; 188 | } 189 | } 190 | else 191 | { 192 | if (key == keys[i]) 193 | { 194 | return true; 195 | } 196 | } 197 | } 198 | return false; 199 | } 200 | 201 | /* 202 | || @description 203 | || | Check if a key is contained within this HashMap 204 | || # 205 | || 206 | || @parameter key the key to remove from this HashMap 207 | */ 208 | void remove(K key) 209 | { 210 | int index = indexOf(key); 211 | if (contains(key)) 212 | { 213 | for (int i = index; i < capacity - 1; i++) 214 | { 215 | keys[i] = keys[i + 1]; 216 | values[i] = values[i + 1]; 217 | } 218 | currentIndex--; 219 | } 220 | } 221 | 222 | void setNullValue(V nullv) 223 | { 224 | nil = nullv; 225 | } 226 | 227 | protected: 228 | K keys[capacity]; 229 | V values[capacity]; 230 | V nil; 231 | int currentIndex; 232 | comparator cb_comparator; 233 | }; 234 | 235 | #endif 236 | // HASHMAP_H 237 | -------------------------------------------------------------------------------- /HashMap/License.txt: -------------------------------------------------------------------------------- 1 | Wiring Project - Open Source Electronics Prototyping Platform 2 | Created 2003 by Hernando Barragan. 3 | http://wiring.co/ 4 | 5 | All files in the Wiring project are provided under the following 6 | license: 7 | 8 | Wiring Project - Open Source Electronics Prototyping Platform 9 | Copyright (C) 2003-2011 Hernando Barragan 10 | Copyright (C) 2010-2011 Brett Hagman 11 | Copyright (C) 2011 Alexander Brevig 12 | 13 | Wiring Project is free software: you can redistribute it and/or modify 14 | it under the terms of the GNU Lesser General Public License as published by 15 | the Free Software Foundation, either version 3 of the License, or 16 | (at your option) any later version. 17 | 18 | Wiring Project is distributed in the hope that it will be useful, 19 | but WITHOUT ANY WARRANTY; without even the implied warranty of 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 | GNU Lesser General Public License for more details. 22 | 23 | You should have received a copy of the GNU Lesser General Public License 24 | along with this source code. If not, see . 25 | 26 | In addition to the license, we request that the Contributors.txt file accompany 27 | any of the files that are used. 28 | 29 | Contacts: 30 | Hernando Barragan 31 | Brett Hagman 32 | Alexander Brevig 33 | 34 | Rogue Robotics Disclaimer: 35 | Rogue Robotics Corporation, Inc., herby disclaims all copyright interest in 36 | Wiring Project (an Open Source Electronics Prototyping Platform) written by 37 | Hernando Barragan, Brett Hagman and Alexander Brevig. 38 | 39 | Brett Hagman 40 | Rogue Robotics 41 | June 4, 2011 42 | 43 | 44 | Below, you will find the full text for both the GNU Lesser General Public 45 | License (Ver 3.0), and the GNU General Public License (Ver 3.0). 46 | 47 | 48 | --- Full LGPL 3.0 text below --- 49 | 50 | GNU LESSER GENERAL PUBLIC LICENSE 51 | Version 3, 29 June 2007 52 | 53 | Copyright (C) 2007 Free Software Foundation, Inc. 54 | Everyone is permitted to copy and distribute verbatim copies 55 | of this license document, but changing it is not allowed. 56 | 57 | 58 | This version of the GNU Lesser General Public License incorporates 59 | the terms and conditions of version 3 of the GNU General Public 60 | License, supplemented by the additional permissions listed below. 61 | 62 | 0. Additional Definitions. 63 | 64 | As used herein, "this License" refers to version 3 of the GNU Lesser 65 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 66 | General Public License. 67 | 68 | "The Library" refers to a covered work governed by this License, 69 | other than an Application or a Combined Work as defined below. 70 | 71 | An "Application" is any work that makes use of an interface provided 72 | by the Library, but which is not otherwise based on the Library. 73 | Defining a subclass of a class defined by the Library is deemed a mode 74 | of using an interface provided by the Library. 75 | 76 | A "Combined Work" is a work produced by combining or linking an 77 | Application with the Library. The particular version of the Library 78 | with which the Combined Work was made is also called the "Linked 79 | Version". 80 | 81 | The "Minimal Corresponding Source" for a Combined Work means the 82 | Corresponding Source for the Combined Work, excluding any source code 83 | for portions of the Combined Work that, considered in isolation, are 84 | based on the Application, and not on the Linked Version. 85 | 86 | The "Corresponding Application Code" for a Combined Work means the 87 | object code and/or source code for the Application, including any data 88 | and utility programs needed for reproducing the Combined Work from the 89 | Application, but excluding the System Libraries of the Combined Work. 90 | 91 | 1. Exception to Section 3 of the GNU GPL. 92 | 93 | You may convey a covered work under sections 3 and 4 of this License 94 | without being bound by section 3 of the GNU GPL. 95 | 96 | 2. Conveying Modified Versions. 97 | 98 | If you modify a copy of the Library, and, in your modifications, a 99 | facility refers to a function or data to be supplied by an Application 100 | that uses the facility (other than as an argument passed when the 101 | facility is invoked), then you may convey a copy of the modified 102 | version: 103 | 104 | a) under this License, provided that you make a good faith effort to 105 | ensure that, in the event an Application does not supply the 106 | function or data, the facility still operates, and performs 107 | whatever part of its purpose remains meaningful, or 108 | 109 | b) under the GNU GPL, with none of the additional permissions of 110 | this License applicable to that copy. 111 | 112 | 3. Object Code Incorporating Material from Library Header Files. 113 | 114 | The object code form of an Application may incorporate material from 115 | a header file that is part of the Library. You may convey such object 116 | code under terms of your choice, provided that, if the incorporated 117 | material is not limited to numerical parameters, data structure 118 | layouts and accessors, or small macros, inline functions and templates 119 | (ten or fewer lines in length), you do both of the following: 120 | 121 | a) Give prominent notice with each copy of the object code that the 122 | Library is used in it and that the Library and its use are 123 | covered by this License. 124 | 125 | b) Accompany the object code with a copy of the GNU GPL and this license 126 | document. 127 | 128 | 4. Combined Works. 129 | 130 | You may convey a Combined Work under terms of your choice that, 131 | taken together, effectively do not restrict modification of the 132 | portions of the Library contained in the Combined Work and reverse 133 | engineering for debugging such modifications, if you also do each of 134 | the following: 135 | 136 | a) Give prominent notice with each copy of the Combined Work that 137 | the Library is used in it and that the Library and its use are 138 | covered by this License. 139 | 140 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 141 | document. 142 | 143 | c) For a Combined Work that displays copyright notices during 144 | execution, include the copyright notice for the Library among 145 | these notices, as well as a reference directing the user to the 146 | copies of the GNU GPL and this license document. 147 | 148 | d) Do one of the following: 149 | 150 | 0) Convey the Minimal Corresponding Source under the terms of this 151 | License, and the Corresponding Application Code in a form 152 | suitable for, and under terms that permit, the user to 153 | recombine or relink the Application with a modified version of 154 | the Linked Version to produce a modified Combined Work, in the 155 | manner specified by section 6 of the GNU GPL for conveying 156 | Corresponding Source. 157 | 158 | 1) Use a suitable shared library mechanism for linking with the 159 | Library. A suitable mechanism is one that (a) uses at run time 160 | a copy of the Library already present on the user's computer 161 | system, and (b) will operate properly with a modified version 162 | of the Library that is interface-compatible with the Linked 163 | Version. 164 | 165 | e) Provide Installation Information, but only if you would otherwise 166 | be required to provide such information under section 6 of the 167 | GNU GPL, and only to the extent that such information is 168 | necessary to install and execute a modified version of the 169 | Combined Work produced by recombining or relinking the 170 | Application with a modified version of the Linked Version. (If 171 | you use option 4d0, the Installation Information must accompany 172 | the Minimal Corresponding Source and Corresponding Application 173 | Code. If you use option 4d1, you must provide the Installation 174 | Information in the manner specified by section 6 of the GNU GPL 175 | for conveying Corresponding Source.) 176 | 177 | 5. Combined Libraries. 178 | 179 | You may place library facilities that are a work based on the 180 | Library side by side in a single library together with other library 181 | facilities that are not Applications and are not covered by this 182 | License, and convey such a combined library under terms of your 183 | choice, if you do both of the following: 184 | 185 | a) Accompany the combined library with a copy of the same work based 186 | on the Library, uncombined with any other library facilities, 187 | conveyed under the terms of this License. 188 | 189 | b) Give prominent notice with the combined library that part of it 190 | is a work based on the Library, and explaining where to find the 191 | accompanying uncombined form of the same work. 192 | 193 | 6. Revised Versions of the GNU Lesser General Public License. 194 | 195 | The Free Software Foundation may publish revised and/or new versions 196 | of the GNU Lesser General Public License from time to time. Such new 197 | versions will be similar in spirit to the present version, but may 198 | differ in detail to address new problems or concerns. 199 | 200 | Each version is given a distinguishing version number. If the 201 | Library as you received it specifies that a certain numbered version 202 | of the GNU Lesser General Public License "or any later version" 203 | applies to it, you have the option of following the terms and 204 | conditions either of that published version or of any later version 205 | published by the Free Software Foundation. If the Library as you 206 | received it does not specify a version number of the GNU Lesser 207 | General Public License, you may choose any version of the GNU Lesser 208 | General Public License ever published by the Free Software Foundation. 209 | 210 | If the Library as you received it specifies that a proxy can decide 211 | whether future versions of the GNU Lesser General Public License shall 212 | apply, that proxy's public statement of acceptance of any version is 213 | permanent authorization for you to choose that version for the 214 | Library. 215 | 216 | --- End of LGPL 3.0 text --- 217 | 218 | --- Full GPL 3.0 text below --- 219 | 220 | 221 | GNU GENERAL PUBLIC LICENSE 222 | Version 3, 29 June 2007 223 | 224 | Copyright (C) 2007 Free Software Foundation, Inc. 225 | Everyone is permitted to copy and distribute verbatim copies 226 | of this license document, but changing it is not allowed. 227 | 228 | Preamble 229 | 230 | The GNU General Public License is a free, copyleft license for 231 | software and other kinds of works. 232 | 233 | The licenses for most software and other practical works are designed 234 | to take away your freedom to share and change the works. By contrast, 235 | the GNU General Public License is intended to guarantee your freedom to 236 | share and change all versions of a program--to make sure it remains free 237 | software for all its users. We, the Free Software Foundation, use the 238 | GNU General Public License for most of our software; it applies also to 239 | any other work released this way by its authors. You can apply it to 240 | your programs, too. 241 | 242 | When we speak of free software, we are referring to freedom, not 243 | price. Our General Public Licenses are designed to make sure that you 244 | have the freedom to distribute copies of free software (and charge for 245 | them if you wish), that you receive source code or can get it if you 246 | want it, that you can change the software or use pieces of it in new 247 | free programs, and that you know you can do these things. 248 | 249 | To protect your rights, we need to prevent others from denying you 250 | these rights or asking you to surrender the rights. Therefore, you have 251 | certain responsibilities if you distribute copies of the software, or if 252 | you modify it: responsibilities to respect the freedom of others. 253 | 254 | For example, if you distribute copies of such a program, whether 255 | gratis or for a fee, you must pass on to the recipients the same 256 | freedoms that you received. You must make sure that they, too, receive 257 | or can get the source code. And you must show them these terms so they 258 | know their rights. 259 | 260 | Developers that use the GNU GPL protect your rights with two steps: 261 | (1) assert copyright on the software, and (2) offer you this License 262 | giving you legal permission to copy, distribute and/or modify it. 263 | 264 | For the developers' and authors' protection, the GPL clearly explains 265 | that there is no warranty for this free software. For both users' and 266 | authors' sake, the GPL requires that modified versions be marked as 267 | changed, so that their problems will not be attributed erroneously to 268 | authors of previous versions. 269 | 270 | Some devices are designed to deny users access to install or run 271 | modified versions of the software inside them, although the manufacturer 272 | can do so. This is fundamentally incompatible with the aim of 273 | protecting users' freedom to change the software. The systematic 274 | pattern of such abuse occurs in the area of products for individuals to 275 | use, which is precisely where it is most unacceptable. Therefore, we 276 | have designed this version of the GPL to prohibit the practice for those 277 | products. If such problems arise substantially in other domains, we 278 | stand ready to extend this provision to those domains in future versions 279 | of the GPL, as needed to protect the freedom of users. 280 | 281 | Finally, every program is threatened constantly by software patents. 282 | States should not allow patents to restrict development and use of 283 | software on general-purpose computers, but in those that do, we wish to 284 | avoid the special danger that patents applied to a free program could 285 | make it effectively proprietary. To prevent this, the GPL assures that 286 | patents cannot be used to render the program non-free. 287 | 288 | The precise terms and conditions for copying, distribution and 289 | modification follow. 290 | 291 | TERMS AND CONDITIONS 292 | 293 | 0. Definitions. 294 | 295 | "This License" refers to version 3 of the GNU General Public License. 296 | 297 | "Copyright" also means copyright-like laws that apply to other kinds of 298 | works, such as semiconductor masks. 299 | 300 | "The Program" refers to any copyrightable work licensed under this 301 | License. Each licensee is addressed as "you". "Licensees" and 302 | "recipients" may be individuals or organizations. 303 | 304 | To "modify" a work means to copy from or adapt all or part of the work 305 | in a fashion requiring copyright permission, other than the making of an 306 | exact copy. The resulting work is called a "modified version" of the 307 | earlier work or a work "based on" the earlier work. 308 | 309 | A "covered work" means either the unmodified Program or a work based 310 | on the Program. 311 | 312 | To "propagate" a work means to do anything with it that, without 313 | permission, would make you directly or secondarily liable for 314 | infringement under applicable copyright law, except executing it on a 315 | computer or modifying a private copy. Propagation includes copying, 316 | distribution (with or without modification), making available to the 317 | public, and in some countries other activities as well. 318 | 319 | To "convey" a work means any kind of propagation that enables other 320 | parties to make or receive copies. Mere interaction with a user through 321 | a computer network, with no transfer of a copy, is not conveying. 322 | 323 | An interactive user interface displays "Appropriate Legal Notices" 324 | to the extent that it includes a convenient and prominently visible 325 | feature that (1) displays an appropriate copyright notice, and (2) 326 | tells the user that there is no warranty for the work (except to the 327 | extent that warranties are provided), that licensees may convey the 328 | work under this License, and how to view a copy of this License. If 329 | the interface presents a list of user commands or options, such as a 330 | menu, a prominent item in the list meets this criterion. 331 | 332 | 1. Source Code. 333 | 334 | The "source code" for a work means the preferred form of the work 335 | for making modifications to it. "Object code" means any non-source 336 | form of a work. 337 | 338 | A "Standard Interface" means an interface that either is an official 339 | standard defined by a recognized standards body, or, in the case of 340 | interfaces specified for a particular programming language, one that 341 | is widely used among developers working in that language. 342 | 343 | The "System Libraries" of an executable work include anything, other 344 | than the work as a whole, that (a) is included in the normal form of 345 | packaging a Major Component, but which is not part of that Major 346 | Component, and (b) serves only to enable use of the work with that 347 | Major Component, or to implement a Standard Interface for which an 348 | implementation is available to the public in source code form. A 349 | "Major Component", in this context, means a major essential component 350 | (kernel, window system, and so on) of the specific operating system 351 | (if any) on which the executable work runs, or a compiler used to 352 | produce the work, or an object code interpreter used to run it. 353 | 354 | The "Corresponding Source" for a work in object code form means all 355 | the source code needed to generate, install, and (for an executable 356 | work) run the object code and to modify the work, including scripts to 357 | control those activities. However, it does not include the work's 358 | System Libraries, or general-purpose tools or generally available free 359 | programs which are used unmodified in performing those activities but 360 | which are not part of the work. For example, Corresponding Source 361 | includes interface definition files associated with source files for 362 | the work, and the source code for shared libraries and dynamically 363 | linked subprograms that the work is specifically designed to require, 364 | such as by intimate data communication or control flow between those 365 | subprograms and other parts of the work. 366 | 367 | The Corresponding Source need not include anything that users 368 | can regenerate automatically from other parts of the Corresponding 369 | Source. 370 | 371 | The Corresponding Source for a work in source code form is that 372 | same work. 373 | 374 | 2. Basic Permissions. 375 | 376 | All rights granted under this License are granted for the term of 377 | copyright on the Program, and are irrevocable provided the stated 378 | conditions are met. This License explicitly affirms your unlimited 379 | permission to run the unmodified Program. The output from running a 380 | covered work is covered by this License only if the output, given its 381 | content, constitutes a covered work. This License acknowledges your 382 | rights of fair use or other equivalent, as provided by copyright law. 383 | 384 | You may make, run and propagate covered works that you do not 385 | convey, without conditions so long as your license otherwise remains 386 | in force. You may convey covered works to others for the sole purpose 387 | of having them make modifications exclusively for you, or provide you 388 | with facilities for running those works, provided that you comply with 389 | the terms of this License in conveying all material for which you do 390 | not control copyright. Those thus making or running the covered works 391 | for you must do so exclusively on your behalf, under your direction 392 | and control, on terms that prohibit them from making any copies of 393 | your copyrighted material outside their relationship with you. 394 | 395 | Conveying under any other circumstances is permitted solely under 396 | the conditions stated below. Sublicensing is not allowed; section 10 397 | makes it unnecessary. 398 | 399 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 400 | 401 | No covered work shall be deemed part of an effective technological 402 | measure under any applicable law fulfilling obligations under article 403 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 404 | similar laws prohibiting or restricting circumvention of such 405 | measures. 406 | 407 | When you convey a covered work, you waive any legal power to forbid 408 | circumvention of technological measures to the extent such circumvention 409 | is effected by exercising rights under this License with respect to 410 | the covered work, and you disclaim any intention to limit operation or 411 | modification of the work as a means of enforcing, against the work's 412 | users, your or third parties' legal rights to forbid circumvention of 413 | technological measures. 414 | 415 | 4. Conveying Verbatim Copies. 416 | 417 | You may convey verbatim copies of the Program's source code as you 418 | receive it, in any medium, provided that you conspicuously and 419 | appropriately publish on each copy an appropriate copyright notice; 420 | keep intact all notices stating that this License and any 421 | non-permissive terms added in accord with section 7 apply to the code; 422 | keep intact all notices of the absence of any warranty; and give all 423 | recipients a copy of this License along with the Program. 424 | 425 | You may charge any price or no price for each copy that you convey, 426 | and you may offer support or warranty protection for a fee. 427 | 428 | 5. Conveying Modified Source Versions. 429 | 430 | You may convey a work based on the Program, or the modifications to 431 | produce it from the Program, in the form of source code under the 432 | terms of section 4, provided that you also meet all of these conditions: 433 | 434 | a) The work must carry prominent notices stating that you modified 435 | it, and giving a relevant date. 436 | 437 | b) The work must carry prominent notices stating that it is 438 | released under this License and any conditions added under section 439 | 7. This requirement modifies the requirement in section 4 to 440 | "keep intact all notices". 441 | 442 | c) You must license the entire work, as a whole, under this 443 | License to anyone who comes into possession of a copy. This 444 | License will therefore apply, along with any applicable section 7 445 | additional terms, to the whole of the work, and all its parts, 446 | regardless of how they are packaged. This License gives no 447 | permission to license the work in any other way, but it does not 448 | invalidate such permission if you have separately received it. 449 | 450 | d) If the work has interactive user interfaces, each must display 451 | Appropriate Legal Notices; however, if the Program has interactive 452 | interfaces that do not display Appropriate Legal Notices, your 453 | work need not make them do so. 454 | 455 | A compilation of a covered work with other separate and independent 456 | works, which are not by their nature extensions of the covered work, 457 | and which are not combined with it such as to form a larger program, 458 | in or on a volume of a storage or distribution medium, is called an 459 | "aggregate" if the compilation and its resulting copyright are not 460 | used to limit the access or legal rights of the compilation's users 461 | beyond what the individual works permit. Inclusion of a covered work 462 | in an aggregate does not cause this License to apply to the other 463 | parts of the aggregate. 464 | 465 | 6. Conveying Non-Source Forms. 466 | 467 | You may convey a covered work in object code form under the terms 468 | of sections 4 and 5, provided that you also convey the 469 | machine-readable Corresponding Source under the terms of this License, 470 | in one of these ways: 471 | 472 | a) Convey the object code in, or embodied in, a physical product 473 | (including a physical distribution medium), accompanied by the 474 | Corresponding Source fixed on a durable physical medium 475 | customarily used for software interchange. 476 | 477 | b) Convey the object code in, or embodied in, a physical product 478 | (including a physical distribution medium), accompanied by a 479 | written offer, valid for at least three years and valid for as 480 | long as you offer spare parts or customer support for that product 481 | model, to give anyone who possesses the object code either (1) a 482 | copy of the Corresponding Source for all the software in the 483 | product that is covered by this License, on a durable physical 484 | medium customarily used for software interchange, for a price no 485 | more than your reasonable cost of physically performing this 486 | conveying of source, or (2) access to copy the 487 | Corresponding Source from a network server at no charge. 488 | 489 | c) Convey individual copies of the object code with a copy of the 490 | written offer to provide the Corresponding Source. This 491 | alternative is allowed only occasionally and noncommercially, and 492 | only if you received the object code with such an offer, in accord 493 | with subsection 6b. 494 | 495 | d) Convey the object code by offering access from a designated 496 | place (gratis or for a charge), and offer equivalent access to the 497 | Corresponding Source in the same way through the same place at no 498 | further charge. You need not require recipients to copy the 499 | Corresponding Source along with the object code. If the place to 500 | copy the object code is a network server, the Corresponding Source 501 | may be on a different server (operated by you or a third party) 502 | that supports equivalent copying facilities, provided you maintain 503 | clear directions next to the object code saying where to find the 504 | Corresponding Source. Regardless of what server hosts the 505 | Corresponding Source, you remain obligated to ensure that it is 506 | available for as long as needed to satisfy these requirements. 507 | 508 | e) Convey the object code using peer-to-peer transmission, provided 509 | you inform other peers where the object code and Corresponding 510 | Source of the work are being offered to the general public at no 511 | charge under subsection 6d. 512 | 513 | A separable portion of the object code, whose source code is excluded 514 | from the Corresponding Source as a System Library, need not be 515 | included in conveying the object code work. 516 | 517 | A "User Product" is either (1) a "consumer product", which means any 518 | tangible personal property which is normally used for personal, family, 519 | or household purposes, or (2) anything designed or sold for incorporation 520 | into a dwelling. In determining whether a product is a consumer product, 521 | doubtful cases shall be resolved in favor of coverage. For a particular 522 | product received by a particular user, "normally used" refers to a 523 | typical or common use of that class of product, regardless of the status 524 | of the particular user or of the way in which the particular user 525 | actually uses, or expects or is expected to use, the product. A product 526 | is a consumer product regardless of whether the product has substantial 527 | commercial, industrial or non-consumer uses, unless such uses represent 528 | the only significant mode of use of the product. 529 | 530 | "Installation Information" for a User Product means any methods, 531 | procedures, authorization keys, or other information required to install 532 | and execute modified versions of a covered work in that User Product from 533 | a modified version of its Corresponding Source. The information must 534 | suffice to ensure that the continued functioning of the modified object 535 | code is in no case prevented or interfered with solely because 536 | modification has been made. 537 | 538 | If you convey an object code work under this section in, or with, or 539 | specifically for use in, a User Product, and the conveying occurs as 540 | part of a transaction in which the right of possession and use of the 541 | User Product is transferred to the recipient in perpetuity or for a 542 | fixed term (regardless of how the transaction is characterized), the 543 | Corresponding Source conveyed under this section must be accompanied 544 | by the Installation Information. But this requirement does not apply 545 | if neither you nor any third party retains the ability to install 546 | modified object code on the User Product (for example, the work has 547 | been installed in ROM). 548 | 549 | The requirement to provide Installation Information does not include a 550 | requirement to continue to provide support service, warranty, or updates 551 | for a work that has been modified or installed by the recipient, or for 552 | the User Product in which it has been modified or installed. Access to a 553 | network may be denied when the modification itself materially and 554 | adversely affects the operation of the network or violates the rules and 555 | protocols for communication across the network. 556 | 557 | Corresponding Source conveyed, and Installation Information provided, 558 | in accord with this section must be in a format that is publicly 559 | documented (and with an implementation available to the public in 560 | source code form), and must require no special password or key for 561 | unpacking, reading or copying. 562 | 563 | 7. Additional Terms. 564 | 565 | "Additional permissions" are terms that supplement the terms of this 566 | License by making exceptions from one or more of its conditions. 567 | Additional permissions that are applicable to the entire Program shall 568 | be treated as though they were included in this License, to the extent 569 | that they are valid under applicable law. If additional permissions 570 | apply only to part of the Program, that part may be used separately 571 | under those permissions, but the entire Program remains governed by 572 | this License without regard to the additional permissions. 573 | 574 | When you convey a copy of a covered work, you may at your option 575 | remove any additional permissions from that copy, or from any part of 576 | it. (Additional permissions may be written to require their own 577 | removal in certain cases when you modify the work.) You may place 578 | additional permissions on material, added by you to a covered work, 579 | for which you have or can give appropriate copyright permission. 580 | 581 | Notwithstanding any other provision of this License, for material you 582 | add to a covered work, you may (if authorized by the copyright holders of 583 | that material) supplement the terms of this License with terms: 584 | 585 | a) Disclaiming warranty or limiting liability differently from the 586 | terms of sections 15 and 16 of this License; or 587 | 588 | b) Requiring preservation of specified reasonable legal notices or 589 | author attributions in that material or in the Appropriate Legal 590 | Notices displayed by works containing it; or 591 | 592 | c) Prohibiting misrepresentation of the origin of that material, or 593 | requiring that modified versions of such material be marked in 594 | reasonable ways as different from the original version; or 595 | 596 | d) Limiting the use for publicity purposes of names of licensors or 597 | authors of the material; or 598 | 599 | e) Declining to grant rights under trademark law for use of some 600 | trade names, trademarks, or service marks; or 601 | 602 | f) Requiring indemnification of licensors and authors of that 603 | material by anyone who conveys the material (or modified versions of 604 | it) with contractual assumptions of liability to the recipient, for 605 | any liability that these contractual assumptions directly impose on 606 | those licensors and authors. 607 | 608 | All other non-permissive additional terms are considered "further 609 | restrictions" within the meaning of section 10. If the Program as you 610 | received it, or any part of it, contains a notice stating that it is 611 | governed by this License along with a term that is a further 612 | restriction, you may remove that term. If a license document contains 613 | a further restriction but permits relicensing or conveying under this 614 | License, you may add to a covered work material governed by the terms 615 | of that license document, provided that the further restriction does 616 | not survive such relicensing or conveying. 617 | 618 | If you add terms to a covered work in accord with this section, you 619 | must place, in the relevant source files, a statement of the 620 | additional terms that apply to those files, or a notice indicating 621 | where to find the applicable terms. 622 | 623 | Additional terms, permissive or non-permissive, may be stated in the 624 | form of a separately written license, or stated as exceptions; 625 | the above requirements apply either way. 626 | 627 | 8. Termination. 628 | 629 | You may not propagate or modify a covered work except as expressly 630 | provided under this License. Any attempt otherwise to propagate or 631 | modify it is void, and will automatically terminate your rights under 632 | this License (including any patent licenses granted under the third 633 | paragraph of section 11). 634 | 635 | However, if you cease all violation of this License, then your 636 | license from a particular copyright holder is reinstated (a) 637 | provisionally, unless and until the copyright holder explicitly and 638 | finally terminates your license, and (b) permanently, if the copyright 639 | holder fails to notify you of the violation by some reasonable means 640 | prior to 60 days after the cessation. 641 | 642 | Moreover, your license from a particular copyright holder is 643 | reinstated permanently if the copyright holder notifies you of the 644 | violation by some reasonable means, this is the first time you have 645 | received notice of violation of this License (for any work) from that 646 | copyright holder, and you cure the violation prior to 30 days after 647 | your receipt of the notice. 648 | 649 | Termination of your rights under this section does not terminate the 650 | licenses of parties who have received copies or rights from you under 651 | this License. If your rights have been terminated and not permanently 652 | reinstated, you do not qualify to receive new licenses for the same 653 | material under section 10. 654 | 655 | 9. Acceptance Not Required for Having Copies. 656 | 657 | You are not required to accept this License in order to receive or 658 | run a copy of the Program. Ancillary propagation of a covered work 659 | occurring solely as a consequence of using peer-to-peer transmission 660 | to receive a copy likewise does not require acceptance. However, 661 | nothing other than this License grants you permission to propagate or 662 | modify any covered work. These actions infringe copyright if you do 663 | not accept this License. Therefore, by modifying or propagating a 664 | covered work, you indicate your acceptance of this License to do so. 665 | 666 | 10. Automatic Licensing of Downstream Recipients. 667 | 668 | Each time you convey a covered work, the recipient automatically 669 | receives a license from the original licensors, to run, modify and 670 | propagate that work, subject to this License. You are not responsible 671 | for enforcing compliance by third parties with this License. 672 | 673 | An "entity transaction" is a transaction transferring control of an 674 | organization, or substantially all assets of one, or subdividing an 675 | organization, or merging organizations. If propagation of a covered 676 | work results from an entity transaction, each party to that 677 | transaction who receives a copy of the work also receives whatever 678 | licenses to the work the party's predecessor in interest had or could 679 | give under the previous paragraph, plus a right to possession of the 680 | Corresponding Source of the work from the predecessor in interest, if 681 | the predecessor has it or can get it with reasonable efforts. 682 | 683 | You may not impose any further restrictions on the exercise of the 684 | rights granted or affirmed under this License. For example, you may 685 | not impose a license fee, royalty, or other charge for exercise of 686 | rights granted under this License, and you may not initiate litigation 687 | (including a cross-claim or counterclaim in a lawsuit) alleging that 688 | any patent claim is infringed by making, using, selling, offering for 689 | sale, or importing the Program or any portion of it. 690 | 691 | 11. Patents. 692 | 693 | A "contributor" is a copyright holder who authorizes use under this 694 | License of the Program or a work on which the Program is based. The 695 | work thus licensed is called the contributor's "contributor version". 696 | 697 | A contributor's "essential patent claims" are all patent claims 698 | owned or controlled by the contributor, whether already acquired or 699 | hereafter acquired, that would be infringed by some manner, permitted 700 | by this License, of making, using, or selling its contributor version, 701 | but do not include claims that would be infringed only as a 702 | consequence of further modification of the contributor version. For 703 | purposes of this definition, "control" includes the right to grant 704 | patent sublicenses in a manner consistent with the requirements of 705 | this License. 706 | 707 | Each contributor grants you a non-exclusive, worldwide, royalty-free 708 | patent license under the contributor's essential patent claims, to 709 | make, use, sell, offer for sale, import and otherwise run, modify and 710 | propagate the contents of its contributor version. 711 | 712 | In the following three paragraphs, a "patent license" is any express 713 | agreement or commitment, however denominated, not to enforce a patent 714 | (such as an express permission to practice a patent or covenant not to 715 | sue for patent infringement). To "grant" such a patent license to a 716 | party means to make such an agreement or commitment not to enforce a 717 | patent against the party. 718 | 719 | If you convey a covered work, knowingly relying on a patent license, 720 | and the Corresponding Source of the work is not available for anyone 721 | to copy, free of charge and under the terms of this License, through a 722 | publicly available network server or other readily accessible means, 723 | then you must either (1) cause the Corresponding Source to be so 724 | available, or (2) arrange to deprive yourself of the benefit of the 725 | patent license for this particular work, or (3) arrange, in a manner 726 | consistent with the requirements of this License, to extend the patent 727 | license to downstream recipients. "Knowingly relying" means you have 728 | actual knowledge that, but for the patent license, your conveying the 729 | covered work in a country, or your recipient's use of the covered work 730 | in a country, would infringe one or more identifiable patents in that 731 | country that you have reason to believe are valid. 732 | 733 | If, pursuant to or in connection with a single transaction or 734 | arrangement, you convey, or propagate by procuring conveyance of, a 735 | covered work, and grant a patent license to some of the parties 736 | receiving the covered work authorizing them to use, propagate, modify 737 | or convey a specific copy of the covered work, then the patent license 738 | you grant is automatically extended to all recipients of the covered 739 | work and works based on it. 740 | 741 | A patent license is "discriminatory" if it does not include within 742 | the scope of its coverage, prohibits the exercise of, or is 743 | conditioned on the non-exercise of one or more of the rights that are 744 | specifically granted under this License. You may not convey a covered 745 | work if you are a party to an arrangement with a third party that is 746 | in the business of distributing software, under which you make payment 747 | to the third party based on the extent of your activity of conveying 748 | the work, and under which the third party grants, to any of the 749 | parties who would receive the covered work from you, a discriminatory 750 | patent license (a) in connection with copies of the covered work 751 | conveyed by you (or copies made from those copies), or (b) primarily 752 | for and in connection with specific products or compilations that 753 | contain the covered work, unless you entered into that arrangement, 754 | or that patent license was granted, prior to 28 March 2007. 755 | 756 | Nothing in this License shall be construed as excluding or limiting 757 | any implied license or other defenses to infringement that may 758 | otherwise be available to you under applicable patent law. 759 | 760 | 12. No Surrender of Others' Freedom. 761 | 762 | If conditions are imposed on you (whether by court order, agreement or 763 | otherwise) that contradict the conditions of this License, they do not 764 | excuse you from the conditions of this License. If you cannot convey a 765 | covered work so as to satisfy simultaneously your obligations under this 766 | License and any other pertinent obligations, then as a consequence you may 767 | not convey it at all. For example, if you agree to terms that obligate you 768 | to collect a royalty for further conveying from those to whom you convey 769 | the Program, the only way you could satisfy both those terms and this 770 | License would be to refrain entirely from conveying the Program. 771 | 772 | 13. Use with the GNU Affero General Public License. 773 | 774 | Notwithstanding any other provision of this License, you have 775 | permission to link or combine any covered work with a work licensed 776 | under version 3 of the GNU Affero General Public License into a single 777 | combined work, and to convey the resulting work. The terms of this 778 | License will continue to apply to the part which is the covered work, 779 | but the special requirements of the GNU Affero General Public License, 780 | section 13, concerning interaction through a network will apply to the 781 | combination as such. 782 | 783 | 14. Revised Versions of this License. 784 | 785 | The Free Software Foundation may publish revised and/or new versions of 786 | the GNU General Public License from time to time. Such new versions will 787 | be similar in spirit to the present version, but may differ in detail to 788 | address new problems or concerns. 789 | 790 | Each version is given a distinguishing version number. If the 791 | Program specifies that a certain numbered version of the GNU General 792 | Public License "or any later version" applies to it, you have the 793 | option of following the terms and conditions either of that numbered 794 | version or of any later version published by the Free Software 795 | Foundation. If the Program does not specify a version number of the 796 | GNU General Public License, you may choose any version ever published 797 | by the Free Software Foundation. 798 | 799 | If the Program specifies that a proxy can decide which future 800 | versions of the GNU General Public License can be used, that proxy's 801 | public statement of acceptance of a version permanently authorizes you 802 | to choose that version for the Program. 803 | 804 | Later license versions may give you additional or different 805 | permissions. However, no additional obligations are imposed on any 806 | author or copyright holder as a result of your choosing to follow a 807 | later version. 808 | 809 | 15. Disclaimer of Warranty. 810 | 811 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 812 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 813 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 814 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 815 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 816 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 817 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 818 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 819 | 820 | 16. Limitation of Liability. 821 | 822 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 823 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 824 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 825 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 826 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 827 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 828 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 829 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 830 | SUCH DAMAGES. 831 | 832 | 17. Interpretation of Sections 15 and 16. 833 | 834 | If the disclaimer of warranty and limitation of liability provided 835 | above cannot be given local legal effect according to their terms, 836 | reviewing courts shall apply local law that most closely approximates 837 | an absolute waiver of all civil liability in connection with the 838 | Program, unless a warranty or assumption of liability accompanies a 839 | copy of the Program in return for a fee. 840 | 841 | END OF TERMS AND CONDITIONS 842 | 843 | --- End of GPL 3.0 text --- 844 | -------------------------------------------------------------------------------- /PusherClient.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | PusherClient, a Pusher (http://pusherapp.com) client for Arduino 3 | Copyright 2011 Kevin Rohling 4 | http://kevinrohling.com 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | const byte HASH_SIZE = 10; 32 | typedef void (*EventDelegate)(String data); 33 | static EventDelegate _bindAllDelegate; 34 | static HashMap _bindMap = HashMap(); 35 | 36 | prog_char stringVar0[] PROGMEM = "{0}"; 37 | prog_char stringVar1[] PROGMEM = "{1}"; 38 | prog_char stringVar2[] PROGMEM = "{2}"; 39 | prog_char pusherPath[] PROGMEM = "/app/{0}?client=js&version=2.1&protocol=7"; 40 | prog_char pusherHostname[] PROGMEM = "ws.pusherapp.com"; 41 | prog_char subscribeEventName[] PROGMEM = "pusher:subscribe"; 42 | prog_char subscribeMessage1[] PROGMEM = "{\"channel\": \"{0}\" }"; 43 | prog_char subscribeMessage2[] PROGMEM = "{\"channel\": \"{0}\", \"auth\": \"{1}\" }"; 44 | prog_char subscribeMessage3[] PROGMEM = "{\"channel\": \"{0}\", \"auth\": \"{1}\", \"channel_data\": { \"user_id\": {2} } }"; 45 | prog_char unsubscribeMessage[] PROGMEM = "{\"channel\": \"{0}\" }"; 46 | prog_char triggerEventMessage[] PROGMEM = "{\"event\": \"{0}\", \"data\": {1} }"; 47 | prog_char eventNameStart[] PROGMEM = "event"; 48 | prog_char unsubscribeEventName[] PROGMEM = "pusher:unsubscribe"; 49 | 50 | 51 | PROGMEM const char *stringTable[] = 52 | { 53 | stringVar0, 54 | stringVar1, 55 | stringVar2, 56 | pusherPath, 57 | pusherHostname, 58 | subscribeEventName, 59 | subscribeMessage1, 60 | subscribeMessage2, 61 | subscribeMessage3, 62 | unsubscribeMessage, 63 | triggerEventMessage, 64 | eventNameStart, 65 | unsubscribeEventName 66 | }; 67 | 68 | String PusherClient::getStringTableItem(int index) { 69 | char buffer[85]; 70 | strcpy_P(buffer, (char*)pgm_read_word(&(stringTable[index]))); 71 | return String(buffer); 72 | } 73 | 74 | PusherClient::PusherClient() 75 | { 76 | _client.setDataArrivedDelegate(dataArrived); 77 | } 78 | 79 | bool PusherClient::connect(String appId) { 80 | String stringVar0 = getStringTableItem(0); 81 | String path = getStringTableItem(3); 82 | path.replace(stringVar0, appId); 83 | 84 | char pathData[path.length() + 1]; 85 | path.toCharArray(pathData, path.length() + 1); 86 | 87 | return _client.connect("ws.pusherapp.com", pathData, 80); 88 | } 89 | 90 | bool PusherClient::connected() { 91 | return _client.connected(); 92 | } 93 | 94 | void PusherClient::disconnect() { 95 | _client.disconnect(); 96 | } 97 | 98 | void PusherClient::monitor () { 99 | _client.monitor(); 100 | } 101 | 102 | void PusherClient::bindAll(EventDelegate delegate) { 103 | _bindAllDelegate = delegate; 104 | } 105 | 106 | void PusherClient::bind(String eventName, EventDelegate delegate) { 107 | _bindMap[eventName] = delegate; 108 | } 109 | 110 | void PusherClient::subscribe(String channel) { 111 | String subscribeEventName = getStringTableItem(5); 112 | String stringVar0 = getStringTableItem(0); 113 | String message = getStringTableItem(6); 114 | message.replace(stringVar0, channel); 115 | triggerEvent(subscribeEventName, message); 116 | } 117 | 118 | void PusherClient::subscribe(String channel, String auth) { 119 | String subscribeEventName = getStringTableItem(5); 120 | String stringVar0 = getStringTableItem(0); 121 | String stringVar1 = getStringTableItem(1); 122 | String message = getStringTableItem(7); 123 | message.replace(stringVar0, channel); 124 | message.replace(stringVar1, auth); 125 | triggerEvent(subscribeEventName, message); 126 | } 127 | 128 | void PusherClient::subscribe(String channel, String auth, String userId) { 129 | String subscribeEventName = getStringTableItem(5); 130 | String stringVar0 = getStringTableItem(0); 131 | String stringVar1 = getStringTableItem(1); 132 | String stringVar2 = getStringTableItem(2); 133 | String message = getStringTableItem(8); 134 | message.replace(stringVar0, channel); 135 | message.replace(stringVar1, auth); 136 | message.replace(stringVar2, userId); 137 | triggerEvent(subscribeEventName, message); 138 | } 139 | 140 | void PusherClient::unsubscribe(String channel) { 141 | String unsubscribeEventName = getStringTableItem(12); 142 | String stringVar0 = getStringTableItem(0); 143 | String message = getStringTableItem(9); 144 | message.replace(stringVar0, channel); 145 | triggerEvent(unsubscribeEventName, message); 146 | } 147 | 148 | void PusherClient::triggerEvent(String eventName, String eventData) { 149 | String stringVar0 = getStringTableItem(0); 150 | String stringVar1 = getStringTableItem(1); 151 | String message = getStringTableItem(10); 152 | 153 | message.replace(stringVar0, eventName); 154 | message.replace(stringVar1, eventData); 155 | 156 | _client.send(message); 157 | } 158 | 159 | 160 | void PusherClient::dataArrived(WebSocketClient client, String data) { 161 | String eventNameStart = getStringTableItem(11); 162 | String eventName = parseMessageMember(eventNameStart, data); 163 | 164 | if (_bindAllDelegate != NULL) { 165 | _bindAllDelegate(data); 166 | } 167 | 168 | EventDelegate delegate = _bindMap[eventName]; 169 | if (delegate != NULL) { 170 | delegate(data); 171 | } 172 | } 173 | 174 | String PusherClient::parseMessageMember(String memberName, String data) { 175 | memberName = "\"" + memberName + "\""; 176 | int memberDataStart = data.indexOf(memberName) + memberName.length(); 177 | 178 | char currentCharacter; 179 | do { 180 | memberDataStart++; 181 | currentCharacter = data.charAt(memberDataStart); 182 | } while (currentCharacter == ' ' || currentCharacter == ':' || currentCharacter == '\"'); 183 | 184 | int memberDataEnd = memberDataStart; 185 | bool isString = data.charAt(memberDataStart-1) == '\"'; 186 | if (!isString) { 187 | do { 188 | memberDataEnd++; 189 | currentCharacter = data.charAt(memberDataEnd); 190 | } while (currentCharacter != ' ' && currentCharacter != ','); 191 | } 192 | else { 193 | char previousCharacter; 194 | currentCharacter = ' '; 195 | do { 196 | memberDataEnd++; 197 | previousCharacter = currentCharacter; 198 | currentCharacter = data.charAt(memberDataEnd); 199 | } while (currentCharacter != '"' || previousCharacter == '\\'); 200 | } 201 | 202 | String result = data.substring(memberDataStart, memberDataEnd); 203 | result.replace("\\\"", "\""); 204 | return result; 205 | } 206 | -------------------------------------------------------------------------------- /PusherClient.h: -------------------------------------------------------------------------------- 1 | /* 2 | PusherClient, a Pusher (http://pusherapp.com) client for Arduino 3 | Copyright 2011 Kevin Rohling 4 | http://kevinrohling.com 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | 26 | #ifndef PUSHERCLIENT_H 27 | #define PUSHERCLIENT_H_ 28 | 29 | #include 30 | #include 31 | #include 32 | #include "Arduino.h" 33 | #include 34 | 35 | //Uncomment this to use WIFLY Client 36 | //#define WIFLY true 37 | 38 | class PusherClient { 39 | 40 | public: 41 | PusherClient(); 42 | typedef void (*EventDelegate)(String data); 43 | bool connect(String appId); 44 | bool connected(); 45 | void disconnect(); 46 | void monitor(); 47 | void bindAll(EventDelegate delegate); 48 | void bind(String eventName, EventDelegate delegate); 49 | void subscribe(String channel); 50 | void subscribe(String channel, String auth); 51 | void subscribe(String channel, String auth, String userId); 52 | void triggerEvent(String eventName, String eventData); 53 | void unsubscribe(String channel); 54 | private: 55 | String _appId; 56 | WebSocketClient _client; 57 | static String getStringTableItem(int index); 58 | static void dataArrived(WebSocketClient client, String data); 59 | static String parseMessageMember(String memberName, String data); 60 | }; 61 | 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Arduino Pusher Client, a Pusher client developed for use on Arduino devices 2 | Blog: [World Domination Using Arduinos And Websockets](http://kevinrohling.wordpress.com/2011/09/14/world-domination-using-arduinos-and-websockets) 3 | 4 | [Pusher] (http://www.pusherapp.com) is a Push Notification service that uses Websockets for relaying messages back and forth between clients. This allows real time messaging between a diverse range of applications running on Web browsers, mobile devices and now Arduinos. It is my hope that allowing devices to easily send information about themselves as well as respond to messages received from applications and other devices will result in some interesting applications. 5 | 6 | ## Installation instructions 7 | 8 | Once you've cloned this repo locally, copy the ArduinoPusherClient directory into your Arduino Sketchbook directory under Libraries then restart the Arduino IDE so that it notices the new library. Now, under File\Examples you should see ArduinoPusherClient. To use the library in your app, select Sketch\Import Library\ArduinoPusherClient. 9 | 10 | ## Examples 11 | 12 | Included with this library is an example, called RobotExample, that uses Pusher events to drive two Servos. This example connects to a channel named "robot_channel" and binds to 5 events: forward, backward, turn_left, turn_right, and stop. When the events are received the appropriate method gets called and adjusts the angle of the servo motors, driving the robot. 13 | 14 | ## How To Use This Library 15 | 16 | ### Connecting to Pusher 17 | 18 | ``` 19 | PusherClient client; 20 | 21 | if(client.connect("your-api-key-here")) { 22 | //Connected! 23 | } 24 | else { 25 | //Uh oh. 26 | } 27 | 28 | void loop() { 29 | client.monitor(); //Must have a call to monitor() inside loop() 30 | } 31 | ``` 32 | 33 | 34 | ### Channels 35 | 36 | ``` 37 | //Subscribing to a Public Channel 38 | client.subscribe("my-channel"); 39 | 40 | //Subscribing to a Private Channel 41 | client.subscribe("private-my-channel", "my-auth-token"); 42 | 43 | //Subscribing to a Presence Channel 44 | client.subscribe("presence-my-channel", "my-auth-token", "my-user-id"); 45 | 46 | //Unsubscribing to a Channel 47 | client.unsubscribe("my-channel"); 48 | 49 | ``` 50 | 51 | ### Triggering Events 52 | 53 | ``` 54 | client.triggerEvent("my-event", "some data about my-event"); 55 | ``` 56 | 57 | ### Binding to Events 58 | 59 | ``` 60 | client.bind("my-event", handleMyEvent); 61 | 62 | void handleMyEvent(String data) { 63 | //Do stuff here 64 | } 65 | ``` 66 | 67 | 68 | ### Binding to all Events 69 | 70 | ``` 71 | client.bindAll(handleAllEvents); 72 | 73 | void handleAllEvents(String data) { 74 | //Do stuff here 75 | } 76 | ``` 77 | 78 | ## Credits 79 | 80 | Arduino Pusher Client uses the [HashMap](http://www.arduino.cc/playground/Code/HashMap) library developed by Alexander Brevig. -------------------------------------------------------------------------------- /WebSocketClient.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | WebsocketClient, a websocket client for Arduino 3 | Copyright 2011 Kevin Rohling 4 | http://kevinrohling.com 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | prog_char stringVar[] PROGMEM = "{0}"; 31 | prog_char clientHandshakeLine1[] PROGMEM = "GET {0} HTTP/1.1"; 32 | prog_char clientHandshakeLine2[] PROGMEM = "Upgrade: WebSocket"; 33 | prog_char clientHandshakeLine3[] PROGMEM = "Connection: Upgrade"; 34 | prog_char clientHandshakeLine4[] PROGMEM = "Host: {0}"; 35 | prog_char clientHandshakeLine5[] PROGMEM = "Origin: ArduinoWebSocketClient"; 36 | prog_char serverHandshake[] PROGMEM = "HTTP/1.1 101"; 37 | 38 | PROGMEM const char *WebSocketClientStringTable[] = 39 | { 40 | stringVar, 41 | clientHandshakeLine1, 42 | clientHandshakeLine2, 43 | clientHandshakeLine3, 44 | clientHandshakeLine4, 45 | clientHandshakeLine5, 46 | serverHandshake 47 | }; 48 | 49 | String WebSocketClient::getStringTableItem(int index) { 50 | char buffer[35]; 51 | strcpy_P(buffer, (char*)pgm_read_word(&(WebSocketClientStringTable[index]))); 52 | return String(buffer); 53 | } 54 | 55 | bool WebSocketClient::connect(char hostname[], char path[], int port) { 56 | bool result = false; 57 | 58 | if (_client.connect(hostname, port)) { 59 | sendHandshake(hostname, path); 60 | result = readHandshake(); 61 | } 62 | 63 | return result; 64 | } 65 | 66 | 67 | bool WebSocketClient::connected() { 68 | return _client.connected(); 69 | } 70 | 71 | void WebSocketClient::disconnect() { 72 | _client.stop(); 73 | } 74 | 75 | void WebSocketClient::monitor () { 76 | char character; 77 | 78 | if (_client.available() > 0 && (character = _client.read()) == 0) { 79 | String data = ""; 80 | bool endReached = false; 81 | while (!endReached) { 82 | character = _client.read(); 83 | endReached = character == -1; 84 | 85 | if (!endReached) { 86 | data += character; 87 | } 88 | } 89 | 90 | if (_dataArrivedDelegate != NULL) { 91 | _dataArrivedDelegate(*this, data); 92 | } 93 | } 94 | } 95 | 96 | void WebSocketClient::setDataArrivedDelegate(DataArrivedDelegate dataArrivedDelegate) { 97 | _dataArrivedDelegate = dataArrivedDelegate; 98 | } 99 | 100 | 101 | void WebSocketClient::sendHandshake(char hostname[], char path[]) { 102 | String stringVar = getStringTableItem(0); 103 | String line1 = getStringTableItem(1); 104 | String line2 = getStringTableItem(2); 105 | String line3 = getStringTableItem(3); 106 | String line4 = getStringTableItem(4); 107 | String line5 = getStringTableItem(5); 108 | 109 | line1.replace(stringVar, path); 110 | line4.replace(stringVar, hostname); 111 | 112 | _client.println(line1); 113 | _client.println(line2); 114 | _client.println(line3); 115 | _client.println(line4); 116 | _client.println(line5); 117 | _client.println(); 118 | } 119 | 120 | bool WebSocketClient::readHandshake() { 121 | bool result = false; 122 | char character; 123 | String handshake = "", line; 124 | int maxAttempts = 300, attempts = 0; 125 | 126 | while(_client.available() == 0 && attempts < maxAttempts) 127 | { 128 | delay(100); 129 | attempts++; 130 | } 131 | 132 | while((line = readLine()) != "") { 133 | handshake += line + '\n'; 134 | } 135 | 136 | String response = getStringTableItem(6); 137 | result = handshake.indexOf(response) != -1; 138 | 139 | if(!result) { 140 | _client.stop(); 141 | } 142 | 143 | return result; 144 | } 145 | 146 | String WebSocketClient::readLine() { 147 | String line = ""; 148 | char character; 149 | 150 | while(_client.available() > 0 && (character = _client.read()) != '\n') { 151 | if (character != '\r' && character != -1) { 152 | line += character; 153 | } 154 | } 155 | 156 | return line; 157 | } 158 | 159 | void WebSocketClient::send (String data) { 160 | _client.print((char)0); 161 | _client.print(data); 162 | _client.print((char)255); 163 | } 164 | 165 | -------------------------------------------------------------------------------- /WebSocketClient.h: -------------------------------------------------------------------------------- 1 | /* 2 | WebsocketClient, a websocket client for Arduino 3 | Copyright 2011 Kevin Rohling 4 | http://kevinrohling.com 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | #ifndef WEBSOCKETCLIENT_H 26 | #define WEBSOCKETCLIENT_H_ 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include "Arduino.h" 33 | 34 | //Uncomment this to use WIFLY Client 35 | #define WIFLY true 36 | 37 | class WebSocketClient { 38 | public: 39 | typedef void (*DataArrivedDelegate)(WebSocketClient client, String data); 40 | bool connect(char hostname[], char path[] = "/", int port = 80); 41 | bool connected(); 42 | void disconnect(); 43 | void monitor(); 44 | void setDataArrivedDelegate(DataArrivedDelegate dataArrivedDelegate); 45 | void send(String data); 46 | private: 47 | String getStringTableItem(int index); 48 | void sendHandshake(char hostname[], char path[]); 49 | EthernetClient _client; 50 | DataArrivedDelegate _dataArrivedDelegate; 51 | bool readHandshake(); 52 | String readLine(); 53 | }; 54 | 55 | 56 | #endif 57 | -------------------------------------------------------------------------------- /examples/RobotExample/RobotExample.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 7 | PusherClient client; 8 | Servo leftServo; 9 | Servo rightServo; 10 | 11 | void setup() { 12 | pinMode(2,OUTPUT); 13 | leftServo.attach(2); 14 | 15 | pinMode(3, OUTPUT); 16 | rightServo.attach(3); 17 | 18 | leftServo.write(95); 19 | rightServo.write(95); 20 | 21 | Serial.begin(9600); 22 | if (Ethernet.begin(mac) == 0) { 23 | Serial.println("Init Ethernet failed"); 24 | for(;;) 25 | ; 26 | } 27 | 28 | if(client.connect("your-api-key-here")) { 29 | client.bind("forward", moveForward); 30 | client.bind("backward", moveBackward); 31 | client.bind("turn_left", turnLeft); 32 | client.bind("turn_right", turnRight); 33 | client.bind("stop", stopMoving); 34 | client.subscribe("robot_channel"); 35 | } 36 | else { 37 | while(1) {} 38 | } 39 | } 40 | 41 | void loop() { 42 | if (client.connected()) { 43 | client.monitor(); 44 | } 45 | else { 46 | leftServo.write(95); 47 | rightServo.write(95); 48 | } 49 | } 50 | 51 | void moveForward(String data) { 52 | leftServo.write(0); 53 | rightServo.write(180); 54 | } 55 | 56 | void moveBackward(String data) { 57 | leftServo.write(180); 58 | rightServo.write(0); 59 | } 60 | 61 | void turnLeft(String data) { 62 | leftServo.write(0); 63 | rightServo.write(0); 64 | } 65 | 66 | void turnRight(String data) { 67 | leftServo.write(180); 68 | rightServo.write(180); 69 | } 70 | 71 | void stopMoving(String data) { 72 | leftServo.write(95); 73 | rightServo.write(95); 74 | } 75 | --------------------------------------------------------------------------------