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.
Related
I'm trying to use D1 mini to fetch some data from website. I created an API key on Thingspeak ThingHttp. However, the client didn't connect properly. I got "connection failed" from the Serial monitor.
Here is my code. I think they are almost the same as this.
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
WiFiClientSecure client;
#define HOST "api.thingspeak.com"
void setup()
{
const char *ssid = "my_wifi";
const char *password = "qwertyui";
const char *API = "W0B96PD71W3Z245Q";
Serial.begin(115200);
WiFi.mode(WIFI_STA);
delay(100);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
IPAddress ip=WiFi.localIP();
Serial.println(ip);
delay(5000);
Serial.println("finish setup");
}
void loop()
{
delay(5000);
if (!client.connect(HOST, 80))
{
Serial.println(F("Connection failed"));
return;
}
Serial.println("***");
}
And here is what I got from the serial monitor.
WiFi connected
IP address:
192.168.0.53
finish setup
Connection failed
Connection failed
It's obvious that it indeed connected to my wifi correctly, but just unable to connect to the server.
Does anyone know how to fix this? Or are there any crucial step I should set on my D1mini?
(I'm using VSCode instead of Arduino IDE)
You're using the wrong port number.
Port 80 is for unencrypted HTTP.
Port 443 is for HTTPS.
You're using WiFiClientSecure, so presumably you're intending to use HTTPS. HTTPS runs on port 443, not port 80. You'll need to change your code to use 443, or you'll need to use WiFiClient in order to work with port 80 (but make sure the API you're trying to connect to allows access over plain HTTP - most will not).
I highly recommend that you use an existing HTTP client rather than implement the protocol yourself as you'll need to with WiFiClient or WiFiClientSecure, which just provide TCP and encrypted TCP connections. You can find examples of how to use ESP8266HTTPClient in the ESP8266 Arduino core repository.
I am connecting 4 nodemcu (esp8266). 1 esp8266 is used as access point and others are connected to it.
When I send UDP packets as a broadcast message to all the esp8266 via with esp8266 as access point it is not recieved by the others however when I use a home router or even by mobile hotspot as access point, the broadcast messages are received by the other esp8266.
Also, I have posted here a part of my code that is used for UDP so there may be some variables that you will see as undeclared but they are originally declared and the code is working when I connect it with Access Point that is not esp8266
Code For Access Point:
#include
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.print("Setting soft-AP ... ");
boolean result = WiFi.softAP("ssid", "password123456");
if(result == true)
{
Serial.println("Ready");
}
else
{
Serial.println("Failed!");
}
}
void loop()
{
Serial.printf("Stations connected = %d\n",
WiFi.softAPgetStationNum());
delay(3000);
}
Code for sending UPD packet as broadcast:
unsigned int localPort = 2000;
IPAddress SendIP(192,168,43,255);
setup()
{
udp.begin(localPort);
Serial.print("Local port: ");
Serial.println(udp.localPort());
}
loop()
{
udp.beginPacket(SendIP, 2000);
udp.write("p");
udp.endPacket();
}
Code for Recieving UDP packets:
void loop()
{
int packetSize = udp.parsePacket();
if(packetSize)
{
udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
p = packetBuffer[0];
Serial.println(p);
function();
}
Please tell me whats the problem with using esp8266 as access point to send UDP packets.
And if esp8266 can not be used please tell me any other chip that can do the work, I want to make a portable system so I cannot use the router.
The default IP address of ESP8266 router in AP mode is 192.168.244.1.
Try to change sending address to 192.168.244.255.
So I’ve got an Arduino Uno and the Ethernet Shield V2 and connected these to a temperature sensor. Everything is working fine, the temperature is shown as desired, the problem is that I don’t seem to be able to save the results in my influxDB database.
Here my sketch:
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
const int sensorPin = A0;
int sensorVal;
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
byte host = {192, 168, 0, 153};
unsigned int port = 8089; // local port to listen on
EthernetUDP Udp;
void setup(){
//////////////////////
// PUT YOUR SETUP CODE HERE TO RUN ONCE
//////////////////////
Serial.begin(9600); // open serial port
Ethernet.begin(mac, host);
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can’t run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println(“Ethernet cable is not connected.”);
}
// start UDP
Udp.begin(port);
}
float getTemperature() {
sensorVal = analogRead(sensorPin);
float voltage = (sensorVal/1024.0) * 5.0;
float temperatureC = (voltage - 0.5)*100;
return temperatureC;
}
void loop(){
//////////////////////
// PUT YOUR MAIN CODE HERE; TO RUN REPEATEDLY
//////////////////////
String line, temperature;
delay(1000);
temperature = String(getTemperature(), 2);
Serial.println(temperature);
line = String(“temperature value=” + temperature);
Serial.println(line);
Serial.println(“Sending UDP packet…”);
Udp.beginPacket(host, port);
Udp.print(“temperature value=”);
Udp.print(temperature);
Udp.endPacket();
}
These are the setting from the config file of the influxDB:
[[udp]]
enabled = true
bind-address = “:8089”
database = “arduino”
retention-policy = “”
InfluxDB precision for timestamps on received points ("" or “n”, “u”, “ms”, “s”, “m”, “h”)
precision = “s”
I would appreciate if somebody could give me some clues about what I’m doing wrong.
Cheers
According to the documentation, you want to pass a remote IP (not local IP) when you call Udp.beginPacket.
Does host represent a remote IP address? Looks like you are using host as a local IP to begin Ethernet as well. It is likely that you are not sending packets to a remote host. Make sure you pass a local IP to Ethernet.begin() and pass a remote IP to Udp.beginPacket().
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
My Arduino web server sketch sporadically fails on:
EthernetClient client = server.available();
if (client)
This morning, it connected just fine on the first run. Now, it can't connect to the client again. A couple of days ago, it worked several times, but failed several time as well. I have the shield connected via an Ethernet cable to my home router. I've verified the IP address assigned to the Arduino. I've tried ports 80 and 8080. What could be going wrong and what else can I try? Could my ISP be blocking something here? Please don't be afraid to suggest the obvious, since I know almost nothing about networks.
If relevant, here is a larger piece of the code, which loops on
Serial.println("Listening");
Code:
#include <SPI.h>
#include <Ethernet.h>n
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0xF7, 0x99 };
IPAddress ip(192,168,2,5);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
String roundOpenTag = "";
String roundCloseTag = "";
void setup()
{
// Start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
int ledPin = 8;
// Initialize the digital pin as an output.
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
Serial.println("Setting up");
}
void loop()
{
// Listen for incoming clients
EthernetClient client = server.available();
Serial.println("Listening");
if (client)
{
Serial.println("Server available");
// An HTTP request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
Serial.println("Client connected");
if (client.available())
{
char c = client.read();
I don't see the purpose in including the rest of the sketch. I really appreciate your help.
You have a empty Seiral.begin() in your setup() function. Try removing it.
Edit:
When you call Serial.begin() you have to provide the baut rate(speed) at which you want to communicate. You can read more about the function at Arduino library page.
You had two problems in your code
You had a empty Serial.begin() function call, without any parameter
You had duplicate Serial.begin() function. You had already specified it in the beginning of the setup() function.