mirror of
https://github.com/Links2004/arduinoWebSockets.git
synced 2025-07-17 09:12:05 +02:00
examples for ESP32
This commit is contained in:
@ -10,15 +10,30 @@
|
|||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
#include <ESP8266WiFiMulti.h>
|
#include <ESP8266WiFiMulti.h>
|
||||||
|
|
||||||
|
#if defined(ESP8266)
|
||||||
|
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
|
||||||
|
#include <ESP8266WiFiMulti.h> #include <ESP8266WiFiMulti.h>
|
||||||
|
ESP8266WiFiMulti WiFiMulti;
|
||||||
|
#elif defined(ESP32)
|
||||||
|
#include <WiFi.h>
|
||||||
|
#include <WiFiMulti.h>
|
||||||
|
WiFiMulti WiFiMulti;
|
||||||
|
|
||||||
|
HardwareSerial Serial1(2);
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <WebSocketsClient.h>
|
#include <WebSocketsClient.h>
|
||||||
|
|
||||||
#include <Hash.h>
|
#include <Hash.h>
|
||||||
|
|
||||||
ESP8266WiFiMulti WiFiMulti;
|
|
||||||
WebSocketsClient webSocket;
|
WebSocketsClient webSocket;
|
||||||
|
|
||||||
#define USE_SERIAL Serial1
|
#define USE_SERIAL Serial1
|
||||||
|
|
||||||
|
#ifndef ESP8266
|
||||||
|
void hexdump(const void *mem, uint32_t len, uint8_t cols = 16);
|
||||||
|
#endif
|
||||||
|
|
||||||
void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
|
void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
|
||||||
|
|
||||||
switch(type) {
|
switch(type) {
|
||||||
@ -90,3 +105,20 @@ void setup() {
|
|||||||
void loop() {
|
void loop() {
|
||||||
webSocket.loop();
|
webSocket.loop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef ESP8266
|
||||||
|
void hexdump(const void *mem, uint32_t len, uint8_t cols) {
|
||||||
|
const uint8_t* src = (const uint8_t*) mem;
|
||||||
|
USE_SERIAL.printf("\n[HEXDUMP] Address: 0x%08X len: 0x%X (%d)", (ptrdiff_t)src, len, len);
|
||||||
|
for(uint32_t i = 0; i < len; i++) {
|
||||||
|
if(i % cols == 0) {
|
||||||
|
USE_SERIAL.printf("\n[0x%08X] 0x%08X: ", (ptrdiff_t)src, i);
|
||||||
|
}
|
||||||
|
USE_SERIAL.printf("%02X ", *src);
|
||||||
|
src++;
|
||||||
|
}
|
||||||
|
USE_SERIAL.printf("\n");
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
@ -7,8 +7,18 @@
|
|||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
|
||||||
#include <ESP8266WiFi.h>
|
#if defined(ESP8266)
|
||||||
#include <ESP8266WiFiMulti.h>
|
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
|
||||||
|
#include <ESP8266WiFiMulti.h> #include <ESP8266WiFiMulti.h>
|
||||||
|
ESP8266WiFiMulti WiFiMulti;
|
||||||
|
#elif defined(ESP32)
|
||||||
|
#include <WiFi.h>
|
||||||
|
#include <WiFiMulti.h>
|
||||||
|
WiFiMulti WiFiMulti;
|
||||||
|
|
||||||
|
HardwareSerial Serial1(2);
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <WebSocketsServer.h>
|
#include <WebSocketsServer.h>
|
||||||
#include <Hash.h>
|
#include <Hash.h>
|
||||||
|
|
||||||
@ -18,6 +28,11 @@ WebSocketsServer webSocket = WebSocketsServer(81);
|
|||||||
|
|
||||||
#define USE_SERIAL Serial1
|
#define USE_SERIAL Serial1
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef ESP8266
|
||||||
|
void hexdump(const void *mem, uint32_t len, uint8_t cols = 16);
|
||||||
|
#endif
|
||||||
|
|
||||||
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
|
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
|
||||||
|
|
||||||
switch(type) {
|
switch(type) {
|
||||||
@ -28,7 +43,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length
|
|||||||
{
|
{
|
||||||
IPAddress ip = webSocket.remoteIP(num);
|
IPAddress ip = webSocket.remoteIP(num);
|
||||||
USE_SERIAL.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);
|
USE_SERIAL.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);
|
||||||
|
|
||||||
// send message to client
|
// send message to client
|
||||||
webSocket.sendTXT(num, "Connected");
|
webSocket.sendTXT(num, "Connected");
|
||||||
}
|
}
|
||||||
@ -84,3 +99,17 @@ void loop() {
|
|||||||
webSocket.loop();
|
webSocket.loop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifndef ESP8266
|
||||||
|
void hexdump(const void *mem, uint32_t len, uint8_t cols) {
|
||||||
|
const uint8_t* src = (const uint8_t*) mem;
|
||||||
|
USE_SERIAL.printf("\n[HEXDUMP] Address: 0x%08X len: 0x%X (%d)", (ptrdiff_t)src, len, len);
|
||||||
|
for(uint32_t i = 0; i < len; i++) {
|
||||||
|
if(i % cols == 0) {
|
||||||
|
USE_SERIAL.printf("\n[0x%08X] 0x%08X: ", (ptrdiff_t)src, i);
|
||||||
|
}
|
||||||
|
USE_SERIAL.printf("%02X ", *src);
|
||||||
|
src++;
|
||||||
|
}
|
||||||
|
USE_SERIAL.printf("\n");
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
@ -25,6 +25,8 @@ function build_sketches()
|
|||||||
|
|
||||||
function get_core()
|
function get_core()
|
||||||
{
|
{
|
||||||
|
echo Setup core for $1
|
||||||
|
|
||||||
cd $HOME/arduino_ide/hardware
|
cd $HOME/arduino_ide/hardware
|
||||||
|
|
||||||
if [ "$1" = "esp8266" ] ; then
|
if [ "$1" = "esp8266" ] ; then
|
||||||
@ -42,5 +44,5 @@ function get_core()
|
|||||||
cd esp32/tools
|
cd esp32/tools
|
||||||
python get.py
|
python get.py
|
||||||
fi
|
fi
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user