Closed denden4444 closed 3 years ago
Hi,
NETWORK_CONTROLLER
to NETWORK_CONTROLLER_WIFI
in config.h?
For ESP8266 you need to have:
#define NETWORK_CONTROLLER NETWORK_CONTROLLER_WIFI
Base64.h
and Base64.cpp
to src
directory of ArduinoWebSockets library and in Base64.cpp change:
#include <avr/pgmspace.h>
to #include <pgmspace.h>
Simple code for ESP8266:
#include <WebSocketClient.h>
const char *SSID = "SKYNET";
const char *password = "***";
WebSocketClient client;
void onOpen(WebSocket &ws) {
Serial.println(F("Connected"));
char message[] = "Hello from Arduino client!";
ws.send(TEXT, message, strlen(message));
}
void onClose(WebSocket &ws, const eWebSocketCloseEvent code, const char *reason, uint16_t length) {
Serial.println(F("Disconnected"));
}
void onMessage(WebSocket &ws, const eWebSocketDataType dataType, const char *message, uint16_t length) {
switch (dataType) {
case TEXT:
Serial.print(F("Received: ")); Serial.println(message);
break;
case BINARY:
Serial.println(F("Received binary data"));
break;
}
}
void onError(const eWebSocketError code) {
Serial.print("Error: "); Serial.println(code);
}
void setup() {
Serial.begin(57600);
while (!Serial) ;
Serial.printf("\nConnecting to %s ", SSID);
WiFi.begin(SSID, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" connected");
Serial.print(F("Device IP: "));
Serial.println(WiFi.localIP());
WiFi.printDiag(Serial);
// ---
client.setOnOpenCallback(onOpen);
client.setOnCloseCallback(onClose);
client.setOnMessageCallback(onMessage);
if (!client.open("192.168.46.9", 3000)) {
Serial.println(F("Connection failed!"));
while (true) ;
}
}
void loop() {
client.listen();
}
Hi D I did the following :
#define NETWORK_CONTROLLER NETWORK_CONTROLLER_WIFI
3.Moved base64.h and base64.cpp to src folder as you mentioned , still cannot compile :
Arduino: 1.8.5 (Windows 8.1), Board: "Generic ESP8266 Module, 80 MHz, ck, 26 MHz, 40MHz, DOUT, 1M (512K SPIFFS), 2, v2 Lower Memory, Disabled, None, Only Sketch, 115200"
Archiving built core (caching) in: C:\Users\den\AppData\Local\Temp\arduino_cache_936511\core\core_esp8266_esp8266_generic_CpuFrequency_80,ResetMethod_ck,CrystalFreq_26,FlashFreq_40,FlashMode_dout,FlashSize_1M512,led_2,LwIPVariant_v2mss536,Debug_Disabled,DebugLevel_None____,FlashErase_none,UploadSpeed_115200_43e2f4996cffbaa3112d086ae799c08c.a C:\Users\den\AppData\Local\Temp\arduino_build_242539/arduino.ar(core_esp8266_main.cpp.o):(.text._ZL12loop_wrapperv+0x4): undefined reference to `loop'
C:\Users\den\AppData\Local\Temp\arduino_build_242539/arduino.ar(core_esp8266_main.cpp.o): In function `loop_wrapper':
C:\Users\den\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.1\cores\esp8266/core_esp8266_main.cpp:57: undefined reference to `loop'
collect2.exe: error: ld returned 1 exit status
exit status 1 Error compiling for board Generic ESP8266 Module.
This report would have more information with "Show verbose output during compilation" option enabled in File -> Preferences.
May I suggest integrating the relevant crypto and base64 into your exisitng code ?
Happy to try anthing else and feedback/assist with testing
Kind regards
Den
Do you have esp8266 by ESP8266 Community version 2.4.1 from Boards Manager installed? (maybe you could try to reinstall)
I have couple of ESP8266 boards (including Generic and WeMos) and all of them are working with this code.
Hi D
Thanks again for the reply.
Yes 2.4.1 installed and no problems elsewhere. Will try get a fresh install going in a vm to check. Dying to get to test your sockets client.
Den
Try to download new version of simple-client.ino
example. Open project, change value of *password
variable and compile.
Hi D Ok :-) To clarify , the steps are : 1.Get the Base 64 library and add to arduino libraries as normal
Let me know if I have skipped or missed anything, going to try the listed steps and will ping back result.
Thank you kindly Den
Hi D Good news it's working :-) Going to perform further testing and will ping back on results if you like ? Is there any other way to handle the base 64 and also reduce code space and size ? What does this do exactly ? In case of errors with arduino-base64 library: Move Base64.h and Base64.cpp to src directory of ArduinoWebSockets library and in Base64.cpp change:
Kind regards
Den
Hi,
Base64 and SHA1 algorithms are required. You can read about it in specification or here: https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers#Server_Handshake_Response
If you know any better lib for Base64 and/or SHA1 then you can easily add them.
Just modify generateSecKey()
and encodeSecKey()
in utility.cpp
Hi D Thanks for the reply ... Re Base64 .. did you know ESP8266 already had Base64 support built in ? https://iot-playground.com/forum/esp8266-arduino-ide-programming/737-base64-encode-was-not-declared-in-this-scope?start=6
I was specifically wondering about this line :
Whats the (subtle) difference ?
Den
pgmspace.h
for ATmega (Boards: Uno, Mega, Pro Mini etc.) and SAMD21 (ARM Cortex M0) is located in avr
while for ESP8266 it's located in root directory.
I use this lib to move strings from dynamic memory to sketch space.
I have started writing this library just because there is no good WebSockets implementation for ATmega (Arduino UNO, Mega etc.) later I decided to support ESP8266 and Cortex M0. Because Arduino doesn't have base64 "straight of the box" I had to use an additional lib.
I'll try to use built in base64 for ESP8266 platform.
Hi D
Thanks for the reply. Just like you I have been looking for the same .. a good library for both webSockets Server and client , the closest i got to it is the one by Links2000 but that is not as flexible in terms of server and client on same library and doesn't handle the connect/disconnect when required easily. I have your client working with links2000 library now and it seems to be working. There is one MAJOR problem though : If the websocket client can't connect to the server it causes a stack dump and the esp resets :-( If you have some time and keen to give it a test and feedback I would really appreciate it as I am battling to find someone to chat with about the issue and hopefully the solution .. it seems websocket knowledge on these platforms is rather scarce.
I can happily send you the code and setup if you'd like to test. I was also wondering why your server isn't ESP8266 ready yet ? Would you like some help testing it and getting it going for ESP ?
Looking forward to your reply.
Kind regards
Den
@denden4444 In the latest version I've added full support for ESP8266 (server and client)
Hi David
How are you ?
Apologies for the late reply .. I've been overloaded with work :-(
Could you please clarify a few things please :
Re Base64 .. did you know ESP8266 already had Base64 support built in ?
Is there no more need for extra installed library ?
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WebSocketsServer.h>
#include <WebSocketsClient.h>
#include <Hash.h>
Is it possible to easily change the names somehow without having to remove the links2000 library ?
I will happily test your library on my ESP's especially with client and server in same ESP, please could you once again provide an example based on latest changes etc. and I will load and test once again.
I am setting up the following :
Thank you kindly
Den
Hi @skaarj1989
It seems like the example on the main gitpage here is not compiling , do you possibly have a working example for esp8266 ? Thank you Den See below for compile errors: