Virtuino_SE_Json f57240bc-fd3d-423c-a2df-b05cecd74d24 Virtuino_SE_ Json WIFI NETWORK Vladlink_99C4BA cb0a0bf5-7021-4ebd-9b60-2afc0f78a28c 72613bb6-59f1-4d1a-8e55-54740ca87c69 PASSWORD 43345911 7fed998f-eea4-4338-9bc8-6aa2295e6069 b2e94ec7-01de-4155-864f-d929f25d5b65 virtuinoRun(); //Necessary function to communicate with Virtuino. Client handler 91 if (debug) { Serial.begin(9600); while (!Serial) continue; } //initAccessPoint(); //enable this line to use the esp board as AP connectToWiFiNetwork(); //enable this line to connect the module to your wifi network wifiServer.begin(); 276 #ifdef ESP8266 #include <ESP8266WiFi.h> The name of your WiFi network (SSID) ssid = "WIFI NETWORK"; #else #include <WiFi.h> #endif const char* WIFI network PASSWORD password = "PASSWORD"; const char* Virtuino default Server port: 8000 wifiServer (8000); WiFiServer Access point network SSID ssid_AP = "Virtuino network"; const char* Access point network PASSWORD password_AP = "1234567890"; const char* Virtuino KEY, only the requests that include this key will be acceptable. // disable the line if you don't want to use a key #define VIRTUINO_KEY "1234" the size of V memory. You can change it to a number <256 #define V_memory_count 255 This array is synchronized with Virtuino SE V memory V_memory [V_memory_count]; float set this variable to false on the finale code to decrease the request duration debug = true; boolean variable1 = 0; float variable2 = 0; float timeStrored =0; unsigned long #define WRONG_KEY_MESSAGE "Wrong Key" #define WELLCOME_MESSAGE "Hello Virtuino" Serial.println("Connecting to "+String(ssid)); // If you don't want to config IP manually disable the next four lines IPAddress ip(192, 168, 1, 150); // where 150 is the desired IP Address IPAddress gateway(192, 168, 1, 1); // set gateway to match your network IPAddress subnet(255, 255, 255, 0); // set subnet mask to match your network WiFi.config(ip, gateway, subnet); // If you don't want to config IP manually disable this line WiFi.mode(WIFI_STA); // Configure the module as station only. WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println(WiFi.localIP()); 788 void connectToWiFiNetwork Serial.print("Setting soft-AP ... "); // Default IP: 192.168.4.1 WiFi.mode(WIFI_AP); // Config module as Access point only. Set WiFi.mode(WIFI_AP_STA); to config module as Acces point and station boolean result = WiFi.softAP(ssid_AP,password_AP); // set the Access point SSID and password if(result == true) { Serial.println("Server Ready"); Serial.println(WiFi.softAPIP()); } else Serial.println("Failed!"); 492 void initAccessPoint String valueAsText = getJsonValue(valueAsJson,"value"); // read the value from the received Json string if (debug) Serial.println("New value received: V"+String(variableIndex)+" value="+valueAsText); if (valueAsText=="?") return String(V_memory[variableIndex]); // return the value of the arduino V memory array else { float value = valueAsText.toFloat(); // convert the value to float. The valueAsText have to be numerical V_memory[variableIndex]=value; // copy the received value to arduino V memory array } return ""; 568 String onCommandReceived int variableIndex String valueAsJson WiFiClient client = wifiServer.available(); if (client) { if (debug) Serial.println("Client connected"); while(!client.available())delay(1); // Wait until the client sends some data String data=""; while (client.connected()) { while (client.available()>0) { char c = client.read(); data+=c; if (debug) Serial.write(c); } client.flush(); if (debug) Serial.println("\nReceived data: "+data); String response= getResponseAsJson(data); client.print(response); if (debug) Serial.println("Response : "+response); client.stop(); } if (debug) Serial.println("Client disconnected"); } 680 void virtuinoRun //---check the KEY #ifdef VIRTUINO_KEY String key = getJsonValue(data, "key"); if (debug) Serial.println("Key="+key); if (!key.equals(VIRTUINO_KEY)) return "{\"status\":\"-1\",\"message\":\""+String(WRONG_KEY_MESSAGE)+"\"}"; #endif //---check connection status String statusInfo = getJsonValue(data, "status"); if (debug) Serial.println("statusInfo="+statusInfo); if (statusInfo.equals("0")) return "{\"status\":\"2\",\"message\":\""+String(WELLCOME_MESSAGE)+"\"}"; String response="{\"status\":\"1\""; for (byte i=0;i<V_memory_count;i++){ // easy way to read a Json object without installing extra json libraries String jsonTag="V"+String(i)+"_"; int pos = data.indexOf(jsonTag); if (pos>0) { int startPos= data.indexOf("{",pos+1); if (startPos>pos) { int endPos= data.indexOf("}", startPos+2); if (endPos>startPos) { String variableAsJson = data.substring(startPos,endPos+1); String variableValue=onCommandReceived(i,variableAsJson); if (variableValue.length()>0) response+=",\""+jsonTag+"\":{\"value\":\""+urlencode(&variableValue)+"\"}"; } } } } //for response+="}"; return response; 1271 String getResponseAsJson String data int pos = jsonAsText.indexOf(jsonTag); if (pos>0) { int startPos=jsonAsText.indexOf(":\"",pos); int lastPos = jsonAsText.indexOf("\"",startPos+2); String value=jsonAsText.substring(startPos+2,lastPos); value=urldecode(&value); if (lastPos>startPos) return value; } return ""; 318 String getJsonValue String jsonAsText String jsonTag String encodedString=""; char c; char code0; char code1; for (int i =0; i < str->length(); i++){ c=str->charAt(i); if (c == '+'){ encodedString+=' '; }else if (c == '%') { i++; code0=str->charAt(i); i++; code1=str->charAt(i); c = (h2int(code0) << 4) | h2int(code1); encodedString+=c; } else{ encodedString+=c; } yield(); } return encodedString; 479 String urldecode String* str String encodedString=""; char c; char code0; char code1; char code2; for (int i =0; i < str->length(); i++){ c=str->charAt(i); if (c == ' '){ encodedString+= '+'; } else if (isalnum(c)){ encodedString+=c; } else{ code1=(c & 0xf)+'0'; if ((c & 0xf) >9){ code1=(c & 0xf) - 10 + 'A'; } c=(c>>4)&0xf; code0=c+'0'; if (c > 9){ code0=c - 10 + 'A'; } code2='\0'; encodedString+='%'; encodedString+=code0; encodedString+=code1; //encodedString+=code2; } yield(); } return encodedString; 678 String urlencode String* str if (c >= '0' && c <='9'){ return((unsigned char)c - '0'); } if (c >= 'a' && c <='f'){ return((unsigned char)c - 'a' + 10); } if (c >= 'A' && c <='F'){ return((unsigned char)c - 'A' + 10); } return(0); 248 unsigned char h2int char c