Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define BLYNK_PRINT Serial // Comment this out to disable prints and save space
- #include <ESP8266WiFi.h>
- #include <BlynkSimpleEsp8266.h>
- char ssid[] = "aaaaaaaaaaaa";
- char pass[] = "sssssssssssssssssssssss";
- char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
- #include <Timers.h> // my favorite timer
- Timers <4> akcja; // 4 wątki
- WidgetLED led1(V30); // vLED ESP indicator for test
- WidgetTerminal terminal31(V31); //ESP terminal for test
- WidgetLED led00(V0); // vLEDs for NANO - declaration for all vLEDs
- WidgetTerminal terminal29(V29); //terminal for NANO
- BLYNK_WRITE_DEFAULT() // send info form APP to NANO
- {
- String strtosend = "";
- int pin = request.pin; // Which exactly pin is handled?
- strtosend += 'V' + String(pin) + ':';
- for (auto i = param.begin(); i < param.end(); ++i) { //only 1 param now
- strtosend += i.asString();
- }
- Serial.println(strtosend);
- }
- void Blynkwrite(String str2) {// command from NANO to APP
- String istr = str2.substring(1, str2.indexOf(':')); // nr vPin
- byte pin2 = istr.toInt();
- String strdata2 = str2.substring(str2.indexOf(':') + 1); // if nr vPin typ int
- int data2 = strdata2.toInt();
- char chardata2[10];
- int dl = strdata2.length() + 1;
- strdata2.toCharArray(chardata2, dl);
- if (str2.charAt(0) == 'L') led00.setVPin(pin2); led00.setValue(data2); led00.setVPin(0); //send LEDs data to APP
- if (str2.charAt(0) == 'S') Blynk.virtualWrite(pin2, strdata2); //send str to APP
- if (str2.charAt(0) == 'I') Blynk.virtualWrite(pin2, data2); //send int to APP
- if (str2.charAt(0) == 'C') Blynk.setProperty(pin2, "color", chardata2); // send color
- if (str2.charAt(0) == 'N') Blynk.setProperty(pin2, "onLabel", chardata2); //send button label to APP
- if (str2.charAt(0) == 'F') Blynk.setProperty(pin2, "offLabel", chardata2); //send button label to APP
- }
- // ....................................... recive string from serial
- bool stringComplete = false;
- String inputString = "";
- byte licznikodbioru = 0;
- int startrecive = 0;
- void recivestrfromserial () {
- if (stringComplete) {
- // Serial.println("od " + String(inputString)); //test
- Blynkwrite(inputString);
- inputString = "";
- stringComplete = false;
- licznikodbioru = 0;
- startrecive = 0;
- }
- }
- void myserialEvent() { // string z Serial
- if (Serial.available()) {
- licznikodbioru++;
- char inChar = (char)Serial.read();
- if (startrecive == 0) {
- if ((inChar == 'S') || (inChar == 'I') || (inChar == 'L') || (inChar == 'C') || (inChar == 'N') || (inChar == 'F')) startrecive = 1;
- }
- if (startrecive == 1) {
- if (inChar == '\r') {
- stringComplete = true;
- inChar = '\0';
- inputString += inChar;
- startrecive = 0;
- } else inputString += inChar;
- }
- if (licznikodbioru > 50) inputString = "";
- }
- }
- void setup()
- {
- akcja.attach(0, 1500, timer1sek); // 1 sek
- Serial.begin(115200);
- WifiBlynk(); //logowanie do wifi i blynk
- } //end setup
- void loop() //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> loop
- {
- akcja.process(); //
- Blynk.run();
- myserialEvent();
- recivestrfromserial();
- }
- //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> end loop
- void timer1sek() {
- blinkvLed();
- blinkLedNANO();
- // Serial.println("ECHO ESP");
- }
- bool wsk3 = 0;
- void blinkvLed() { //test
- wsk3 = !wsk3;
- if (wsk3) led1.off(); else led1.on();
- }
- void blinkLedNANO() { //test
- String strtosend = 'V' + String(0) + ':' + String(wsk3);
- Serial.println(strtosend);
- }
- void WifiBlynk() { //logowanie do wifi i blynk nie wstrzymujące wejścia do pętli głównej
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, pass);
- Blynk.config(auth);
- WiFi.status();
- Blynk.connect();
- }
- bool isFirstConnect = true;
- BLYNK_CONNECTED() {
- if (isFirstConnect) {
- Blynk.syncAll(); //ważna procedura - po starcie arduino synchronizuje wszystkiw wirtualne piny ze zmiennymi programu
- isFirstConnect = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement