Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Timers.h>
- Timers<5> akcja;
- #include <ESP8266WiFi.h>
- #include <WebSocketsServer.h>
- #include "piny.h"
- #include "program.h"
- const char* ssid = "xxxxxx"; // WIFI network SSID
- const char* password = "xxxxxxxxx"; // WIFI network PASSWORD
- WebSocketsServer webSocket = WebSocketsServer(8000); // Default Virtuino Server port = 8000
- 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
- IPAddress gateway(192, 168, 1, 1); // set gateway to match your network. Replace with your router IP
- #define ESP01 1
- #define D1MINI 2
- int procesor = D1MINI;
- //=============================================== setup
- void setup() {
- Serial.begin(115200);
- akcja.attach(0, 2100, vLED_timer);
- akcja.attach(1, 3005, test); //
- while (!Serial) delay(1);
- setup_program();
- connectToWiFiNetwork();
- webSocket.begin();
- webSocket.onEvent(webSocketEvent);
- }
- //===================================================== loop
- void loop() {
- akcja.process(); // timer
- webSocket.loop();
- loop_program();
- }
- /*=====================================================
- ====================== UTILS =======================
- =====================================================
- You don't need to make changes to the code below
- */
- //===================================================== connectToWiFiNetwork
- void connectToWiFiNetwork() {
- if (monitor_stan = monitor_on) Serial.println("Connecting to " + String(ssid));
- // 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
- IPAddress subnet(255, 255, 255, 0); // set subnet mask to match your network
- WiFi.config(ip, gateway, subnet); // If you don't want to config IP manually disable this line
- WiFi.mode(WIFI_STA); // Configure the module as station only.
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- if (monitor_stan == monitor_on) {
- Serial.println("\nWiFi connected");
- Serial.println(WiFi.localIP()); // Insert this IP on Virtuino IoT server settings
- }
- }
- //===================================================== sendValue
- // This function sends a value to a Tag.
- // The tag and the value are converted to a json message before sending
- bool sendValue(const char* tag, String value) {
- String json = "{\"";
- json += tag;
- json += "\":\"";
- json += value;
- json += "\"}";
- if (monitor_stan == monitor_on)Serial.println("Send: " + json);
- return webSocket.broadcastTXT(json); // This function sends the message to all connected clients.
- }
- //===================================================== webSocketEvent
- //This is the server handler. It receives all the messages
- void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
- switch (type) {
- case WStype_DISCONNECTED:
- if (monitor_stan = monitor_on) Serial.printf("[%u] Disconnected!\n", num);
- break;
- case WStype_CONNECTED: {
- IPAddress ip = webSocket.remoteIP(num);
- 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]);
- sendPinsStatus(); // send the initial pin and variable values to the connected clients
- break;
- }
- case WStype_TEXT: // a new message received
- if (monitor_stan == monitor_on)Serial.printf("[%u] Received: %s\n", num, payload);
- //-- The incoming payload is a json message. The following code extracts the value from json without extra library
- String str = (char*)payload;
- int p1 = str.indexOf("{\"");
- if (p1 == 0) {
- int p2 = str.indexOf("\":");
- if (p2 > p1) {
- String tag = str.substring(p1 + 2, p2);
- p1 = str.indexOf(":\"", p2);
- if (p1 > 0) {
- p2 = str.lastIndexOf("\"}");
- if (p2 > 0) {
- String value = str.substring(p1 + 2, p2);
- onValueReceived(tag, value);
- }
- }
- }
- }
- break;
- }
- }
- //============================================================== vDelay
- void vDelay(int delayInMillis) {
- long t = millis() + delayInMillis;
- while (millis() < t) webSocket.loop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement