Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESP8266 won't connect to server #1

Open
deepikavira opened this issue Feb 19, 2016 · 62 comments
Open

ESP8266 won't connect to server #1

deepikavira opened this issue Feb 19, 2016 · 62 comments

Comments

@deepikavira
Copy link

@washo4evr, have you used this library for a server code written by you? In your comment you write that you are trying to connect to spark fun via HTTP commands. Please clarify if this is a socket.io connection that you are trying to establish or just a TCP connection.

@washo4evr
Copy link
Owner

I had forgotten to update the headet
it is now done
this library will not try to connect to sparkfun via HTTP
you need to run a node.js server with socket.io installed and running on it.
i have provided an example

@washo4evr
Copy link
Owner

you need to adjust
char host[] = "192.168.0.5";
int port = 1234;
to match your own server

@deepikavira
Copy link
Author

Ok thank you so much for replying. I am stuck at getting the ESP connect to our server. I will try using your app.js code and see if I can connect to it.

@washo4evr
Copy link
Owner

let me know if you need any assistance running it.
ESP is still considered beta

@deepikavira
Copy link
Author

So now I ran your server. i.e app.js then I ran the Hello_Time_esp8266 code on the ESP module. It is able to connect to the wifi, and gets the ip address but for connection to server it fails. Here is the output

WiFi connected
IP address:
172.16.42.43
172.16.42.43
connection failed.

I wanted to know if
char host[] = "192.168.0.5";
int port = 1234; fields remain same while trying to connect to your server.

@washo4evr
Copy link
Owner

you need to adjust these 2 lines depending on your server
if you used app.js then port should still be 1234
but you will have to change the IP to match yours

@washo4evr
Copy link
Owner

you will also need to update a line in index.html

<script src='//192.168.0.5:1234/socket.io/socket.io.js'></script>

to match your server IP

@deepikavira
Copy link
Author

Can you tell me how to find what ip address did the server get?

@washo4evr
Copy link
Owner

you could use http://www.whatsmyip.org/
also, if you are trying to connect to a remote server (and not a local server running in a computer near you) you might have to redirect the port 1234 to you server IP address
on the INO file, you would then put xxx.xxx.xxx.xxx (according to http://www.whatsmyip.org/)
same for index.html
and on your router, you would redirect all traffic for port 1234 to the local IP of the server
(usually something like 192.168.0.55)

@deepikavira
Copy link
Author

@washo4evr Thank you so much!!! I am able to connect to our servers now. Your library has made my day. I will keep you in loop as to how my project goes.

@washo4evr
Copy link
Owner

Awesome!!
Glad I could be of some assistance
Good luck with your project

@deepikavira
Copy link
Author

One last question, can you throw some light on how you have implemented custom events in your code? Are you waiting on some specific message from the server to start listening to it?

@washo4evr
Copy link
Owner

you can send messages (in app.js or index.html) via socket.emit('arduino' ,"Hello World!"); or client.send (in arduino)
and you can use RID in arduino code to check what you have received or socket.on('connection', function (data) {console.log(data); });
as long as the "word" matches on both ends, it will work
I will update the example because 'atime' is not very explicit and include a full documentation as soon as I can

@deepikavira
Copy link
Author

@washo4evr : I have been using your library for a week now. Its really helpful and I have been able to send some data to our server. We have a little problem now. We have a custom even named init device which expects "message" in the following form:
client.send("init_device", "message", ""{'device_id': '1111', 'device_model': '2'}"");
when I send this on our server side we don't receive any data. When I tried sending his same string without the quote marks i.e
client.send("init_device", "message", "{device_id: 1111, device_model: 2}"); we do receive the string but unfortunately we cannot parse it in json. If you have faced any issue of this kind do let me know. It would be helpful to discuss. I also noticed that if i send a single quote (') my server receives it as (') why does the server adds this backslash on its own I have no idea.

@washo4evr washo4evr reopened this Feb 26, 2016
@washo4evr
Copy link
Owner

Hey,
I know why
Rmessage can only be one word for now
JSON will be added very soon
it all comes down to the syntax of (inside socketio.cpp // function send)
String message = "42["" + RID + "",{"" + Rname + "":"" + Rcontent + ""}]";
I will modify it to accept JSON
probably will be another function sendJSON or something that will allow to send formated strings
I will keep you posted

@deepikavira
Copy link
Author

Okay I will also try adding support on my end. Lets see if I can get something going. Thanks for replying

@washo4evr
Copy link
Owner

Hi,
If you send something like

String message = "42["JSON",{"Date":"17.03.2012 15:28 : 47","bindings": [{"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://."},{"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete."},{"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"}]}]";

from the ESP8266, you need, inside app.js to add

function ParseJson(jsondata) {
try {
return JSON.parse(jsondata);
} catch (error) {
return null;
}
}
socket.on('JSON', function (data) {
// console.log(data);
var jsonStr = JSON.stringify(data);
var parsed = ParseJson(jsonStr);
//console.log(parsed);
console.log(parsed.bindings[0].ircEvent);
});

the last console.log will return PRIVMSG

Im still adding support inside the library but that works already like that

@washo4evr
Copy link
Owner

I have updated the library as well as the esp8266 example and app.js

let me know if everything works

@deepikavira
Copy link
Author

Okay will try the updated code on Monday. I don't have my gadgets with me at the moment! Thanks a lot.

@deepikavira
Copy link
Author

@washo4evr . Hi so I tried with the updated code, I can send strings to my server without any problem now. Later I figured out that there was problem when I tried sending " specifically.

I had a question for you though, like the client.send() method do you have any receive method that will display the message received from the server?

@washo4evr
Copy link
Owner

Im glad you got it working
client.monitor() is the function that will get the reply
it will update RID, Rname and Rcontent if you sent a regular client.send
for now, until the next version, the JSON answer is not automatically parsed
(RID will still be updated)

@deepikavira
Copy link
Author

@washo4evr. Its been two weeks that I have tested my module on the server. Everything is fine except power failure. When my module gets accidentally powered down it cannot connect back to the server. I have to re-start my server instance each time something like this occurs. Upon debugging the code I found that this particular code snippet from your SocketIOClient.cpp file is where the client fails to connect to the server over web socket.

bool SocketIOClient::readHandshake() {
......
.....
....
....
...
// reconnect on websocket connection
if (!client.connect(hostname, port))
{ Serial.print(F("Websocket failed."));
return false;}

Why do we have to reconnect over websocket connection? Cant the communication go through socket.io itself

@washo4evr
Copy link
Owner

you need to do that after the upgrade, else you are not connected via websocket

on power loss, the module should be able to reconnect
at the point, the server should give a new SID to the module

this is the behavior I get here

@washo4evr washo4evr reopened this Mar 16, 2016
@deepikavira
Copy link
Author

Hey so from what I understood is that we first establish a connection over socket.io then we upgrade and establish connection via websockets. once the websocket connection is done we then start sending data via custom events like atime etc.. Please correct me if I got it wrong

@deepikavira
Copy link
Author

Actually, this time it failed to connect both times. Do you think there could be a issue on the server side?

@washo4evr
Copy link
Owner

did you change the delay?
it could be server related
how fast does the 1st part go? is it quick to connect?

@deepikavira
Copy link
Author

I did try 0 , 5000, 10000 delays so far. Yes the first part actually goes through in a second or two actually.

@deepikavira
Copy link
Author

https://www.youtube.com/watch?v=sQ4PkcvcJmU&feature=youtu.be

In this video I have tried to capture the output window, the delay is 2000 before it tries to connect over Websocket

@washo4evr
Copy link
Owner

the 1st wait is a bit long
maybe amazonaws is far (ping high)

you could try to change the if by a while and see how long it takes
(add a delay(500) to avoid spamming the server

@Galilei11
Copy link

@washo4evr, Thank you for your work, I did try to use your socketio programs 4 days ago. It works but, I have a big problem, with the too long connecting time.
After starts the Arduino it needs 18sec until goes to "Connecting via Websocket" and 61sec to "Upgrade to WebSocket confirmed" !!!!!
I have red the comments above, i wrote these lines to cpp too:
...... if (!client.connect(hostname, port)){Serial.print(F("Websocket failed again"));.......

This is the timestamped log about it:

WiFi connected
IP address:
10.1.0.100
was here: connect timestamp (ms): 1275
was here: ::sendHandshake timestamp (ms): 18048
was here: ::readHandshake timestamp (ms): 18048
was here: ::waitForInput timestamp (ms): 18048
was here: ::readLine timestamp (ms): 18048
HTTP/1.1 200 OK
was here: ::eatHeader timestamp (ms): 18049
was here: ::readLine timestamp (ms): 18049
Content-Type: text/plain; charset=UTF-8
was here: ::readLine timestamp (ms): 18057
Content-Length: 100
was here: ::readLine timestamp (ms): 18065
Access-Control-Allow-Credentials: true
was here: ::readLine timestamp (ms): 18073
Access-Control-Allow-Origin: Arduino
was here: ::readLine timestamp (ms): 18082
Set-Cookie: io=dbrTorAM3M-ko-HBAAAF
was here: ::readLine timestamp (ms): 18090
Date: Thu, 17 Mar 2016 09:39:18 GMT
was here: ::readLine timestamp (ms): 18090
Connection: keep-alive
was here: ::readLine timestamp (ms): 18098
was here: ::readLine timestamp (ms): 18107
97:0{"sid":"dbrTorAM3M-ko-HBAAAF","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}
Connected. SID=dbrTorAM3M-ko-HBAAAF
Connecting via Websocket
was here: ::waitForInput timestamp (ms): 60787
was here: ::readLine timestamp (ms): 60787
HTTP/1.1 101 Switching Protocols
was here: ::readLine timestamp (ms): 60788
Upgrade: websocket
was here: ::readLine timestamp (ms): 60788
Connection: Upgrade
was here: ::readLine timestamp (ms): 60788
Sec-WebSocket-Accept: HQ5C77iTalBq00QNQp3bAkwF7JY=
was here: ::eatHeader timestamp (ms): 60796
was here: ::readLine timestamp (ms): 60796
was here: while (client.available() timestamp (ms): 61419
was here: ::readLine timestamp (ms): 61419
��40��40
was here: SocketIOClient::parser timestamp (ms): 61419
Message size = 2
Received message = 40
was here: SocketIOClient::parser case 4 timestamp (ms): 61419
Upgrade to WebSocket confirmed
was here: SocketIOClient::parser case 40 timestamp (ms): 61420
was here: while (client.available() timestamp (ms): 61428
was here: ::readLine timestamp (ms): 61436
�_42["welcome",{"message":"Connected !!!!"}]�_42["welcome",{"message":"Connected !!!!"}]
was here: SocketIOClient::parser timestamp (ms): 61444
Message size = 42
Received message = 42["welcome",{"message":"Connected !!!!"}]
was here: SocketIOClient::parser case 4 timestamp (ms): 61461
RID = welcome
Rname = message
Rcontent = Connected !!!!
42["welcome",{"message":"Connected !!!!"}]
was here: SocketIOClient::parser case 42 timestamp (ms): 61469
was here: connectedben timestamp (ms): 61478
was here: ::send(String timestamp (ms): 61478
{"sensor":"gps","time":1351824120,"data":[48.756081,2.302038]}
was here: ::send(String timestamp (ms): 62301
was here: ::sendJSON timestamp (ms): 62910
was here: while (client.available() timestamp (ms): 63758
was here: ::readLine timestamp (ms): 63758
�/42["atime",{"time":"2016-03-17T09:40:03.346Z"}]�/42["atime",{"time":"2016-03-17T09:40:03.346Z"}]
was here: SocketIOClient::parser timestamp (ms): 63758
Message size = 47

@washo4evr
Copy link
Owner

it is indeed a very long time to connect
what board are you using?
is the server remote or local?
for instance, here, doing tests in local, it takes about 5 seconds to be connected with websocket

@Galilei11
Copy link

I use nodeMCU1.0 -ESP-12E with Arduino IDE 1.6.7 and ESP8266 2.1.0-rc2
first I tried it on my local apache server (localhost) :
18sec until goes to "Connecting via Websocket"
and 61sec to "Upgrade to WebSocket confirmed"

second I tried with remote remote server:
7sec until goes to "Connecting via Websocket"
and 21sec to "Upgrade to WebSocket confirmed"

My sw and hw system can connect within 1sec with this arduino websocket, https://github.com/Links2004/arduinoWebSockets

@washo4evr
Copy link
Owner

Hi,
I still had the 2.0.0
upgraded to 2.1.0 and it took 19 sec from ON to connected via socket with arduino 1.6.5.
I don't know why it is even slower in your case when connecting locally
Is it the same delay if you connect from your computer to your node server?
what version are you running of node / socket?

@Galilei11
Copy link

hi,
I tried Hello_Time_ESP8266.ino with different ESP8266 core versions:
2.0.0-rc1 : connect time on local server 18sec and 61sec AND client.monitor() works
2.0.0-rc2 : connect time on local server 18sec and 61sec AND client.monitor() works
2.1.0-rc1 : connect time on local server 18sec and 61sec AND client.monitor() NOT works !!!

on local server the node install file is: node-v5.6.0-x64.msi

@washo4evr
Copy link
Owner

did you try after disabling your antivirus / firewall?
I don't see a lot of possibilities for such long delays.
wish I had a better answer for you...
especially since next version won't change any of the connection parameters, only add new REST functions

@Galilei11
Copy link

A new problem, in SocketIOClient::parser doesnt work well if the incoming socket lenght bigger than 126 byte, I think the program doesnt takes different between small and larger size of soket.

antivirus / firewall: I think if the antivirus / firewall makes the problem the connect never works, but it works with long delay.

@Galilei11
Copy link

the problem, in SocketIOClient::parser fixed, now works

   int sizemsg = databuffer[index + 1];   // 0-125 byte, index ok
  if(databuffer[index + 1]>125)       
      { 
        sizemsg = databuffer[index + 2];    // 126-255 byte
        index+=1;       // index correction to start
      } 

@washo4evr
Copy link
Owner

awesome, great fix, thx.
I will incorporate it in the next version

@Galilei11
Copy link

Hi, pls help, I cant reconnect to server after I stop and start the socket.io server.
I tried lot of combinations of connect, reconnect, disconnect, connected...... nothing success :(
What is the solution? (client:nodeMCU 1.0 and server: socket.io 1.4.5)

@washo4evr
Copy link
Owner

you need to check for the absence of communication from the server and if nothing received for the last xxx seconds / minutes then, you can do client.stop or reconnect
I will check something : maybe the server sends a special text to say that it is going offline, in which case, I could read that text and do auto client.stop.

i have been super busy recently, but the project is still alive.

@deepikavira
Copy link
Author

@washo4evr Have you ever tried using a mobile app to feed the wifi SSID and Password into the chip at run time? I am trying to so that I can take my project anywhere and connect to the WIFI network available there without having to program the chip exclusively for my home WIFI where I have been developing the project

@washo4evr
Copy link
Owner

That is a great idea
I have not tried it yet
It would require running the ESP in access point mode, receive the data and then "reboot" to connect to another network.

I will give it a try, not sure when. I start my new job on monday.
I will keep you posted

@deepikavira
Copy link
Author

@washo4evr Congratulations on your new job. I did some changes on a code from the internet for the ESP8266 to host a web-server to take user input for the Wi-Fi network name and password. Just have to work a way to reboot and use this information to connect to an external server now. I will keep you posted as well

@washo4evr
Copy link
Owner

washo4evr commented Apr 15, 2016

thanks :)

you could store the date in EEPROM and then do a soft-reboot using the reset pin
or
http://www.instructables.com/id/two-ways-to-reset-arduino-in-software/step2/using-just-software/

@washo4evr
Copy link
Owner

you can actually use softAP in a function and then end it and create a server with the data the just received.
I havent had tim eto compile but in theory, it works

any progress on your end?

@deepikavira
Copy link
Author

@washo4evr I am indeed using softAP at the time of setup and then later I have initiated the client mode. The sequence in which you write these two statements is very important.
WiFi.softAP(AP_NameChar, WiFiAPPSK);
WiFi.begin(ssid, password);

Apparently you don't have to restart the ESP if you do it this way. I basically tried using the ESP in client+AP mode but that didn't work. I could not connect my phone to the ESP(in this case as an AP).
Then I tried using it in softAP and restarting it as client after gathering the required information. This worked pretty well but restarting it was a trouble each time. (I was using certain flags to tell the ESP to start in AP mode or client mode). Then finally from one of the online blogs I found out that if we start it as softAP and then later give the WiFi.begin(ssid, password) command then it can work in the dual mode perfectly fine. i.e host a web server and side by side connect to other servers on the web.

Long story short this is the way I am currently using my ESP WiFi.softAP(). On the webpage that I am hosting I wait for user to feed in the wifi network name & SSID my ESP is supposed to connect to. Once I get that information then I give the WiFi.begin(ssid, password); command and proceed to use the ESP in client mode for most part.

Cheers!!

@johnduong1510
Copy link

Hello,
I use your "Hello_Time_ESP8266.ino" to test with your "app.js". ESP8266 connected to my ssid. And then it stucked. It did not return any message, even "connection failed" ?
image

@washo4evr
Copy link
Owner

Which version of Socket.IO are you using?

@johnduong1510
Copy link

johnduong1510 commented Sep 22, 2017 via email

@washo4evr
Copy link
Owner

this library, as of today, is not compatible with Socket.IO 2.x
It is a plan for the future but I have no ETA

@johnduong1510
Copy link

johnduong1510 commented Sep 22, 2017 via email

@washo4evr
Copy link
Owner

any 1.x.y version of socket.IO library will work

@farhad1372
Copy link

hey @washo4evr .
i'm using your library for smart home. i hvae node js server and esp8266.
i can connect to websocket but after that websocket disconnected and i faced this error :
HTTP/1.1 404 Not Found.
in serial monitor.
i dont know how can i fix this or where is the problem.
complete error :
`WiFi connected
IP address:
192.168.1.103
HTTP/1.1 200 OK
Content-Type: text/plain; charset=UTF-8
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: Arduino
Content-Length: 103
Date: Mon, 09 Aug 2021 07:58:57 GMT
Connection: keep-alive

Connected. SID=
96:0{"sid":"HP8qKqdU9N3vT1agAAAB","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":5000}2:40Connecting via Websocket
HTTP/1.1 404 Not Found
Connection: Keep-Alive
Cache-Control: private, no-cache, no-store, must-revalidate, max-age=0
Pragma: no-cache
Content-Type: text/html
Content-Length: 708
Date: Mon, 09 Aug 2021 07:58:59 GMT

<title> 404 Not Found </title>

404

Not Found

The resource requested could not be found on this server!

connection failed`

any solution ?

@washo4evr
Copy link
Owner

Hi farhad1372,

which versions are you suing? (arduino version, node.js version...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants