Advertisement
krzysp

virtuino6_base_virtuino unit

Jan 3rd, 2023 (edited)
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 3.10 KB | Source Code | 0 0
  1. #include <ESP8266WiFi.h>
  2.  
  3. //--- SETTINGS ------------------------------------------------
  4. const char* ssid = "xxxx";                // WIFI network SSID
  5. const char* password = "xxxxxxxxx";          // WIFI network PASSWORD
  6. WiFiServer server(8000);                   // Default Virtuino Server port
  7. 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
  8. IPAddress gateway(192, 168, 2, 1);         // set gateway to match your network. Replace with your router IP
  9.  
  10. //---VirtuinoCM  Library settings --------------
  11. #include "VirtuinoCM.h"
  12. VirtuinoCM virtuino;
  13.  
  14. boolean debug = true;              // set this variable to false on the finale code to decrease the request time.
  15.  
  16. //============================================================== connectToWiFiNetwork
  17. void connectToWiFiNetwork() {
  18.   Serial.println("Connecting to " + String(ssid));
  19.   // If you don't want to config IP manually disable the next two lines
  20.   IPAddress subnet(255, 255, 255, 0);        // set subnet mask to match your network
  21.   WiFi.config(ip, gateway, subnet);          // If you don't want to config IP manually disable this line
  22.   WiFi.mode(WIFI_STA);                       // Config module as station only.
  23.   WiFi.begin(ssid, password);
  24.   while (WiFi.status() != WL_CONNECTED) {
  25.     delay(500);
  26.     Serial.print(".");
  27.   }
  28.   Serial.println("");
  29.   Serial.println("WiFi connected");
  30.   Serial.println(WiFi.localIP());
  31. }
  32.  
  33. //==============================================================
  34. void virtuinoRun() {
  35.   WiFiClient client = server.available();
  36.   if (!client) return;
  37.   if (debug) Serial.println("Connected");
  38.   unsigned long timeout = millis() + 3000;
  39.   while (!client.available() && millis() < timeout) delay(1);
  40.   if (millis() > timeout) {
  41.     Serial.println("timeout");
  42.     client.flush();
  43.     client.stop();
  44.     return;
  45.   }
  46.   virtuino.readBuffer = "";  // clear Virtuino input buffer. The inputBuffer stores the incoming characters
  47.   while (client.available() > 0) {
  48.     char c = client.read();         // read the incoming data
  49.     virtuino.readBuffer += c;       // add the incoming character to Virtuino input buffer
  50.     if (debug) Serial.write(c);
  51.   }
  52.   client.flush();
  53.   if (debug) Serial.println("\nReceived data: " + virtuino.readBuffer);
  54.   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
  55.   if (debug) Serial.println("Response : " + *response);
  56.   client.print(*response);
  57.   client.flush();
  58.   delay(10);
  59.   client.stop();
  60.   if (debug) Serial.println("Disconnected");
  61. }
  62.  
  63.  
  64. void v_setup() {
  65.   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
  66.   //  virtuino.key = "1234";                     //This is the Virtuino password. Only requests the start with this key are accepted from the library
  67.  
  68.   connectToWiFiNetwork();
  69.   server.begin();
  70. }
Tags: virtuino6
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement