Advertisement
krzysp

virtuino IoT base unit main()

Jan 5th, 2023
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 4.40 KB | Source Code | 0 0
  1. #include <Timers.h>
  2. Timers<5> akcja;
  3. #include <ESP8266WiFi.h>
  4. #include <WebSocketsServer.h>
  5. #include "piny.h"
  6. #include "program.h"
  7.  
  8. const char* ssid = "xxxxxx";                // WIFI network SSID
  9. const char* password = "xxxxxxxxx";          // WIFI network PASSWORD
  10. WebSocketsServer webSocket = WebSocketsServer(8000); // Default Virtuino Server port = 8000
  11. IPAddress ip(192, 168, 1, 150);                    // where 150 is the desired IP Address. The first three numbers must be the same as the router IP
  12. IPAddress gateway(192, 168, 1, 1);                 // set gateway to match your network. Replace with your router IP
  13.  
  14. #define ESP01 1
  15. #define D1MINI 2
  16. int procesor = D1MINI;
  17.  
  18. //=============================================== setup
  19.  
  20. void setup() {
  21.   Serial.begin(115200);
  22.  
  23.   akcja.attach(0, 2100, vLED_timer);
  24.   akcja.attach(1, 3005, test);  //
  25.  
  26.   while (!Serial) delay(1);
  27.   setup_program();
  28.   connectToWiFiNetwork();
  29.   webSocket.begin();
  30.   webSocket.onEvent(webSocketEvent);
  31. }
  32.  
  33. //===================================================== loop
  34.  
  35. void loop() {
  36.   akcja.process();  // timer
  37.   webSocket.loop();
  38.   loop_program();
  39.  
  40. }
  41.  
  42.  
  43. /*=====================================================
  44.   ======================  UTILS =======================
  45.   =====================================================
  46.   You don't need to make changes to the code below
  47. */
  48. //===================================================== connectToWiFiNetwork
  49. void connectToWiFiNetwork() {
  50.   if (monitor_stan = monitor_on)  Serial.println("Connecting to " + String(ssid));
  51.  
  52.   // If you don't want to config IP manually disable the next two lines and the disables IPAddress variables  at the top of the code
  53.   IPAddress subnet(255, 255, 255, 0);        // set subnet mask to match your network
  54.   WiFi.config(ip, gateway, subnet);          // If you don't want to config IP manually disable this line
  55.   WiFi.mode(WIFI_STA);                         // Configure the module as station only.
  56.   WiFi.begin(ssid, password);
  57.   while (WiFi.status() != WL_CONNECTED) {
  58.     delay(500);
  59.     Serial.print(".");
  60.   }
  61.   if (monitor_stan == monitor_on) {
  62.     Serial.println("\nWiFi connected");
  63.     Serial.println(WiFi.localIP());              // Insert this IP on Virtuino IoT server settings
  64.   }
  65. }
  66.  
  67.  
  68. //===================================================== sendValue
  69. // This function sends a value to a Tag.
  70. // The tag and the value are converted to a json message before sending
  71.  
  72. bool sendValue(const char* tag, String value) {
  73.   String json = "{\"";
  74.   json += tag;
  75.   json += "\":\"";
  76.   json += value;
  77.   json += "\"}";
  78.   if (monitor_stan == monitor_on)Serial.println("Send: " + json);
  79.   return webSocket.broadcastTXT(json);     // This function sends the message to all connected clients.
  80. }
  81.  
  82. //===================================================== webSocketEvent
  83. //This is the server handler. It receives all the messages
  84.  
  85. void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
  86.   switch (type) {
  87.     case WStype_DISCONNECTED:
  88.       if (monitor_stan = monitor_on) Serial.printf("[%u] Disconnected!\n", num);
  89.       break;
  90.     case WStype_CONNECTED: {
  91.         IPAddress ip = webSocket.remoteIP(num);
  92.         if (monitor_stan == monitor_on) Serial.printf("[%u] New client connected - IP: %d.%d.%d.%d \n", num, ip[0], ip[1], ip[2], ip[3]);
  93.         sendPinsStatus();         // send the initial pin and variable values to the connected clients
  94.         break;
  95.       }
  96.     case WStype_TEXT:  // a new message received
  97.       if (monitor_stan == monitor_on)Serial.printf("[%u] Received: %s\n", num, payload);
  98.       //-- The incoming payload is a json message. The following code extracts the value from json without extra library
  99.       String str = (char*)payload;
  100.       int p1 = str.indexOf("{\"");
  101.       if (p1 == 0) {
  102.         int p2 = str.indexOf("\":");
  103.         if (p2 > p1) {
  104.           String tag = str.substring(p1 + 2, p2);
  105.           p1 = str.indexOf(":\"", p2);
  106.           if (p1 > 0) {
  107.             p2 = str.lastIndexOf("\"}");
  108.             if (p2 > 0) {
  109.               String value = str.substring(p1 + 2, p2);
  110.               onValueReceived(tag, value);
  111.             }
  112.           }
  113.         }
  114.       }
  115.       break;
  116.   }
  117. }
  118.  
  119. //============================================================== vDelay
  120. void vDelay(int delayInMillis) {
  121.   long t = millis() + delayInMillis;
  122.   while (millis() < t) webSocket.loop();
  123. }
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement