krzysp

grzałka virtuino iot blok main()

Jan 10th, 2023
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 4.49 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 = "ty0";                // WIFI network SSID
  9. const char* password = "kp12345678";          // WIFI network PASSWORD
  10. WebSocketsServer webSocket = WebSocketsServer(8000); // Default Virtuino Server port = 8000
  11. 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
  12. IPAddress gateway(192, 168, 2, 1);         // set gateway to match your network. Replace with your router IP
  13.  
  14. //=============================================== setup
  15.  
  16. void setup() {
  17.   Serial.begin(115200);
  18.   if (monitor_stan == monitor_on) {
  19.     delay(100);
  20.     Serial.println();
  21.     Serial.println(F(__FILE__));
  22.   }
  23.   while (!Serial) delay(1);
  24.   connectToWiFiNetwork();
  25.   webSocket.begin();
  26.   webSocket.onEvent(webSocketEvent);
  27.  
  28.   akcja.attach(0, 2100, LED_timer);
  29.   akcja.attach(1, 60050, czas_timer);  //
  30.  
  31.   p_setup();      // setup program
  32. }
  33.  
  34. //===================================================== loop
  35.  
  36. void loop() {
  37.   akcja.process();  // timer
  38.   webSocket.loop();
  39.   program_grzalka();      // program
  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