Skip to content

Problem connecting to websockets server #43

@happytm

Description

@happytm

I created this sketch but it gets this error message in browser:

Settings-web v1.3.3
72WebSocket connection to 'ws:/' failed: WebSocket is closed before the connection is established.
script.js?857bfbda46b61d04e47d:1 WebSocket connection to 'ws://10.1.10.189:81/' failed: WebSocket is closed before the connection is established.
(anonymous) @ script.js?857bfbda46b61d04e47d:1
setTimeout (async)
open @ script.js?857bfbda46b61d04e47d:1
init @ script.js?857bfbda46b61d04e47d:1
parse @ script.js?857bfbda46b61d04e47d:1
requset @ script.js?857bfbda46b61d04e47d:1
await in requset (async)
load @ script.js?857bfbda46b61d04e47d:1
Settings @ script.js?857bfbda46b61d04e47d:1
window.onload @ script.js?857bfbda46b61d04e47d:1
load (async)
(anonymous) @ script.js?857bfbda46b61d04e47d:1
(anonymous) @ script.js?857bfbda46b61d04e47d:1
script.js?857bfbda46b61d04e47d:1 WebSocket connection to 'ws://10.1.10.189:81/' failed: WebSocket is closed before the connection is established.

#include <esp_wifi.h>
#include <EEPROM.h>
#include <LittleFS.h>
#include <SettingsGyverWS.h>
#include <TableFileStatic.h>

const char* apSSID = "ESP"; const char* apPassword = ""; const int apChannel = 6; const int hidden = 0;                   // If hidden is 0 change show hidden to true in remote code.
String ssid,password;
int device, rssi, sensorValues[4], sensorTypes[4]; 
float voltage;
uint8_t mac[6],receivedCommand[6],showConfig[256];

SettingsGyverWS sett("Sensors Monitor/Control");

void build(sets::Builder& b) {
    b.Plot(H(plot1), "/file_plot1.tbl");  // из бинарной таблицы
    b.Plot(H(plot2), "/file_plot2.csv");  // из csv таблицы
}

void setup() {
    Serial.begin(115200);
    Serial.println();

    EEPROM.begin(512);

    EEPROM.readBytes(0, showConfig,256);for(int i=0;i<256;i++){Serial.printf("%d ", showConfig[i]);}Serial.println();
  
    if (showConfig[0] == 0 || showConfig[0] == 255){
    // Setup device numbers and wifi Channel for remote devices in EEPROM permanantly.
    for (int i = 6; i < 256; i = i+10) {EEPROM.writeByte(i, i);EEPROM.writeByte(i+1, 107);EEPROM.writeByte(i+2, apChannel);}
    EEPROM.writeByte(0, apChannel);EEPROM.commit();
    }
  
    WiFi.softAP(apSSID, apPassword, apChannel, hidden);
    esp_wifi_set_event_mask(WIFI_EVENT_MASK_NONE); // This line is must to activate probe request received event handler.
    Serial.print("AP started with SSID: ");Serial.println(apSSID);

    int n = WiFi.scanNetworks();
    int strongestRSSI = -80;
    for (int i = 0; i < n; i++) {if (WiFi.RSSI(i) > strongestRSSI) {strongestRSSI = WiFi.RSSI(i); ssid = WiFi.SSID(i);}}
    if (WiFi.SSID(0) == ssid && WiFi.encryptionType(0) == WIFI_AUTH_OPEN) {WiFi.begin(ssid.c_str(), password.c_str());}

    LittleFS.begin(true);

    sett.begin();
    sett.onBuild(build);

    WiFi.onEvent(probeRequest,WiFiEvent_t::ARDUINO_EVENT_WIFI_AP_PROBEREQRECVED);
    Serial.print("Waiting for probe requests ... ");
}


void loop() {
    sett.tick();
   }         

void probeRequest(WiFiEvent_t event, WiFiEventInfo_t info) 
{ 
  Serial.println();
  Serial.print("Probe Received :  ");for (int i = 0; i < 6; i++) {Serial.printf("%02X", info.wifi_ap_probereqrecved.mac[i]);if (i < 5)Serial.print(":");}Serial.println();
  Serial.print("Connect at IP: ");Serial.print(WiFi.localIP()); Serial.println(" to monitor and control whole network");
  
  // Allow data from device ID ending in number 6, voltage value between 2.4V and 3.6V. and signal is stonger than -70.
  for (int i = 6; i < 256; i = i+10) if (info.wifi_ap_probereqrecved.mac[0] == i )
  {
    if (info.wifi_ap_probereqrecved.mac[1] > 120 && info.wifi_ap_probereqrecved.mac[1] < 180)
    {
      // This helps reduce interference from unknown devices from far away with weak signals.
      if (info.wifi_ap_probereqrecved.rssi > -70)  
      {
      
      device = info.wifi_ap_probereqrecved.mac[0];
            
      for (int i = 0; i < 6; i++) mac[i] = showConfig[i+device];           // Prepare command to be sent to remote device.
      for (int j = 0; j < 4; j++) sensorTypes[j] = showConfig[j+device+6]; // Assign sensor types to the particular device.
      Serial.println("Contents of command data saved in EEPROM for this device: "); EEPROM.readBytes(0, showConfig,256);for(int i=0;i<10;i++){Serial.printf("%d ", showConfig[i+device]);} Serial.println();
             
      esp_err_t err = esp_wifi_set_mac(WIFI_IF_AP, &mac[0]);  //https://randomnerdtutorials.com/get-change-esp32-esp8266-mac-address-arduino/ https://github.com/justcallmekoko/ESP32Marauder/issues/418
      Serial.print("Command sent to remote device :  "); for (int i = 0; i < 6; i++) { Serial.print(mac[i]);} Serial.println();        
                
      rssi = info.wifi_ap_probereqrecved.rssi;         
      voltage = info.wifi_ap_probereqrecved.mac[1];
      voltage = voltage * 2 / 100;
      sensorValues[0] = info.wifi_ap_probereqrecved.mac[2];sensorValues[1] = info.wifi_ap_probereqrecved.mac[3];sensorValues[2] = info.wifi_ap_probereqrecved.mac[4];sensorValues[3] = info.wifi_ap_probereqrecved.mac[5];
            
            
       // ===== бинарная таблица =====
            TableFileStatic t(&LittleFS, "/file_plot1.tbl");

            // инициализация, должна быть вызвана хотя бы один раз после непосредственного создания файла
            t.init(13, cell_t::Uint32, cell_t::Int8, cell_t::Float, cell_t::Int8, cell_t::Int8, cell_t::Int8, cell_t::Int8, cell_t::Int8, cell_t::Int8, cell_t::Int8, cell_t::Int8, cell_t::Int8, cell_t::Int8);

            // добавление в файл
            t.append(sett.rtc.getUnix(), device, voltage, rssi*(-1), random(100), sensorTypes[0], sensorValues[0], sensorTypes[1], sensorValues[1], sensorTypes[2], sensorValues[2], sensorTypes[3], sensorValues[3]);
            Serial.println(sett.rtc.getUnix());
           
         
      if (voltage < 2.50) { 
        Serial.print("Low battery for device: "); Serial.println(String(device)); 
        }     // if voltage of battery gets to low, print the warning below.
      }           
    }
  }     
       // In some cases send command only once.
       if (mac[1] == 105 || mac[1] == 106 || mac[1] == 108 || mac[1] == 110){for (int i = 6; i < 256; i = i+10) {EEPROM.writeByte(i+1, 107);EEPROM.writeByte(i+2, apChannel);EEPROM.commit();}}
} 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions