Skip to content

How to manage esp32 client connect to heroku websocket nodejs server #11

@MaxESP

Description

@MaxESP

Dear Sir ,

I have a nodejs server running on a local machine connect to esp32 client that working fine .

Server:
const express = require('express')
const app = express()
const server = require('http').createServer(app);
const WebSocket = require('ws');

const wss = new WebSocket.Server({ server:server });

wss.on('connection', function connection(ws) {
console.log('A new client Connected!');
ws.send('Welcome New Client!');

ws.on('message', function incoming(message) {
console.log('received: %s', message);

server.listen(83, () => console.log(Lisening on port :83))

client side 👍

#include <WiFi.h>
#include <WiFiClient.h>
//#include <WebSocketsServer.h>
#include <WebSockets.h>
#include <WebSocketsClient.h>
//#include <WebServer.h>

const char* ssid = "X";
const char* password = "X";

IPAddress local_IP(192, 168, 1,65);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255,255,255,0);
IPAddress primaryDNS(8, 8, 8, 8);

// Initialise websockets, web server and servo
//WebSocketsServer webSocket = WebSocketsServer(1337);

WebSocketsClient webSocket;

void setup(void) {

Serial.begin(115200);
// Initialize the output variables as outputs

// Connect to Wi-Fi network with SSID and password
if (!WiFi.config(local_IP, gateway, subnet, primaryDNS)) {
Serial.println("STA Failed to configure");
}

// Connect to Wi-Fi network with SSID and password
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
// while (WiFi.status() != WL_CONNECTED) {
// delay(500);
//digitalWrite(32,HIGH);
//
// //Serial.print(".");
// }
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());

webSocket.begin("192.168.1.3", 83, "/");
delay(4000);
webSocket.onEvent(webSocketEvent);

// Configure web server to host HTML files
//server.onNotFound({
// if(!handleFileRead(server.uri()))
// server.send(404, "text/plain", "FileNotFound");
// });
// server.begin();
Serial.println("HTTP server started");

}

void loop() {
// put your main code here, to run repeatedly:
webSocket.loop();
//String message= "hello from client";
//webSocket.broadcastTXT(message);

}

void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {

//const int PROG = digitalRead(27);

switch(type) {

// Runs when a user disconnects
case WStype_DISCONNECTED: {
  Serial.printf("[WSc] Disconnected!\n");
  break;
}

// Runs when a user connects
case WStype_CONNECTED: {
//  IPAddress ip = webSocket.remoteIP(num);
  Serial.printf("[WSc] Connected to url: %s\n", payload);
       
        webSocket.sendTXT("hello i am Connected");
       
       }
  break;
case WStype_TEXT:{
  Serial.printf("[WSc] get text: %s\n", payload);
    String text = String((char *) &payload[0]);

  Serial.println (text);
    break;     

}

// send data to server
// webSocket.sendBIN(payload, length);
// break;
// case WStype_ERROR:
// case WStype_FRAGMENT_TEXT_START:
// case WStype_FRAGMENT_BIN_START:
// case WStype_FRAGMENT:
// case WStype_FRAGMENT_FIN:
// break;
}

}

How i manage to put this server on heroku cloud to be working?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions