krzysp

virtuino grzejnik blok virtuino.h

Jan 2nd, 2023
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 4.97 KB | Source Code | 0 0
  1. #include <ESP8266WiFi.h>
  2.  
  3. //--- SETTINGS ------------------------------------------------
  4. const char* ssid = "xxxx";                // WIFI network SSID
  5. const char* password = "xxxxxxxxxx";          // 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. String V0 = "";
  15. String V1 = "";
  16. String V2 = "";
  17. String V3 = "";
  18. String V4 = "";
  19. String V5 = "";
  20. String V6 = "";
  21. String V7 = "";
  22. String V8 = "";
  23. String V9 = "";
  24. String V10 = "";
  25.  
  26.  
  27. boolean debug = true;              // set this variable to false on the finale code to decrease the request time.
  28.  
  29. //============================================================== connectToWiFiNetwork
  30. void connectToWiFiNetwork() {
  31.   Serial.println("Connecting to " + String(ssid));
  32.   // If you don't want to config IP manually disable the next two lines
  33.   IPAddress subnet(255, 255, 255, 0);        // set subnet mask to match your network
  34.   WiFi.config(ip, gateway, subnet);          // If you don't want to config IP manually disable this line
  35.   WiFi.mode(WIFI_STA);                       // Config module as station only.
  36.   WiFi.begin(ssid, password);
  37.   while (WiFi.status() != WL_CONNECTED) {
  38.     delay(500);
  39.     Serial.print(".");
  40.   }
  41.   Serial.println("");
  42.   Serial.println("WiFi connected");
  43.   Serial.println(WiFi.localIP());
  44. }
  45.  
  46.  
  47. //============================================================== onCommandReceived
  48. //==============================================================
  49. /* This function is called every time Virtuino app sends a request to server to change a Pin value
  50.    The 'variableType' can be   V=Virtual Pin
  51.    The 'variableIndex' is the pin number index of Virtuino app
  52.    The 'valueAsText' is the value that has sent from the app  in Strin format */
  53. void vLED_timer();
  54. void Read_V1(String value_Text);
  55. void Read_V2(String value_Text);
  56. void Read_V3(String value_Text);
  57. void Read_V4(String value_Text);
  58. void onReceived(char variableType, uint8_t variableIndex, String valueAsText) {
  59.   if (variableType == 'V') {
  60.     //  float value = valueAsText.toFloat();        // convert the value to float. The valueAsText have to be numerical
  61.     //  if (variableIndex < V_memory_count) V[variableIndex] = value;          // copy the received value to arduino V memory array
  62.  
  63.     if (variableIndex == 1) Read_V1(valueAsText);
  64.     if (variableIndex == 2) Read_V2(valueAsText);
  65.     if (variableIndex == 3) Read_V3(valueAsText);
  66.     if (variableIndex == 4) Read_V4(valueAsText);
  67.   }
  68. }
  69.  
  70. //==============================================================
  71. /* This function is called every time Virtuino app requests to read a pin value*/
  72. String onRequested(char variableType, uint8_t variableIndex) {
  73.   if (variableType == 'V') {
  74.  
  75.  
  76.     if (variableIndex == 0) {
  77.       vLED_timer();
  78.       Serial.println("=================");
  79.     }
  80.     if (variableIndex == 0) return  V0;
  81.     if (variableIndex == 6) return  V6;
  82.     if (variableIndex == 7) return  V7;
  83.     if (variableIndex == 8) return  V8;
  84.   }
  85.   return "";
  86. }
  87.  
  88.  
  89. //==============================================================
  90. void virtuinoRun() {
  91.   WiFiClient client = server.available();
  92.   if (!client) return;
  93.   if (debug) Serial.println("Connected");
  94.   unsigned long timeout = millis() + 3000;
  95.   while (!client.available() && millis() < timeout) delay(1);
  96.   if (millis() > timeout) {
  97.     Serial.println("timeout");
  98.     client.flush();
  99.     client.stop();
  100.     return;
  101.   }
  102.   virtuino.readBuffer = "";  // clear Virtuino input buffer. The inputBuffer stores the incoming characters
  103.   while (client.available() > 0) {
  104.     char c = client.read();         // read the incoming data
  105.     virtuino.readBuffer += c;       // add the incoming character to Virtuino input buffer
  106.     if (debug) Serial.write(c);
  107.   }
  108.   client.flush();
  109.   if (debug) Serial.println("\nReceived data: " + virtuino.readBuffer);
  110.   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
  111.   if (debug) Serial.println("Response : " + *response);
  112.   client.print(*response);
  113.   client.flush();
  114.   delay(10);
  115.   client.stop();
  116.   if (debug) Serial.println("Disconnected");
  117. }
  118.  
  119.  
  120. void v_setup() {
  121.   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
  122.   //  virtuino.key = "1234";                     //This is the Virtuino password. Only requests the start with this key are accepted from the library
  123.  
  124.   connectToWiFiNetwork();
  125.   server.begin();
  126. }
Tags: virtuino
Advertisement
Add Comment
Please, Sign In to add comment