Skip to content

Instantly share code, notes, and snippets.

@venetanji
Last active June 13, 2022 19:19
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save venetanji/d71dc271ebf51236ec6ce99aa48eee26 to your computer and use it in GitHub Desktop.
#include <ESP8266WiFi.h>
#include <ArduinoWebsockets.h>
// include wpa2 enterprise code
extern "C" {
#include "user_interface.h"
#include "wpa2_enterprise.h"
}
// SSID, Username and password. Update with yours!
static const char* ssid = "ssid";
static const char* username = "username"; //
static const char* password = "password";
using namespace websockets;
WebsocketsClient client;
const char* websockets_server_host = "xxx.xxx.xxx.xxx"; //Enter websocket server adress
const uint16_t websockets_server_port = 80; //for node-red use 1880
void setup() {
Serial.begin(115200);
delay(1000);
// Setting ESP into STATION mode only (no AP mode or dual mode)
wifi_set_opmode(STATION_MODE);
struct station_config wifi_config;
memset(&wifi_config, 0, sizeof(wifi_config));
strcpy((char*)wifi_config.ssid, ssid);
wifi_station_set_config(&wifi_config);
// Clean up to be sure no old data is still inside
wifi_station_clear_cert_key();
wifi_station_clear_enterprise_ca_cert();
wifi_station_set_wpa2_enterprise_auth(1);
wifi_station_set_enterprise_username((uint8*)username, strlen(username));
wifi_station_set_enterprise_identity((uint8*)username, strlen(username));
wifi_station_set_enterprise_password((uint8*)password, strlen(password));
wifi_station_connect();
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
bool connected = client.connect(websockets_server_host, websockets_server_port, "/");
if(connected) {
Serial.println("Connected!");
client.send("Hello Server");
} else {
Serial.println("Not Connected!");
}
}
String c;
void loop() {
if (Serial.available() > 0) { // if serial is available
c = Serial.readStringUntil('\n'); // get the last string from the serial
client.send(c.substring(0, c.length() - 1)); // trim the new line and send to the websocket
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment