├── README.md ├── VirtuinoESP.cpp ├── VirtuinoESP.h ├── Virtuino_ESP_WifiServer.cpp ├── Virtuino_ESP_WifiServer.h ├── _updates_info.txt ├── examples ├── Virtuino_ESP32_example │ └── Virtuino_ESP32_example.ino ├── Virtuino_ESP_text_example │ └── Virtuino_ESP_text_example.ino ├── Virtuino_NodeMCU_ESP8266_example_1 │ └── Virtuino_NodeMCU_ESP8266_example_1.ino └── Virtuino_WeMos_ESP8266_example_1 │ └── Virtuino_WeMos_ESP8266_example_1.ino └── library.properties /README.md: -------------------------------------------------------------------------------- 1 | # virtuinoESP 2 | VirtuinoESP library ver 1.8.0 for ESP8266 and ESP32 boards 3 | -------------------------------------------------------------------------------- /VirtuinoESP.cpp: -------------------------------------------------------------------------------- 1 | /* Virtuino general ESP web server Library ver 1.8.0 2 | * Created by Ilias Lamprou 3 | * Updated Aug 16 2019 4 | * Download latest Virtuino android app from the link: https://play.google.com/store/apps/details?id=com.virtuino_automations.virtuino 5 | */ 6 | 7 | 8 | //========= VirtuinoEsp8266 Class methods 9 | 10 | //========= Virtuino general methods 11 | // void vDigitalMemoryWrite(int digitalMemoryIndex, int value) write a value to a Virtuino digital memory (digitalMemoryIndex=0..31, value range = 0 or 1) 12 | // int vDigitalMemoryRead(int digitalMemoryIndex) read the value of a Virtuino digital memory (digitalMemoryIndex=0..31, returned value range = 0 or 1) 13 | // void vMemoryWrite(int memoryIndex, float value); write a value to Virtuino memory (memoryIndex=0..31, value range as float value) 14 | // float vMemoryRead(int memoryIndex); read a value of Virtuino memory (memoryIndex=0..31, returned a float value 15 | // int getPinValue(int pin); read the value of a Pin. Usefull to read the value of a PWM pin 16 | // long lastCommunicationTime; Stores the last communication with Virtuino time 17 | 18 | #include "VirtuinoESP.h" 19 | 20 | 21 | 22 | 23 | //======================================================================================== wifiRun() 24 | //======================================================================================== 25 | //======================================================================================== 26 | 27 | 28 | VirtuinoESP::VirtuinoESP(){} 29 | 30 | 31 | 32 | 33 | //====================================================================================== 34 | void VirtuinoESP::checkVirtuinoCommand(String* command){ 35 | int pos=command->lastIndexOf(esp_COMMAND_END_CHAR); 36 | if (pos>5){ // check the size of command 37 | command->remove(pos+1); // clear the end of command buffer 38 | //------------------ get command password 39 | String commandPassword=""; 40 | int k=command->indexOf(esp_COMMAND_START_CHAR); 41 | if (k>0) { 42 | commandPassword = command->substring(0,k); 43 | command->remove(0,k); // clear the start part of the command buffer 44 | } 45 | responseBuffer=""; 46 | if ((password.length()==0) || (commandPassword.equals(password))) { // check if password correct 47 | while (command->indexOf(esp_COMMAND_START_CHAR)>=0){ 48 | int cStart=command->indexOf(esp_COMMAND_START_CHAR); 49 | int cEnd=command->indexOf(esp_COMMAND_END_CHAR); 50 | if (cEnd-cStart>5){ 51 | String oneCommand = command->substring(cStart+1,cEnd); // get one command 52 | char commandType = getCommandType(oneCommand.charAt(0)); 53 | if (commandType!='E') { 54 | int pin= getCommandPin(&oneCommand); 55 | if (pin!=-1){ 56 | boolean returnInfo=false; 57 | if (oneCommand.charAt(4)=='?') returnInfo=true; 58 | oneCommand.remove(0,4); 59 | if (oneCommand.length()remove(0,cEnd+1-cStart); 78 | } // while 79 | } else responseBuffer+=getErrorCommand(esp_ERROR_PASSWORD); // the password is not correct 80 | } 81 | else responseBuffer+=getErrorCommand(esp_ERROR_SIZE); // the size of command is not correct 82 | 83 | 84 | 85 | } 86 | 87 | 88 | //================================================================================== urlencode 89 | //================================================================================== 90 | String VirtuinoESP::urlencode(String* str){ 91 | String encodedString=""; 92 | char c; 93 | char code0; 94 | char code1; 95 | char code2; 96 | for (int i =0; i < str->length(); i++){ 97 | c=str->charAt(i); 98 | if (c == ' '){ 99 | encodedString+= '+'; 100 | } else if (isalnum(c)){ 101 | encodedString+=c; 102 | } else{ 103 | code1=(c & 0xf)+'0'; 104 | if ((c & 0xf) >9){ 105 | code1=(c & 0xf) - 10 + 'A'; 106 | } 107 | c=(c>>4)&0xf; 108 | code0=c+'0'; 109 | if (c > 9){ 110 | code0=c - 10 + 'A'; 111 | } 112 | code2='\0'; 113 | encodedString+='%'; 114 | encodedString+=code0; 115 | encodedString+=code1; 116 | //encodedString+=code2; 117 | } 118 | yield(); 119 | } 120 | return encodedString; 121 | 122 | } 123 | 124 | //================================================================================== h2int 125 | //================================================================================== 126 | unsigned char VirtuinoESP::h2int(char c){ 127 | if (c >= '0' && c <='9'){ 128 | return((unsigned char)c - '0'); 129 | } 130 | if (c >= 'a' && c <='f'){ 131 | return((unsigned char)c - 'a' + 10); 132 | } 133 | if (c >= 'A' && c <='F'){ 134 | return((unsigned char)c - 'A' + 10); 135 | } 136 | return(0); 137 | } 138 | //================================================================================== urldecode 139 | //================================================================================== 140 | String VirtuinoESP::urldecode(String* str){ 141 | String encodedString=""; 142 | char c; 143 | char code0; 144 | char code1; 145 | for (int i =0; i < str->length(); i++){ 146 | c=str->charAt(i); 147 | if (c == '+'){ 148 | encodedString+=' '; 149 | }else if (c == '%') { 150 | i++; 151 | code0=str->charAt(i); 152 | i++; 153 | code1=str->charAt(i); 154 | c = (h2int(code0) << 4) | h2int(code1); 155 | encodedString+=c; 156 | } else{ 157 | encodedString+=c; 158 | } 159 | yield(); 160 | } 161 | 162 | return encodedString; 163 | } 164 | 165 | 166 | //================================================================= getCommandType 167 | //This function returns the command type which was received from app 168 | //The second character of command string determines the command type 169 | // I digital input read command 170 | // Q digital ouput write 171 | // D digital memory read - write commang 172 | // A analog input analog input read command 173 | // O analog output write command 174 | // V float memory read - write command 175 | // C connect or disconect command 176 | // E error 177 | 178 | // Other characters are recognized as an error and function returns the character E 179 | //This is a system fuction. Don't change this code 180 | 181 | char VirtuinoESP::getCommandType(char c){ 182 | if (!(c== 'I' || c == 'Q' || c == 'D' ||c == 'A' ||c == 'O' || c == 'V' || c == 'C' || c == 'T')){ 183 | c='E'; //error command 184 | } 185 | return c; 186 | } 187 | 188 | //================================================================= getCommandPin 189 | //This function returns the pin number of the command string which was received from app 190 | //Fuction checks if pin number is correct. If not it returns -1 as error code 191 | //This is a system fuction. Don't change this code 192 | 193 | int VirtuinoESP::getCommandPin(String* aCommand){ 194 | char p1= aCommand->charAt(1); 195 | char p2= aCommand->charAt(2); 196 | String s="-1"; 197 | if (isDigit(p1) && isDigit(p2)) { 198 | s=""; 199 | s+=p1; 200 | s+=p2; 201 | } 202 | return s.toInt(); 203 | } 204 | 205 | //================================================================= getCommandValue 206 | float VirtuinoESP::getCommandValue(String* aCommand){ 207 | boolean er=false; 208 | for (int i=0;ilength();i++){ 209 | char c=aCommand->charAt(i); 210 | if (! ((c=='.') || (c=='-') || (isDigit(aCommand->charAt(i))))) er=true; 211 | } 212 | if (er==false) return aCommand->toFloat(); 213 | 214 | return 0; 215 | } 216 | //================================================================== getErrorCommand 217 | String VirtuinoESP::getErrorCommand(byte code){ 218 | String s=""; 219 | s+=esp_COMMAND_START_CHAR; 220 | s+=esp_COMMAND_ERROR; 221 | s+=code; 222 | s+=esp_COMMAND_END_CHAR; 223 | return s; 224 | } 225 | 226 | //================================================================= executeCommandFromApp 227 | void VirtuinoESP::executeReceivedCommand(char activeCommandType, int activeCommandPin ,String* commandString, boolean returnInfo){ 228 | //------------------------------------ 229 | // The value activeCommandType contains command type such as Digital output, Analog output etc. 230 | // The value activeCommandPin contains the pin number of the command 231 | // The value activeCommandValue contains the value (0,1 or 2 for digital, 0-255 for analog outputs, 0-1023 for analog memory) 232 | // In this void we have to check if the command is correct and then execute the command 233 | // After executing the command we have to send a response to Virtuinio app 234 | 235 | String response="";//getErrorCommand(esp_ERROR_UNKNOWN); 236 | String pinString=""; 237 | int boardPin=-1; 238 | 239 | if (activeCommandPin<10) pinString = "0"+String(activeCommandPin); 240 | else pinString=String(activeCommandPin); 241 | float activeCommandValue=0; 242 | switch (activeCommandType) { 243 | case 'T': 244 | if ((activeCommandPin>=0) & (activeCommandPin < 100)){ 245 | if (returnInfo) { 246 | if (textRequestedHandler==NULL) return; 247 | String testReply = textRequestedHandler(activeCommandPin); 248 | if (testReply==NO_REPLY) return; 249 | response=""; 250 | response =esp_COMMAND_START_CHAR; 251 | response +=activeCommandType; 252 | response +=pinString; 253 | response +="="; 254 | response += urlencode(&testReply); 255 | response +=esp_COMMAND_END_CHAR; // response 256 | } 257 | else { 258 | if (textReceivedHandler!=NULL) { 259 | String decodeStr=urldecode(commandString); 260 | textReceivedHandler(activeCommandPin,decodeStr); 261 | } 262 | } 263 | } 264 | break; 265 | case 'I': 266 | boardPin=getBoardDigitalPin(activeCommandPin); 267 | if (boardPin!=-1) 268 | response =esp_COMMAND_START_CHAR+String(activeCommandType)+pinString+"="+String(digitalRead(boardPin))+esp_COMMAND_END_CHAR; // response 269 | else response =esp_COMMAND_START_CHAR+String(activeCommandType)+pinString+"=2"+esp_COMMAND_END_CHAR; // response 270 | break; 271 | case 'Q': 272 | boardPin=getBoardDigitalPin(activeCommandPin); 273 | if (boardPin!=-1){ 274 | if (returnInfo) response =esp_COMMAND_START_CHAR+String(activeCommandType)+pinString+"="+String(digitalRead(boardPin))+esp_COMMAND_END_CHAR; // response 275 | else{ 276 | activeCommandValue = getCommandValue(commandString); 277 | if ((activeCommandValue == 0) || (activeCommandValue == 1)) { 278 | digitalWrite(boardPin,activeCommandValue); 279 | arduinoPinsValue[activeCommandPin]=activeCommandValue; 280 | response =esp_COMMAND_START_CHAR+String(activeCommandType)+pinString+"="+String(digitalRead(boardPin))+esp_COMMAND_END_CHAR; // response 281 | } else response =getErrorCommand(esp_ERROR_VALUE); 282 | } 283 | } else response =esp_COMMAND_START_CHAR+String(activeCommandType)+pinString+"=0"+esp_COMMAND_END_CHAR; // response 284 | break; 285 | 286 | case 'D': 287 | if ((activeCommandPin>=0) & (activeCommandPin=0) & (activeCommandPin=0 && pin=0) && (digitalMemoryIndex=0) && (digitalMemoryIndex=0) && (memoryIndex=0) && (memoryIndex 11 | #else 12 | #include 13 | #endif 14 | 15 | 16 | WiFiServer* wifiServer; 17 | 18 | Virtuino_ESP_WifiServer::Virtuino_ESP_WifiServer(WiFiServer* server):VirtuinoESP::VirtuinoESP(){ 19 | wifiServer=server; 20 | } 21 | 22 | 23 | //============================================================== run 24 | //============================================================== 25 | //============================================================== 26 | void Virtuino_ESP_WifiServer::Virtuino_ESP_WifiServer::run(){ 27 | WiFiClient client = wifiServer->available(); // Check if a client has connected 28 | if (client) { 29 | while(!client.available()){ // Wait until the client sends some data 30 | delay(1); 31 | } 32 | String req = client.readStringUntil('\r'); // Read the first line of the request 33 | if (DEBUG) Serial.println(req); 34 | 35 | 36 | int pos = req.indexOf("GET /"); 37 | if (pos!=-1){ // We have a GET message 38 | req.remove(0,pos+5); // Clear the characters GET / 39 | if (DEBUG) Serial.println("Command="+req); 40 | checkVirtuinoCommand(&req); // Virtuino library checks the command 41 | // Now the string responseBuffer contains the text that the library have to send to virtuino as reply to request 42 | String responseHttp= "HTTP/1.1 200 OK\r\n Content-Type: text/html\r\nConnection: close\r\n\r\n"; 43 | responseHttp+=responseBuffer; 44 | responseHttp+="\r\n\r\n"; 45 | if (DEBUG) Serial.println("responseHttp="+responseHttp); 46 | client.print(responseHttp); 47 | client.flush(); 48 | responseBuffer=""; 49 | delay(1); 50 | if (DEBUG) Serial.println("Client disonnected"); 51 | lastCommunicationTime=millis(); 52 | } 53 | else { 54 | if (DEBUG) Serial.println("invalid request"); 55 | client.stop(); 56 | } 57 | 58 | } 59 | } 60 | 61 | 62 | 63 | 64 | 65 | //====================================================================================== vDelay 66 | //====================================================================================== 67 | void Virtuino_ESP_WifiServer::vDelay(long milliseconds){ 68 | long timeStart=millis(); 69 | while (millis()-timeStart 22 | #else 23 | #include 24 | #endif 25 | 26 | 27 | 28 | #include "Arduino.h" 29 | #include "VirtuinoESP.h" 30 | 31 | 32 | //==================================================================== 33 | class Virtuino_ESP_WifiServer : public VirtuinoESP { 34 | public: 35 | Virtuino_ESP_WifiServer(WiFiServer* server); 36 | void run(); 37 | void vDelay(long milliseconds); 38 | 39 | private: 40 | 41 | }; 42 | 43 | 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /_updates_info.txt: -------------------------------------------------------------------------------- 1 | Library 1.8.0 2 | Added supporting for Virtuino ver 5 "Text Value Display" 3 | This library is not compatible with previous libraries. The sketches that use text commands need changes 4 | 5 | Library 1.72 6 | Bug fixes to support the latest ESP32 library 7 | 8 | Library 1.71 9 | Bug fixes to support the latest ESP32 library 10 | 11 | Library 1.70 12 | The library supports ESP32 board 13 | 14 | Library 1.64 15 | Bug fixed: Function vDelay(long time) added 16 | 17 | Library 1.63 18 | Bug fixed: Supports any range of pwm pins 19 | 20 | Library 1.62 21 | Added: Text Commands 22 | 23 | -------------------------------------------------------------------------------- /examples/Virtuino_ESP32_example/Virtuino_ESP32_example.ino: -------------------------------------------------------------------------------- 1 | 2 | /* Virtuino ESP32 ESP8266 web server and WiFi access point example No1 3 | * Created by Ilias Lamprou 4 | * Updated Feb 1 2018 5 | * Before running this code config the settings below as the instructions on the right 6 | * 7 | * Download latest Virtuino android app from the link: https://play.google.com/store/apps/details?id=com.virtuino_automations.virtuino&hl=el 8 | * Contact address for questions or comments: iliaslampr@gmail.com 9 | */ 10 | 11 | 12 | /*========= Virtuino General methods 13 | * 14 | * void vDigitalMemoryWrite(int digitalMemoryIndex, int value) write a value to a Virtuino digital memory (digitalMemoryIndex=0..31, value range = 0 or 1) 15 | * int vDigitalMemoryRead(int digitalMemoryIndex) read the value of a Virtuino digital memory (digitalMemoryIndex=0..31, returned value range = 0 or 1) 16 | * void vMemoryWrite(int memoryIndex, float value); write a value to Virtuino memory (memoryIndex=0..31, value range as float value) 17 | * float vMemoryRead(int memoryIndex); read a value of Virtuino memory (memoryIndex=0..31, returned a float value 18 | * run(); neccesary command to communicate with Virtuino android app (on start of void loop) 19 | * int getPinValue(int pin); read the value of a Pin. Usefull to read the value of a PWM pin 20 | * long lastCommunicationTime; Stores the last communication with Virtuino time 21 | * void vDelay(long milliseconds); 22 | */ 23 | 24 | #include 25 | 26 | #include "Virtuino_ESP_WifiServer.h" 27 | 28 | //--- SETTINGS ------------------------------------------------ 29 | const char* ssid = "WIFI NETWORK"; // WIFI network SSID 30 | const char* password = "PASSWORD"; // WIFI network PASSWORD 31 | WiFiServer server(8000); // Server port 32 | //------------------------------------------------------------- 33 | 34 | 35 | Virtuino_ESP_WifiServer virtuino(&server); 36 | 37 | 38 | 39 | //============================================================== connectToWiFiNetwork 40 | void connectToWiFiNetwork(){ 41 | Serial.println("Connecting to "+String(ssid)); 42 | 43 | // If you don't want to config IP manually disable the next four lines 44 | IPAddress ip(192, 168, 1, 150); // where 150 is the desired IP Address 45 | IPAddress gateway(192, 168, 1, 1); // set gateway to match your network 46 | IPAddress subnet(255, 255, 255, 0); // set subnet mask to match your network 47 | WiFi.config(ip, gateway, subnet); // If you don't want to config IP manually disable this line 48 | 49 | WiFi.mode(WIFI_STA); // Config module as station only. 50 | WiFi.begin(ssid, password); 51 | while (WiFi.status() != WL_CONNECTED) { 52 | delay(500); 53 | Serial.print("."); 54 | } 55 | Serial.println(""); 56 | Serial.println("WiFi connected"); 57 | Serial.println(WiFi.localIP()); 58 | } 59 | 60 | //================================================================= 61 | void initAccessPoint(){ 62 | Serial.print("Setting soft-AP ... "); // Default IP: 192.168.4.1 63 | WiFi.mode(WIFI_AP); // Config module as Access point only. Set WiFi.mode(WIFI_AP_STA); to config module as Acces point and station 64 | boolean result = WiFi.softAP("Virtuino Network", "12345678"); // SSID: Virtuino Network Password:12345678 65 | if(result == true) { 66 | Serial.println("Server Ready"); 67 | Serial.println(WiFi.softAPIP()); 68 | } 69 | else Serial.println("Failed!"); 70 | } 71 | 72 | 73 | 74 | 75 | //============================================================== setup 76 | //============================================================== 77 | //============================================================== 78 | void setup() { 79 | //----- Virtuino settings 80 | virtuino.DEBUG=true; // set this value TRUE to enable the serial monitor status 81 | virtuino.password="1234"; // Set a password or prefix to your web server for more protection 82 | // avoid special characters like ! $ = @ # % & * on your password. Use only numbers or text characters 83 | 84 | Serial.begin(9600); 85 | connectToWiFiNetwork(); 86 | //initAccessPoint(); 87 | server.begin(); 88 | 89 | pinMode(2,OUTPUT); 90 | 91 | } 92 | 93 | 94 | //============================================================== loop 95 | //============================================================== 96 | //============================================================== 97 | void loop() { 98 | virtuino.run(); 99 | 100 | // enter your loop code here. 101 | 102 | int value1 = random(100); // replace the random values with your sensors' values 103 | int value2 = random(100); 104 | 105 | virtuino.vMemoryWrite(1,value1); // write the value1 to virtual memory V1. On Virtuino panel add a value display to virtual pin V1 106 | virtuino.vMemoryWrite(2,value2); // write the value2 to virtual memory V2 On Virtuino panel add a value display to virtual pin V2 107 | 108 | 109 | //------ avoid to use delay() function in your code 110 | virtuino.vDelay(5000); // wait 5 seconds 111 | 112 | 113 | } 114 | 115 | 116 | -------------------------------------------------------------------------------- /examples/Virtuino_ESP_text_example/Virtuino_ESP_text_example.ino: -------------------------------------------------------------------------------- 1 | 2 | /* Virtuino ESP8266/ESP32 web server and WiFi access point example 3 | * Example: How to send or receive Text Commands from Virtuino app 4 | * Created by Ilias Lamprou 5 | * Updated Aug 17 2019 6 | * Before running this code config the settings below as the instructions on the right 7 | * 8 | * 9 | * Download latest Virtuino android app from the link: https://play.google.com/store/apps/details?id=com.virtuino_automations.virtuino&hl=el 10 | */ 11 | 12 | 13 | /*========= Virtuino General methods 14 | * 15 | * void vDigitalMemoryWrite(int digitalMemoryIndex, int value) write a value to a Virtuino digital memory (digitalMemoryIndex=0..31, value range = 0 or 1) 16 | * int vDigitalMemoryRead(int digitalMemoryIndex) read the value of a Virtuino digital memory (digitalMemoryIndex=0..31, returned value range = 0 or 1) 17 | * void vMemoryWrite(int memoryIndex, float value); write a value to Virtuino memory (memoryIndex=0..31, value range as float value) 18 | * float vMemoryRead(int memoryIndex); read a value of Virtuino memory (memoryIndex=0..31, returned a float value 19 | * run(); neccesary command to communicate with Virtuino android app (on start of void loop) 20 | * int getPinValue(int pin); read the value of a Pin. Usefull to read the value of a PWM pin 21 | * long lastCommunicationTime; Stores the last communication with Virtuino time 22 | */ 23 | 24 | 25 | // 1. On Arduino IDE software select the ESP32 or ESP8266 board 26 | // 2. Open the Virtuino library file: VirtuinoESP.h, enable the line #define NODEMCU and disable the other boards 27 | // 3. Connect the board to usb and upload this code 28 | 29 | #if defined(ARDUINO_ARCH_ESP32) 30 | #include 31 | #else 32 | #include 33 | #endif 34 | 35 | #include "Virtuino_ESP_WifiServer.h" 36 | 37 | 38 | const char* ssid = "WIFI NETWORK"; //Enter your wifi network ssid and password 39 | const char* password = "PASSWORD"; 40 | WiFiServer server(8000); // Server port 8000 is the Virtuino default port 41 | 42 | 43 | Virtuino_ESP_WifiServer virtuino(&server); 44 | 45 | 46 | //--- Init some text variables. Thess are the variables for Virtuino T pins 47 | String T0 =""; 48 | String T1 =""; 49 | //String T2 =""; 50 | //String T3 =""; 51 | 52 | //============================================================== setup 53 | //============================================================== 54 | //============================================================== 55 | void setup() { 56 | //----- Virtuino settings 57 | virtuino.DEBUG=true; // set this value TRUE to enable the serial monitor status 58 | virtuino.password="1234"; // Set a password to your web server for more protection 59 | // avoid special characters like ! $ = @ # % & * on your password. Use only numbers or text characters 60 | // virtuino.setTextReceivedCallback(onTextReceived); 61 | virtuino.setTextRequestedCallback(onTextRequested); 62 | 63 | Serial.begin(9600); // Enable this line only if DEBUG=true 64 | delay(10); 65 | 66 | connectESP_as_client(); // Connect the module to your wifi network 67 | //connectESP_as_AccessPoint(); // or setup the board as AccessPoint 68 | 69 | // ---- Start the server 70 | server.begin(); 71 | Serial.println("Server started"); 72 | 73 | //---- Enter your setup code below 74 | 75 | //pinMode(6,OUTPUT); // On Virtuino panel add a switch to pin D4 to enable or disable the board led 76 | pinMode(5,INPUT); // connect a switch. On Virtuino panel add a Led to pin D5 77 | 78 | } 79 | 80 | //======================================================= connectESP_as_client 81 | void connectESP_as_client(){ 82 | //---- 1. Settings as Station - Connect to a WiFi network 83 | Serial.println("Connecting to "+String(ssid)); 84 | // If you don't want to config IP manually disable the next four lines 85 | IPAddress ip(192, 168, 1, 150); // where 150 is the desired IP Address 86 | IPAddress gateway(192, 168, 1, 1); // set gateway to match your network 87 | IPAddress subnet(255, 255, 255, 0); // set subnet mask to match your network 88 | WiFi.config(ip, gateway, subnet); // If you don't want to config IP manually disable this line 89 | 90 | WiFi.mode(WIFI_STA); // Config module as station only. 91 | WiFi.begin(ssid, password); 92 | while (WiFi.status() != WL_CONNECTED) { 93 | delay(500); 94 | Serial.print("."); 95 | } 96 | Serial.println(""); 97 | Serial.println("WiFi connected"); 98 | Serial.println(WiFi.localIP()); 99 | } 100 | 101 | //======================================================= connectESP_as_AccessPoint 102 | void connectESP_as_AccessPoint(){ 103 | //---- 2. Settings as Access point - Create a private Wifi Network. 104 | Serial.print("Setting soft-AP ... "); // Default IP: 192.168.4.1 105 | WiFi.mode(WIFI_AP); // Config module as Acces point only. Set WiFi.mode(WIFI_AP_STA); to config module as Acces point and station 106 | boolean result = WiFi.softAP("ESP_WiFI_Network", "12345678"); // SSID: ESP_WiFI_Network Password:12345678 107 | if(result == true) Serial.println("Server Ready"); 108 | else Serial.println("Failed!"); 109 | 110 | } 111 | 112 | //============================================================== loop 113 | //============================================================== 114 | //============================================================== 115 | void loop() { 116 | virtuino.run(); 117 | 118 | // enter your loop code here. 119 | //------ avoid to use delay() function in your code 120 | 121 | T0="Hello Virtuino "+String(random(100)); // on Virtuino panel add a "Text Value Display" to get this text 122 | 123 | virtuino.vDelay(5000); // use this instead of the default delay(5000) 124 | } 125 | 126 | 127 | 128 | //======================================================= 129 | /* This void is called by Virtuino library any time it receives a text from Virtuino app 130 | You have to store the text to a variable and then to do something with the text*/ 131 | void onTextReceived(uint8_t textPin, String receivedText){ 132 | Serial.println("====Received T"+String(textPin)+"="+receivedText); 133 | if (textPin==0) T0=receivedText; 134 | else if (textPin==1) T1=receivedText; 135 | //else if (textPin==2) T2=receivedText; 136 | 137 | //else if (textPin==3) { 138 | // T3=receivedText; 139 | // if (T3=="TURN ON") digitalWrite(..... 140 | // } 141 | } 142 | 143 | //======================================================= 144 | /* Every time Virtuino app requests about a text of a T pin, the library calls this function 145 | You have to return the string for which the app requests the text*/ 146 | String onTextRequested(uint8_t textPin){ 147 | Serial.println("==== Requested T"+String(textPin)); 148 | if (textPin==0) return T0; 149 | else if (textPin==1) return T1; 150 | //else if (textPin==2) return T2; 151 | 152 | return virtuino.NO_REPLY; 153 | } 154 | -------------------------------------------------------------------------------- /examples/Virtuino_NodeMCU_ESP8266_example_1/Virtuino_NodeMCU_ESP8266_example_1.ino: -------------------------------------------------------------------------------- 1 | 2 | /* Virtuino NodeMCU ESP8266 web server and WiFi access point example No1 3 | * Created by Ilias Lamprou 4 | * Updated Feb 1 2016 5 | * Before running this code config the settings below as the instructions on the right 6 | * 7 | * 8 | * Download latest Virtuino android app from the link: https://play.google.com/store/apps/details?id=com.virtuino_automations.virtuino&hl=el 9 | * Contact address for questions or comments: iliaslampr@gmail.com 10 | */ 11 | 12 | 13 | /*========= Virtuino General methods 14 | * 15 | * void vDigitalMemoryWrite(int digitalMemoryIndex, int value) write a value to a Virtuino digital memory (digitalMemoryIndex=0..31, value range = 0 or 1) 16 | * int vDigitalMemoryRead(int digitalMemoryIndex) read the value of a Virtuino digital memory (digitalMemoryIndex=0..31, returned value range = 0 or 1) 17 | * void vMemoryWrite(int memoryIndex, float value); write a value to Virtuino memory (memoryIndex=0..31, value range as float value) 18 | * float vMemoryRead(int memoryIndex); read a value of Virtuino memory (memoryIndex=0..31, returned a float value 19 | * run(); neccesary command to communicate with Virtuino android app (on start of void loop) 20 | * int getPinValue(int pin); read the value of a Pin. Usefull to read the value of a PWM pin 21 | * long lastCommunicationTime; Stores the last communication with Virtuino time 22 | */ 23 | 24 | 25 | // 1. On Arduino IDE software select the board NodeMCU. If the board isn't in boards list, open the boards manager, on the search line enter esp8266 and download all ESP8266 boards 26 | // 2. Open the Virtuino library file: VirtuinoESP.h, enable the line #define NODEMCU and disable the other boards 27 | // 3. Connect the NODEMCU ESP8266 module to usb and upload this code 28 | 29 | #include 30 | #include "Virtuino_ESP_WifiServer.h" 31 | 32 | 33 | const char* ssid = "WIFI NETWORK NAME"; 34 | const char* password = "PASSWORD"; 35 | WiFiServer server(8000); // Server port 36 | 37 | 38 | Virtuino_ESP_WifiServer virtuino(&server); 39 | 40 | //NodeMCU ESP8266 pins= D0,D1,D2,D3,D4,D5,D6,D7,D8,D9,D10 41 | 42 | // Example variables 43 | int storedValue=0; 44 | int counter =0; 45 | long storedTime=0; 46 | 47 | //============================================================== setup 48 | //============================================================== 49 | //============================================================== 50 | void setup() { 51 | //----- Virtuino settings 52 | virtuino.DEBUG=true; // set this value TRUE to enable the serial monitor status 53 | virtuino.password="1234"; // Set a password to your web server for more protection 54 | // avoid special characters like ! $ = @ # % & * on your password. Use only numbers or text characters 55 | 56 | Serial.begin(9600); // Enable this line only if DEBUG=true 57 | delay(10); 58 | 59 | //----- NodeMCU module settings 60 | //----- prepare GPIO2 61 | pinMode(2, OUTPUT); 62 | digitalWrite(2, 0); 63 | 64 | //---- 1. Settings as Station - Connect to a WiFi network 65 | Serial.println("Connecting to "+String(ssid)); 66 | 67 | // If you don't want to config IP manually disable the next four lines 68 | IPAddress ip(192, 168, 1, 150); // where 150 is the desired IP Address 69 | IPAddress gateway(192, 168, 1, 1); // set gateway to match your network 70 | IPAddress subnet(255, 255, 255, 0); // set subnet mask to match your network 71 | WiFi.config(ip, gateway, subnet); // If you don't want to config IP manually disable this line 72 | 73 | WiFi.mode(WIFI_STA); // Config module as station only. 74 | WiFi.begin(ssid, password); 75 | while (WiFi.status() != WL_CONNECTED) { 76 | delay(500); 77 | Serial.print("."); 78 | } 79 | Serial.println(""); 80 | Serial.println("WiFi connected"); 81 | Serial.println(WiFi.localIP()); 82 | 83 | //---- 2. Settings as Access point - Create a private Wifi Network. Enable the next five lines to use module as Acces point 84 | // Serial.print("Setting soft-AP ... "); // Default IP: 192.168.4.1 85 | // WiFi.mode(WIFI_AP); // Config module as Acces point only. Set WiFi.mode(WIFI_AP_STA); to config module as Acces point and station 86 | // boolean result = WiFi.softAP("NodeMCU", "12345678"); // SSID: NodeMCU Password:12345678 87 | // if(result == true) Serial.println("Server Ready"); 88 | // else Serial.println("Failed!"); 89 | 90 | // ---- Start the server 91 | server.begin(); 92 | Serial.println("Server started"); 93 | 94 | //---- Enter your setup code below 95 | 96 | pinMode(D4,OUTPUT); // On Virtuino panel add a switch to pin D4 to enable or disable the board led 97 | pinMode(D3,OUTPUT); // connect a relay or a led to this pin. On Virtuino panel add a switch to pin D3 98 | pinMode(D5,INPUT); // connect a switch. On Virtuino panel add a Led to pin D5 99 | 100 | 101 | 102 | } 103 | 104 | 105 | //============================================================== loop 106 | //============================================================== 107 | //============================================================== 108 | void loop() { 109 | virtuino.run(); 110 | 111 | // enter your loop code here. 112 | //------ avoid to use delay() function in your code 113 | 114 | 115 | //--- example 1 Enable or disable the board led using virtual pin 116 | //--- On Virtuino app panel add a Switch to Virtual Pin 0 to enable or disable the nodemcu board led 117 | int v=virtuino.vDigitalMemoryRead(0); // Read virtual memory 0 from Virtuino app 118 | if (v!=storedValue) { 119 | Serial.println("-------Virtual pin DV0 is changed to="+String(v)); 120 | if (v==1) digitalWrite(D4,0); 121 | else digitalWrite(D4,1); 122 | storedValue=v; 123 | } 124 | 125 | 126 | //---- Example 2 127 | //---- Write a random value to virtual pin 2. On virtuino panel add an indicator to V2 128 | virtuino.vMemoryWrite(2,random(1000)); 129 | 130 | 131 | //---- Example 3 132 | //---- Create a counter every 5 seconds 133 | //---- Use this technique. Avoid to use the delay function 134 | long t= millis(); // read the time 135 | if (t>storedTime+5000){ 136 | counter++; // increase counter by 1 137 | if (counter>20) counter=0; // limit = 20 138 | storedTime = t; 139 | virtuino.vMemoryWrite(12,counter); // write counter to virtual pin V12 140 | } 141 | } 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | -------------------------------------------------------------------------------- /examples/Virtuino_WeMos_ESP8266_example_1/Virtuino_WeMos_ESP8266_example_1.ino: -------------------------------------------------------------------------------- 1 | 2 | /* Virtuino WeMos ESP8266 web server and WiFi access point example No1 3 | * Created by Ilias Lamprou 4 | * Updated Feb 1 2016 5 | * Before running this code config the settings below as the instructions on the right 6 | * 7 | * 8 | * Download latest Virtuino android app from the link: https://play.google.com/store/apps/details?id=com.virtuino_automations.virtuino&hl=el 9 | * Contact address for questions or comments: iliaslampr@gmail.com 10 | */ 11 | 12 | 13 | /*========= Virtuino General methods 14 | * 15 | * void vDigitalMemoryWrite(int digitalMemoryIndex, int value) write a value to a Virtuino digital memory (digitalMemoryIndex=0..31, value range = 0 or 1) 16 | * int vDigitalMemoryRead(int digitalMemoryIndex) read the value of a Virtuino digital memory (digitalMemoryIndex=0..31, returned value range = 0 or 1) 17 | * void vMemoryWrite(int memoryIndex, float value); write a value to Virtuino memory (memoryIndex=0..31, value range as float value) 18 | * float vMemoryRead(int memoryIndex); read a value of Virtuino memory (memoryIndex=0..31, returned a float value 19 | * run(); neccesary command to communicate with Virtuino android app (on start of void loop) 20 | * int getPinValue(int pin); read the value of a Pin. Usefull to read the value of a PWM pin 21 | * long lastCommunicationTime; Stores the last communication with Virtuino time 22 | */ 23 | 24 | 25 | // 1. On Arduino IDE software select the board WeMos. If the board isn't in boards list, open the boards manager, on the search line enter esp8266 and download all ESP8266 boards 26 | // 2. Open the Virtuino library file: VirtuinoESP.h, enable the line #define WEMOS_D1_MINI_PRO and disable the other boards 27 | // 3. Connect the WeMos ESP8266 module to usb and upload this code 28 | 29 | #include 30 | #include "Virtuino_ESP_WifiServer.h" 31 | 32 | 33 | const char* ssid = "WIFI NETWORK"; 34 | const char* password = "PASSWORD"; 35 | WiFiServer server(8000); // Server port 36 | 37 | 38 | Virtuino_ESP_WifiServer virtuino(&server); 39 | 40 | //WeMos ESP8266 pins= D0,D1,D2,D3,D4,D5,D6,D7,D8 41 | 42 | // Example variables 43 | int storedValue=0; 44 | int counter =0; 45 | long storedTime=0; 46 | 47 | //============================================================== setup 48 | //============================================================== 49 | //============================================================== 50 | void setup() { 51 | //----- Virtuino settings 52 | virtuino.DEBUG=true; // set this value TRUE to enable the serial monitor status 53 | virtuino.password="1234"; // Set a password to your web server for more protection 54 | // avoid special characters like ! $ = @ # % & * on your password. Use only numbers or text characters 55 | 56 | Serial.begin(9600); // Enable this line only if DEBUG=true 57 | delay(10); 58 | 59 | //----- WeMos module settings 60 | //----- prepare GPIO2 61 | pinMode(2, OUTPUT); 62 | digitalWrite(2, 0); 63 | 64 | //---- 1. Settings as Station - Connect to a WiFi network 65 | Serial.println("Connecting to "+String(ssid)); 66 | 67 | // If you don't want to config IP manually disable the next four lines 68 | IPAddress ip(192, 168, 1, 150); // where 150 is the desired IP Address 69 | IPAddress gateway(192, 168, 1, 1); // set gateway to match your network 70 | IPAddress subnet(255, 255, 255, 0); // set subnet mask to match your network 71 | WiFi.config(ip, gateway, subnet); // If you don't want to config IP manually disable this line 72 | 73 | WiFi.mode(WIFI_STA); // Config module as station only. 74 | WiFi.begin(ssid, password); 75 | while (WiFi.status() != WL_CONNECTED) { 76 | delay(500); 77 | Serial.print("."); 78 | } 79 | Serial.println(""); 80 | Serial.println("WiFi connected"); 81 | Serial.println(WiFi.localIP()); 82 | 83 | //---- 2. Settings as Access point - Create a private Wifi Network. Enable the next five lines to use module as Acces point 84 | // Serial.print("Setting soft-AP ... "); // Default IP: 192.168.4.1 85 | // WiFi.mode(WIFI_AP); // Config module as Acces point only. Set WiFi.mode(WIFI_AP_STA); to config module as Acces point and station 86 | // boolean result = WiFi.softAP("WeMos", "12345678"); // SSID: NodeMCU Password:12345678 87 | // if(result == true) Serial.println("Server Ready"); 88 | // else Serial.println("Failed!"); 89 | 90 | // ---- Start the server 91 | server.begin(); 92 | Serial.println("Server started"); 93 | 94 | //---- Enter your setup code below 95 | 96 | pinMode(D4,OUTPUT); // On Virtuino panel add a switch to pin D4 to enable or disable the board led 97 | pinMode(D3,OUTPUT); // connect a relay or a led to this pin. On Virtuino panel add a switch to pin D3 98 | pinMode(D5,INPUT); // connect a switch. On Virtuino panel add a Led to pin D5 99 | 100 | 101 | 102 | } 103 | 104 | 105 | //============================================================== loop 106 | //============================================================== 107 | //============================================================== 108 | void loop() { 109 | virtuino.run(); 110 | 111 | // enter your loop code here. 112 | //------ avoid to use delay() function in your code 113 | 114 | 115 | //--- example 1 Enable or disable the board led using virtual pin 116 | //--- On Virtuino app panel add a Switch to Virtual Pin 0 to enable or disable the nodemcu board led 117 | int v=virtuino.vDigitalMemoryRead(0); // Read virtual memory 0 from Virtuino app 118 | if (v!=storedValue) { 119 | Serial.println("-------Virtual pin DV0 is changed to="+String(v)); 120 | if (v==1) digitalWrite(D4,0); 121 | else digitalWrite(D4,1); 122 | storedValue=v; 123 | } 124 | 125 | 126 | //---- Example 2 127 | //---- Write a random value to virtual pin 2. On virtuino panel add an indicator to V2 128 | virtuino.vMemoryWrite(2,random(1000)); 129 | 130 | 131 | //---- Example 3 132 | //---- Create a counter every 5 seconds 133 | //---- Use this technique. Avoid to use the delay function 134 | long t= millis(); // read the time 135 | if (t>storedTime+5000){ 136 | counter++; // increase counter by 1 137 | if (counter>20) counter=0; // limit = 20 138 | storedTime = t; 139 | virtuino.vMemoryWrite(12,counter); // write counter to virtual pin V12 140 | } 141 | } 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=Virtuino ESP 2 | version=1.8.0 3 | author= 4 | maintainer= 5 | sentence=Required for communication with Virtuino app 6 | paragraph= 7 | category=Communication 8 | url=https://github.com/iliaslamprou/virtuinoESP 9 | architectures=* 10 | --------------------------------------------------------------------------------