krzysp

my virtuino base code 2- virtuino unit v1.h

Dec 30th, 2022 (edited)
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 4.79 KB | Source Code | 0 0
  1.  
  2. #include <ESP8266WiFi.h>
  3.  
  4. //--- SETTINGS ------------------------------------------------
  5. const char* ssid = "xxxx";                // WIFI network SSID
  6. const char* password = "xxxxxxx";          // WIFI network PASSWORD
  7. WiFiServer server(8000);                   // Default Virtuino Server port
  8. IPAddress ip(192, 168, 2, 170);            // where 150 is the desired IP Address. The first three numbers must be the same as the router IP
  9. IPAddress gateway(192, 168, 2, 1);         // set gateway to match your network. Replace with your router IP
  10.  
  11. //---VirtuinoCM  Library settings --------------
  12. #include "VirtuinoCM.h"
  13. VirtuinoCM virtuino;
  14. //#define V_memory_count 100          // the size of V memory. You can change it to a number <=255)
  15. //int V[V_memory_count];           // This array is synchronized with Virtuino V memory. You can change the type to int, long etc.
  16. //---
  17. String V1 = "";
  18. String V2 = "";
  19. String V4 = "";
  20. String V5 = "";
  21. String V6 = "";
  22. String V7 = "";
  23.  
  24.  
  25. boolean debug = true;              // set this variable to false on the finale code to decrease the request time.
  26.  
  27. //============================================================== connectToWiFiNetwork
  28. void connectToWiFiNetwork() {
  29.   Serial.println("Connecting to " + String(ssid));
  30.   // If you don't want to config IP manually disable the next two lines
  31.   IPAddress subnet(255, 255, 255, 0);        // set subnet mask to match your network
  32.   WiFi.config(ip, gateway, subnet);          // If you don't want to config IP manually disable this line
  33.   WiFi.mode(WIFI_STA);                       // Config module as station only.
  34.   WiFi.begin(ssid, password);
  35.   while (WiFi.status() != WL_CONNECTED) {
  36.     delay(500);
  37.     Serial.print(".");
  38.   }
  39.   Serial.println("");
  40.   Serial.println("WiFi connected");
  41.   Serial.println(WiFi.localIP());
  42. }
  43.  
  44.  
  45. //============================================================== onCommandReceived
  46. //==============================================================
  47. /* This function is called every time Virtuino app sends a request to server to change a Pin value
  48.    The 'variableType' can be   V=Virtual Pin
  49.    The 'variableIndex' is the pin number index of Virtuino app
  50.    The 'valueAsText' is the value that has sent from the app  in Strin format */
  51. void Read_V1(String value_Text);
  52. void Read_V2(String value_Text);
  53. void onReceived(char variableType, uint8_t variableIndex, String valueAsText) {
  54.   if (variableType == 'V') {
  55.     //  float value = valueAsText.toFloat();        // convert the value to float. The valueAsText have to be numerical
  56.     //  if (variableIndex < V_memory_count) V[variableIndex] = value;          // copy the received value to arduino V memory array
  57.  
  58.     if (variableIndex == 1) Read_V1(valueAsText);
  59.     if (variableIndex == 2) Read_V2(valueAsText);
  60.   }
  61. }
  62.  
  63. //==============================================================
  64. /* This function is called every time Virtuino app requests to read a pin value*/
  65. String onRequested(char variableType, uint8_t variableIndex) {
  66.   if (variableType == 'V') {
  67.     if (variableIndex == 4) return  V4;
  68.     if (variableIndex == 5) return  V5;
  69.     if (variableIndex == 7) return  V7;
  70.   }
  71.   return "";
  72. }
  73.  
  74.  
  75. //==============================================================
  76. void virtuinoRun() {
  77.   WiFiClient client = server.available();
  78.   if (!client) return;
  79.   if (debug) Serial.println("Connected");
  80.   unsigned long timeout = millis() + 3000;
  81.   while (!client.available() && millis() < timeout) delay(1);
  82.   if (millis() > timeout) {
  83.     Serial.println("timeout");
  84.     client.flush();
  85.     client.stop();
  86.     return;
  87.   }
  88.   virtuino.readBuffer = "";  // clear Virtuino input buffer. The inputBuffer stores the incoming characters
  89.   while (client.available() > 0) {
  90.     char c = client.read();         // read the incoming data
  91.     virtuino.readBuffer += c;       // add the incoming character to Virtuino input buffer
  92.     if (debug) Serial.write(c);
  93.   }
  94.   client.flush();
  95.   if (debug) Serial.println("\nReceived data: " + virtuino.readBuffer);
  96.   String* response = virtuino.getResponse();   // get the text that has to be sent to Virtuino as reply. The library will check the inptuBuffer and it will create the response text
  97.   if (debug) Serial.println("Response : " + *response);
  98.   client.print(*response);
  99.   client.flush();
  100.   delay(10);
  101.   client.stop();
  102.   if (debug) Serial.println("Disconnected");
  103. }
  104.  
  105.  
  106. void v_setup() {
  107.   virtuino.begin(onReceived, onRequested, 256); //Start Virtuino. Set the buffer to 256. With this buffer Virtuino can control about 28 pins (1 command = 9bytes) The T(text) commands with 20 characters need 20+6 bytes
  108.   //  virtuino.key = "1234";                     //This is the Virtuino password. Only requests the start with this key are accepted from the library
  109.  
  110.   connectToWiFiNetwork();
  111.   server.begin();
  112. }
  113.  
Tags: virtuino
Advertisement
Add Comment
Please, Sign In to add comment