Links2004 / arduinoWebSockets

arduinoWebSockets
GNU Lesser General Public License v2.1
1.87k stars 553 forks source link

Ethernet shield #171

Open ivizot opened 7 years ago

ivizot commented 7 years ago

I am sorry for probable dumb question - I am quite new to Arduino world. How do I use the library with Ethernet shield? Any help appreciated! Thank you!

Links2004 commented 7 years ago

what board do you use an ESP8266 or something else?

tobiasilg commented 7 years ago

First of all - thank you Markus @Links2004 & all others involved for your amazing work!

Please give me a hint: I want to use a Arduino UNO (in case the library is consuming too much space -> a Arduino Mega) with the ENC28J60 (NO ESP8266 at all). My use case should be, that the Arduino is running as a Websocket-Server. It should deliver a webpage where I can shut on or off something or display a status etc. Thats the part I could do myself...

Is there any example for this use case? I installed the 1.3.0 branch and the only example without any ESP8266 library is the "WebSocketClientAVR". I am thus right that the UNO is not capable of serving websockets? The WebSocketServer_LedControl is what I would need running on an UNO with the ENC28J60 ethernet instead of the ESP8266.

Links2004 commented 7 years ago

Theoretical it will work, but i have never test this combination. you need to find a other webserver for the Arduino Mega since the ESP8266WebServer will not run there. the websocket server code itself supports the ENC28J60.

websocket sever min code:

#define LED_RED     15
#define LED_GREEN   12
#define LED_BLUE    13

#define USE_SERIAL Serial

String html_page = F("<html><head><script>var connection = new WebSocket('ws://'+location.hostname+':81/', ['arduino']);connection.onopen = function () {  connection.send('Connect ' + new Date()); }; connection.onerror = function (error) {    console.log('WebSocket Error ', error);};connection.onmessage = function (e) {  console.log('Server: ', e.data);};function sendRGB() {  var r = parseInt(document.getElementById('r').value).toString(16);  var g = parseInt(document.getElementById('g').value).toString(16);  var b = parseInt(document.getElementById('b').value).toString(16);  if(r.length < 2) { r = '0' + r; }   if(g.length < 2) { g = '0' + g; }   if(b.length < 2) { b = '0' + b; }   var rgb = '#'+r+g+b;    console.log('RGB: ' + rgb); connection.send(rgb); }</script></head><body>LED Control:<br/><br/>R: <input id=\"r\" type=\"range\" min=\"0\" max=\"255\" step=\"1\" oninput=\"sendRGB();\" /><br/>G: <input id=\"g\" type=\"range\" min=\"0\" max=\"255\" step=\"1\" oninput=\"sendRGB();\" /><br/>B: <input id=\"b\" type=\"range\" min=\"0\" max=\"255\" step=\"1\" oninput=\"sendRGB();\" /><br/></body></html>");

WebSocketsServer webSocket = WebSocketsServer(81);

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

    switch(type) {
        case WStype_DISCONNECTED:
            USE_SERIAL.printf("[%u] Disconnected!\n", num);
            break;
        case WStype_CONNECTED: {
            USE_SERIAL.printf("[%u] Connected url: %s\n", num, payload);

            // send message to client
            webSocket.sendTXT(num, "Connected");
        }
            break;
        case WStype_TEXT:
            USE_SERIAL.printf("[%u] get Text: %s\n", num, payload);

            if(payload[0] == '#') {
                // we get RGB data

                // decode rgb data
                uint32_t rgb = (uint32_t) strtol((const char *) &payload[1], NULL, 16);

                analogWrite(LED_RED,    ((rgb >> 16) & 0xFF));
                analogWrite(LED_GREEN,  ((rgb >> 8) & 0xFF));
                analogWrite(LED_BLUE,   ((rgb >> 0) & 0xFF));
            }

            break;
    }

}

void setup() {
....
 // start webSocket server
    webSocket.begin();
    webSocket.onEvent(webSocketEvent);
....
}

void loop() {
.....
    webSocket.loop();
.....
}

you only need to combine it with the ENC28J60 initialization and a Webserver.