Wiznet makers

mason

Published March 31, 2022 ©

173 UCC

21 WCC

33 VAR

0 Contests

0 Followers

0 Following

Original Link

Pixl.js Bluetooth to Ethernet MQTT Bridge

Pixl.js Bluetooth to Ethernet MQTT Bridge

COMPONENTS
PROJECT DESCRIPTION
  This is just a very simple example of using Puck.js to send received Bluetooth LE advertising packets over MQTT to an server. Just wire up an Arduino WIZnet adaptor?as shown on this page?then upload the following code with the?MQTTSERVER?variable set to the IP address of a server running a (non-HTTPS) MQTT server. To view the received packets, you can run a command such as this if you have the Mosquitto tools installed on your PC:
mosquitto_sub -h 192.168.1.70 -v -t "/advertise"
NOTE:?This is a simple example and isn't designed to deal with a large volume of advertising packets. To make this more robust you might want to consider adding a whitelist of BLE addresses that will be forwarded.

Source Code?

var MQTTSERVER = "192.168.1.70";
var eth, mqtt;
var packets = 0;
var infoInterval;

function onInit() {
  Terminal.println("nn"); // add some clear lines
  Terminal.println("Ethernet setup");
  SPI1.setup({ mosi:D11, miso:D12, sck:D13 });
  eth = require("WIZnet").connect(SPI1, D10 /*CS*/);
  eth.setIP({mac:"00:08:dc:ab:cd:ef"});
  eth.setIP(); // DHCP
  Terminal.println("Ethernet ok, "+eth.getIP().ip);
  Terminal.print("MQTT...");
  mqtt = require("MQTT").connect({
    host: MQTTSERVER,
  });
  mqtt.on('connected', function() {
    Terminal.println("Connected!");
    startScan();
  });
  mqtt.on('disconnected', function() {
    Terminal.println("MQTT disconnected");
    stopScan();
  });
}

function startScan() {
  infoInterval = setInterval(function() {
    Terminal.println((packets/10)+" adverts/sec");
    packets = 0;
  }, 10000);
  NRF.setScan(function(dev) {
    delete dev.data; // we don't want to send the RAW data
    mqtt.publish("/advertise", JSON.stringify(dev));
    packets++;
  });
}

function stopScan() {
  clearInterval(infoInterval);
  NRF.setScan(); // stop scanning
}

onInit();
Documents
Comments Write