0@0
0@0
175
0@0
169
383
0@0
2
4
1
240@200
260@200 corner: 326.0d@220
240@200 corner: 346.0d@220
265.0d@214.0d
Virtuino SE
b14d3e4f-8b3b-4bb8-9354-3b5d04d5cc82
Virtuino SE
virtuinoRun();
15
Serial.begin(9600);
while (!Serial) {;}
connectToWiFiNetwork(); //enable this line to connect the module your wifi network
wifiServer.begin();
pinMode(LED_BUILTIN, OUTPUT);
197
#ifdef
ESP8266
#include
<ESP8266WiFi.h>
WIFI network SSID
ssid
= "WIFI NETWORK";
#else
#include <WiFi.h>
#endif
const char*
WIFI network PASSWORD
password
= "PASSWORD";
const char*
Server port
wifiServer
(8000);
WiFiServer
Virtuino KEY, only the requests that include this key will be acceptable
#define
VIRTUINO_KEY
"1234"
#define
WRONG_KEY_MESSAGE
"Wrong Key"
#define
WELLCOME_MESSAGE
"Hello Virtuino"
debug
=true;
boolean
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.println("=== onCommandReceived variableIndex="+String(variableIndex)+" valueAsText="+valueAsText);
//--- code to enable or disable onboard led
if (variableIndex==1){ // Virtuino V1 memory -> onboard led
if (valueAsText=="?") return String(digitalRead(LED_BUILTIN)); // return the state of LED_BUILTIN as text
int v=valueAsText.toInt();
if (v==1) digitalWrite(LED_BUILTIN,HIGH); // change the state of pin LED_BUILTIN
else if (v==0) digitalWrite(LED_BUILTIN,LOW);
}
return "";
552
String
onCommandReceived
int
variableIndex
String
valueAsText
WiFiClient client = wifiServer.available(); // Check if a client has connected
if (client) {
if (debug) Serial.println("New client");
while(!client.available())delay(1); // Wait until the client sends some data
String req = client.readStringUntil('\r'); // Read the first line of the request
if (debug) Serial.println(req);
//-- check request
int pos = req.indexOf("GET /");
if (pos!=-1){ // We have a GET message
req.remove(0,pos+5); // Clear the characters GET /
int k= req.indexOf(" HTTP/");
if (k>0) req.remove(k); // Clear the tail of the line
//---the variable req contains the Virtuino command string.
String commandResponse = prepareResponse(&req); //Prepare the text that the server has to return to app
String responseHttp= "HTTP/1.1 200 OK\r\n Content-Type: text/html\r\nConnection: close\r\n\r\n";
responseHttp+=commandResponse; // add the commandResponse to request response
responseHttp+="\r\n\r\n";
if (debug) Serial.println("ResponseHttp="+responseHttp);
client.print(responseHttp);
client.print("status:0");
delay(1);
if (debug) Serial.println("Client disonnected");
client.flush();
}
else {
Serial.println("invalid request");
client.stop();
}
}
1442
void
virtuinoRun
//---check the KEY
#ifdef VIRTUINO_KEY
String virtuinoKey="";
int keyPosStart = commands->indexOf("&KEY=");
if (keyPosStart!=-1) {
int keyPosEnd = commands->indexOf('&',keyPosStart+5);
if (keyPosEnd!=-1) virtuinoKey= commands->substring(keyPosStart+5,keyPosEnd);
else virtuinoKey= commands->substring(keyPosStart);
}
if (!virtuinoKey.equals(VIRTUINO_KEY)) return WRONG_KEY_MESSAGE;
#endif
//--- read all virtuino commands from request
String commandResponse="";
boolean checkNext=true;
int cStart=-1;
while (checkNext){
cStart=commands->indexOf('&',cStart+1);
if (cStart!=-1) {
int cEnd=commands->indexOf('&',cStart+1);
String oneCommand="";
if (cEnd<0) oneCommand=commands->substring(cStart+1);
else oneCommand=commands->substring(cStart+1,cEnd);
if (debug) Serial.println("cmd: "+oneCommand);
char commandType = oneCommand.charAt(0);
if (commandType=='C') return "&CNT="+String(WELLCOME_MESSAGE); // Virtuino sends the command &C=? to check the connection status
if ((commandType=='V') || (commandType=='T')) {
int equalCharPosition = oneCommand.indexOf('=');
int variableIndex = oneCommand.substring(1,equalCharPosition).toInt();
String valueString = oneCommand.substring(equalCharPosition+1);
String returnedValue = onCommandReceived(variableIndex,valueString);
if (returnedValue.length()>0) {
commandResponse+='&';
commandResponse+=commandType;
commandResponse+=String(variableIndex);
commandResponse+='=';
commandResponse+=returnedValue;
}
}
}
else checkNext=false; // the last command has checked
}
if (debug) Serial.println("Response="+commandResponse );
if (commandResponse.length()==0) commandResponse="OK";
return commandResponse;
1919
String
prepareResponse
String*
commands
310
(0 to: 310)
0
9600
arduinoIDE
112
4096
181367213
ESP8266 ESP-01 V090
161130820