I been trying to get my ESP8266 to connect to a websocket, but no luck so far. Things that are ruled out are:
The ESP8266 is connected to WiFi and has access to Internet (checked using a HTTP request).
Not a problem with the socket server, I spun up my own websocket server and it was not hit.
Tried port 80 and 443.
My code is as follows, keeps printing
Not Connected!
:
#include <Arduino.h>
#include "WebSocketClient.h"
#include "ESP8266WiFi.h"
WebSocketClient ws(true);
void setup() {
Serial.begin(115200);
WiFi.begin("SSID", "PASSWORD");
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
}
void loop() {
if (!ws.isConnected()) {
ws.connect("echo.websocket.org", "/", 80);
Serial.println("Not connected!");
} else {
ws.send("hello");
String msg;
if (ws.getMessage(msg)) {
Serial.println(msg);
}
}
delay(500);
}
WebSocketClient ws(true); tries to connect securely and uses WiFiClientSecure in stead of WiFiClient. This will probably require specifying a certificate or some such.
Try WebSocketClient ws(false); to see if that works (tries to force an insecure connection).
I searched how to configure a DHCP server on ESP32 Arduino to distribute addresses for clients that connect to my ESP32 access point, but unfortunately I did not get any source code for that.
Any help?
As long as you use WiFi.softAP(), you do not need to explicitly configure a DHCP server on the ESP32. It will happen automatically - the library looks after it for you.
Here is a minimal example, where - in addition to setting the ESP32 up as an access point - a TCP server is also started on port 80.
WiFiServer server(80);
static const char *ap_ssid = "ESP32-001";
static const char *ap_pass = "temp_pass";
void setup() {
Serial.begin(115200);
WiFi.softAP(ap_ssid, ap_pass);
Serial.print("Access point running. IP address: ");
Serial.print(WiFi.softAPIP());
Serial.println("");
server.begin();
}
void loop() {
WiFiClient client = server.available();
if (client) {
String client_ip = client.remoteIP().toString();
Serial.print("Client connected. IP address = ");
Serial.print(client_ip);
Serial.println("");
client.println("Hello ...");
client.stop();
}
}
I have attached the serial output in a screenshot below. Notice the
dhcps: send_offer>>udp_sendto result 0
message.
The problem is that I want to execute the code below, but when ESP8266 is shut down, then I start it again, everything is gone.
So, is there a solution that I can make this ESP8266 work the same controlled by my Arduino Uno.
My program blow is to control the GPIO2 through web browser.
Thanks a lot to all of you!!
My Codes:
#include <ESP8266WiFi.h>
#include <aREST.h>
// Create aREST instance
aREST rest = aREST();
// WiFi parameters
const char* ssid = "Protect Big Dragon 4";
const char* password = "18717772056";
// The port to listen for incoming TCP connections
#define LISTEN_PORT 80
// Create an instance of the server
WiFiServer server(LISTEN_PORT);
void setup(void)
{
// Start Serial
Serial.begin(115200);
// Give name and ID to device
rest.set_id("2");
rest.set_name("lamp_control");
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.println(WiFi.localIP());
}
void loop() {
// Handle REST calls
WiFiClient client = server.available();
if (!client) {
return;
}
while(!client.available()){
delay(1);
}
rest.handle(client);
}
So your ESP8266 loses your code when it is rebooted.
Sounds like the memory is faulty.
Try a different ESP8266 and let us know what occurs.
I am quite new to IoT, but I am building a system where I have multiple IoT devices in a home connected to internet using ESP8266 module. But I do not have a particular gateway in any home. I am relying on WiFi router. To convey any message to the device, from internet, I need to identify the particular device.
I am using DDNS for the home router's IP. But how can I send requests coming from the server to all the devices connected.
I have this code in my ESP8266 modules.
ESP 1:
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
MDNSResponder mdns;
ESP8266WebServer server(80);
void setup(){
server.on("/esp_unique_01/", [](){
server.send(200, "text/html", webPage);
});
server.on("/esp_unique_01/socket1On", [](){
server.send(200, "text/html", webPage);
digitalWrite(gpio0_pin, HIGH);
delay(1000);
});
server.on("/esp_unique_01/socket1Off", [](){
server.send(200, "text/html", webPage);
digitalWrite(gpio0_pin, LOW);
delay(1000);
});
}
ESP 2:
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
MDNSResponder mdns;
ESP8266WebServer server(80);
void setup(){
server.on("/esp_unique_01/", [](){
server.send(200, "text/html", webPage);
});
server.on("/esp_unique_02/socket1On", [](){
server.send(200, "text/html", webPage);
digitalWrite(gpio0_pin, HIGH);
delay(1000);
});
server.on("/esp_unique_02/socket1Off", [](){
server.send(200, "text/html", webPage);
digitalWrite(gpio0_pin, LOW);
delay(1000);
});
}
Both of these ESP modules are having a dynamic IP. Any lead would be highly appreciated.
a mixture from the example of MDNS :
char hostString[16] = {0};
sprintf(hostString, "ESP_%06X", ESP.getChipId());
if (!MDNS.begin(hostString)) {
Serial.println("Error setting up MDNS responder!");
}
Serial.println("mDNS responder started");
// Start TCP (HTTP) server
server.begin();
Serial.println("TCP server started");
// Add service to MDNS-SD
MDNS.addService("http", "tcp", 80);
So you gonna have http service description for node1. Run second node's web server on a different port and define it.
The solution is to assign static IP addresses to your ESP8266 modules and to use different ports to access them from internet. Here is the code to assign a static address to ESP8266:
IPAddress ip(192,168,1,xx); // desired static IP address
IPAddress gateway(192,168,1,yy); // IP address of the router
IPAddress subnet(255,255,255,0);
WiFi.begin(ssid, password);
WiFi.config(ip, gateway, subnet);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Now that you know local IP addresses of your ESP8266 devices you can choose ports to be forwarded to your ESPs. For example you can choose ports 8266, 8267, 8268, 8269, ... or any other above 1024 and then set the port forwarding in your router settings so port 8266 will be forwarded to the IP address of your first ESP and port 80, port 8267 will be forwarded to the IP address of your second ESP and port 80 etc.
Afterwards you can access your ESPs from internet using URLs looking like http://xx.xx.xx.xx:8266, http://xx.xx.xx.xx:8267, http://xx.xx.xx.xx:8268, http://xx.xx.xx.xx:8269, ...
Since you are already using dynamic DNS service to access your ESP8266 when there are no other ESPs in your local network, that means you can just add :8266, :8267, :8268, :8269, ... to the URL you are already using. In other words, if the URL consisting of what you have chosen + the part generated by your dynamic DNS service is http://myhome.something.com then you will now use http://myhome.something.com:8266, http://myhome.something.com:8267 etc.
I'm trying to send information from the arduino board to my computer through the Wi-Fi network.
for my project's purposes it has to be a UDP connection
I use the "Send and Receive UDP String" example (http://arduino.cc/en/Tutorial/WiFiSendReceiveUDPString)
with a few changes:
#include <SPI.h>
#include <WiFi.h>
#include <WiFiUdp.h>
int status = WL_IDLE_STATUS;
char ssid[] = "itay_net"; // your network SSID (name)
char pass[] = "0527414540"; // your network password (use for WPA, or use as key for WEP)
unsigned int localPort = 50505; // local port to listen on
IPAddress remote_ip(192, 168, 1, 100);
unsigned int remote_port = 50505;
char ReplyBuffer[] = "acknowledged"; // a string to send back
WiFiUDP Udp;
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while(true);
}
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
// you're connected now, so print out the data:
Serial.print("You're connected to the network");
delay(10000);
printWifiStatus();
Serial.println("\nStarting connection to server...");
// if you get a connection, report back via serial:
Udp.begin(localPort);
}
void loop() {
int bite_send;
Udp.beginPacket(remote_ip, remote_port);
bite_send = Udp.write("hello");
Udp.endPacket();
Serial.println("the packet was sent");
Serial.println(bite_send);
delay(1000);
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
It compiles and connects to the network just fine.
the only problem is that I can't tell if the packet was sent because I see no trace of it on Wireshark.
I also wrote a socket on java that listens to the port (50505) and should display the message from the packet, but it didn't work either.
(I can copy the java code here but i can assure you that it is not the problem 'cause I tested it with a different java server and it worked, so the problem should be on the Arduino side)
a few details to narrow it down:
I believe the "remote ip" is correct but even if it isn't - I still should have seen it in the Wireshark, so it can't be the problem.
I should mention that the Wi-Fi shield works, I successfully sent pings and ran other examples (such as SimpleWebServerWifi).
I'm using an original Arduino Uno R3 board and an original Wi-Fi shield.
The arduino IDE is the newest version.
I updated the Wi-Fi shield with the newest update I found on GitHub.
I also ran the same "Send and Receive UDP String" code (with the necessary changes) on my Ethernet shield and it did work.
I don't know what else to try - please help.
any help will be appreciated.
Itay
I dont think you have a reply buffer packet. google arduino wifisendrecieve and you will see the example they have that has a reply packet labeled as 'acknowledged'. Hope this helps