Links2004 / arduinoWebSockets

arduinoWebSockets
GNU Lesser General Public License v2.1
1.89k stars 555 forks source link

Questions about support for Arduino Nano 33 IoT #468

Closed ocrdu closed 5 years ago

ocrdu commented 5 years ago

First of all, many thanks for making the Websockets library for the Arduino.

Now, prepare for some clueless questions 8-) about this library:

The error is below, and the code is below that.

Thanks in advance for any and all time you may want to put into answering my questions.

Mit freundlichen Grüßen,

Oscar.



Error:


                 from c:\documents and settings\odu.pc1\local settings\application data\arduino15\packages\arduino\tools\arm-none-eabi-gcc\7-2017q4\arm-none-eabi\include\c++\7.2.1\string:40,

                 from c:\documents and settings\odu.pc1\local settings\application data\arduino15\packages\arduino\tools\arm-none-eabi-gcc\7-2017q4\arm-none-eabi\include\c++\7.2.1\stdexcept:39,

                 from c:\documents and settings\odu.pc1\local settings\application data\arduino15\packages\arduino\tools\arm-none-eabi-gcc\7-2017q4\arm-none-eabi\include\c++\7.2.1\array:39,

                 from c:\documents and settings\odu.pc1\local settings\application data\arduino15\packages\arduino\tools\arm-none-eabi-gcc\7-2017q4\arm-none-eabi\include\c++\7.2.1\tuple:39,

                 from c:\documents and settings\odu.pc1\local settings\application data\arduino15\packages\arduino\tools\arm-none-eabi-gcc\7-2017q4\arm-none-eabi\include\c++\7.2.1\functional:54,

                 from C:\Program Files\Arduino\libraries\WebSockets\src/WebSockets.h:40,

                 from C:\Program Files\Arduino\libraries\WebSockets\src/WebSocketsServer.h:28,

                 from C:\Documents and Settings\odu.PC1\Desktop\websocket\websocket.ino:2:

c:\documents and settings\odu.pc1\local settings\application data\arduino15\packages\arduino\tools\arm-none-eabi-gcc\7-2017q4\arm-none-eabi\include\c++\7.2.1\bits\stl_algobase.h:243:56: error: macro "min" passed 3 arguments, but takes just 2

     min(const _Tp& __a, const _Tp& __b, _Compare __comp)

c:\documents and settings\odu.pc1\local settings\application data\arduino15\packages\arduino\tools\arm-none-eabi-gcc\7-2017q4\arm-none-eabi\include\c++\7.2.1\bits\stl_algobase.h:265:56: error: macro "max" passed 3 arguments, but takes just 2

     max(const _Tp& __a, const _Tp& __b, _Compare __comp)

exit status 1
Error compiling for board Arduino NANO 33 IoT.


Code:

#include <WiFiNINA.h>
#include <WebSocketsServer.h>
#include "arduino_secrets.h"

char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;

// Globals
WebSocketsServer webSocket = WebSocketsServer(80);

// Called when receiving any WebSocket message
void onWebSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {

  // Figure out the type of WebSocket event
  switch(type) {

    // Client has disconnected
    case WStype_DISCONNECTED:
      Serial.printf("[%u] Disconnected!\n", num);
      break;

    // New client has connected
    case WStype_CONNECTED: {
        IPAddress ip = webSocket.remoteIP(num);
        Serial.printf("[%u] Connection from ", num);
        Serial.println(ip.toString());
      }
      break;

    // Echo text message back to client
    case WStype_TEXT:
      Serial.printf("[%u] Text: %s\n", num, payload);
      webSocket.sendTXT(num, payload);
      break;

    // For everything else: do nothing
    case WStype_BIN:
    case WStype_ERROR:
    case WStype_FRAGMENT_TEXT_START:
    case WStype_FRAGMENT_BIN_START:
    case WStype_FRAGMENT:
    case WStype_FRAGMENT_FIN:
    default:
    break;
  }
}

void setup() {

  // Start Serial port
  Serial.begin(9600);

  // Connect to access point
  Serial.println("Connecting");
  WiFi.begin(ssid, password);
  while ( WiFi.status() != WL_CONNECTED ) {
    delay(500);
    Serial.print(".");
  }

  // Print our IP address
  Serial.println("Connected!");
  Serial.print("My IP address: ");
  Serial.println(WiFi.localIP());

  // Start WebSocket server and assign callback
  webSocket.begin();
  webSocket.onEvent(onWebSocketEvent);
}

void loop() {

  // Look for and handle WebSocket data
  webSocket.loop();
}
Links2004 commented 5 years ago

Hi,

the Arduino Nano has very less RAM an no real C++ support. there is the ATmega branche (https://github.com/Links2004/arduinoWebSockets/tree/ATmega) to get around the C++ problem, but most likely the Arduino Nano has to less RAM. you can try it if you like.

you can try to add

#undef max
#undef min

before the includes, but since the Arduino pre compiler does many strange things in the background im am not sure if this will work. If you are using a real IDE you have a better change with this.

Note: the ATmega branche has less features and keeps the functionality at the bare minimum to fit the AVR controllers.

ocrdu commented 5 years ago

This is one of the "new" Nano's, with an ARM Cortex-M0+ CPU, 256K flash and 32KB SRAM; basically an Arduino MKR WiFi 1010 on a smaller PCB.

But thanks, I'll have a go and muck about with it some more.

ocrdu commented 5 years ago

FYI: One compile error left now:

C:\Documents and Settings\odu.PC1\Desktop\websocket\websocket.ino: In function 'void onWebSocketEvent(uint8_t, WStype_t, uint8_t*, size_t)':
websocket:27:34: error: 'class WebSocketsServer' has no member named 'remoteIP'
         IPAddress ip = webSocket.remoteIP(num);
                                  ^~~~~~~~
exit status 1
'class WebSocketsServer' has no member named 'remoteIP'

Probably an unknown WEBSOCKETS_NETWORK_TYPE.

Code now is (undefs added, printfs replaced):

#undef max
#undef min
#include <WiFiNINA.h>
#include <WebSocketsServer.h>
#include "arduino_secrets.h"

char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;

// Globals
WebSocketsServer webSocket = WebSocketsServer(80);

// Called when receiving any WebSocket message
void onWebSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {

  // Figure out the type of WebSocket event
  switch(type) {

    // Client has disconnected
    case WStype_DISCONNECTED:
      Serial.print((char) num);
      Serial.println(" disconnected!");
      break;

    // New client has connected
    case WStype_CONNECTED: {
        IPAddress ip = webSocket.remoteIP(num);
        Serial.print((char) num);
        Serial.print("Connection from ");
        Serial.println(ip);
      }
      break;

    // Echo text message back to client
    case WStype_TEXT:
      Serial.print((char) num);
      Serial.print(" Text: ");
      Serial.println((char*) payload);
      webSocket.sendTXT(num, payload);
      break;

    // For everything else: do nothing
    case WStype_BIN:
    case WStype_ERROR:
    case WStype_FRAGMENT_TEXT_START:
    case WStype_FRAGMENT_BIN_START:
    case WStype_FRAGMENT:
    case WStype_FRAGMENT_FIN:
    default:
    break;
  }
}

void setup() {

  // Start Serial port
  Serial.begin(9600);

  // Connect to access point
  Serial.println("Connecting");
  WiFi.begin(ssid, pass);
  while ( WiFi.status() != WL_CONNECTED ) {
    delay(500);
    Serial.print(".");
  }

  // Print our IP address
  Serial.println("Connected!");
  Serial.print("My IP address: ");
  Serial.println(WiFi.localIP());

  // Start WebSocket server and assign callback
  webSocket.begin();
  webSocket.onEvent(onWebSocketEvent);
}

void loop() {

  // Look for and handle WebSocket data
  webSocket.loop();
}

Time to dig further into the library and see what's what.

Links2004 commented 5 years ago

The non ESP network stack has no get remote IP function, hence the function is not implemented. you need to remove the remoteIP call.

        IPAddress ip = webSocket.remoteIP(num);
        Serial.print((char) num);
        Serial.print("Connection from ");
        Serial.println(ip);

If you able to get it working, i will add the code to the examples if you like.

johnreine commented 5 years ago

I am having this problem as well.. any progress?

ocrdu commented 5 years ago

No progress from me at least; I was in a hurry and have implemented the web interface I was making with SSE instead of a websocket; I will look into it again when I have more time.

johnreine commented 5 years ago

SSE?

That would work for me! I too am in a rush..

Might you have any example code you used to get it working? :)

John

On Mon, Sep 9, 2019 at 10:50 AM Oscar den Uijl notifications@github.com wrote:

No progress from me at least; I was in a hurry and have implemented the web interface I was making with SSE instead of a websocket; I will look into it again when I have more time.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/Links2004/arduinoWebSockets/issues/468?email_source=notifications&email_token=AMGUXMZTNW6VZ5TFY36BXE3QIZPENA5CNFSM4ISNTD4KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD6H3WMQ#issuecomment-529513266, or mute the thread https://github.com/notifications/unsubscribe-auth/AMGUXM775EC5Q672CCW2RXDQIZPENANCNFSM4ISNTD4A .

--

John Reine CGSN Electrical Engineering Lead Woods Hole Oceanographic Institution LOSOS Bldg., MS#57 Woods Hole, MA 02543 phone: 508-289-3336 email: jreine@whoi.edu

ocrdu commented 5 years ago

The code I use is here: https://github.com/ocrdu/arduino-webinterface-sse

No documentation yet; try at your own risk 8-).