washo4evr / Socket.io-v1.x-Library

Socket.io Library for Arduino
108 stars 58 forks source link

Cannot connect if host is Heroku #55

Closed alessandrocapra closed 6 years ago

alessandrocapra commented 6 years ago

Hi, I successfully used the library to connect to my local webserver. Next step for me is to be able to send data from Arduino to the NodeJS webserver hosted on Heroku, but this piece of code

if (!client.connect(host, port)) { Serial.println("connection failed"); return; }

actually kicks in, therefore the connection seems to not even start. Is it a limit of this library? Any way to overcome it?

Ruttmann commented 6 years ago

With Heroku or any other hosting where you only need to inform the URL, it's not necessary to inform the port for connection, as the example below:

if (!client.connect(hostname)) Serial.println(F("Not connected."));

By passing only the hostname, the library will try to connect on port 80. For me it worked on Heroku without problems...

alessandrocapra commented 6 years ago

Hi @Ruttmann , thanks for your reply! Like you suggested, I adapted the .connect command to have only host as parameter, but unfortunately it still does not work...can you perhaps spot some mistakes in my code here? It totally works on the local webserver, so it is something that definitely has to do with the connection to Heroku IMO. If you don't find anything odd, would you please share a sketch where you actually connect to Heroku?

Cheers, Alessandro

#include <ESP8266WiFi.h>
#include <SocketIOClient.h>

const char* ssid     = "ssid";
const char* password = "pass";

//String host     = "192.168.2.132";                 // change with correct host on Heroku, this is for dev only
//const int port  = 3000;
String host     = "https://something.herokuapp.com/";
//const int port  = 29289;

SocketIOClient client;

int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

#define BUTTON_PIN 5

void setup() {
  // set button pin as an input
  pinMode(BUTTON_PIN, INPUT);

  Serial.begin(115200);
  delay(10);

  wifiConnection();
}

void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(BUTTON_PIN);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == LOW) {
      // if the current state is LOW then the button went from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);

      client.emit("sensor", "{\"message\":\"up\"}");
    } else {
      // if the current state is HIGH then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;
}

void wifiConnection() {
  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  if (!client.connect(host)) {
    Serial.println("connection failed");
    return;
  }

  if (client.connected())
  {
    client.emit("sensor", "{\"message\":\"connected\"}");
  }
}
alessandrocapra commented 6 years ago

Ok, I found the issue! When specifying the host, avoid including the http:// or any other trailing slashes. So it became String host = "something.herokuapp.com";