Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
629
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 45.06 KB | None | 0 0
  1. ////////
  2. //
  3. // Sketch Arduino pour le Plugin JEEDOUINO v097+ de JEEDOM
  4. // Connection via Ethernet
  5. //
  6. // Généré le 2019-08-25 07:38:08.
  7. // Pour l'équipement test arduino jeedom (EqID : 7 ).
  8. // Modèle de carte : auno.
  9. ////////
  10. #define DEBUGtoSERIAL 1 // 0, ou 1 pour debug dans la console serie
  11. #define UseWatchdog 0
  12. #define NODHCP 1 // 0 pour IP via DHCP, 1 pour IP fixée dans le sketch.
  13. #define UseDHT 1
  14. #define UseDS18x20 1
  15. #define UseTeleInfo 0
  16. #define UseLCD16x2 0 // 0 = None(Aucun) / 1 = LCD Standard 6 pins / 2 = LCD via I2C
  17. #define UseEthernet 0 // Choix de la lib suivant shield ethernet : 0 = W5100 / 1 = ENC28J60 / 2 = W5500 - Voir note ci-dessous
  18. #define UseHCSR04 0
  19. #define UsePwm_input 0 // Code obsolete (sera supprimé) - Entrée Numérique Variable (0-255 sur 10s) en PULL-UP
  20. #define UseBMP180 0 // pour BMP085/180 Barometric Pressure & Temp Sensor
  21. #define UseServo 0 // Pour controler la postion d'un servo.
  22. #define UseWS2811 0 // Pour gerer les led stips a base de WS2811/2 avec l'excellente lib Adafruit_NeoPixel
  23.  
  24. // Vous permet d'inclure du sketch perso - voir Doc / FAQ.
  25. // Il faut activer l'option dans la configuration du plugin.
  26. // Puis choisir le nombre de variables utilisateur sous l'onglet Pins/GPIO de votre équipement.
  27. #define UserSketch 0
  28. // Tags pour rechercher l'emplacement pour votre code :
  29. //UserVars
  30. //UserSetup
  31. //UserLoop
  32.  
  33. #if (UseWatchdog == 1)
  34. #include <avr/wdt.h>
  35. #endif
  36.  
  37. #include <SPI.h>
  38. // Pour shield avec W5100
  39. #if (UseEthernet == 0)
  40. #include <Ethernet.h>
  41. #endif
  42. // Pour shield avec W5500
  43. #if (UseEthernet == 2)
  44. #include <Ethernet2.h>
  45. #endif
  46. // Pour shield avec ENC28J60 - Note : il faut passer NODHCP à 1 ci-dessus.
  47. // Attention, problèmes de mémoire possibles sur arduino nano/uno/328 avec cette lib (v1.59)!
  48. // Pour la récupérer, et l'installer dans l'IDE, voir : https://github.com/ntruchsess/arduino_uip/tree/Arduino_1.5.x
  49. //
  50. // Il faudra modifier dans le fichier \arduino-IDE\libraries\arduino_uip-master\utility\uipethernet-conf
  51. // les lignes suivantes:
  52. //#define UIP_SOCKET_NUMPACKETS 3
  53. //#define UIP_CONF_MAX_CONNECTIONS 2
  54. //#define UIP_CONF_UDP 0
  55. //
  56. #if (UseEthernet == 1)
  57. #include <UIPEthernet.h> // v1.59
  58. #endif
  59. // Traitement spécifique a cette librairie (pb de deconnection):
  60. int UIPEFailCount = 0;
  61. unsigned long UIPEFailTime = millis();
  62.  
  63. ////////
  64. // DHT
  65. // https://github.com/adafruit/DHT-sensor-library
  66. #if (UseDHT == 1)
  67. #include <DHT.h>
  68. #endif
  69.  
  70. ////////
  71. // DS18x20
  72. // https://github.com/PaulStoffregen/OneWire
  73. #if (UseDS18x20 == 1)
  74. #include <OneWire.h>
  75. #endif
  76.  
  77. byte IP_ARDUINO[] = { 192, 168, 1, 203 };
  78. byte IP_JEEDOM[] = { 192, 168, 1, 9 };
  79. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xE7 };
  80. EthernetServer server(8000);
  81.  
  82. #include <EEPROM.h>
  83.  
  84. // CONFIGURATION VARIABLES
  85.  
  86. #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  87. #define NB_DIGITALPIN 54
  88. #define NB_ANALOGPIN 16
  89. #else
  90. #define NB_DIGITALPIN 14
  91. #define NB_ANALOGPIN 6
  92. #endif
  93. #define NB_TOTALPIN ( NB_DIGITALPIN + NB_ANALOGPIN)
  94.  
  95. // Etat des pins de l'arduino ( Mode )
  96. char Status_pins[NB_TOTALPIN];
  97. byte pin_id;
  98. byte echo_pin;
  99.  
  100. String eqLogic = "";
  101. String inString = "";
  102. String Message = "";
  103. byte BootMode;
  104.  
  105. // Pour la detection des changements sur pins en entree
  106. byte PinValue;
  107. byte OLDPinValue[NB_TOTALPIN ];
  108. unsigned long AnalogPinValue;
  109. unsigned long OLDAnalogPinValue[NB_TOTALPIN ];
  110. unsigned long CounterPinValue[NB_TOTALPIN ];
  111. unsigned long PinNextSend[NB_TOTALPIN ];
  112. byte swtch[NB_TOTALPIN];
  113. // pour envoi ver jeedom
  114. String jeedom = "\0";
  115. // reception commande
  116. char c[250];
  117. byte n=0;
  118. byte RepByJeedom=0;
  119. // Temporisation sorties
  120. unsigned long TempoPinHIGH[NB_TOTALPIN ]; // pour tempo pins sorties HIGH
  121. unsigned long TempoPinLOW[NB_TOTALPIN ]; // pour tempo pins sorties LOW
  122. unsigned long pinTempo=0;
  123. unsigned long NextRefresh=0;
  124. unsigned long ProbeNextSend=millis();
  125. unsigned long timeout = 0;
  126.  
  127. #if (UseDHT == 1)
  128. DHT *myDHT[NB_TOTALPIN];
  129. #endif
  130.  
  131. #if (UseServo == 1)
  132. #include <Servo.h>
  133. Servo myServo[NB_TOTALPIN];
  134. #endif
  135.  
  136. #if (UseWS2811 == 1)
  137. // More info at https://github.com/adafruit/Adafruit_NeoPixel
  138. #include <Adafruit_NeoPixel.h>
  139. // Parameter 1 = number of pixels in strip
  140. // Parameter 2 = Arduino pin number (most are valid)
  141. // Parameter 3 = pixel type flags, add together as needed:
  142. // NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
  143. // NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
  144. // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
  145. // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
  146. // NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
  147. #define WS2811PIN 6
  148. Adafruit_NeoPixel strip = Adafruit_NeoPixel(50, WS2811PIN, NEO_GRB + NEO_KHZ800);
  149. #endif
  150.  
  151. #if (UseTeleInfo == 1)
  152. // TeleInfo / Software serial
  153. #include <SoftwareSerial.h>
  154. byte teleinfoRX = 0;
  155. byte teleinfoTX = 0;
  156. SoftwareSerial teleinfo(6,7); // definir vos pins RX , TX
  157. #endif
  158.  
  159. #if (UseLCD16x2 == 1)
  160. // LiquidCrystal Standard (not i2c)
  161. #include <LiquidCrystal.h>
  162. LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
  163. #endif
  164. #if (UseLCD16x2 == 2)
  165. // LiquidCrystal i2c
  166. #include <Wire.h>
  167. #include <LiquidCrystal_I2C.h>
  168. LiquidCrystal_I2C lcd(0x27,16,2);
  169. #endif
  170. #if (UseBMP180 == 1)
  171. // BMP085/180 Barometric Pressure & Temp Sensor
  172. // https://learn.adafruit.com/bmp085/downloads
  173. #include <Wire.h>
  174. #include <Adafruit_BMP085.h>
  175. Adafruit_BMP085 bmp;
  176. #endif
  177.  
  178. #if (UserSketch == 1)
  179. // UserVars
  180. // Vos declarations de variables / includes etc....
  181. //#include <your_stuff_here.h>
  182. #endif
  183.  
  184. // SETUP
  185.  
  186. void setup()
  187. {
  188. jeedom.reserve(256);
  189. Message.reserve(16);
  190. inString.reserve(4);
  191. #if (DEBUGtoSERIAL == 1)
  192. Serial.begin(9600); // Init du Port serie/USB
  193. Serial.setTimeout(5); // Timeout 5ms
  194. Serial.println(F("JEEDOUINO IS HERE."));
  195. #endif
  196. if (EEPROM.read(13) != 'J')
  197. {
  198. Init_EEPROM();
  199. #if (NODHCP == 0)
  200. if (Ethernet.begin(mac) == 0) // 1er demarrage 1er flash Jeedouino, on essaye via dhcp
  201. {
  202. #if (DEBUGtoSERIAL == 1)
  203. Serial.println(F("Connection via DHCP failed."));
  204. #endif
  205. #if (UseWatchdog == 1)
  206. wdt_enable(WDTO_15MS); // try reboot
  207. #endif
  208. while(1){}
  209. }
  210. IPAddress IP_ARDUINO = Ethernet.localIP();
  211. jeedom = F("&ipwifi=");
  212. jeedom += IP_ARDUINO[0];
  213. jeedom += '.';
  214. jeedom += IP_ARDUINO[1];
  215. jeedom += '.';
  216. jeedom += IP_ARDUINO[2];
  217. jeedom += '.';
  218. jeedom += IP_ARDUINO[3];
  219. SendToJeedom();
  220. #else
  221. Ethernet.begin(mac, IP_ARDUINO);
  222. #endif
  223. }
  224. else Ethernet.begin(mac, IP_ARDUINO);
  225.  
  226. #if (DEBUGtoSERIAL == 1)
  227. Serial.println(F("Connection to LAN."));
  228. #endif
  229. server.begin();
  230. Load_EEPROM(1);
  231.  
  232. #if (DEBUGtoSERIAL == 1)
  233. Serial.print(F("\nEqLogic:"));
  234. Serial.println(eqLogic);
  235. #endif
  236.  
  237. #if (UseTeleInfo == 1)
  238. teleinfo.begin(1200); // vitesse par EDF
  239. #endif
  240.  
  241. #if (UseLCD16x2 == 1)
  242. lcd.begin(16, 2);
  243. lcd.setCursor(0,0);
  244. lcd.print(F("JEEDOUINO v097+"));
  245. #endif
  246. #if (UseLCD16x2 == 2)
  247. lcd.init();
  248. lcd.backlight();
  249. lcd.home();
  250. lcd.print(F("JEEDOUINO v097+"));
  251. #endif
  252.  
  253. #if (UseBMP180 == 1)
  254. bmp.begin();
  255. #endif
  256.  
  257. #if (UseWS2811 == 1)
  258. strip.begin();
  259. strip.show();
  260. #endif
  261.  
  262. #if (UserSketch == 1)
  263. UserSetup(); // Appel de votre setup()
  264. #endif
  265. }
  266. //// User Setup
  267. #if (UserSketch == 1)
  268. void UserSetup()
  269. {
  270. // Votre setup()
  271. }
  272. #endif
  273.  
  274. // LOOP
  275.  
  276. void loop()
  277. {
  278. // TRAITEMENT DES TEMPO SORTIES SI IL Y EN A
  279. jeedom="";
  280. for (int i = 2; i < NB_TOTALPIN; i++)
  281. {
  282. if (TempoPinHIGH[i]!=0 && TempoPinHIGH[i]<millis()) // depassement de la temporisation
  283. {
  284. TempoPinHIGH[i]=0; // Suppression de la temporisation
  285. PinWriteHIGH(i);
  286. }
  287. else if (TempoPinLOW[i]!=0 && TempoPinLOW[i]<millis()) // depassement de la temporisation
  288. {
  289. TempoPinLOW[i]=0; // Suppression de la temporisation
  290. PinWriteLOW(i);
  291. }
  292. }
  293. // FIN TEMPO
  294.  
  295. // On ecoute le reseau
  296. EthernetClient client = server.available();
  297.  
  298. if (client)
  299. {
  300. // on regarde si on recois des donnees
  301. n=0;
  302. #if (DEBUGtoSERIAL == 1)
  303. Serial.println(F("\nRECEIVING:"));
  304. #endif
  305. timeout = millis()+30000; // 30s
  306. while (client.connected() and timeout>millis())
  307. {
  308. if (client.available())
  309. {
  310. c[n] = client.read();
  311. if (c[n]=='\r') c[n]='\n';
  312. if (c[n]=='\n')
  313. {
  314. while (client.available()) c[n+1] = client.read();
  315. break;
  316. }
  317. n++;
  318. }
  319. }
  320. #if (DEBUGtoSERIAL == 1)
  321. if (timeout<millis()) Serial.println(F("\nTimeOut:"));
  322. for (int i = 0; i <= n; i++) Serial.print(c[i]);
  323. #endif
  324.  
  325. if (n && c[n]=='\n')
  326. {
  327. n--;
  328. // on les traites
  329. if (c[0]=='C' && c[n]=='C') // Configuration de l'etat des pins
  330. {
  331. // NB_TOTALPIN = NB_DIGITALPIN + NB_ANALOGPIN
  332.  
  333. if (n==(NB_TOTALPIN+1)) // Petite securite
  334. {
  335. for (int i = 0; i < NB_TOTALPIN; i++)
  336. {
  337. EEPROM.update(30+i, c[i+1]); // Sauvegarde mode des pins
  338. }
  339. Load_EEPROM(0); // On met en place
  340. client.print(F("COK")); // On reponds a JEEDOM
  341. jeedom+=F("&REP=COK");
  342. ProbeNextSend=millis()+60000; // Décalage pour laisser le temps aux differents parametrages d'arriver de Jeedom
  343. }
  344. }
  345. else if (c[0]=='E' && c[n]=='Q') // Recuperation de l' eqLogic de Jeedom concernant cet arduino
  346. {
  347. eqLogic = "";
  348. EEPROM.update(15, n); // Sauvegarde de la longueur du eqLogic
  349. for (int i = 1; i < n; i++)
  350. {
  351. EEPROM.update(15+i, c[i]-'0'); // Sauvegarde de l' eqLogic
  352. eqLogic += (char)c[i];
  353. }
  354. client.print(F("EOK")); // On reponds a JEEDOM
  355. jeedom+=F("&REP=EOK");
  356. ProbeNextSend=millis()+60000; // Décalage pour laisser le temps aux differents parametrages d'arriver de Jeedom
  357. }
  358. else if (c[0]=='I' && c[n]=='P') // Recuperation de l' IP de Jeedom ( I192.168.000.044P )
  359. {
  360. if (n<17) // Petite securite
  361. {
  362. int ip=0;
  363. inString="";
  364. for (int i = 1; i <= n; i++) //jusqu'a n car il faut un caractere non digit pour finir
  365. {
  366. if (isDigit(c[i]))
  367. {
  368. inString += (char)c[i];
  369. }
  370. else
  371. {
  372. IP_JEEDOM[ip]=inString.toInt();
  373. inString="";
  374. ip++;
  375. }
  376. }
  377. EEPROM.update(26, IP_JEEDOM[0]); // Sauvegarde de l' IP
  378. EEPROM.update(27, IP_JEEDOM[1]);
  379. EEPROM.update(28, IP_JEEDOM[2]);
  380. EEPROM.update(29, IP_JEEDOM[3]);
  381. client.print(F("IPOK")); // On reponds a JEEDOM
  382. jeedom+=F("&REP=IPOK");
  383. ProbeNextSend=millis()+60000; // Décalage pour laisser le temps aux differents parametrages d'arriver de Jeedom
  384. }
  385. }
  386. else if (c[0]=='S' && c[n]=='S') // Modifie la valeur d'une pin sortie
  387. {
  388. jeedom+=F("&REP=SOK");
  389. for (int i = 1; i < n; i++)
  390. {
  391. if (isDigit(c[i])) c[i]=c[i]-'0';
  392. }
  393.  
  394. pin_id=10*int(c[1])+int(c[2]); // recuperation du numero de la pin
  395.  
  396. Set_OutputPin(pin_id);
  397. client.print(F("SOK")); // On reponds a JEEDOM
  398. ProbeNextSend=millis()+10000; // Décalage pour laisser le temps au differents parametrages d'arriver de Jeedom
  399. }
  400. else if ((c[0]=='S' || c[0]=='R') && c[n]=='C') // Reçoie la valeur SAUVEE d'une pin compteur (suite reboot)
  401. { // ou RESET suite sauvegarde equipement.
  402. if (n>3) // Petite securite
  403. {
  404. for (int i = 1; i < n; i++)
  405. {
  406. if (isDigit(c[i])) c[i]=c[i]-'0';
  407. }
  408.  
  409. if (c[0]=='R') CounterPinValue[pin_id]=0; // On reset la valeur si demandé.
  410.  
  411. pin_id=10*int(c[1])+int(c[2]); // récupération du numéro de la pin
  412. int multiple=1;
  413. for (int i = n-1; i >= 3; i--) // récupération de la valeur
  414. {
  415. CounterPinValue[pin_id] += int(c[i])*multiple;
  416. multiple *= 10;
  417. }
  418. PinNextSend[pin_id]=millis()+2000;
  419. NextRefresh=millis()+2000;
  420. ProbeNextSend=millis()+10000; // Décalage pour laisser le temps au differents parametrages d'arriver de Jeedom
  421.  
  422. client.print(F("SCOK")); // On reponds a JEEDOM
  423. jeedom+=F("&REP=SCOK");
  424. }
  425. }
  426. else if (c[0]=='S' && c[n]=='F') // Modifie la valeur de toutes les pins sortie (suite reboot )
  427. {
  428. // NB_TOTALPIN = NB_DIGITALPIN + NB_ANALOGPIN
  429. if (n==(NB_TOTALPIN+1)) // Petite securite
  430. {
  431. jeedom+=F("&REP=SFOK");
  432. for (int i = 2; i < NB_TOTALPIN; i++)
  433. {
  434. switch (Status_pins[i])
  435. {
  436. case 'o': // output
  437. case 's': // switch
  438. case 'l': // low_relais
  439. case 'h': // high_relais
  440. case 'u': // output_pulse
  441. case 'v': // low_pulse
  442. case 'w': // high_pulse
  443. if (c[i+1]=='0')
  444. {
  445. PinWriteLOW(i);
  446. }
  447. else if (c[i+1]=='1')
  448. {
  449. PinWriteHIGH(i);
  450. }
  451. break;
  452. }
  453. }
  454. RepByJeedom=0; // Demande repondue, pas la peine de redemander a la fin de loop()
  455. client.print(F("SFOK")); // On reponds a JEEDOM
  456. ProbeNextSend=millis()+20000; // Décalage pour laisser le temps au differents parametrages d'arriver de Jeedom
  457. }
  458. }
  459. else if (c[0]=='S' && (c[n]=='L' || c[n]=='H' || c[n]=='A')) // Modifie la valeur de toutes les pins sortie a LOW / HIGH / SWITCH / PULSE
  460. {
  461. if (n==2 || n==7) // Petite securite : S2L / S2H / S2A / SP00007L /SP00007H
  462. {
  463. jeedom+=F("&REP=SOK");
  464. for (int i = 1; i < n; i++)
  465. {
  466. if (isDigit(c[i])) c[i]=c[i]-'0';
  467. }
  468. if (c[1]=='P') pinTempo = 10000*int(c[2])+1000*int(c[3])+100*int(c[4])+10*int(c[5])+int(c[6]);
  469. for (int i = 2; i < NB_TOTALPIN; i++)
  470. {
  471. TempoPinHIGH[i] = 0;
  472. TempoPinLOW[i] = 0;
  473. switch (Status_pins[i])
  474. {
  475. case 'o': // output
  476. case 's': // switch
  477. case 'l': // low_relais
  478. case 'h': // high_relais
  479. case 'u': // output_pulse
  480. case 'v': // low_pulse
  481. case 'w': // high_pulse
  482. if (c[n]=='L')
  483. {
  484. if (c[1] == 'P') TempoPinHIGH[i] = pinTempo;
  485. PinWriteLOW(i);
  486. }
  487. else if (c[n] == 'H')
  488. {
  489. if (c[1] == 'P') TempoPinLOW[i] = pinTempo;
  490. PinWriteHIGH(i);
  491. }
  492. else
  493. {
  494. if (swtch[i]==1) PinWriteLOW(i);
  495. else PinWriteHIGH(i);
  496. }
  497. break;
  498. }
  499. }
  500. client.print(F("SOK")); // On reponds a JEEDOM
  501. ProbeNextSend=millis()+10000; // Décalage pour laisser le temps au differents parametrages d'arriver de Jeedom
  502. }
  503. }
  504. else if (c[0]=='B' && c[n]=='M') // Choix du BootMode
  505. {
  506. BootMode=int(c[1]-'0');
  507. EEPROM.update(14, BootMode);
  508.  
  509. client.print(F("BMOK")); // On reponds a JEEDOM
  510. jeedom+=F("&REP=BMOK");
  511. ProbeNextSend=millis()+3000; // Décalage pour laisser le temps au differents parametrages d'arriver de Jeedom
  512. }
  513. #if (UseHCSR04 == 1)
  514. else if (c[0]=='T' && c[n]=='E') // Trigger pin + pin Echo pour le support du HC-SR04 (ex: T0203E)
  515. {
  516. if (n==5) // Petite securite
  517. {
  518. client.print(F("SOK")); // On reponds a JEEDOM
  519. jeedom+=F("&REP=SOK");
  520. ProbeNextSend=millis()+10000; // Décalage pour laisser le temps aux differents parametrages d'arriver de Jeedom
  521.  
  522. for (int i = 1; i < n; i++)
  523. {
  524. if (isDigit(c[i])) c[i]=c[i]-'0';
  525. }
  526. pin_id=10*int(c[1])+int(c[2]); // recuperation du numero de la pin trigger
  527. echo_pin=10*int(c[3])+int(c[4]); // recuperation du numero de la pin echo
  528.  
  529. digitalWrite(pin_id, HIGH); // impulsion de 10us pour demander la mesure au HC-SR04
  530. delayMicroseconds(10);
  531. digitalWrite(pin_id, LOW);
  532. long distance = pulseIn(echo_pin, HIGH); // attente du retour de la mesure (en us) - timeout 1s
  533. distance = distance * 0.034 / 2; // conversion en distance (cm). NOTE : V=340m/s, fluctue en foncion de la temperature
  534. // on envoi le resultat a jeedom
  535. jeedom += '&';
  536. jeedom += echo_pin;
  537. jeedom += '=';
  538. jeedom += distance;
  539. }
  540. }
  541. #endif
  542. #if (UseLCD16x2 == 1 || UseLCD16x2 == 2)
  543. else if (c[0]=='S' && c[n]=='M') // Send Message to LCD
  544. {
  545. client.print(F("SMOK")); // On reponds a JEEDOM
  546. jeedom+=F("&REP=SMOK");
  547. ProbeNextSend=millis()+10000; // Décalage pour laisser le temps aux differents parametrages d'arriver de Jeedom
  548.  
  549. //pin_id=10*int(c[1]-'0')+int(c[2]-'0');
  550. lcd.clear();
  551. Message = "";
  552. int i = 3; // Normal, utilise dans les 2x FOR
  553. for (i; i < n; i++) //S17Title|MessageM >>> S 17 Title | Message M // Title & Message <16chars chacun
  554. {
  555. if (c[i] == '|') break;
  556. Message += (char)c[i];
  557. }
  558. lcd.setCursor(0,0); // Title
  559. lcd.print(Message);
  560. i++;
  561. Message = "";
  562. for (i; i < n; i++)
  563. {
  564. Message += (char)c[i];
  565. }
  566. lcd.setCursor(0,1); // Message
  567. lcd.print(Message);
  568. }
  569. #endif
  570. #if (UseWS2811 == 1)
  571. else if (c[0]=='C' && c[n]=='R') // COLOR : C09LFF00FFR ou C09M12R pin 09 color L or effect M
  572. {
  573. for (int i = 1; i < n; i++)
  574. {
  575. if (isDigit(c[i])) c[i]=c[i]-'0';
  576. if ((c[i] >= 'A') && (c[i] <= 'F')) c[i]=c[i] - 'A' + 10; // For hex
  577. }
  578. if (c[3]=='M')
  579. {
  580. client.print(F("SOK")); // On reponds a JEEDOM avant le TIMEOUT
  581. pinTempo = 10 * int(c[4]) + int(c[5]);
  582. #if (DEBUGtoSERIAL == 1)
  583. Serial.print(F("\startShow: "));
  584. Serial.println(pinTempo);
  585. #endif
  586. startShow(pinTempo);
  587. }
  588. else if (c[3]=='L')
  589. {
  590. client.print(F("SOK")); // On reponds a JEEDOM avant le TIMEOUT
  591. if (n == 10) // Petite securite
  592. {
  593. uint8_t r = 16 * int(c[4]) + int(c[5]);
  594. uint8_t g = 16 * int(c[6]) + int(c[7]);
  595. uint8_t b = 16 * int(c[8]) + int(c[9]);
  596. #if (DEBUGtoSERIAL == 1)
  597. Serial.print(F("\R: "));
  598. Serial.println(r);
  599. Serial.print(F("\G: "));
  600. Serial.println(g);
  601. Serial.print(F("\B: "));
  602. Serial.println(b);
  603. #endif
  604. for(uint16_t z = 0; z < strip.numPixels(); z++)
  605. {
  606. strip.setPixelColor(z, r, b, g);
  607. }
  608. strip.show();
  609. }
  610. }
  611. else client.print(F("NOK")); // On reponds a JEEDOM
  612. }
  613. #endif
  614. #if (UserSketch == 1)
  615. else if (c[0]=='U' && c[n]=='R') // UseR Action
  616. {
  617. client.print(F("SOK")); // On reponds a JEEDOM
  618. UserAction();
  619. }
  620. #endif
  621.  
  622. else
  623. {
  624. client.print(F("NOK")); // On reponds a JEEDOM
  625. jeedom+=F("&REP=NOK");
  626. }
  627. }
  628. }
  629. client.stop();
  630. // On ecoute les pins en entree
  631. //jeedom="";
  632. for (int i = 2; i < NB_TOTALPIN; i++)
  633. {
  634. byte BPvalue = 0;
  635. switch (Status_pins[i])
  636. {
  637. case 'i': // input
  638. case 'p': // input_pullup
  639. PinValue = digitalRead(i);
  640. if (PinValue!=OLDPinValue[i] && (PinNextSend[i]<millis() || NextRefresh<millis()))
  641. {
  642. OLDPinValue[i]=PinValue;
  643. jeedom += '&';
  644. jeedom += i;
  645. jeedom += '=';
  646. jeedom += PinValue;
  647. PinNextSend[i]=millis()+1000; // Delai pour eviter trop d'envois
  648. }
  649. break;
  650. case 'n': // BP_input_pulldown
  651. BPvalue = 1;
  652. case 'q': // BP_input_pullup
  653. PinValue = digitalRead(i);
  654. if (PinValue != OLDPinValue[i])
  655. {
  656. PinNextSend[i] = millis() + 50; // Delai antirebond
  657. OLDPinValue[i] = PinValue;
  658. ProbeNextSend = millis() + 5000; // decale la lecture des sondes pour eviter un conflit
  659. }
  660. if (PinNextSend[i] < millis() && PinValue != swtch[i])
  661. {
  662. if (PinValue == BPvalue) CounterPinValue[i] += 1;
  663. OLDAnalogPinValue[i] = millis() + 250; // Delai entre clicks
  664. swtch[i] = PinValue;
  665. }
  666. if (OLDAnalogPinValue[i] < millis() && CounterPinValue[i] != 0)
  667. {
  668. if (PinValue == BPvalue) CounterPinValue[i] = 99; // Appui long
  669. jeedom += '&';
  670. jeedom += i;
  671. jeedom += '=';
  672. jeedom += CounterPinValue[i];
  673. CounterPinValue[i] = 0;
  674. OLDAnalogPinValue[i] = millis() + 1000;
  675. }
  676. break;
  677. #if (UsePwm_input == 1)
  678. case 'g': // input_variable suivant tempo
  679. PinValue = digitalRead(i);
  680. // Calcul
  681. if (PinNextSend[i]>millis()) // bouton laché avant les 10s
  682. {
  683. pinTempo=255-((PinNextSend[i]-millis())*255/10000); // pas de 25.5 par seconde
  684. }
  685. else pinTempo=255; // si bouton laché après les 10s, on bloque la valeur a 255
  686.  
  687. if (PinValue!=OLDPinValue[i]) // changement état entrée = bouton appuyé ou bouton relaché
  688. {
  689. OLDPinValue[i]=PinValue;
  690. if (swtch[i]==1) // on vient de lacher le bouton.
  691. {
  692. swtch[i]=0; // on enregistre le laché.
  693. jeedom += '&';
  694. jeedom += i;
  695. jeedom += '=';
  696. jeedom += pinTempo;
  697. PinNextSend[i]=millis();
  698. }
  699. else
  700. {
  701. swtch[i]=1; // on vient d'appuyer sur le bouton, on enregistre.
  702. PinNextSend[i]=millis()+10000; // Delai pour la tempo de maintient du bouton.
  703. CounterPinValue[i]==millis(); // reutilisation pour economie de ram
  704. ProbeNextSend=millis()+15000; // decale la lecture des sondes pour eviter un conflit
  705. }
  706. }
  707. else
  708. {
  709. if (swtch[i]==1 && CounterPinValue[i]<millis())
  710. {
  711. jeedom += '&';
  712. jeedom += i;
  713. jeedom += '=';
  714. jeedom += pinTempo;
  715. CounterPinValue[i]==millis()+1000; // reactualisation toutes les secondes pour ne pas trop charger Jeedom
  716. }
  717. }
  718. break;
  719. #endif
  720. case 'a': // analog_input
  721. AnalogPinValue = analogRead(i);
  722. if (AnalogPinValue!=OLDAnalogPinValue[i] && (PinNextSend[i]<millis() || NextRefresh<millis()))
  723. {
  724. if (abs(AnalogPinValue-OLDAnalogPinValue[i])>20) // delta correctif pour eviter les changements negligeables
  725. {
  726. int j=i;
  727. if (i<54) j=i+40; // petit correctif car dans Jeedom toutes les pins Analog commencent a l'id 54+
  728. OLDAnalogPinValue[i]=AnalogPinValue;
  729. //jeedom += '&' + j + '=' + AnalogPinValue;
  730. jeedom += '&';
  731. jeedom += j;
  732. jeedom += '=';
  733. jeedom += AnalogPinValue;
  734. PinNextSend[i]=millis()+5000; // Delai pour eviter trop d'envois
  735. }
  736. }
  737. break;
  738. case 'c': // compteur_pullup CounterPinValue
  739. PinValue = digitalRead(i);
  740. if (PinValue!=OLDPinValue[i])
  741. {
  742. OLDPinValue[i]=PinValue;
  743. CounterPinValue[i]+=PinValue;
  744. }
  745. if (NextRefresh<millis() || PinNextSend[i]<millis())
  746. {
  747. jeedom += '&';
  748. jeedom += i;
  749. jeedom += '=';
  750. jeedom += CounterPinValue[i];
  751. PinNextSend[i]=millis()+10000; // Delai 10s pour eviter trop d'envois
  752. }
  753. break;
  754. #if (UseDHT == 1)
  755. case 'd': // DHT11
  756. case 'e': // DHT21
  757. case 'f': // DHT22
  758. if (PinNextSend[i]<millis() and ProbeNextSend<millis())
  759. {
  760. jeedom += '&';
  761. jeedom += i;
  762. jeedom += '=';
  763. jeedom += int (myDHT[i]->readTemperature()*100);
  764. jeedom += '&';
  765. jeedom += i+1000;
  766. jeedom += '=';
  767. jeedom += int (myDHT[i]->readHumidity()*100);
  768. PinNextSend[i]=millis()+300000; // Delai 60s entre chaque mesures pour eviter trop d'envois
  769. ProbeNextSend=millis()+10000; // Permet de decaler la lecture entre chaque sonde DHT sinon ne marche pas cf librairie (3000 mini)
  770. //jeedom += F("&FREERAM=");
  771. //jeedom += freeRam();
  772. }
  773. break;
  774. #endif
  775. #if (UseDS18x20 == 1)
  776. case 'b': // DS18x20
  777. if (PinNextSend[i]<millis() and ProbeNextSend<millis())
  778. {
  779. jeedom += '&';
  780. jeedom += i;
  781. jeedom += '=';
  782. jeedom += read_DSx(i); // DS18x20
  783. PinNextSend[i]=millis()+300000; // Delai 60s entre chaque mesures pour eviter trop d'envois
  784. ProbeNextSend=millis()+10000; // Permet de laisser du temps pour les commandes 'action', probabilite de blocage moins grande idem^^
  785. }
  786. break;
  787. #endif
  788. #if (UseTeleInfo == 1)
  789. case 'j': // teleinfoRX
  790. if (PinNextSend[i]<millis() || NextRefresh<millis())
  791. {
  792. #if (DEBUGtoSERIAL == 1)
  793. Serial.print(F("\nTeleinfoRX ("));
  794. Serial.print(i);
  795. Serial.print(F(") : "));
  796. #endif
  797. char recu = 0;
  798. int cntChar=0;
  799. timeout = millis()+2000; // 2s
  800. while (recu != 0x02 and timeout>millis())
  801. {
  802. if (teleinfo.available()) recu = teleinfo.read() & 0x7F;
  803. /* #if (DEBUGtoSERIAL == 1)
  804. Serial.print(recu);
  805. #endif */
  806. }
  807. jeedom += F("&ADCO=");
  808. timeout = millis()+2000; // 2s
  809. while (timeout>millis())
  810. {
  811. if (teleinfo.available())
  812. {
  813. recu = teleinfo.read() & 0x7F;
  814. /* #if (DEBUGtoSERIAL == 1)
  815. Serial.print(recu);
  816. #endif */
  817. cntChar++;
  818. if (cntChar > 280) break;
  819. if (recu == 0) break;
  820. if (recu == 0x04) break; // EOT
  821. if (recu == 0x03) break; // permet d'eviter ce caractere dans la chaine envoyée (economise du code pour le traiter)
  822. if (recu == 0x0A) continue; // Debut de groupe
  823. if (recu == 0x0D)
  824. {
  825. jeedom += ';'; // Fin de groupe
  826. continue;
  827. }
  828. if (recu<33)
  829. {
  830. jeedom += '_';
  831. }
  832. else jeedom += recu;
  833. }
  834. }
  835. #if (DEBUGtoSERIAL == 1)
  836. Serial.println(F("/finRX"));
  837. #endif
  838. PinNextSend[i]=millis()+30000; // Delai 30s entre chaque mesures pour eviter trop d'envois
  839. }
  840. break;
  841. #endif
  842. #if (UseBMP180 == 1)
  843. case 'r': // BMP085/180
  844. if (PinNextSend[i]<millis())
  845. {
  846. jeedom += '&';
  847. jeedom += i;
  848. jeedom += '=';
  849. jeedom += bmp.readTemperature();
  850. jeedom += '&';
  851. jeedom += i + 1000;
  852. jeedom += '=';
  853. jeedom += bmp.readPressure();
  854. PinNextSend[i] = millis() + 60000; // Delai 60s entre chaque mesures pour eviter trop d'envois
  855. }
  856. break;
  857. #endif
  858.  
  859. }
  860. }
  861. if (NextRefresh<millis())
  862. {
  863. NextRefresh=millis()+60000; // Refresh auto toutes les 60s
  864. if (RepByJeedom) // sert a verifier que jeedom a bien repondu a la demande dans Load_eeprom
  865. {
  866. jeedom += F("&ASK=1"); // Sinon on redemande
  867. }
  868. }
  869.  
  870. #if (UserSketch == 1)
  871. UserLoop(); // Appel de votre loop() permanent
  872. // if (NextRefresh<millis()) UserLoop(); // Appel de votre loop() toutes les 60s
  873. #endif
  874.  
  875. /* #if (UseLCD16x2 == 1 || UseLCD16x2 == 2)
  876. lcd.setCursor(0,1);
  877. lcd.print(jeedom);
  878. #endif */
  879.  
  880. if (jeedom != "") SendToJeedom();
  881. }
  882. //// User Loop + Action
  883. #if (UserSketch == 1)
  884. void UserLoop()
  885. {
  886. // Votre loop()
  887. // pour envoyer une valeur a jeedom, il suffit de remplir la variable jeedom comme cela :
  888. // jeedom += '&';
  889. // jeedom += u; // avec u = numero de la pin "info" dans l'equipement jeedom - info pin number
  890. // jeedom += '=';
  891. // jeedom += info; // la valeur a envoyer - info value to send
  892. //
  893. // Ex:
  894. // jeedom += '&';
  895. // jeedom += 500; // Etat pin 500
  896. // jeedom += '=';
  897. // jeedom += '1'; // '0' ou '1'
  898. //
  899. // jeedom += '&';
  900. // jeedom += 504; // pin 504
  901. // jeedom += '=';
  902. // jeedom += millis(); // valeur numerique
  903. //
  904. // jeedom += '&';
  905. // jeedom += 506; // pin 506
  906. // jeedom += '=';
  907. // jeedom += "Jeedouino%20speaking%20to%20Jeedom..."; // valeur string
  908.  
  909. // /!\ attention de ne pas mettre de code bloquant (avec trop de "delays") - max time 2s
  910. }
  911. void UserAction()
  912. {
  913. // Ens cas d'une reception d'une commande user action depuis jeedom
  914. // c[0]='U' & c[n]='R')
  915. //
  916. // c[1] = c[1]-'0'; ==5 (user pin start at 500)
  917. // c[2] = c[2]-'0';
  918. // c[3] = c[3]-'0';
  919. // pin_id = 100 * int(c[1]) + 10 * int(c[2]) + int(c[3]); // pin action number
  920. //
  921. // c[4] to c[n-1] // pin action value
  922. //
  923. // Ex:
  924. // U5000R -> U 500 0 R = binary 0 pin 500
  925. // U5001R -> U 500 1 R = binary 1 pin 500
  926. // U502128R -> U 502 128 R = Slider Value 128 pin 502
  927. // U507[Jeedom] Message|Ceci est un testR -> U 507 [Jeedom] Message | Ceci est un test R = Message pin 507
  928.  
  929. // /!\ attention de ne pas mettre de code bloquant (avec trop de "delays") - max time 2s
  930. }
  931. #endif
  932.  
  933. // FONCTIONS
  934.  
  935. void SendToJeedom()
  936. {
  937. EthernetClient JEEDOMclient = server.available();
  938. #if (DEBUGtoSERIAL == 1)
  939. Serial.print(F("\nSending: "));
  940. Serial.println(jeedom);
  941. Serial.print(F("To eqLogic: "));
  942. Serial.println(eqLogic);
  943. #endif
  944. int J=JEEDOMclient.connect(IP_JEEDOM, 80);
  945. if (J)
  946. {
  947. JEEDOMclient.print(F("GET /plugins/jeedouino/core/php/Callback.php?BoardEQ="));
  948. JEEDOMclient.print(eqLogic);
  949. JEEDOMclient.print(jeedom);
  950. JEEDOMclient.println(F(" HTTP/1.1"));
  951. JEEDOMclient.print(F("Host: "));
  952. JEEDOMclient.print(IP_JEEDOM[0]);
  953. JEEDOMclient.print('.');
  954. JEEDOMclient.print(IP_JEEDOM[1]);
  955. JEEDOMclient.print('.');
  956. JEEDOMclient.print(IP_JEEDOM[2]);
  957. JEEDOMclient.print('.');
  958. JEEDOMclient.println(IP_JEEDOM[3]);
  959. delay(111);
  960. JEEDOMclient.println(F("Connection: close"));
  961. JEEDOMclient.println();
  962. delay(111);
  963. JEEDOMclient.stop();
  964. #if (DEBUGtoSERIAL == 1)
  965. Serial.print(F("At IP: "));
  966. Serial.print(IP_JEEDOM[0]);
  967. Serial.print('.');
  968. Serial.print(IP_JEEDOM[1]);
  969. Serial.print('.');
  970. Serial.print(IP_JEEDOM[2]);
  971. Serial.print('.');
  972. Serial.println(IP_JEEDOM[3]);
  973. #endif
  974.  
  975. UIPEFailTime = millis();
  976. UIPEFailCount = 0;
  977. }
  978. else
  979. {
  980. JEEDOMclient.stop();
  981. UIPEFailCount++;
  982. #if (DEBUGtoSERIAL == 1)
  983. Serial.print(F("connection failed : "));
  984. Serial.println(J);
  985. Serial.print(F("UIPEFailCount : "));
  986. Serial.println(UIPEFailCount);
  987. #endif
  988. if (UIPEFailCount>10 and millis()>UIPEFailTime+60000)
  989. {
  990. #if (DEBUGtoSERIAL == 1)
  991. Serial.println(F("Waiting 10s & reboot if wdg"));
  992. #endif
  993. delay(10000); // tentative soft pour laisser le temps a la lib de se resaisir
  994. #if (UseWatchdog == 1)
  995. wdt_enable(WDTO_15MS); // try reboot
  996. #endif
  997. delay(20000); // tentative soft pour laisser le temps a la lib de se resaisir
  998. JEEDOMclient.stop();
  999. Ethernet.begin(mac, IP_ARDUINO);
  1000. server.begin();
  1001. UIPEFailTime = millis()+60000;
  1002. delay(999);
  1003. }
  1004. }
  1005. jeedom="";
  1006. delay(444);
  1007. //JEEDOMclient.stop();
  1008. }
  1009.  
  1010. void Set_OutputPin(int i)
  1011. {
  1012. TempoPinHIGH[i]=0;
  1013. TempoPinLOW[i]=0;
  1014.  
  1015. switch (Status_pins[i])
  1016. {
  1017. #if (UseServo == 1)
  1018. case 'x':
  1019. pinTempo = 100 * int(c[3]) + 10 * int(c[4]) + int(c[5]);
  1020. myServo[i].write(pinTempo);
  1021. delay(15);
  1022. break;
  1023. #endif
  1024. case 'o': // output // S131S pin 13 set to 1 (ou S130S pin 13 set to 0)
  1025. case 'l': // low_relais // S13S pin 13 set to 0
  1026. case 'h': // high_relais // S13S pin 13 set to 1
  1027. if (c[3]==0)
  1028. {
  1029. PinWriteLOW(i);
  1030. }
  1031. else
  1032. {
  1033. PinWriteHIGH(i);
  1034. }
  1035. break;
  1036.  
  1037. case 's': // switch // S13 pin 13 set to 1 si 0 sinon set to 0 si 1
  1038. if (swtch[i]==1)
  1039. {
  1040. PinWriteLOW(i);
  1041. }
  1042. else
  1043. {
  1044. PinWriteHIGH(i);
  1045. }
  1046. break;
  1047.  
  1048. //
  1049. // ON VERIFIE SI UNE TEMPORISATION EST DEMANDEE SUR UNE DES SORTIES
  1050. // On essai d'etre sur une precision de 0.1s mais ca peut fluctuer en fonction de la charge cpu
  1051. // Testé seulement sur mega2560
  1052. //
  1053. case 'u': // output_pulse // Tempo ON : S1309999S : pin 13 set to 0 during 999.9 seconds then set to 1 (S1319999 : set to 1 then to 0)
  1054. pinTempo=10000*int(c[4])+1000*int(c[5])+100*int(c[6])+10*int(c[7])+int(c[8]);
  1055. // pinTempo est donc en dixieme de seconde
  1056. pinTempo = pinTempo*100+millis(); // temps apres lequel la pin doit retourner dans l'autre etat.
  1057.  
  1058. // Peut buguer quand millis() arrive vers 50jours si une tempo est en cours pendant la remise a zero de millis().
  1059. // Risque faible si les tempo sont de l'ordre de la seconde (impulsions sur relais par ex.).
  1060. if (c[3]==0)
  1061. {
  1062. TempoPinHIGH[i]=pinTempo;
  1063. PinWriteLOW(i);
  1064. }
  1065. else if (c[3]==1)
  1066. {
  1067. TempoPinLOW[i]=pinTempo;
  1068. PinWriteHIGH(i);
  1069. }
  1070. break;
  1071.  
  1072. case 'v': // low_pulse // Tempo ON : S139999S : pin 13 set to 0 during 999.9 seconds then set to 1
  1073. if (c[3]==0)
  1074. {
  1075. pinTempo=10000*int(c[4])+1000*int(c[5])+100*int(c[6])+10*int(c[7])+int(c[8]);
  1076. // pinTempo est donc en dixieme de seconde
  1077. pinTempo = pinTempo*100+millis(); // temps apres lequel la pin doit retourner dans l'autre etat.
  1078.  
  1079. TempoPinHIGH[i]=pinTempo;
  1080. PinWriteLOW(i);
  1081. }
  1082. else
  1083. {
  1084. PinWriteHIGH(i);
  1085. }
  1086. break;
  1087.  
  1088. case 'w': // high_pulse // Tempo ON : S139999S : pin 13 set to 1 during 999.9 seconds then set to 0
  1089. if (c[3]==0)
  1090. {
  1091. PinWriteLOW(i);
  1092. }
  1093. else
  1094. {
  1095. pinTempo=10000*int(c[4])+1000*int(c[5])+100*int(c[6])+10*int(c[7])+int(c[8]);
  1096. // pinTempo est donc en dixieme de seconde
  1097. pinTempo = pinTempo*100+millis(); // temps apres lequel la pin doit retourner dans l'autre etat.
  1098.  
  1099. TempoPinLOW[i]=pinTempo;
  1100. PinWriteHIGH(i);
  1101. }
  1102. break;
  1103.  
  1104. case 'm': // pwm_output
  1105. pinTempo=100*int(c[3])+10*int(c[4])+int(c[5]); // the duty cycle: between 0 (always off) and 255 (always on).
  1106. analogWrite(i, pinTempo);
  1107. break;
  1108. }
  1109. }
  1110.  
  1111. void Load_EEPROM(int k)
  1112. {
  1113. // on recupere le BootMode
  1114. BootMode=EEPROM.read(14);
  1115. // Recuperation de l'eqLogic
  1116. eqLogic = "";
  1117. n=EEPROM.read(15); // Recuperation de la longueur du eqLogic
  1118. if (n>0) // bug probable si eqLogic_id<10 dans jeedom
  1119. {
  1120. for (int i = 1; i < n; i++)
  1121. {
  1122. eqLogic += EEPROM.read(15+i);
  1123. }
  1124. }
  1125. // Recuperation de l'IP
  1126. IP_JEEDOM[0]=EEPROM.read(26);
  1127. IP_JEEDOM[1]=EEPROM.read(27);
  1128. IP_JEEDOM[2]=EEPROM.read(28);
  1129. IP_JEEDOM[3]=EEPROM.read(29);
  1130.  
  1131. // on met en place le mode des pins
  1132. jeedom="";
  1133. byte y=1;
  1134. #if (UseTeleInfo == 1)
  1135. teleinfoRX = 0;
  1136. teleinfoTX = 0;
  1137. #endif
  1138. #if (DEBUGtoSERIAL == 1)
  1139. Serial.println(F("Conf. Pins:"));
  1140. for (int i = 0; i < NB_TOTALPIN; i++) Serial.print((char)EEPROM.read(30+i));
  1141. Serial.println();
  1142. #endif
  1143. for (int i = 2; i < NB_TOTALPIN; i++)
  1144. {
  1145. Status_pins[i] = EEPROM.read(30+i); // Etats des pins
  1146.  
  1147. // INITIALISATION DES TABLEAUX DE TEMPO SORTIES
  1148. TempoPinHIGH[i] = 0;
  1149. TempoPinLOW[i] = 0;
  1150. //
  1151. switch (Status_pins[i])
  1152. {
  1153. case 'i': // input
  1154. case 'a': // analog_input
  1155. case 'n': // BP_input_pulldown
  1156. pinMode(i, INPUT);
  1157. break;
  1158. #if (UseTeleInfo == 1)
  1159. case 'j': // teleinfoRX pin
  1160. teleinfoRX = i;
  1161. pinMode(i, INPUT);
  1162. break;
  1163. case 'k': // teleinfoTX pin
  1164. teleinfoTX = i;
  1165. pinMode(i, OUTPUT);
  1166. break;
  1167. #endif
  1168. #if (UseDHT == 1)
  1169. case 'd': // DHT11
  1170. myDHT[i] = new DHT(i, 11); // DHT11
  1171. PinNextSend[i]=millis()+300000;
  1172. break;
  1173. case 'e': // DHT21
  1174. myDHT[i] = new DHT(i, 21); // DHT21
  1175. PinNextSend[i]=millis()+300000;
  1176. break;
  1177. case 'f': // DHT 22
  1178. myDHT[i] = new DHT(i, 22); // DHT22
  1179. PinNextSend[i]=millis()+300000;
  1180. break;
  1181. #endif
  1182. #if (UseDS18x20 == 1)
  1183. case 'b': // DS18x20
  1184. PinNextSend[i]=millis()+300000;
  1185. break;
  1186. #endif
  1187. #if (UseServo == 1)
  1188. case 'x':
  1189. myServo[i].attach(i);
  1190. break;
  1191. #endif
  1192. case 't': // trigger pin
  1193. pinMode(i, OUTPUT);
  1194. digitalWrite(i, LOW);
  1195. break;
  1196. case 'z': // echo pin
  1197. pinMode(i, INPUT);
  1198. break;
  1199. case 'p': // input_pullup
  1200. case 'g': // pwm_input
  1201. case 'q': // BP_input_pullup
  1202. pinMode(i, INPUT_PULLUP); // pour eviter les parasites en lecture, mais inverse l'etat de l'entree : HIGH = input open, LOW = input closed
  1203. // Arduino Doc : An internal 20K-ohm resistor is pulled to 5V.
  1204. swtch[i]=0; // init pour pwm_input
  1205. OLDPinValue[i]=1;
  1206. PinNextSend[i]=millis();
  1207. break;
  1208. case 'c': // compteur_pullup
  1209. pinMode(i, INPUT_PULLUP); // pour eviter les parasites en lecture, mais inverse l'etat de l'entree : HIGH = input open, LOW = input closed
  1210. // Arduino Doc : An internal 20K-ohm resistor is pulled to 5V.
  1211. if (k)
  1212. {
  1213. jeedom += F("&CPT_"); // On demande à Jeedom de renvoyer la dernière valeur connue pour la pin i
  1214. jeedom += i;
  1215. jeedom += '=';
  1216. jeedom += i;
  1217. }
  1218. break;
  1219. case 'o': // output
  1220. case 's': // switch
  1221. case 'l': // low_relais
  1222. case 'h': // high_relais
  1223. case 'u': // output_pulse
  1224. case 'v': // low_pulse
  1225. case 'w': // high_pulse
  1226. pinMode(i, OUTPUT);
  1227. // restauration de l'etat des pins DIGITAL OUT au demarrage
  1228. if (k)
  1229. {
  1230. switch (BootMode)
  1231. {
  1232. case 0:
  1233. // On laisse tel quel
  1234. break;
  1235. case 1:
  1236. PinWriteLOW(i);
  1237. break;
  1238. case 2:
  1239. PinWriteHIGH(i);
  1240. break;
  1241. case 3:
  1242. PinWriteHIGH(i);
  1243. // On demande a Jeedom d'envoyer la valeur des pins
  1244. if (y)
  1245. {
  1246. jeedom += F("&ASK=1");
  1247. y=0;
  1248. RepByJeedom=1; // sert a verifier que jeedom a bien repondu a la demande
  1249. }
  1250. break;
  1251. case 4:
  1252. if (EEPROM.read(110+i) == 0) PinWriteLOW(i);
  1253. else PinWriteHIGH(i);
  1254. break;
  1255. case 5:
  1256. PinWriteLOW(i);
  1257. // On demande a Jeedom d'envoyer la valeur des pins
  1258. if (y)
  1259. {
  1260. jeedom += F("&ASK=1");
  1261. y=0;
  1262. RepByJeedom=1; // sert a verifier que jeedom a bien repondu a la demande
  1263. }
  1264. break;
  1265. }
  1266. }
  1267. // fin restauration
  1268.  
  1269. break;
  1270.  
  1271. case 'm': // pwm_output
  1272. pinMode(i, OUTPUT);
  1273. break;
  1274. }
  1275. }
  1276. #if (UseTeleInfo == 1)
  1277. if (teleinfoRX != 0)
  1278. {
  1279. #if (DEBUGtoSERIAL == 1)
  1280. Serial.print(F("\nteleinfoRX:"));
  1281. Serial.println(teleinfoRX);
  1282. Serial.print(F("\nteleinfoTX:"));
  1283. Serial.println(teleinfoTX);
  1284. #endif
  1285. //SoftwareSerial teleinfo(teleinfoRX, teleinfoTX);
  1286. }
  1287. #endif
  1288. if (jeedom!="") SendToJeedom();
  1289. }
  1290. void PinWriteHIGH(long p)
  1291. {
  1292. digitalWrite(p, HIGH);
  1293. swtch[p]=1;
  1294. jeedom += '&';
  1295. jeedom += p;
  1296. jeedom += F("=1");
  1297. // Si bootmode=4 sauvegarde de l'etat de la pin (en sortie) - !!! Dangereux pour l'eeprom à long terme !!!
  1298. if (BootMode==4) EEPROM.update(110+p, 1);
  1299. #if (DEBUGtoSERIAL == 1)
  1300. Serial.print(F("SetPin "));
  1301. Serial.print(p);
  1302. Serial.println(F(" to 1"));
  1303. #endif
  1304. }
  1305. void PinWriteLOW(long p)
  1306. {
  1307. digitalWrite(p, LOW);
  1308. swtch[p]=0;
  1309. jeedom += '&';
  1310. jeedom += p;
  1311. jeedom += F("=0");
  1312. // Si bootmode=4 sauvegarde de l'etat de la pin (en sortie) - !!! Dangereux pour l'eeprom à long terme !!!
  1313. if (BootMode==4) EEPROM.update(110+p, 0);
  1314. #if (DEBUGtoSERIAL == 1)
  1315. Serial.print(F("SetPin "));
  1316. Serial.print(p);
  1317. Serial.println(F(" to 0"));
  1318. #endif
  1319. }
  1320.  
  1321. void Init_EEPROM()
  1322. {
  1323. // Un marqueur
  1324. EEPROM.update(13, 'J'); // JEEDOUINO
  1325.  
  1326. // BootMode choisi au demarrage de l'arduino
  1327. // 0 = Pas de sauvegarde - Toutes les pins sorties non modifi�es au d�marrage.
  1328. // 1 = Pas de sauvegarde - Toutes les pins sorties mises � LOW au d�marrage.
  1329. // 2 = Pas de sauvegarde - Toutes les pins sorties mises � HIGH au d�marrage.
  1330. // 3 = Sauvegarde sur JEEDOM - Toutes les pins sorties mises suivant leur sauvegarde dans Jeedom. Jeedom requis, sinon pins mises � OFF.
  1331. // 4 = Sauvegarde sur EEPROM- Toutes les pins sorties mises suivant leur sauvegarde dans l\'EEPROM. Autonome, mais dur�e de vie de l\'eeprom fortement r�duite.
  1332. EEPROM.update(14, 2);
  1333. BootMode=2;
  1334.  
  1335. // Initialisation par default
  1336. for (int i = 30; i < 200; i++)
  1337. {
  1338. EEPROM.update(i, 1); // Valeur des pins OUT au 1er demarrage ( mes relais sont actis a 0, donc je met 1 pour eviter de les actionner au 1er boot)
  1339. }
  1340. EEPROM.update(26, IP_JEEDOM[0]); // Sauvegarde de l' IP
  1341. EEPROM.update(27, IP_JEEDOM[1]);
  1342. EEPROM.update(28, IP_JEEDOM[2]);
  1343. EEPROM.update(29, IP_JEEDOM[3]);
  1344.  
  1345. eqLogic = F("7"); // Sauvegarde de eqLogic pour 1er boot apres 1er flashage
  1346. EEPROM.update(15, 2); // Sauvegarde de la longueur du eqLogic
  1347. for (int i = 1; i < 2; i++)
  1348. {
  1349. EEPROM.update(15+i, eqLogic[i-1]-'0'); // Sauvegarde de l' eqLogic
  1350. }
  1351.  
  1352. // fin initialisation
  1353. }
  1354. //int freeRam ()
  1355. //{
  1356. // extern int __heap_start, *__brkval;
  1357. // int v;
  1358. // return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
  1359. //}
  1360. #if (UseDS18x20 == 1)
  1361. int read_DSx(int pinD)
  1362. {
  1363. byte present = 0;
  1364. byte type_s;
  1365. byte data[12];
  1366. byte addr[8];
  1367. OneWire ds(pinD);
  1368.  
  1369. if ( !ds.search(addr))
  1370. {
  1371. ds.reset_search();
  1372. #if (DEBUGtoSERIAL == 1)
  1373. Serial.println(F("ds not found..."));
  1374. #endif
  1375. delay(250);
  1376. return 0;
  1377. }
  1378.  
  1379. if (OneWire::crc8(addr, 7) != addr[7]) //Check if there is no errors on transmission
  1380. {
  1381. #if (DEBUGtoSERIAL == 1)
  1382. Serial.println(F("CRC invalide..."));
  1383. #endif
  1384. return 0;
  1385. }
  1386.  
  1387. // the first ROM byte indicates which chip
  1388. switch (addr[0])
  1389. {
  1390. case 0x10:
  1391. #if (DEBUGtoSERIAL == 1)
  1392. Serial.println(F(" Chip = DS18S20")); // or old DS1820
  1393. #endif
  1394. type_s = 1;
  1395. break;
  1396. case 0x28:
  1397. #if (DEBUGtoSERIAL == 1)
  1398. Serial.println(F(" Chip = DS18B20"));
  1399. #endif
  1400. type_s = 0;
  1401. break;
  1402. case 0x22:
  1403. #if (DEBUGtoSERIAL == 1)
  1404. Serial.println(F(" Chip = DS1822"));
  1405. #endif
  1406. type_s = 0;
  1407. break;
  1408. default:
  1409. #if (DEBUGtoSERIAL == 1)
  1410. Serial.println(F("Device is not a DS18x20 family device."));
  1411. #endif
  1412. return 0;
  1413. }
  1414.  
  1415. ds.reset();
  1416. ds.select(addr);
  1417. ds.write(0x44,1); // start conversion, with parasite power on at the end
  1418. delay(800);
  1419. present = ds.reset();
  1420. ds.select(addr);
  1421. ds.write(0xBE); // Read Scratchpad
  1422. byte ii;
  1423. for ( ii = 0; ii < 9; ii++)
  1424. { // we need 9 bytes
  1425. data[ii] = ds.read();
  1426. }
  1427.  
  1428. // convert the data to actual temperature
  1429.  
  1430. unsigned int raw = (data[1] << 8) | data[0];
  1431. if (type_s)
  1432. {
  1433. raw = raw << 3; // 9 bit resolution default
  1434. if (data[7] == 0x10)
  1435. {
  1436. // count remain gives full 12 bit resolution
  1437. raw = (raw & 0xFFF0) + 12 - data[6];
  1438. }
  1439. }
  1440. else
  1441. {
  1442. byte cfg = (data[4] & 0x60);
  1443. if (cfg == 0x00) raw = raw << 3; // 9 bit resolution, 93.75 ms
  1444. else if (cfg == 0x20) raw = raw << 2; // 10 bit res, 187.5 ms
  1445. else if (cfg == 0x40) raw = raw << 1; // 11 bit res, 375 ms
  1446.  
  1447. }
  1448. #if (DEBUGtoSERIAL == 1)
  1449. Serial.println(raw/16);
  1450. #endif
  1451. return raw;
  1452. }
  1453. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement