send wifi credentials to ESP32 via web Bluetooth

320 Views Asked by At

Im trying to send wifi credentials to esp32 BLE device using web bluetooth. I'm getting the error Received invalid JSON for every combination I've tried so far. Right now the code is simple and goes:

const connectToDeviceAndSubscribeToUpdates = async () => {
    const device = await navigator.bluetooth.requestDevice({
        // filers: ["ESP32-E89F6DD0DF34"],
        acceptAllDevices: true,
        optionalServices: ["0000aaaa-ead2-11e7-80c1-9a214cf093ae"],
    });

    let server = await device.gatt.connect();
    const service = await server.getPrimaryService(
        "0000aaaa-ead2-11e7-80c1-9a214cf093ae"
    );

    let characteristic = await service.getCharacteristic(
        "00005555-ead2-11e7-80c1-9a214cf093ae"
    );
    console.log(characteristic);

    var thing = await characteristic.readValue();
    var encoder = new TextEncoderStream();
    let data = { ssidPrim: "", pwPrim: "", ssidSec: "", pwSec: "" };
    await characteristic.writeValueWithoutResponse(
        encoder.encode(JSON.stringify(data))
    );
    console.log(response);
};

document.getElementById("connect").addEventListener("click", (e) => {
    connectToDeviceAndSubscribeToUpdates();
});

the JSON let data = { ssidPrim: "", pwPrim: "", ssidSec: "", pwSec: "" }; has been modified to all shapes for example {ssid:'',pass:''} {ssid:'',password:''} {ssid:'',passwd:''} {information:[{ssidPrim:'',passPrim:''}]} etc. (the empty quotes were filled).

Also I've tried to send it with the methods writeValue 'writeValueWithResponseandwriteValueWithoutResponse` all the same results.

I've read a lot of tutorials but moste of them in C, C++ and I could translate the information into JavaScript.

Right now I'll try any ideas.

The ESP module is Olimex ESP32-WROOM-32F.

I'm using this function in windows powershell to read from the device

function read-com {
 $port= new-Object System.IO.Ports.SerialPort COM8,115200,None,8,one
 $port.Open()
 do {
 $line = $port.ReadLine()
 Write-Host $line 
 } while ($port.IsOpen)
 }

Theese are the information from connecting using the above funtion to sending information via web-bluetooth

ets Jul 29 2019 12:21:46

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0030,len:1344
load:0x40078000,len:13836
load:0x40080400,len:3608
entry 0x400805f0
Build: Feb 22 2023 22:32:28
Read from preferences:
primary SSID: hhhh password: hhhh
secondary SSID: eeee password: tttttttt
BLE advertising using ESP32-E89F6DD0DF34
Start scanning for networks
Found AP: UPC1690653 RSSI: -42
Found AP: UPC5088637 RSSI: -71
Found AP: Wi-Free #InternetUPCNajszybszy RSSI: -71
Found AP: UPC1690653_EXT RSSI: -73
Found AP: UPC3006601 RSSI: -77
Found AP: Wi-Free #InternetUPCNajszybszy RSSI: -77
Found AP: UPC244036972 RSSI: -83
Found AP: Wi-Free #InternetUPCNajszybszy RSSI: -85
Found AP: UPC4415581 RSSI: -85
Found AP: UPCA1C5DBA RSSI: -86
Found AP: NETIASPOT-72E0C0 RSSI: -86
Found AP: Wi-Free #InternetUPCNajszybszy RSSI: -87
Found AP: UPC1369909 RSSI: -88
Found AP: UPC1176318 RSSI: -90
Found AP: Apartamentowiec Bonczyka_25 RSSI: -91
Found AP: Wi-Free #InternetUPCNajszybszy RSSI: -91
Found AP: [air purifier]_E30AJT1003236X RSSI: -95
Found AP: Wi-Free #InternetUPCNajszybszy RSSI: -95
Found AP: UPC245739629 RSSI: -95
Found AP: UPC5562094 RSSI: -97
Found AP: Wi-Free #InternetUPCNajszybszy RSSI: -97
Could not find any AP


##################################
Internal Total heap 258752, internal Free Heap 151220
SPIRam Total heap 0, SPIRam Free Heap 0
ChipRevision 3, Cpu Freq 240, SDK Version v4.4.3
Flash Size 4194304, Flash Speed 80000000
##################################


BLE client connected
BLE onRead request
Stored settings: {"ssidPrim":"hhhh","pwPrim":"hhhh","ssidSec":"eeee","pwSec":"tttttttt"}
BLE client disconnected
BLE client connected
BLE onRead request
Stored settings: {"ssidPrim":"hhhh","pwPrim":"hhhh","ssidSec":"eeee","pwSec":"tttttttt"}
Received over BLE: {"information":{"ssidPrim":"UPC1690653","pwPrim":"******"}}
Received invalid JSON
0

There are 0 best solutions below