Advertisement
Krzyspx

esp_NANO__2

Nov 14th, 2016
4,140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.07 KB | None | 0 0
  1. #include <Timers.h> //  my favorite timer
  2. Timers <4> akcja; // 4  wątki
  3. #define led_green 7
  4. #define led_red   8 // na D8
  5.  
  6. //>>>>>>>>>>>>>>>>>>> my Programs
  7.  
  8. void Program_vPin0(String strvPin) { // blink led from ESP timer
  9.   digitalWrite(led_red, intfromstr(strvPin));
  10. }
  11. //etc ...
  12.  
  13. void Program_vPin29(String strvPin) {
  14. }
  15.  
  16. //from APP to NANO   >>>>>>>>>>>>>>>>
  17. // equivalent BLYNK_WRITE(VPin)
  18.  
  19. void Blynkwrite(String str2) {
  20.  
  21.   String istr = str2.substring(1, str2.indexOf(':')); // nr vPin
  22.   byte pin2 = istr.toInt();
  23.   String  strdata2 = str2.substring(str2.indexOf(':') + 1); // if nr vPin typ int
  24.   int  data2 = strdata2.toInt();
  25.  
  26.   if (str2.charAt(0) == 'V') {
  27.  
  28.     switch (pin2 + 1) { // Switch to progrma for recived vPin
  29.       case 1: Program_vPin0(strdata2);  break;
  30.       // etc....
  31.       case 30: Program_vPin29(strdata2);  break;
  32.       default: ;
  33.     }
  34.   }
  35. }
  36.  
  37. // equivalent   Blynk.virtualWrite(VPin, Data);
  38.  
  39. void BlynkvirtualWrite_str (byte vPin, String z) { // send str to APP
  40.   String  str1 = 'S' + String(vPin) + ':' + z;
  41.   sendstrtoserial(str1);
  42. }
  43.  
  44. void BlynkvirtualWrite_int (byte vPin, int z)  { // send int to APP
  45.   String  str1 = 'I' + String(vPin) + ':' + String(z);
  46.   sendstrtoserial(str1);
  47. }
  48.  
  49. void BlynkvirtualWrite_led(byte vPin, byte Value) { // send LEDs data to APP
  50.   String  str1 = 'L' + String(vPin) + ':' + String(Value);
  51.   sendstrtoserial(str1);
  52. }
  53.  
  54. void BlynkvirtualWrite_col(byte vPin, String col) { // send color data to APP
  55.   String  str1 = 'C' + String(vPin) + ':' + col;
  56.   sendstrtoserial(str1);
  57. }
  58.  
  59. void BlynkvirtualWrite_On(byte vPin, String ON) { // send onLabel data to APP
  60.   String  str1 = 'N' + String(vPin) + ':' + ON;
  61.   sendstrtoserial(str1);
  62. }
  63. void BlynkvirtualWrite_Off(byte vPin, String OFF) { // send offLabel data to APP
  64.   String  str1 = 'F' + String(vPin) + ':' + OFF;
  65.   sendstrtoserial(str1);
  66. }
  67.  
  68. int intfromstr(String str) {
  69.   String    strdata3 = str.substring(str.indexOf(':') + 1); // if nr vPin typ int
  70.   return strdata3.toInt();
  71. }
  72.  
  73. // ....................................... recive string from serial
  74.  
  75. bool  stringComplete = false;
  76. String inputString = "";
  77. byte licznikodbioru = 0;
  78. int startrecive = 0;
  79.  
  80. void recivestrfromserial () {
  81.   if (stringComplete) {
  82.     Serial.println("od  " + String(inputString)); //test
  83.     Blynkwrite(inputString);
  84.     inputString = "";
  85.     stringComplete = false;
  86.     licznikodbioru = 0;
  87.     startrecive = 0;
  88.   }
  89. }
  90.  
  91. void serialEvent() { //odbiór string z Serial
  92.   if (Serial.available()) {
  93.     licznikodbioru++;
  94.     char inChar = (char)Serial.read();
  95.     if (startrecive == 0) {
  96.       if ((inChar == 'V'))  startrecive = 1;
  97.     }
  98.     if (startrecive == 1) {
  99.       if (inChar == '\r')  {
  100.         stringComplete = true;
  101.         inChar = '\0';
  102.         inputString += inChar;
  103.         startrecive = 0;
  104.       } else  inputString += inChar;
  105.     }
  106.     if (licznikodbioru > 50) inputString = "";
  107.   }
  108. }
  109.  
  110. // ................................. send string to serial
  111. void sendstrtoserial (String toserial) {
  112.   Serial.println(toserial);
  113. }
  114.  
  115. void setup()
  116. {
  117.   pinMode(led_red, OUTPUT);
  118.   pinMode(led_green, OUTPUT);
  119.   akcja.attach(0, 5000, timer1sek); //  1 sek
  120.  
  121.   Serial.begin(115200);  //
  122. }
  123. void loop()
  124. {
  125.   akcja.process(); //
  126.   recivestrfromserial();
  127. }
  128.  
  129. void timer1sek() { //test
  130.   blinkLedgreen(); // blink led on NANO
  131.   blinkvLed(); //blink vLed from NANO
  132.   blinkvLed2(); //blink vLed from NANO
  133. }
  134.  
  135. bool flagaled = 0;
  136. void blinkLedgreen() { //test
  137.   flagaled = !flagaled;
  138.   digitalWrite(led_green, flagaled);
  139. }
  140. int x = 0;
  141.  
  142. void blinkvLed() { //for test
  143.   if (flagaled)   BlynkvirtualWrite_led(3, 255); else    BlynkvirtualWrite_led(3, 0);
  144.   x++;
  145.   BlynkvirtualWrite_int(7, x);
  146.  
  147. }
  148. void blinkvLed2() { //for test
  149.   String col1 = "#FF0000";
  150.   String col2 = "#00FF00";
  151.   if (flagaled)   BlynkvirtualWrite_led(4, 255); else    BlynkvirtualWrite_led(4, 0);
  152.   if (flagaled)   BlynkvirtualWrite_col(5, col1); else    BlynkvirtualWrite_col(5, col2);
  153.   if (flagaled)   BlynkvirtualWrite_Off(5, "UP"); else    BlynkvirtualWrite_Off(5, "DOWN");
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement