Advertisement
Krzyspx

brama2 ver1

Aug 30th, 2016
5,723
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.17 KB | None | 0 0
  1. //działa!!!!
  2. /*
  3.  
  4. V1 - vLED OK
  5. v2 - vLED ERROR
  6. v5 - vLED pompa
  7. v6 - vLED pilot
  8. v7 - vLED przełącznik OnOff na A6
  9. v10 - przycisk pompa
  10. V12 - ds18b20
  11. v20 - menu wybór pilota
  12. v21 - przycisk pilota
  13. */
  14. char kod[] = "*****************b2_ds_rt_p_b_v1*******************";
  15. char auth[] = "xxxxxxxxxxxxxxxxxxx";
  16.  
  17. #define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
  18. #include <ESP8266_Lib.h>
  19. #include <BlynkSimpleShieldEsp8266.h>
  20. const int resetesp = 4;
  21.  
  22. #include <Timers.h> // ulubiona biblioteka Timers
  23. Timers <4> akcja; // n niezależnych wątków
  24.  
  25. #include <OneWire.h>
  26. #include <DS18B20.h>
  27. const byte ONEWIRE_PIN = 13; // 1-Wire bus Arduino pin
  28. byte sensorAddress[8] = {0x28, 0xFF, 0x42, 0x56, 0x52, 0x16, 0x4, 0xE0}; // // brama2 płytka test
  29. OneWire onewire(ONEWIRE_PIN); // 1-Wire object
  30. DS18B20 sensors(&onewire); // DS18B20 sensors object
  31.  
  32. #include <RemoteTransmitter.h>
  33. ElroTransmitter elroTransmitter(3);   //wy transmitera
  34.  
  35. #include <RCSwitch.h>
  36. RCSwitch mySwitch = RCSwitch(); //nie wiem co to - pewnie nadanie nazwy własnej procedurze
  37.  
  38.  
  39. #define EspSerial Serial // Set ESP8266 Serial object
  40. ESP8266 wifi(&EspSerial);
  41.  
  42. WidgetLED led1(V1); // vLED_ok
  43. WidgetLED led2(V2); // vLED_error
  44. WidgetLED led5(V5); // vLED pompa
  45. WidgetLED led6(V6); // vLED pilot
  46. WidgetLED led7(V7); // vLED przełącznikOnOff
  47.  
  48. int led_ok = 7;
  49. int led_error = 8;
  50.  
  51. const int czasgrzania = 4 ; // ilość sek włączenia pompy x 10
  52.  
  53.  
  54. //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>    procedury
  55. int flaga1s = 0;
  56. void timer1sek() //coś do zrobienia co 1 sek
  57. {
  58.   sprawdzBLYNK();
  59.   miganieLED_ok();
  60.   statusLED_error();
  61.   miganievLED();
  62.   sensorT();
  63.   transmitBLYNK();
  64.   swOnOff();
  65.  
  66.   flaga1s = 1;
  67.  
  68.  
  69. }
  70.  
  71. int stanswOnOff = HIGH;
  72. void swOnOff(){
  73.   stanswOnOff = digitalRead(A5);
  74.   if (stanswOnOff == 0) {
  75.     led7.off();
  76.   }
  77.   else {
  78.     led7.on();
  79.   }
  80. }
  81.  
  82. int stanLED_ok = HIGH;
  83. void miganieLED_ok()
  84. {
  85.   stanLED_ok = !stanLED_ok;
  86.   if (stanLED_ok == 0) {
  87.     digitalWrite(led_ok, LOW);
  88.   }
  89.   else {
  90.     digitalWrite(led_ok, HIGH);
  91.   }
  92. }
  93.  
  94. void statusLED_error()
  95. {
  96.   int s = digitalRead(led_error);
  97.   if (s == 0) {
  98.     led2.off(); //miganie vLED dla kontroli połączenia z UNO
  99.   }
  100.   else {
  101.     led2.on();
  102.   }
  103. }
  104.  
  105. int wskrunAr = 0; //vLED miga co sek
  106. void miganievLED()
  107. {
  108.   wskrunAr = !wskrunAr ;
  109.   if (wskrunAr == 0) {
  110.     led1.off(); //miganie vLED dla kontroli połączenia z UNO
  111.   }
  112.   else {
  113.     led1.on();
  114.   }
  115. }
  116.  
  117. float temperature = 0;
  118. float temperature1 = 0;
  119. int temp1 = 0;
  120. void sensorT()
  121. {
  122.   if (sensors.available())
  123.   {
  124.     temperature = sensors.readTemperature(sensorAddress);    // Reads the temperature from sensor
  125.     temperature1 = (temperature * 10);
  126.     temp1 = temperature1;
  127.     temperature1 = temp1;
  128.     temperature = (temperature1 / 10);
  129.     Serial.print(temperature);
  130.     Serial.println((" 'C"));
  131.     sensors.request(sensorAddress);
  132.   }
  133. }
  134.  
  135. int noconnection = 0;
  136. void sprawdzBLYNK()
  137. {
  138.   digitalWrite(resetesp, HIGH); //dla pewności
  139.   bool r = Blynk.connected();
  140.   if (r == 1) { //jeśli jest połączenie to zgaś LED
  141.     digitalWrite(led_error, LOW);  //zgaszenie LED gdy jest połaczenie z BLYNK server
  142.     noconnection = 0;
  143.   }
  144.   else {
  145.     digitalWrite(led_error, HIGH);
  146.     noconnection = 1;
  147.   }
  148. }
  149. void(* resetFunc) (void) = 0;//declare reset function at address 0
  150.  
  151. int czyrestart = 0;
  152. void restartArduino() { //długa pętla 10-30 sek
  153.   if (noconnection == 1) {
  154.     if (czyrestart == 1) {
  155.       resetFunc(); //call reset     //restart
  156.     } else {
  157.       czyrestart = 1;
  158.     }
  159.   }
  160. }
  161.  
  162.  
  163. void transmitBLYNK() { //procedura wysyłania do serwera BLYNK
  164.   wyslij_V12();
  165. }
  166.  
  167. void wyslij_V12() {
  168.   String str = String(temperature, 1) + " ℃";
  169.   Blynk.virtualWrite(V12, str);
  170. }
  171.  
  172. //BLYNK_READ(V12) //temperatura
  173. //{}
  174.  
  175.  
  176. int vPin20 = 0;
  177. BLYNK_WRITE(V20) //wybór pilota - menu rozwijane
  178. {
  179.   vPin20 = param.asInt();
  180. }
  181.  
  182. int ppilot = 0;
  183. int ppilotold = 0;
  184. int onoff = 0;
  185. BLYNK_WRITE(V21) //vprzycisk do pilotów on/off
  186. {
  187.   //  BLYNK_LOG("V3=%s", param.asStr());
  188.   ppilot = param.asInt();
  189. }
  190.  
  191. int grzanie = -1; //licznik czasu włączenia pompy
  192.  
  193. void licznikgrzania()
  194. {
  195.    grzanie = grzanie - 1;
  196.  if (grzanie < 0) {
  197.  grzanie =  -1;
  198.  }
  199.   else {
  200.     if (grzanie == 0) {
  201.       elroTransmitter.sendSignal(27, 'B', LOW);
  202.       //elroTransmitter.sendSignal(8, 'A', LOW);
  203.       led5.off();
  204.     }
  205.     else {
  206.       elroTransmitter.sendSignal(27, 'B', HIGH); // wł pompy powtarzane co 10 sek
  207.     }
  208.   }
  209. }
  210.  
  211. int pompaonoff = 0;
  212. int pompaonoffold = 0;
  213. BLYNK_WRITE(V10) //vprzycisk załączenia pompy
  214. {
  215.   pompaonoff = param.asInt();
  216.   if (pompaonoff == pompaonoffold) {} //sprawdzanie zmiany stanu przycisku  vPIN V10 załączenie pompy
  217. else {
  218.   grzanie = czasgrzania; //naciśniecie v przycisku generuje powójny czas uruchomienia pompy
  219.   elroTransmitter.sendSignal(27, 'B', HIGH); // wł pompy
  220.   led5.on();
  221.   pompaonoffold = pompaonoff;
  222. }
  223. }
  224.  
  225. bool isFirstConnect = true;
  226. // This function will run every time Blynk connection is established
  227. BLYNK_CONNECTED() {
  228.   if (isFirstConnect) {
  229.     Blynk.syncAll();  //ważna procedura - po starcie arduino synchronizuje wszystkiw wirtualne piny ze zmiennymi programu
  230.     isFirstConnect = false;
  231.   }
  232. }
  233.  
  234. //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>   procedury END
  235.  
  236. //+++++++++++++++++++++++++++++++++++++++++++++++++++   setup
  237. void setup()
  238. {
  239.  
  240.   pinMode(led_ok, OUTPUT); // dioda sygnalizacyjna pracy pętli 1 sek w przerwaniu
  241.   digitalWrite(led_ok, HIGH);
  242.   pinMode(led_error, OUTPUT); // dioda sygnalizacyjna pracy pętli 1 sek w przerwaniu
  243.   digitalWrite(led_error, HIGH);
  244.  
  245.   pinMode(A6, INPUT_PULLUP); //uszkodziłem
  246.   pinMode(A5, INPUT_PULLUP);
  247.   //reset ESP
  248.   pinMode(resetesp, OUTPUT);
  249.   digitalWrite(resetesp, LOW); //reset esp
  250.   digitalWrite(led_ok, LOW);
  251.   delay(2000);
  252.   digitalWrite(resetesp, HIGH);
  253.   delay(200); //stany przejściowe esp
  254.   digitalWrite(led_ok, HIGH);
  255.  
  256.  
  257.   akcja.attach(0, 1500, timer1sek); // uruchamianie timera 1 sek
  258.   akcja.attach(1, 30000, restartArduino); // pętla dłuższa 10 s reset BLYNK gdy nie ma połączenia
  259.   akcja.attach(2, 10000, licznikgrzania); // pętla dłuższa 10 s rlicznik grzania
  260.  
  261.   sensors.begin();  // DS18B20 sensors setup
  262.   sensors.request(sensorAddress);  // Requests sensor for measurement
  263.  
  264.   Serial.begin(115200);  // Set console baud rate
  265.   EspSerial.begin(115200);  // Set ESP8266 baud rate
  266.   Blynk.begin(auth, wifi, "aaa", "bbbbbbbb");
  267.   Serial.println(kod); //sygnatura
  268.   Serial.println(auth); //sygnatura2
  269.   Serial.println("Arduino.org 1.7.10"); //sygnatura
  270.  
  271.   wskrunAr = 0; //vLED
  272.   stanLED_ok = 0;
  273.  
  274.   mySwitch.enableReceive(0);  // Receiver on inerrupt 0 => that is pin #2
  275.  
  276.  
  277. } // koniec pęli setup
  278. //++++++++++++++++++++++++++++++++++++++++++++++++++++   setup END
  279.  
  280. //---------------------------------------------------- loop
  281. void loop()
  282. {
  283.   akcja.process(); // inicjalizacja lub aktualizacja wszystkich procedur(wątków, zdarzeń itp.)
  284.   Blynk.run();
  285.  
  286.   int V3_down = 0;
  287.  
  288.   if (ppilot == ppilotold) {} //sprawdzanie zmiany stanu przycisku  vPIN pilot (V3)
  289.   else {
  290.     V3_down = 1; //jest zmiana stanu klawisza vPin3
  291.     ppilotold = ppilot;
  292.   }
  293.   if ((V3_down == 1)) {
  294.     onoff = ppilot; //zapisanie stanu klawisza do wysłania kodu on/off
  295.  
  296.     switch (vPin20)  // Switch on Elro - wybór kodu pilota w zależności od ustawienia w menu rozwijanym
  297.     {
  298.       case 1:
  299.         elroTransmitter.sendSignal(8, 'A', onoff);
  300.         break;
  301.       case 2:
  302.         elroTransmitter.sendSignal(8, 'B', onoff);
  303.         break;
  304.       case 3:
  305.         elroTransmitter.sendSignal(3, 'C', onoff);
  306.         break;
  307.       case 4:
  308.         elroTransmitter.sendSignal(3, 'D', onoff);
  309.         break;
  310.       case 5:
  311.         elroTransmitter.sendSignal(15, 'D', onoff);
  312.         // Serial.println("5on");
  313.         break;
  314.       case 6:
  315.         elroTransmitter.sendSignal(27, 'D', onoff);
  316.         break;
  317.       case 7:
  318.         elroTransmitter.sendSignal(27, 'A', onoff);
  319.         break;
  320.       case 8:
  321.         elroTransmitter.sendSignal(27, 'B', onoff);
  322.         break;
  323.       case 9:
  324.         elroTransmitter.sendSignal(27, 'C', onoff);
  325.         break;
  326.       default: ;
  327.  
  328.     }
  329.     if (onoff == 1) {
  330.       led6.on(); //wysłanie potwierdzenia na vLED o on/off komendzie pilota
  331.     }
  332.     else {
  333.       led6.off();
  334.     }
  335.  
  336.     V3_down = 0;
  337.  
  338.   }
  339.  
  340.   // procedura odczytu tego co się odebralo na 433
  341.   if (mySwitch.available()) {
  342.     int value = mySwitch.getReceivedValue();
  343.     if (value == 0) {
  344.       Serial.print("Unknown encoding");
  345.     } else {
  346.       Serial.print("----------------------kod "); //testowanie co jest odbierane
  347.       Serial.println( mySwitch.getReceivedValue() );
  348.     }
  349.     if ( 14013756 == mySwitch.getReceivedValue()) {
  350.       //digitalWrite(ledPinA4, LOW); led się zapala na chwilę gdy odebrany jest własciwy kod - potrzebne w testach
  351.       grzanie = czasgrzania;
  352.       elroTransmitter.sendSignal(27, 'B', HIGH); // wł pompy
  353.       //elroTransmitter.sendSignal(8, 'A', HIGH); //wł lampy (testy)
  354.       led5.on();
  355.     }
  356.     mySwitch.resetAvailable();
  357.   }
  358.  
  359.  } //koniec loop
  360. //----------------------------------------------------- loop END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement