ESP8266 Treppenlichtsteuerung mit OTA zum Firmware Upload
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.

treppe.cpp 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. #include "treppe.h"
  2. // #define DEBUG_TIMING
  3. /*
  4. - dimmer_tick: increment pwm jeden tick, bis anim beendet
  5. - return: fsm_pend.anim_beendet
  6. */
  7. bool Treppe::dimmer_tick(dimmer_t *dimmer, bool dim_type) {
  8. dimmer->pwm += dimmer->delta_pwm;
  9. #ifdef DIMMDEBUG
  10. Serial.printf("%.0f", dimmer->pwm);
  11. #endif
  12. if (dim_type == DIM_STUFEN) {
  13. pwmController.setChannelPWM(dimmer->stufe,
  14. static_cast<uint16_t>(dimmer->pwm));
  15. } else { // DIM_LDR
  16. pwmController.setAllChannelsPWM(static_cast<uint16_t>(dimmer->pwm));
  17. }
  18. dimmer->tick++;
  19. if (dimmer->tick < dimmer->ticks) {
  20. #ifdef DIMMDEBUG
  21. Serial.print("-");
  22. #endif
  23. return false;
  24. }
  25. #ifdef DIMMDEBUG
  26. Serial.println("");
  27. #endif
  28. if (dim_type == DIM_LDR) {
  29. Serial.printf("DIM_LDR: start: %d, ziel: %d\n", dimmer->start_pwm,
  30. dimmer->ziel_pwm);
  31. return true;
  32. } else { // DIM_STUFEN
  33. Serial.printf("DIM_STUFEN: stufe: %d, start: %d, ziel: %d\n",
  34. dimmer->stufe, dimmer->start_pwm, dimmer->ziel_pwm);
  35. if (fsm_outputs.laufrichtung == LR_HOCH) {
  36. if (dimmer->stufe >= stufen - 1)
  37. return true;
  38. dimmer->stufe++;
  39. } else { // LR_RUNTER
  40. if (dimmer->stufe <= 0)
  41. return true;
  42. dimmer->stufe--;
  43. }
  44. dimmer->tick = 0;
  45. dimmer->pwm = dimmer->start_pwm;
  46. }
  47. return false;
  48. }
  49. // startbedingunen für animation
  50. void Treppe::start_animation(dimmer_t *dimmer, bool dim_type, uint16_t on_pwm,
  51. uint16_t off_pwm) {
  52. fsm_pend.anim_beendet = false;
  53. if (dim_type == DIM_STUFEN) {
  54. if (fsm_outputs.laufrichtung == LR_HOCH)
  55. dimmer->stufe = 0;
  56. else
  57. dimmer->stufe = stufen - 1;
  58. dimmer->ticks = param.time_per_stair / INT_TIME; // [ms]
  59. } else { // DIM_LDR
  60. dimmer->ticks = param.time_ldr / INT_TIME; // [ms]
  61. }
  62. if (fsm_outputs.dimmrichtung == DR_AUFDIMMEN) {
  63. dimmer->start_pwm = off_pwm;
  64. dimmer->ziel_pwm = on_pwm;
  65. dimmer->delta_pwm = (float)(on_pwm - off_pwm) / (float)dimmer->ticks;
  66. } else {
  67. dimmer->start_pwm = on_pwm;
  68. dimmer->ziel_pwm = off_pwm;
  69. dimmer->delta_pwm = (float)(off_pwm - on_pwm) / (float)dimmer->ticks;
  70. }
  71. dimmer->tick = 0;
  72. dimmer->pwm = dimmer->start_pwm;
  73. #ifndef LDR_REGELUNG
  74. idle_pwm_ist = param.idle_pwm_max;
  75. #endif
  76. Serial.printf("stufe %d, ticks %d, delta %f, start %d, ziel %d\n",
  77. dimmer->stufe, dimmer->ticks, dimmer->delta_pwm,
  78. dimmer->start_pwm, dimmer->ziel_pwm);
  79. }
  80. void Treppe::print_state_on_change() {
  81. static FSMTreppeModelClass::ExtU_FSMTreppe_T last_in;
  82. static FSMTreppeModelClass::ExtY_FSMTreppe_T last_out;
  83. if (fsm_inputs.anim_beendet != last_in.anim_beendet ||
  84. fsm_inputs.sensor_oben != last_in.sensor_oben ||
  85. fsm_inputs.sensor_unten != last_in.sensor_unten ||
  86. fsm_inputs.ldr_schwelle != last_in.ldr_schwelle ||
  87. fsm_outputs.dimmrichtung != last_out.dimmrichtung ||
  88. fsm_outputs.laufrichtung != last_out.laufrichtung ||
  89. fsm_outputs.status != last_out.status) {
  90. last_in.anim_beendet = fsm_inputs.anim_beendet;
  91. last_in.sensor_oben = fsm_inputs.sensor_oben;
  92. last_in.sensor_unten = fsm_inputs.sensor_unten;
  93. last_in.ldr_schwelle = fsm_inputs.ldr_schwelle;
  94. last_out.dimmrichtung = fsm_outputs.dimmrichtung;
  95. last_out.laufrichtung = fsm_outputs.laufrichtung;
  96. last_out.status = fsm_outputs.status;
  97. Serial.printf("FSM IN: s_u: %d, s_o: %d, a_b: %d, l_s: %d => ",
  98. fsm_inputs.sensor_oben, fsm_inputs.sensor_unten,
  99. fsm_inputs.anim_beendet, fsm_inputs.ldr_schwelle);
  100. Serial.printf("OUT: LR: %d DR: %d ST: %d\n", fsm_outputs.laufrichtung,
  101. fsm_outputs.dimmrichtung, fsm_outputs.status);
  102. }
  103. }
  104. void Treppe::overwrite_sensors(bool s_oben, bool s_unten) {
  105. fsm_pend.web_ctrl_s_oben = s_oben;
  106. fsm_pend.web_ctrl_s_unten = s_unten;
  107. }
  108. void Treppe::read_sensors() {
  109. const bool s_oben = digitalRead(SENSOR_OBEN);
  110. const bool s_unten = digitalRead(SENSOR_UNTEN);
  111. fsm_pend.sensor_oben = false;
  112. fsm_pend.sensor_unten = false;
  113. // rising trigger => 1 cycle true !
  114. if (s_oben && !fsm_pend.last_s_oben) {
  115. fsm_pend.sensor_oben = true;
  116. }
  117. if (s_unten && !fsm_pend.last_s_unten) {
  118. fsm_pend.sensor_unten = true;
  119. }
  120. fsm_pend.last_s_oben = s_oben;
  121. fsm_pend.last_s_unten = s_unten;
  122. // check for manipulation over webserver
  123. if (fsm_pend.web_ctrl_s_oben) {
  124. fsm_pend.sensor_oben = true;
  125. fsm_pend.web_ctrl_s_oben = false;
  126. }
  127. if (fsm_pend.web_ctrl_s_unten) {
  128. fsm_pend.sensor_unten = true;
  129. fsm_pend.web_ctrl_s_unten = false;
  130. }
  131. }
  132. float Treppe::read_ldr() {
  133. /*
  134. Reads Illuminance in Lux
  135. FUTURE USE : show current Illuminance on Webserver in order to calibrate
  136. Voltage Divider 1 (R13, R14):
  137. R13 = 220k, R14 = 82k
  138. V(ADC) = V(in1) * R14/(R13+R14)
  139. -> V(in1) = V(ADC) * (R13+R14)/R14
  140. V(ADC) = analogRead(A0)/1023.00
  141. -> V(in1) = analogRead(A0)/1023.00 * (R13+R14)/R14
  142. = analogRead(A0) * (R13+R14)/(R14*1023.00)
  143. = analogRead(A0) * (220k+82k)/(82k*1023.00)
  144. = analogRead(A0) * 0.0036
  145. Voltage Divider 2 (LDR, R1 || (R13+R14))
  146. R1 = 47k, R13+R14 = 302k -> R1||(R13+R14) = 40,67k
  147. Vcc/V(in1) = R(LDR) / (R1||(R13+R14))
  148. -> R(LDR) = Vcc/V(in1) * (R1||(R13+R14))
  149. R(LDR) = 3.3V * 40.67k / V(in1)
  150. Join formulas:
  151. R(LDR) = 3.3V * 40.67k / (0.0036 * analogRead(A0))
  152. = 37280.00/analogRead(A0)
  153. ldr_ohm = R(LDR)
  154. E(LDR) = 6526.5 * R(LDR)^-2 (see Excel Regression)
  155. E(LDR) = 6526.5 / (R(LDR)^2)
  156. ldr_value = E(LDR)
  157. */
  158. // float ldr_ohm = 37280.00 / analogRead(A0);
  159. float vol_adc = analogRead(A0) * 0.0036;
  160. if(vol_adc > 3.29) // prevent negative values !
  161. vol_adc = 3.29;
  162. float ldr_ohm = 40.67 * (3.3 - vol_adc) / vol_adc;
  163. float ldr_value = 6526.6 / (ldr_ohm * ldr_ohm);
  164. #ifdef LDRDEBUG
  165. Serial.printf("vol_adc: %f Ohm: %f lux: %f Comp: %d\n", vol_adc, ldr_ohm, ldr_value,
  166. param.ldr_schwelle);
  167. #endif
  168. return ldr_value;
  169. }
  170. void Treppe::sample_ldr() {
  171. ldr_average += read_ldr();
  172. ldr_average_cnt++;
  173. if(ldr_average_cnt > LDR_AVERAGE_SAMPLES) {
  174. float ldr_avg = static_cast<float>(ldr_average / LDR_AVERAGE_SAMPLES);
  175. Serial.printf("ldr_avg: %f schwelle: %d\n", ldr_avg, param.ldr_schwelle);
  176. if(check_ldr(ldr_avg)) {
  177. update_soll_pwm_with_ldr(ldr_avg);
  178. }
  179. ldr_average = 0.0;
  180. ldr_average_cnt = 0;
  181. }
  182. }
  183. void Treppe::update_soll_pwm_with_ldr(float ldr_avg) {
  184. // LDR quasi linear :)
  185. /*
  186. soll ldr_average
  187. ---- = ------------
  188. ist ldr_schwelle
  189. */
  190. #ifdef LDR_REGELUNG
  191. if(ldr_avg >= param.ldr_schwelle) {
  192. return;
  193. }
  194. idle_pwm_soll = (1 - (ldr_avg / param.ldr_schwelle)) * param.idle_pwm_max;
  195. Serial.printf("Update idle_pwm_soll_with_ldr: %d\n", idle_pwm_soll);
  196. if(idle_pwm_ist != idle_pwm_soll)
  197. fsm_inputs.ldr_changed = true;
  198. #endif
  199. }
  200. bool Treppe::check_ldr(float ldr_avg) {
  201. static uint8_t active = 0;
  202. if (ldr_avg < param.ldr_schwelle) {
  203. active = 1;
  204. }
  205. if (ldr_avg > param.ldr_schwelle + LDR_HYS) {
  206. active = 0;
  207. }
  208. fsm_inputs.ldr_schwelle = active;
  209. return active;
  210. }
  211. void Treppe::task() {
  212. #ifdef DEBUG_TIMING
  213. uint32_t m = micros();
  214. #endif
  215. sample_ldr();
  216. #ifdef DEBUG_TIMING
  217. Serial.print("1:");
  218. Serial.println(micros() - m);
  219. m = micros();
  220. #endif
  221. read_sensors();
  222. fsm_inputs.sensor_oben = fsm_pend.sensor_oben;
  223. fsm_inputs.sensor_unten = fsm_pend.sensor_unten;
  224. fsm_inputs.anim_beendet = fsm_pend.anim_beendet;
  225. #ifdef DEBUG_TIMING
  226. Serial.print("2:");
  227. Serial.println(micros() - m);
  228. m = micros();
  229. #endif
  230. FSMTreppe_Obj.setExternalInputs(&fsm_inputs);
  231. FSMTreppe_Obj.step();
  232. fsm_outputs = FSMTreppe_Obj.getExternalOutputs();
  233. #ifdef DEBUG_TIMING
  234. Serial.print("3:");
  235. Serial.println(micros() - m);
  236. m = micros();
  237. #endif
  238. print_state_on_change();
  239. #ifdef DEBUG_TIMING
  240. Serial.print("4:");
  241. Serial.println(micros() - m);
  242. m = micros();
  243. #endif
  244. if (fsm_outputs.status == ST_AUFDIMMEN_HOCH ||
  245. fsm_outputs.status == ST_ABDIMMEN_HOCH ||
  246. fsm_outputs.status == ST_AUFDIMMEN_RUNTER ||
  247. fsm_outputs.status == ST_ABDIMMEN_RUNTER) {
  248. if (fsm_pend.anim_beendet) {
  249. start_animation(&dimmer_stufen, DIM_STUFEN, param.active_pwm,
  250. idle_pwm_ist);
  251. }
  252. else {
  253. fsm_pend.anim_beendet = dimmer_tick(&dimmer_stufen, DIM_STUFEN);
  254. }
  255. }
  256. else if (fsm_outputs.status == ST_AUFDIMMEN_LDR ||
  257. fsm_outputs.status == ST_ABDIMMEN_LDR) {
  258. if (fsm_pend.anim_beendet) {
  259. #ifdef LDR_REGELUNG
  260. start_animation(&dimmer_ldr, DIM_LDR, idle_pwm_soll, 0);
  261. idle_pwm_ist = idle_pwm_soll;
  262. #else
  263. start_animation(&dimmer_ldr, DIM_LDR, idle_pwm_ist, 0);
  264. #endif
  265. }
  266. else
  267. fsm_pend.anim_beendet = dimmer_tick(&dimmer_ldr, DIM_LDR);
  268. }
  269. else if(fsm_outputs.status == ST_LDR_CHANGED) {
  270. fsm_inputs.ldr_changed = false;
  271. #ifdef LDR_REGELUNG
  272. if (fsm_pend.anim_beendet) {
  273. if(idle_pwm_soll < idle_pwm_ist) {
  274. fsm_outputs.dimmrichtung = DR_ABDIMMEN;
  275. start_animation(&dimmer_ldr, DIM_LDR, idle_pwm_ist, idle_pwm_soll);
  276. }
  277. else {
  278. fsm_outputs.dimmrichtung = DR_AUFDIMMEN;
  279. start_animation(&dimmer_ldr, DIM_LDR, idle_pwm_soll, idle_pwm_ist);
  280. }
  281. idle_pwm_ist = idle_pwm_soll;
  282. }
  283. else {
  284. fsm_pend.anim_beendet = dimmer_tick(&dimmer_ldr, DIM_LDR);
  285. }
  286. #endif
  287. }
  288. if (fsm_outputs.status == ST_RUHEZUSTAND ||
  289. fsm_outputs.status == ST_INAKTIV_LDR) {
  290. if (param_changed) {
  291. param_changed = false;
  292. param = param_pend;
  293. save_param_to_eeprom();
  294. Serial.printf("Parameter Change applied!\n");
  295. }
  296. }
  297. #ifdef DEBUG_TIMING
  298. Serial.print("5:");
  299. Serial.println(micros() - m);
  300. #endif
  301. }
  302. void Treppe::setup() {
  303. pwmController.resetDevices();
  304. // Deactive PCA9685 Phase Balancer due to LED Flickering
  305. // https://github.com/NachtRaveVL/PCA9685-Arduino/issues/15
  306. // see also lib/PCA9685-Arduin/PCA9685.h:204
  307. pwmController.init(PCA9685_PhaseBalancer_None);
  308. // pwmController.init(PCA9685_PhaseBalancer_Linear);
  309. pwmController.setPWMFrequency(100);
  310. // pwmController.setAllChannelsPWM(idle_pwm);
  311. // WARNING: before getting Parameters of Flash, make sure plausible parameters
  312. // are written in flash!
  313. EEPROM.get(EEP_START_ADDR, param); // get Parameters of flash
  314. param_pend = param;
  315. pinMode(13, OUTPUT);
  316. pinMode(0, OUTPUT);
  317. digitalWrite(13, HIGH);
  318. digitalWrite(0, HIGH);
  319. pinMode(A0, INPUT);
  320. pinMode(SENSOR_OBEN, INPUT);
  321. pinMode(SENSOR_UNTEN, INPUT);
  322. pinMode(OE, OUTPUT);
  323. digitalWrite(OE, 0);
  324. Serial.printf("Treppe: stufen=%d\n", stufen);
  325. }
  326. void Treppe::save_param_to_eeprom() {
  327. EEPROM.put(EEP_START_ADDR,
  328. param); // copy Parameters so "EEPROM"-section in RAM
  329. EEPROM.commit(); // write "EEPROM"-section to flash
  330. }
  331. void Treppe::set_idle_pwm_max(const uint16_t value,
  332. const vorgabe_typ_t vorgabe_typ) {
  333. if (vorgabe_typ == VORGABE_PROZENT) {
  334. param_pend.idle_pwm_max = param_pend.active_pwm * value / 100;
  335. } else if (vorgabe_typ == VORGABE_12BIT) {
  336. param_pend.idle_pwm_max = value;
  337. }
  338. if (param_pend.idle_pwm_max > param_pend.active_pwm) {
  339. param_pend.idle_pwm_max = param_pend.active_pwm;
  340. }
  341. param_changed = true;
  342. Serial.printf("Treppe: param_pend.idle_pwm_max=%d\n",
  343. param_pend.idle_pwm_max);
  344. }
  345. void Treppe::set_active_pwm(const uint16_t value,
  346. const vorgabe_typ_t vorgabe_typ) {
  347. if (vorgabe_typ == VORGABE_PROZENT) {
  348. param_pend.active_pwm = 4095 * value / 100;
  349. } else if (vorgabe_typ == VORGABE_12BIT) {
  350. param_pend.active_pwm = value;
  351. }
  352. if (param_pend.active_pwm > 4095) {
  353. param_pend.idle_pwm_max = 4095;
  354. }
  355. param_changed = true;
  356. Serial.printf("Treppe: param_pend.active_pwm=%d\n", param_pend.active_pwm);
  357. }
  358. void Treppe::set_time_ldr(const uint16_t value) {
  359. param_pend.time_ldr = value;
  360. if (param_pend.time_ldr > TIME_MS_MAX)
  361. param_pend.time_ldr = TIME_MS_MAX;
  362. param_changed = true;
  363. Serial.printf("Treppe: time_ldr=%d\n", param_pend.time_ldr);
  364. }
  365. void Treppe::set_time_per_stair(const uint16_t value) {
  366. param_pend.time_per_stair = value;
  367. if (param_pend.time_per_stair > TIME_MS_MAX)
  368. param_pend.time_per_stair = TIME_MS_MAX;
  369. param_changed = true;
  370. Serial.printf("Treppe: time_per_stair=%d\n", param_pend.time_per_stair);
  371. }
  372. void Treppe::set_ldr_schwelle(const uint16_t value,
  373. const vorgabe_typ_t vorgabe_typ) {
  374. if (vorgabe_typ == VORGABE_PROZENT) {
  375. // ?!
  376. param_pend.ldr_schwelle = 10 * value / 100;
  377. } else if (vorgabe_typ == VORGABE_12BIT) {
  378. param_pend.ldr_schwelle = value;
  379. }
  380. param_changed = true;
  381. Serial.printf("Treppe: ldr_schwelle=%d\n", param_pend.ldr_schwelle);
  382. }
  383. void Treppe::param_to_json(char *json_str, size_t sz) {
  384. snprintf(json_str, sz, "{\n\
  385. \"time_ldr\": %d,\n\
  386. \"time_per_stair\": %d,\n\
  387. \"idle_pwm_max\": %d,\n\
  388. \"active_pwm\": %d,\n\
  389. \"ldr_schwelle\": %d\n}\n",
  390. param.time_ldr, param.time_per_stair, param.idle_pwm_max,
  391. param.active_pwm, param.ldr_schwelle);
  392. }