Advertisement
Krzyspx

ESP_nano_2

Nov 14th, 2016
4,080
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.95 KB | None | 0 0
  1.  
  2. #define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
  3. #include <ESP8266WiFi.h>
  4. #include <BlynkSimpleEsp8266.h>
  5. char ssid[] = "aaaaaaaaaaaa";
  6. char pass[] = "sssssssssssssssssssssss";
  7. char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
  8.  
  9. #include <Timers.h> //  my favorite timer
  10. Timers <4> akcja; // 4  wątki
  11.  
  12. WidgetLED led1(V30); // vLED ESP indicator for test
  13. WidgetTerminal terminal31(V31); //ESP terminal  for test
  14.  
  15. WidgetLED led00(V0); // vLEDs for NANO   - declaration for all vLEDs
  16.  
  17. WidgetTerminal terminal29(V29); //terminal for NANO
  18.  
  19. BLYNK_WRITE_DEFAULT() // send info form APP to NANO
  20. {
  21.   String strtosend = "";
  22.   int pin = request.pin;      // Which exactly pin is handled?
  23.   strtosend += 'V' + String(pin) + ':';
  24.   for (auto i = param.begin(); i < param.end(); ++i) { //only 1 param now
  25.     strtosend += i.asString();
  26.   }
  27.   Serial.println(strtosend);
  28. }
  29.  
  30. void Blynkwrite(String str2) {//  command from NANO to APP
  31.  
  32.   String istr = str2.substring(1, str2.indexOf(':')); // nr vPin
  33.   byte pin2 = istr.toInt();
  34.   String  strdata2 = str2.substring(str2.indexOf(':') + 1); // if nr vPin typ int
  35.   int  data2 = strdata2.toInt();
  36.   char chardata2[10];
  37.   int dl =  strdata2.length() + 1;
  38.   strdata2.toCharArray(chardata2, dl);
  39.  
  40.   if (str2.charAt(0) == 'L') led00.setVPin(pin2); led00.setValue(data2); led00.setVPin(0);  //send LEDs data to APP
  41.   if (str2.charAt(0) == 'S')  Blynk.virtualWrite(pin2, strdata2); //send str to APP
  42.   if (str2.charAt(0) == 'I')  Blynk.virtualWrite(pin2, data2); //send int to APP
  43.   if (str2.charAt(0) == 'C')  Blynk.setProperty(pin2, "color", chardata2);  // send color
  44.   if (str2.charAt(0) == 'N')  Blynk.setProperty(pin2, "onLabel", chardata2); //send button label to APP
  45.   if (str2.charAt(0) == 'F')  Blynk.setProperty(pin2, "offLabel", chardata2); //send button label to APP
  46. }
  47.  
  48. // ....................................... recive string from serial
  49. bool  stringComplete = false;
  50. String inputString = "";
  51. byte licznikodbioru = 0;
  52. int startrecive = 0;
  53.  
  54. void recivestrfromserial () {
  55.   if (stringComplete) {
  56.   //  Serial.println("od  " + String(inputString)); //test
  57.     Blynkwrite(inputString);
  58.     inputString = "";
  59.     stringComplete = false;
  60.     licznikodbioru = 0;
  61.     startrecive = 0;
  62.   }
  63. }
  64.  
  65. void myserialEvent() { // string z Serial
  66.   if (Serial.available()) {
  67.     licznikodbioru++;
  68.     char inChar = (char)Serial.read();
  69.     if (startrecive == 0) {
  70.       if ((inChar == 'S') || (inChar == 'I') || (inChar == 'L') || (inChar == 'C') || (inChar == 'N') || (inChar == 'F'))  startrecive = 1;
  71.     }
  72.     if (startrecive == 1) {
  73.       if (inChar == '\r')  {
  74.         stringComplete = true;
  75.         inChar = '\0';
  76.         inputString += inChar;
  77.         startrecive = 0;
  78.       } else  inputString += inChar;
  79.     }
  80.     if (licznikodbioru > 50) inputString = "";
  81.   }
  82. }
  83.  
  84. void setup()
  85. {
  86.   akcja.attach(0, 1500, timer1sek); //  1 sek
  87.   Serial.begin(115200);
  88.   WifiBlynk(); //logowanie do wifi i blynk
  89. } //end setup
  90.  
  91. void loop()  //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> loop
  92. {
  93.   akcja.process(); //
  94.   Blynk.run();
  95.   myserialEvent();
  96.   recivestrfromserial();
  97. }
  98. //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>  end loop
  99. void timer1sek() {
  100.   blinkvLed();
  101.   blinkLedNANO();
  102.   //    Serial.println("ECHO ESP");
  103. }
  104. bool wsk3 = 0;
  105. void  blinkvLed() { //test
  106.   wsk3 = !wsk3;
  107.   if (wsk3)  led1.off(); else  led1.on();
  108. }
  109.  
  110. void  blinkLedNANO() { //test
  111.   String strtosend = 'V' + String(0) + ':' + String(wsk3);
  112.    Serial.println(strtosend);
  113. }
  114.  
  115. void WifiBlynk() { //logowanie do wifi i blynk nie wstrzymujące wejścia do pętli głównej
  116.   WiFi.mode(WIFI_STA);
  117.   WiFi.begin(ssid, pass);
  118.   Blynk.config(auth);
  119.   WiFi.status();
  120.   Blynk.connect();
  121. }
  122.  
  123. bool isFirstConnect = true;
  124. BLYNK_CONNECTED() {
  125.   if (isFirstConnect) {
  126.     Blynk.syncAll();  //ważna procedura - po starcie arduino synchronizuje wszystkiw wirtualne piny ze zmiennymi programu
  127.     isFirstConnect = false;
  128.   }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement