Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bshambaugh/4ea7de432a6aee345ebc4707ea42e11e to your computer and use it in GitHub Desktop.
Save bshambaugh/4ea7de432a6aee345ebc4707ea42e11e to your computer and use it in GitHub Desktop.
Websocket Server sending to Heltec ESP32 sending to Heltec CubeCell
/**************************websocket_example.js*************************************************/
var bodyParser = require("body-parser");
const express = require('express'); //express framework to have a higher level of methods
const app = express(); //assign app variable the express class/method
var http = require('http');
var path = require("path");
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
const server = http.createServer(app);//create a server
//***************this snippet gets the local ip of the node.js server. copy this ip to the client side code and add ':3000' *****
//****************exmpl. 192.168.56.1---> var sock =new WebSocket("ws://192.168.56.1:3000");*************************************
require('dns').lookup(require('os').hostname(), function (err, add, fam) {
console.log('addr: '+add);
})
/**********************websocket setup**************************************************************************************/
//var expressWs = require('express-ws')(app,server);
const WebSocket = require('ws');
const s = new WebSocket.Server({ server });
//when browser sends get request, send html file to browser
// viewed at http://localhost:30000
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname + '/index.html'));
});
//*************************************************************************************************************************
//***************************ws chat server********************************************************************************
//app.ws('/echo', function(ws, req) {
s.on('connection',function(ws,req){
ws.on('close', function(){
console.log("lost one client");
});
ws.send("new client connected");
console.log("new client connected");
var interval = setInterval(function(){
console.log('Hello World');
ws.send("hello from server");
}, 2000);
});
server.listen(3000);
// ESP32_websocket_recieve_and_transmit
/* Heltec Automation send communication test example
*
* Function:
* 1. Send data from a CubeCell device over hardware
*
*
* this project also realess in GitHub:
* https://github.com/HelTecAutomation/ASR650x-Arduino
* */
#include <ESP32_LoRaWAN.h>
#include "Arduino.h"
#define RF_FREQUENCY 915000000 // Hz
#define TX_OUTPUT_POWER 14 // dBm
#define LORA_BANDWIDTH 0 // [0: 125 kHz,
// 1: 250 kHz,
// 2: 500 kHz,
// 3: Reserved]
#define LORA_SPREADING_FACTOR 7 // [SF7..SF12]
#define LORA_CODINGRATE 1 // [1: 4/5,
// 2: 4/6,
// 3: 4/7,
// 4: 4/8]
#define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx
#define LORA_SYMBOL_TIMEOUT 0 // Symbols
#define LORA_FIX_LENGTH_PAYLOAD_ON false
#define LORA_IQ_INVERSION_ON false
#define RX_TIMEOUT_VALUE 1000
#define BUFFER_SIZE 200 // Define the payload size here
char txpacket[BUFFER_SIZE];
char rxpacket[BUFFER_SIZE];
static RadioEvents_t RadioEvents;
void OnTxDone( void );
void OnTxTimeout( void );
int16_t txNumber;
int16_t rssi,rxSize;
uint32_t license[4] = {0xE43F317B, 0x48CAC77E, 0xC8891C04, 0xB77AFA9C};
/* Heltec Automation send communication test example
*
* Function:
* 1. Send data from a CubeCell device over hardware
*
*
* this project also realess in GitHub:
* https://github.com/HelTecAutomation/ASR650x-Arduino
* */
#include "Arduino.h"
#include <WiFi.h>
#include <WebSocketClient.h>
boolean handshakeFailed=0;
String data= "";
char path[] = "/"; //identifier of this device
const char* ssid = "ssid";
const char* password = "password";
char* host = "10.0.0.5"; //replace this ip address with the ip address of your Node.Js server
const int espport= 3000;
WebSocketClient webSocketClient;
unsigned long previousMillis = 0;
unsigned long currentMillis;
unsigned long interval=300; //interval for sending data to the websocket server in ms
// Use WiFiClient class to create TCP connections
WiFiClient client;
void setup() {
Serial.begin(115200);
while (!Serial);
SPI.begin(SCK,MISO,MOSI,SS);
Mcu.init(SS,RST_LoRa,DIO0,DIO1,license);
txNumber=0;
rssi=0;
RadioEvents.TxDone = OnTxDone;
RadioEvents.TxTimeout = OnTxTimeout;
Radio.Init( &RadioEvents );
Radio.SetChannel( RF_FREQUENCY );
Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
LORA_SPREADING_FACTOR, LORA_CODINGRATE,
LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
true, 0, 0, LORA_IQ_INVERSION_ON, 3000 );
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
delay(1000);
wsconnect();
// wifi_set_sleep_type(LIGHT_SLEEP_T);
}
void loop() {
delay(1000);
Serial.printf("\r\nsending packet \"%s\" , length %d\r\n",txpacket, strlen(txpacket));
Radio.Send( (uint8_t *)txpacket, strlen(txpacket) ); //send the package out
Serial.println("hello");
if (client.connected()) {
webSocketClient.getData(data);
Serial.println("I am a connected client");
Serial.println(data);
txNumber++;
sprintf(txpacket,"%s",data);
sprintf(txpacket+strlen(txpacket),"%d",txNumber);
sprintf(txpacket+strlen(txpacket),"%s"," Rssi : ");
sprintf(txpacket+strlen(txpacket),"%d",rssi);
if (data.length() > 0) {
Serial.println(data);
}
}
}
//*********************************************************************************************************************
//***************function definitions**********************************************************************************
void wsconnect(){
// Connect to the websocket server
if (client.connect(host, espport)) {
Serial.println("Connected");
} else {
Serial.println("Connection failed.");
delay(1000);
if(handshakeFailed){
handshakeFailed=0;
ESP.restart();
}
handshakeFailed=1;
}
// Handshake with the server
webSocketClient.path = path;
webSocketClient.host = host;
if (webSocketClient.handshake(client)) {
Serial.println("Handshake successful");
} else {
Serial.println("Handshake failed.");
delay(4000);
if(handshakeFailed){
handshakeFailed=0;
ESP.restart();
}
handshakeFailed=1;
}
}
void OnTxDone( void )
{
Serial.print("TX done......");
}
void OnTxTimeout( void )
{
Radio.Sleep( );
Serial.print("TX Timeout......");
}
// CubeCell receive code
/* Heltec Automation Receive communication test example
*
* Function:
* 1. Receive the same frequency band lora signal program
*
*
* this project also realess in GitHub:
* https://github.com/HelTecAutomation/ASR650x-Arduino
* */
#include "LoRaWan_APP.h"
#include "Arduino.h"
/*
* set LoraWan_RGB to 1,the RGB active in loraWan
* RGB red means sending;
* RGB green means received done;
*/
#ifndef LoraWan_RGB
#define LoraWan_RGB 0
#endif
#define RF_FREQUENCY 915000000 // Hz
#define TX_OUTPUT_POWER 14 // dBm
#define LORA_BANDWIDTH 0 // [0: 125 kHz,
// 1: 250 kHz,
// 2: 500 kHz,
// 3: Reserved]
#define LORA_SPREADING_FACTOR 7 // [SF7..SF12]
#define LORA_CODINGRATE 1 // [1: 4/5,
// 2: 4/6,
// 3: 4/7,
// 4: 4/8]
#define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx
#define LORA_SYMBOL_TIMEOUT 0 // Symbols
#define LORA_FIX_LENGTH_PAYLOAD_ON false
#define LORA_IQ_INVERSION_ON false
#define RX_TIMEOUT_VALUE 1000
#define BUFFER_SIZE 200 // Define the payload size here
char txpacket[BUFFER_SIZE];
char rxpacket[BUFFER_SIZE];
static RadioEvents_t RadioEvents;
int16_t txNumber;
int16_t rssi,rxSize;
void setup() {
Serial.begin(115200);
txNumber=0;
rssi=0;
RadioEvents.RxDone = OnRxDone;
Radio.Init( &RadioEvents );
Radio.SetChannel( RF_FREQUENCY );
Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON,
0, true, 0, 0, LORA_IQ_INVERSION_ON, true );
turnOnRGB(COLOR_SEND,0); //change rgb color
Serial.println("into RX mode");
}
void loop()
{
Radio.Rx( 0 );
delay(500);
Radio.IrqProcess( );
}
void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
{
rssi=rssi;
rxSize=size;
memcpy(rxpacket, payload, size );
rxpacket[size]='\0';
turnOnRGB(COLOR_RECEIVED,0);
Radio.Sleep( );
Serial.printf("\r\nreceived packet \"%s\" with rssi %d , length %d\r\n",rxpacket,rssi,rxSize);
}
@bshambaugh
Copy link
Author

refactor ESP32 code with this knowledge::::

/* Heltec Automation send communication test example
*

#include <ESP32_LoRaWAN.h>
#include "Arduino.h"

#define RF_FREQUENCY 915000000 // Hz

#define TX_OUTPUT_POWER 14 // dBm

#define LORA_BANDWIDTH 0 // [0: 125 kHz,
// 1: 250 kHz,
// 2: 500 kHz,
// 3: Reserved]
#define LORA_SPREADING_FACTOR 7 // [SF7..SF12]
#define LORA_CODINGRATE 1 // [1: 4/5,
// 2: 4/6,
// 3: 4/7,
// 4: 4/8]
#define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx
#define LORA_SYMBOL_TIMEOUT 0 // Symbols
#define LORA_FIX_LENGTH_PAYLOAD_ON false
#define LORA_IQ_INVERSION_ON false

#define RX_TIMEOUT_VALUE 1000
#define BUFFER_SIZE 200 // Define the payload size here

char txpacket[BUFFER_SIZE];
char rxpacket[BUFFER_SIZE];

static RadioEvents_t RadioEvents;
void OnTxDone( void );
void OnTxTimeout( void );

int16_t txNumber;

int16_t rssi,rxSize;

uint32_t license[4] = {0xE43F317B, 0x48CAC77E, 0xC8891C04, 0xB77AFA9C};

char thisString[] = "testing";

void setup() {
Serial.begin(115200);
while (!Serial);
SPI.begin(SCK,MISO,MOSI,SS);
Mcu.init(SS,RST_LoRa,DIO0,DIO1,license);

txNumber=0;
rssi=0;

RadioEvents.TxDone = OnTxDone;
RadioEvents.TxTimeout = OnTxTimeout;

Radio.Init( &RadioEvents );
Radio.SetChannel( RF_FREQUENCY );
Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
                               LORA_SPREADING_FACTOR, LORA_CODINGRATE,
                               LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
                               true, 0, 0, LORA_IQ_INVERSION_ON, 3000 ); 

}

void loop()
{
delay(1000);
txNumber++;
sprintf(txpacket,"%s",thisString);
sprintf(txpacket+strlen(txpacket),"%d",txNumber);
sprintf(txpacket+strlen(txpacket),"%s"," Rssi : ");
sprintf(txpacket+strlen(txpacket),"%d",rssi);

Serial.printf("\r\nsending packet "%s" , length %d\r\n",txpacket, strlen(txpacket));

Radio.Send( (uint8_t *)txpacket, strlen(txpacket) ); //send the package out
}

void OnTxDone( void )
{
Serial.print("TX done......");
}

void OnTxTimeout( void )
{
Radio.Sleep( );
Serial.print("TX Timeout......");
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment