helpers.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #ifndef HELPERS_H
  2. #define HELPERS_H
  3. static const uint8_t monthDays[]={31,28,31,30,31,30,31,31,30,31,30,31};
  4. #define LEAP_YEAR(Y) ( ((1970+Y)>0) && !((1970+Y)%4) && ( ((1970+Y)%100) || !((1970+Y)%400) ) )
  5. struct strDateTime
  6. {
  7. byte hour;
  8. byte minute;
  9. byte second;
  10. int year;
  11. byte month;
  12. byte day;
  13. byte wday;
  14. } ;
  15. //
  16. // Summertime calculates the daylight saving for a given date.
  17. //
  18. boolean summertime(int year, byte month, byte day, byte hour, byte tzHours)
  19. // input parameters: "normal time" for year, month, day, hour and tzHours (0=UTC, 1=MEZ)
  20. {
  21. if (month<3 || month>10) return false; // keine Sommerzeit in Jan, Feb, Nov, Dez
  22. if (month>3 && month<10) return true; // Sommerzeit in Apr, Mai, Jun, Jul, Aug, Sep
  23. if (month==3 && (hour + 24 * day)>=(1 + tzHours + 24*(31 - (5 * year /4 + 4) % 7)) || month==10 && (hour + 24 * day)<(1 + tzHours + 24*(31 - (5 * year /4 + 1) % 7)))
  24. return true;
  25. else
  26. return false;
  27. }
  28. //
  29. // Check the Values is between 0-255
  30. //
  31. boolean checkRange(String Value)
  32. {
  33. if (Value.toInt() < 0 || Value.toInt() > 255)
  34. {
  35. return false;
  36. }
  37. else
  38. {
  39. return true;
  40. }
  41. }
  42. void WriteStringToEEPROM(int beginaddress, String string)
  43. {
  44. char charBuf[string.length()+1];
  45. string.toCharArray(charBuf, string.length()+1);
  46. for (int t= 0; t<sizeof(charBuf);t++)
  47. {
  48. EEPROM.write(beginaddress + t,charBuf[t]);
  49. }
  50. }
  51. String ReadStringFromEEPROM(int beginaddress)
  52. {
  53. byte counter=0;
  54. char rChar;
  55. String retString = "";
  56. while (1)
  57. {
  58. rChar = EEPROM.read(beginaddress + counter);
  59. if (rChar == 0) break;
  60. if (counter > 31) break;
  61. counter++;
  62. retString.concat(rChar);
  63. }
  64. return retString;
  65. }
  66. void EEPROMWritelong(int address, long value)
  67. {
  68. byte four = (value & 0xFF);
  69. byte three = ((value >> 8) & 0xFF);
  70. byte two = ((value >> 16) & 0xFF);
  71. byte one = ((value >> 24) & 0xFF);
  72. //Write the 4 bytes into the eeprom memory.
  73. EEPROM.write(address, four);
  74. EEPROM.write(address + 1, three);
  75. EEPROM.write(address + 2, two);
  76. EEPROM.write(address + 3, one);
  77. }
  78. long EEPROMReadlong(long address)
  79. {
  80. //Read the 4 bytes from the eeprom memory.
  81. long four = EEPROM.read(address);
  82. long three = EEPROM.read(address + 1);
  83. long two = EEPROM.read(address + 2);
  84. long one = EEPROM.read(address + 3);
  85. //Return the recomposed long by using bitshift.
  86. return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);
  87. }
  88. void ConvertUnixTimeStamp( unsigned long TimeStamp, struct strDateTime* DateTime)
  89. {
  90. uint8_t year;
  91. uint8_t month, monthLength;
  92. uint32_t time;
  93. unsigned long days;
  94. time = (uint32_t)TimeStamp;
  95. DateTime->second = time % 60;
  96. time /= 60; // now it is minutes
  97. DateTime->minute = time % 60;
  98. time /= 60; // now it is hours
  99. DateTime->hour = time % 24;
  100. time /= 24; // now it is days
  101. DateTime->wday = ((time + 4) % 7) + 1; // Sunday is day 1
  102. year = 0;
  103. days = 0;
  104. while((unsigned)(days += (LEAP_YEAR(year) ? 366 : 365)) <= time) {
  105. year++;
  106. }
  107. DateTime->year = year; // year is offset from 1970
  108. days -= LEAP_YEAR(year) ? 366 : 365;
  109. time -= days; // now it is days in this year, starting at 0
  110. days=0;
  111. month=0;
  112. monthLength=0;
  113. for (month=0; month<12; month++) {
  114. if (month==1) { // february
  115. if (LEAP_YEAR(year)) {
  116. monthLength=29;
  117. } else {
  118. monthLength=28;
  119. }
  120. } else {
  121. monthLength = monthDays[month];
  122. }
  123. if (time >= monthLength) {
  124. time -= monthLength;
  125. } else {
  126. break;
  127. }
  128. }
  129. DateTime->month = month + 1; // jan is month 1
  130. DateTime->day = time + 1; // day of month
  131. DateTime->year += 1970;
  132. }
  133. String GetMacAddress()
  134. {
  135. uint8_t mac[6];
  136. char macStr[18] = {0};
  137. WiFi.macAddress(mac);
  138. sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  139. return String(macStr);
  140. }
  141. // convert a single hex digit character to its integer value (from https://code.google.com/p/avr-netino/)
  142. unsigned char h2int(char c)
  143. {
  144. if (c >= '0' && c <='9'){
  145. return((unsigned char)c - '0');
  146. }
  147. if (c >= 'a' && c <='f'){
  148. return((unsigned char)c - 'a' + 10);
  149. }
  150. if (c >= 'A' && c <='F'){
  151. return((unsigned char)c - 'A' + 10);
  152. }
  153. return(0);
  154. }
  155. String urldecode(String input) // (based on https://code.google.com/p/avr-netino/)
  156. {
  157. char c;
  158. String ret = "";
  159. for(byte t=0;t<input.length();t++)
  160. {
  161. c = input[t];
  162. if (c == '+') c = ' ';
  163. if (c == '%') {
  164. t++;
  165. c = input[t];
  166. t++;
  167. c = (h2int(c) << 4) | h2int(input[t]);
  168. }
  169. ret.concat(c);
  170. }
  171. return ret;
  172. }
  173. #endif