Smart-Home am Beispiel der Präsenzerkennung im Raum Projektarbeit Lennart Heimbs, Johannes Krug, Sebastian Dohle und Kevin Holzschuh bei Prof. Oliver Hofmann SS2019
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TinyGsmCommon.h 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**
  2. * file TinyGsmCommon.h
  3. * author Volodymyr Shymanskyy
  4. * license LGPL-3.0
  5. * copyright Copyright (c) 2016 Volodymyr Shymanskyy
  6. * date Nov 2016
  7. */
  8. #ifndef TinyGsmCommon_h
  9. #define TinyGsmCommon_h
  10. #if defined(SPARK) || defined(PARTICLE)
  11. #include "Particle.h"
  12. #elif defined(ARDUINO)
  13. #if ARDUINO >= 100
  14. #include "Arduino.h"
  15. #else
  16. #include "WProgram.h"
  17. #endif
  18. #endif
  19. #include <Client.h>
  20. #include "TinyGsmFifo.h"
  21. #ifndef TINY_GSM_YIELD
  22. #define TINY_GSM_YIELD() { delay(0); }
  23. #endif
  24. #define TINY_GSM_ATTR_NOT_AVAILABLE __attribute__((error("Not available on this modem type")))
  25. #define TINY_GSM_ATTR_NOT_IMPLEMENTED __attribute__((error("Not implemented")))
  26. #if defined(__AVR__)
  27. #define TINY_GSM_PROGMEM PROGMEM
  28. typedef const __FlashStringHelper* GsmConstStr;
  29. #define GFP(x) (reinterpret_cast<GsmConstStr>(x))
  30. #define GF(x) F(x)
  31. #else
  32. #define TINY_GSM_PROGMEM
  33. typedef const char* GsmConstStr;
  34. #define GFP(x) x
  35. #define GF(x) x
  36. #endif
  37. #ifdef TINY_GSM_DEBUG
  38. namespace
  39. {
  40. template<typename T>
  41. static void DBG(T last)
  42. {
  43. TINY_GSM_DEBUG.println(last);
  44. }
  45. template<typename T, typename... Args>
  46. static void DBG(T head, Args... tail)
  47. {
  48. TINY_GSM_DEBUG.print(head);
  49. TINY_GSM_DEBUG.print(' ');
  50. DBG(tail...);
  51. }
  52. }
  53. #else
  54. #define DBG(...)
  55. #endif
  56. template<class T>
  57. const T& TinyGsmMin(const T& a, const T& b)
  58. {
  59. return (b < a) ? b : a;
  60. }
  61. template<class T>
  62. const T& TinyGsmMax(const T& a, const T& b)
  63. {
  64. return (b < a) ? a : b;
  65. }
  66. template<class T>
  67. uint32_t TinyGsmAutoBaud(T& SerialAT, uint32_t minimum = 9600, uint32_t maximum = 115200)
  68. {
  69. static uint32_t rates[] = { 115200, 57600, 38400, 19200, 9600, 74400, 74880, 230400, 460800, 2400, 4800, 14400, 28800 };
  70. for (unsigned i = 0; i < sizeof(rates)/sizeof(rates[0]); i++) {
  71. uint32_t rate = rates[i];
  72. if (rate < minimum || rate > maximum) {
  73. continue;
  74. }
  75. DBG("Trying baud rate", rate, "...");
  76. SerialAT.begin(rate);
  77. delay(10);
  78. for (int i=0; i<3; i++) {
  79. SerialAT.print("AT\r\n");
  80. String input = SerialAT.readString();
  81. if (input.indexOf("OK") >= 0) {
  82. DBG("Modem responded at rate", rate);
  83. return rate;
  84. }
  85. }
  86. }
  87. return 0;
  88. }
  89. static inline
  90. IPAddress TinyGsmIpFromString(const String& strIP)
  91. {
  92. int Parts[4] = {0, };
  93. int Part = 0;
  94. for (uint8_t i=0; i<strIP.length(); i++) {
  95. char c = strIP[i];
  96. if (c == '.') {
  97. Part++;
  98. if (Part > 3) {
  99. return IPAddress(0,0,0,0);
  100. }
  101. continue;
  102. } else if (c >= '0' && c <= '9') {
  103. Parts[Part] *= 10;
  104. Parts[Part] += c - '0';
  105. } else {
  106. if (Part == 3) {
  107. break;
  108. }
  109. }
  110. }
  111. return IPAddress(Parts[0], Parts[1], Parts[2], Parts[3]);
  112. }
  113. static inline
  114. String TinyGsmDecodeHex7bit(String &instr)
  115. {
  116. String result;
  117. byte reminder = 0;
  118. int bitstate = 7;
  119. for (unsigned i=0; i<instr.length(); i+=2) {
  120. char buf[4] = { 0, };
  121. buf[0] = instr[i];
  122. buf[1] = instr[i+1];
  123. byte b = strtol(buf, NULL, 16);
  124. byte bb = b << (7 - bitstate);
  125. char c = (bb + reminder) & 0x7F;
  126. result += c;
  127. reminder = b >> bitstate;
  128. bitstate--;
  129. if (bitstate == 0) {
  130. char c = reminder;
  131. result += c;
  132. reminder = 0;
  133. bitstate = 7;
  134. }
  135. }
  136. return result;
  137. }
  138. static inline
  139. String TinyGsmDecodeHex8bit(String &instr)
  140. {
  141. String result;
  142. for (unsigned i=0; i<instr.length(); i+=2) {
  143. char buf[4] = { 0, };
  144. buf[0] = instr[i];
  145. buf[1] = instr[i+1];
  146. char b = strtol(buf, NULL, 16);
  147. result += b;
  148. }
  149. return result;
  150. }
  151. static inline
  152. String TinyGsmDecodeHex16bit(String &instr)
  153. {
  154. String result;
  155. for (unsigned i=0; i<instr.length(); i+=4) {
  156. char buf[4] = { 0, };
  157. buf[0] = instr[i];
  158. buf[1] = instr[i+1];
  159. char b = strtol(buf, NULL, 16);
  160. if (b) { // If high byte is non-zero, we can't handle it ;(
  161. #if defined(TINY_GSM_UNICODE_TO_HEX)
  162. result += "\\x";
  163. result += instr.substring(i, i+4);
  164. #else
  165. result += "?";
  166. #endif
  167. } else {
  168. buf[0] = instr[i+2];
  169. buf[1] = instr[i+3];
  170. b = strtol(buf, NULL, 16);
  171. result += b;
  172. }
  173. }
  174. return result;
  175. }
  176. #endif