Advertisement
Krzyspx

ESP_nano_

Nov 11th, 2016
3,296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.63 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[] = "xxx";
  6. char pass[] = "yyyyyyyyyyyyyy";
  7. char auth[] = "sssssssssssssssssssssssss";
  8.  
  9. WidgetLED led1(V30); // vLED indikator ESP for test
  10. WidgetTerminal terminal31(V31); //terminal ESP for test
  11.  
  12. WidgetLED led00(V0); // vLEDs for NANO
  13. WidgetLED led01(V1); //
  14. WidgetLED led02(V2); //
  15. WidgetLED led03(V3); //
  16. WidgetLED led04(V4); //
  17.  
  18. #include <WidgetRTC.h>
  19. WidgetRTC rtc;
  20. BLYNK_ATTACH_WIDGET(rtc, V25); //rtc for NANO
  21. WidgetTerminal terminal29(V29); //terminal for NANO
  22.  
  23. BLYNK_WRITE_DEFAULT() // send info form APP to NANO
  24. {
  25.   String strtosend = "";
  26.   int pin = request.pin;      // Which exactly pin is handled?
  27.   strtosend += 'V' + String(pin) + ':';
  28.   for (auto i = param.begin(); i < param.end(); ++i) { //only 1 param now
  29.     strtosend += i.asString();
  30.   }
  31.   Serial.println(strtosend);
  32. }
  33.  
  34. void Blynkwrite(String str2) {//  command from NANO to APP
  35.   byte pin2;
  36.   String strdata2;
  37.   int data2;
  38.   if (str2.charAt(0) != 'v') pin2 = 199; else {
  39.     String istr = str2.substring(1, str2.indexOf(':')); // nr vPin
  40.     pin2 = istr.toInt();
  41.     strdata2 = str2.substring(str2.indexOf(':') + 1); // if nr vPin typ int
  42.     data2 = strdata2.toInt();
  43.   }
  44.   switch (pin2 + 1) { // Switch to progrma for recived vPin
  45.     case 1: if (data2) led00.on(); else led00.off();  break; //V0 to V4 - vLEDs
  46.     case 2: ;  break;
  47.     case 3: ;  break;
  48.     case 4: ;  break;
  49.     case 5: ;  break;
  50.     case 6: ESP_BlynkvirtualWrite_int(pin2, data2);  break;  //V5 to V24 /data int
  51.     //etc..                                                   // V25 rtc
  52.  
  53.     case 26: ESP_BlynkvirtualWrite_str(pin2, strdata2);  break; // V26 to V 28 data str
  54.                                                                
  55.     case 30: ;  break;                                           // V29 terminal
  56.     //   case 200:   Serial.println("ERROR - Brak char V");  break; //Program for error
  57.     default: ;
  58.   }
  59. }
  60.  
  61. void ESP_BlynkvirtualWrite_int(byte vPin, int intdata)
  62. {
  63.   Blynk.virtualWrite(vPin, intdata);
  64. }
  65. void ESP_BlynkvirtualWrite_str(byte vPin, String strdata)
  66. {
  67.   Blynk.virtualWrite(vPin, strdata);
  68. }
  69.  
  70. // ....................................... recive string from serial
  71. bool  stringComplete = false;
  72. String inputString = "";
  73. byte licznikodbioru = 0;
  74.  
  75. void recivestrfromserial () {
  76.   if (stringComplete) {
  77.     Serial.println("odebralem  " + String(inputString)); //test
  78.     Blynkwrite(inputString);
  79.     inputString = "";
  80.     stringComplete = false;
  81.     licznikodbioru = 0;
  82.   }
  83. }
  84.  
  85. void myserialEvent() { // string z Serial
  86.   while (Serial.available()) {
  87.     //  Serial.println("odbior");
  88.     licznikodbioru++;
  89.     char inChar = (char)Serial.read();
  90.     inputString += inChar;
  91.     if (inChar == '\n') {
  92.       stringComplete = true;
  93.     }
  94.     if (licznikodbioru > 50) inputString = "";
  95.   }
  96. }
  97.  
  98. void setup()
  99. {
  100.   Serial.begin(115200);
  101.   WifiBlynk(); //logowanie do wifi i blynk
  102. } //end setup
  103.  
  104. void loop()  //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> loop
  105. {
  106.   Blynk.run();
  107.   myserialEvent();
  108.   recivestrfromserial();
  109. }
  110. //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>  end loop
  111.  
  112. void WifiBlynk() { //logowanie do wifi i blynk nie wstrzymujące wejścia do pętli głównej
  113.   WiFi.begin(ssid, pass);
  114.   Blynk.config(auth);
  115.   WiFi.status();
  116.   Blynk.connect();
  117. }
  118.  
  119. bool isFirstConnect = true;
  120. BLYNK_CONNECTED() {
  121.   if (isFirstConnect) {
  122.     Blynk.syncAll();  //ważna procedura - po starcie arduino synchronizuje wszystkiw wirtualne piny ze zmiennymi programu
  123.     isFirstConnect = false;
  124.   }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement