Advertisement
Guest User

ERDF_OCTOBRE_LED2

a guest
Sep 30th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.34 KB | None | 0 0
  1. #include <Adafruit_WS2801.h>
  2.  
  3. #include <LiquidCrystal_PCF8574.h>
  4.  
  5. ///////////////////////////////////////
  6. #include <SPI.h> // librairie pour la communication SPI
  7. #include <MFRC522.h> // librairie pour les lecteurs RFID
  8. #include <Wire.h> // librairie pour la connection i2c
  9.  
  10.  
  11. LiquidCrystal_PCF8574 lcd(0x27); // set the LCD address to 0x27 for a 16 chars and 2 line display
  12. //librairie leds
  13. #include <Adafruit_NeoPixel.h>
  14. #ifdef __AVR__
  15. #include <avr/power.h>
  16. #endif
  17.  
  18. #define PIN 8
  19. // READERS ////////////////////////////////////////////////////////////////////////////////
  20. #define RST_PIN 9
  21. #define SS_1_PIN A0
  22. #define SS_2_PIN A1
  23. #define SS_3_PIN A2
  24. #define SS_4_PIN A3
  25. #define SS_5_PIN A4
  26. #define SS_6_PIN 4
  27. #define SS_7_PIN 5
  28. #define SS_8_PIN 6
  29.  
  30. #define NR_OF_READERS 8
  31.  
  32. byte ssPins[] = {SS_1_PIN, SS_2_PIN, SS_3_PIN, SS_4_PIN, SS_5_PIN, SS_6_PIN, SS_7_PIN, SS_8_PIN};
  33. //byte ssPins[] = {SS_1_PIN};
  34.  
  35. MFRC522 mfrc522[NR_OF_READERS]; // Create MFRC522 instance.
  36.  
  37. byte nuidPICC[3];
  38.  
  39. //
  40.  
  41. //BOUTONS ////////////////////////////////////////////////////////////////////////////////
  42.  
  43. const int boutonPlus = A5;
  44. const int boutonMoins = 10;
  45. int boutonTarifHaut = 11;
  46. int boutonTarifMoy = 12;
  47. int boutonTarifBas = 13;
  48.  
  49. int compteurMenu = 1;
  50. int puiss_old = 0;
  51. int puiss_sous_old = 0;
  52. float conso_max = 8000;
  53. float conso_annee = 11000;
  54.  
  55. //
  56.  
  57. //VARIABLES COMPTEUR ////////////////////////////////////////////////////////////////////////////////
  58.  
  59. int PUISSANCE_APP[14] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  60. int PUISSANCE_ANNEE[14] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  61. int TOT_PUISSANCE_APP = 0;
  62. int PUISSANCE_MAX = 0;
  63. int P_SOUSCRITE = 0;
  64. int PUISSANCE_COUP = 0;
  65. int P_CONSOMMEE = 0;
  66.  
  67. //
  68.  
  69. //BANDE LEDS////////////////////////////////////////////////////////////////////////////////
  70.  
  71. //Adafruit_NeoPixel strip = Adafruit_NeoPixel(13, PIN, NEO_GRB + NEO_KHZ800);
  72. int dataPin = 2;
  73. int clockPin = 3;
  74.  
  75. // Set the first variable to the NUMBER of pixels. 25 = 25 pixels in a row
  76. Adafruit_WS2801 strip = Adafruit_WS2801(12, dataPin, clockPin);
  77.  
  78. int dump_byte_array(byte *buffer, byte bufferSize) {
  79. for (byte i = 0; i < bufferSize; i++) {
  80. Serial.print(buffer[i] < 0x10 ? " 0" : " ");
  81. Serial.print(buffer[i], HEX);
  82. int tot = +buffer[i];
  83. return tot;
  84. }
  85. }
  86.  
  87. void menu() {
  88. if (compteurMenu == 1) {
  89. lcd.clear();
  90. lcd.setCursor(8, 1) ;
  91. lcd.print("BASE") ;
  92. lcd.setCursor(3, 2) ;
  93. lcd.print("NOM DU CONTRAT") ;
  94. }
  95. if (compteurMenu == 2) {
  96. lcd.clear();
  97. lcd.setCursor(4, 1) ;
  98. lcd.print(P_CONSOMMEE) ;
  99. lcd.setCursor(10, 1) ;
  100. lcd.print("KWH") ;
  101. lcd.setCursor(8, 2) ;
  102. lcd.print("BASE") ;
  103. }
  104.  
  105. if (compteurMenu == 3) {
  106. lcd.clear();
  107. lcd.setCursor(4, 1) ;
  108. lcd.print(TOT_PUISSANCE_APP) ;
  109. lcd.setCursor(10, 1) ;
  110. lcd.print(" VA") ;
  111. lcd.setCursor(4, 2) ;
  112. lcd.print("PUISSANCE APP") ;
  113. }
  114.  
  115. if (compteurMenu == 4) {
  116. lcd.clear();
  117. lcd.setCursor(8, 1) ;
  118. lcd.print("0 VA") ;
  119. lcd.setCursor(4, 2) ;
  120. lcd.print("PUISSANCE MAX") ;
  121. }
  122.  
  123. if (compteurMenu == 5) {
  124. lcd.clear();
  125. lcd.setCursor(8, 1) ;
  126. lcd.print(P_SOUSCRITE) ;
  127. lcd.setCursor(10, 1) ;
  128. lcd.print("KVA") ;
  129. lcd.setCursor(5, 2) ;
  130. lcd.print("P SOUSCRITE") ;
  131. }
  132.  
  133. if (compteurMenu == 6) {
  134. lcd.clear();
  135. lcd.setCursor(7, 1) ;
  136. lcd.print(P_SOUSCRITE*1000) ;
  137. lcd.setCursor(12, 1) ;
  138. lcd.print("VA") ;
  139. lcd.setCursor(3, 2) ;
  140. lcd.print("PUISSANCE COUP") ;
  141. }
  142.  
  143. if (compteurMenu == 7) {
  144. lcd.clear();
  145. lcd.setCursor(5, 1) ;
  146. lcd.print("00000000000") ;
  147. lcd.setCursor(4, 2) ;
  148. lcd.print("NUMERO DE PRM") ;
  149. }
  150.  
  151. if (compteurMenu == 8) {
  152.  
  153. lcd.clear();
  154. lcd.setCursor(6, 1) ;
  155. lcd.print("16:20:15") ;
  156. lcd.setCursor(3, 2) ;
  157. lcd.print("HEURE COURANTE") ;
  158. }
  159.  
  160. if (compteurMenu == 9) {
  161.  
  162. lcd.clear();
  163. lcd.setCursor(6, 1) ;
  164. lcd.print("19/05/15") ;
  165. lcd.setCursor(4, 2) ;
  166. lcd.print("DATE COURANTE") ;
  167. }
  168.  
  169. if (compteurMenu == 10) {
  170. lcd.clear();
  171. lcd.setCursor(7, 1) ;
  172. lcd.print("OUVERT") ;
  173. lcd.setCursor(5, 2) ;
  174. lcd.print("CONTACT SEC") ;
  175. }
  176.  
  177. if (compteurMenu == 11) {
  178. lcd.clear();
  179. lcd.setCursor(5, 1) ;
  180. lcd.print("HISTORIQUE") ;
  181. lcd.setCursor(6, 2) ;
  182. lcd.print("MODE TIC") ;
  183. }
  184.  
  185. if (compteurMenu == 12) {
  186. lcd.clear();
  187. lcd.setCursor(8, 1) ;
  188. lcd.print("0 KWH") ;
  189. lcd.setCursor(4, 2) ;
  190. lcd.print("INDEX DISTRIB") ;
  191. }
  192.  
  193. if (compteurMenu == 13) {
  194. lcd.clear();
  195. lcd.setCursor(4, 1) ;
  196. lcd.print("012345678912") ;
  197. lcd.setCursor(4, 2) ;
  198. lcd.print("NUMERO SERIE") ;
  199. }
  200.  
  201. if (compteurMenu == 14) {
  202. lcd.clear();
  203. lcd.setCursor(8, 1) ;
  204. lcd.print("0 KWH") ;
  205. lcd.setCursor(6, 2) ;
  206. lcd.print("INDEX MID") ;
  207. }
  208.  
  209. if (compteurMenu == 15) {
  210. lcd.clear();
  211. lcd.setCursor(8, 1) ;
  212. lcd.print("0 KWH") ;
  213. lcd.setCursor(3, 2) ;
  214. lcd.print("INDEX INJECTION") ;
  215. }
  216.  
  217. if (compteurMenu == 16) {
  218. lcd.clear();
  219. lcd.setCursor(8, 1) ;
  220. lcd.print("0000") ;
  221. lcd.setCursor(3, 2) ;
  222. lcd.print("REGISTRE ERREUR") ;
  223. }
  224.  
  225. if (compteurMenu == 17) {
  226. lcd.clear();
  227. lcd.setCursor(6, 1) ;
  228. lcd.print("F2208938") ;
  229. lcd.setCursor(4, 2) ;
  230. lcd.print("CHECKSUM MID") ;
  231. }
  232.  
  233. if (compteurMenu == 18) {
  234. lcd.clear();
  235. lcd.setCursor(3, 2) ;
  236. lcd.print("PAS DE MESSAGE");
  237. }
  238. }
  239. int printDec(byte *buffer, byte bufferSize) {
  240. int tot;
  241. for (byte i = 0; i < bufferSize; i++) {
  242. // Serial.print(buffer[i] < 0x10 ? " 0" : " ");
  243. tot = tot + buffer[i];
  244. //Serial.print(buffer[i], DEC);
  245. }
  246. return tot;
  247. }
  248.  
  249. void carte_pose(uint32_t c, uint8_t wait) {
  250. for (uint16_t i = 0; i < 8; i++) {
  251. strip.setPixelColor(i, c);
  252. strip.show();
  253. delay(wait);
  254. }
  255. }
  256.  
  257. void chgt_contrat(uint32_t c, uint8_t wait) {
  258. for (int i = 12; i >= 0; i--) {
  259. strip.setPixelColor(i, c);
  260. strip.show();
  261. delay(wait);
  262. }
  263. }
  264.  
  265. void petage_plombs(uint32_t c, uint8_t wait) {
  266. lcd.clear();
  267. lcd.setCursor(8, 2) ;
  268. lcd.print("PANNE");
  269. for (int j = 0; j < 10; j++) { //do 10 cycles of chasing
  270. for (int q = 0; q < 3; q++) {
  271. for (uint16_t i = 0; i < strip.numPixels(); i = i + 3) {
  272. strip.setPixelColor(i + q, c); //turn every third pixel on
  273. }
  274. strip.show();
  275.  
  276. delay(wait);
  277.  
  278. for (uint16_t i = 0; i < strip.numPixels(); i = i + 3) {
  279. strip.setPixelColor(i + q, 0); //turn every third pixel off
  280. }
  281. }
  282. }
  283. }
  284. // Create a 24 bit color value from R,G,B
  285. uint32_t Color(byte r, byte g, byte b)
  286. {
  287. uint32_t c;
  288. c = r;
  289. c <<= 8;
  290. c |= g;
  291. c <<= 8;
  292. c |= b;
  293. return c;
  294. }
  295.  
  296. //
  297.  
  298. void setup() {
  299.  
  300. Serial.begin(9600); // Initialize serial communications with the PC
  301. SPI.begin(); // Init SPI bus
  302.  
  303. pinMode(boutonPlus, INPUT_PULLUP);
  304. pinMode(boutonMoins, INPUT_PULLUP);
  305. pinMode(boutonTarifHaut, INPUT_PULLUP);
  306. pinMode(boutonTarifMoy, INPUT_PULLUP);
  307. pinMode(boutonTarifBas, INPUT_PULLUP);
  308.  
  309. /*for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
  310. mfrc522[reader].PCD_Init(ssPins[reader], RST_PIN); // Init each MFRC522 card
  311. //"delay(80);
  312. //mfrc522[reader].PCD_SetRegisterBitMask(mfrc522[reader].RFCfgReg, (0x07<<4));
  313. }*/
  314. lcd.begin(20, 4); // initialize the lcd
  315. //lcd.cursor_on() ;
  316. //lcd.blink_on() ;
  317. lcd.setBacklight(255);
  318. lcd.setCursor(0, 0) ;
  319. lcd.print("READY !") ;
  320. strip.begin();
  321. strip.show(); // Initialize all pixels to 'off'
  322.  
  323. menu();
  324. Serial.println("ready");
  325. }
  326.  
  327. /////////////////////////////////////////////////////////////////////////////////
  328.  
  329. void loop() {
  330. int newcard;
  331.  
  332. puiss_old = TOT_PUISSANCE_APP;
  333. TOT_PUISSANCE_APP = conso_max * 0.08;
  334. P_CONSOMMEE = conso_annee * 0.08;
  335. for (int i = 0; i < 14; i++) {
  336. PUISSANCE_APP[i] = 0;
  337. PUISSANCE_ANNEE[i]=0;
  338. }
  339. puiss_sous_old = P_SOUSCRITE;
  340. for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
  341. if (digitalRead(boutonTarifHaut) == LOW) {
  342. Serial.print("P_SOUSCRITE = 9");
  343. P_SOUSCRITE = 9;
  344. }
  345. if (digitalRead(boutonTarifMoy) == LOW) {
  346. Serial.print("P_SOUSCRITE = 6");
  347. P_SOUSCRITE = 6;
  348. }
  349. if (digitalRead(boutonTarifBas) == LOW) {
  350. Serial.print("P_SOUSCRITE = 3");
  351. P_SOUSCRITE = 3;
  352. }
  353.  
  354. if (digitalRead(boutonPlus) == LOW) {
  355. compteurMenu = compteurMenu + 1;
  356. Serial.print("Plus");
  357.  
  358. delay(50);
  359. }
  360.  
  361. if (digitalRead(boutonMoins) == LOW) {
  362. compteurMenu = compteurMenu - 1;
  363. Serial.print("Moins");
  364.  
  365. delay(50);
  366.  
  367. }
  368.  
  369. if (compteurMenu == 19) compteurMenu = 1;
  370. if (compteurMenu == 0) compteurMenu = 18;
  371. mfrc522[reader].PCD_Init(ssPins[reader], RST_PIN); // Init each MFRC522 card
  372. newcard = mfrc522[reader].PICC_IsNewCardPresent();
  373. if (newcard && mfrc522[reader].PICC_ReadCardSerial()) {
  374. byte val_card = printDec(mfrc522[reader].uid.uidByte, mfrc522[reader].uid.size);
  375. Serial.print("valeur carte : ");
  376. Serial.print(val_card);
  377. switch (val_card) {
  378. case 250: //chauffage electrique
  379. PUISSANCE_APP[0] = conso_max * 0.41;
  380. PUISSANCE_ANNEE[0] = conso_annee * 0.41;
  381. break;
  382. case 109: //eau chaude
  383. PUISSANCE_APP[1] = conso_max * 0.18;
  384. PUISSANCE_ANNEE[1] = conso_annee * 0.18;
  385. break;
  386. case 229: //lumiere
  387. PUISSANCE_APP[2] = conso_max * 0.03;
  388. PUISSANCE_ANNEE[2] = conso_annee * 0.03;
  389. break;
  390. case 10: // froid
  391. PUISSANCE_APP[3] = conso_max * 0.06;
  392. PUISSANCE_ANNEE[3] = conso_annee * 0.06;
  393. break;
  394. case 100: //loisirs
  395. PUISSANCE_APP[4] = conso_max * 0.11;
  396. PUISSANCE_ANNEE[4] = conso_annee * 0.11;
  397. break;
  398. case 88: //electro-menager
  399. PUISSANCE_APP[5] = conso_max * 0.05;
  400. PUISSANCE_ANNEE[5] = conso_annee * 0.05;
  401. break;
  402. case 86: //cuisson
  403. PUISSANCE_APP[6] = conso_max * 0.08;
  404. PUISSANCE_ANNEE[6] = conso_annee * 0.08;
  405. break;
  406. case 148: //chauffage eco -10%
  407. PUISSANCE_APP[7] = (conso_max * 0.41) - (conso_max * 0.41) * 0.2;
  408. PUISSANCE_ANNEE[7] = (conso_annee * 0.41) - (conso_annee * 0.41) * 0.1;
  409. break;
  410. case 131: //eau chaude eco -40%
  411. PUISSANCE_APP[8] = (conso_max * 0.18) - (conso_max * 0.18) * 0.4;
  412. PUISSANCE_ANNEE[8] = (conso_annee * 0.18) - (conso_annee * 0.18) * 0.4;
  413. break;
  414. case 130: //cuisson eco -30%
  415. PUISSANCE_APP[9] = (conso_max * 0.08) - (conso_max * 0.08) * 0.3;
  416. PUISSANCE_ANNEE[9] = (conso_annee * 0.08) - (conso_annee * 0.08) * 0.3;
  417. break;
  418. case 239: //lumiere eco -20%
  419. PUISSANCE_APP[10] = (conso_max * 0.03) - (conso_max * 0.03) * 0.2;
  420. PUISSANCE_ANNEE[10] = (conso_annee * 0.03) - (conso_annee * 0.03) * 0.2;
  421. break;
  422. case 168: //electro-menager eco -20%
  423. PUISSANCE_APP[11] = (conso_max * 0.05) - (conso_max * 0.05) * 0.2;
  424. PUISSANCE_ANNEE[11] = (conso_annee * 0.05) - (conso_annee * 0.05) * 0.2;
  425. break;
  426. case 127: //loisirs eco -40%
  427. PUISSANCE_APP[12] = (conso_max * 0.11) - (conso_max * 0.11) * 0.4;
  428. PUISSANCE_ANNEE[12] = (conso_annee * 0.11) - (conso_annee * 0.11) * 0.4;
  429. break;
  430. case 146: //froid eco -20%
  431. PUISSANCE_APP[13] = (conso_max * 0.06) - (conso_max * 0.06) * 0.2;
  432. PUISSANCE_ANNEE[13] = (conso_annee * 0.06) - (conso_annee * 0.06) * 0.2;
  433. break;
  434.  
  435. }
  436. }
  437.  
  438. //colorWipe(Color(63, 0, 0), 50);
  439.  
  440. }
  441. for (int i = 0 ; i < 14 ; i++)
  442. {
  443. TOT_PUISSANCE_APP = TOT_PUISSANCE_APP + PUISSANCE_APP[i] ;
  444. P_CONSOMMEE = P_CONSOMMEE + PUISSANCE_ANNEE[i] ;
  445. }
  446. Serial.print("puissance tot : ");
  447. Serial.println(TOT_PUISSANCE_APP);
  448. if (puiss_old != TOT_PUISSANCE_APP) {
  449. carte_pose(Color(40, 0, 0), 50);
  450. carte_pose(Color(0, 0, 0), 50);
  451. }
  452. if ( puiss_sous_old != P_SOUSCRITE) {
  453. chgt_contrat(Color(0, 0, 40), 80);
  454. chgt_contrat(Color(0, 0, 0), 80);
  455. }
  456. if (TOT_PUISSANCE_APP > P_SOUSCRITE*1000) {
  457. petage_plombs(Color(0, 40, 0), 30);
  458. }
  459. menu();
  460.  
  461.  
  462.  
  463. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement