I've been trying to set up an access point combined with a web server using the ESP8266WiFi libraries for Arduino. I am using an ESP8266 Huzzah module with NodeMCU firmware installed (newest master).
Setting up an AP using softAP works fine, I can also assert a PSK to it and choose which channel to use (I've chosen the one with the least traffic, in this case ch 11). The problem is when trying to access the web server from a connected device, it always ends up with a timeout exception. The ESP's IP is in this case 192.168.4.1 and the port is 80.
I've tried connecting the ESP to another AP (Home router) in STA mode and I am successfully able to connect to the ESP through the given IP Address.
To sum it all up:
ESP8266WebServer + WIFI_AP (Station) => Does not work!
ESP8266WebServer + WIFI_STA (Client) => Works!
This is how I set the web server up, together with AP:
In Setup:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
[...]
ESP8266WebServer server(80);
[...]
Serial.print("Setting soft-AP ... ");
WiFi.disconnect(true);
WiFi.mode(WIFI_AP);
delay(100);
boolean result = WiFi.softAP("TestAP", "0123456789", 11); // This does of course return true, and the access point shows up on my device
if(result == true)
{
Serial.println("Access Point Ready");
Serial.println(WiFi.softAPIP()); // Prints 192.168.4.1
}
else
{
Serial.println("Access Point Failed!");
}
[...]
server.on("/", [](){
server.send(200, "text/html", webPage);
delay(1000);
});
[...]
server.begin();
In Loop:
server.handleClient();
Has anyone else done this before and had it work? What am I actually doing wrong here?
Ok, so it seems that I figured it out partly...
All the time, I was using my smartphone to connect to the ESP AP and trying to access the web server. I tried connecting from my computer and by all the stupidness in this world, it works.
Why I can not connect via the phone is a mystery to me, maybe someone can tell me what is going on here...
Basically on certain android devices look out for a notification about staying connected as mentioned in the comments.
If you don't accept staying connected your phone will still show as "connected with out internet" and the page will not load, once you have accepted the notification about staying connected then you can load your wemos page.
On an android nvidia based tablet this was not required, but on my motorola smart phone it would not work until i accepted that box.
Related
This is my code
#include <WiFi.h>
const char* ssid = "wifiname";
const char* password = "12345678";
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA); // SETS TO STATION MODE!
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
Serial.print("IP is ");
Serial.println(WiFi.localIP());
}
void loop() {
}
I can found my iPhone's personal hotspot using the WiFiscan routine, but I can't connect to it.
I tested your code and I was able to connect my ESP32 to my iPhone 8+. Since you were able to scan your iPhone's hotspot, you could try the following:
Make sure that you are using the 2.4GHz hotspot and NOT the 5GHz. If you have a iPhone 12, which now supports 5GHz hotspot, disable 5GHz by going to Settings -> Personal Hotspot -> enable(!) "Maximize compatibility"
Try to connect with an other wifi (in the 2.4GHz range)
(sounds stupid but): did you try to change the SSID and PW on your iPhone? Are you sure that your "ssid" and "password"-variable are both correct (no spelling mistakes)? For testing purposes you could even try to exclude special characters and only use letters and numbers.
If all fails it might be a HW issue. Did you try to use a different ESP32?
FWIW: I have apparently been able to connect the ESP32 chip's Wifi to my iPhone13 pro running IOS 15.3.1. The line of code:
WiFi.mode(WIFI_STA); // SETS TO STATION MODE!
was the break thru allowing me to make this connection. Note that I can make the connection even with settings->personal hotspot->Maximize Compatibility DISABLED.
I believe the iPhone hotspot turns off (or at least stops broadcasting an SSID) shortly after leaving the "Personal Hotspot" screen in "Settings". Leaving the "Personal Hotspot" screen open with my iPhone unlocked allowed me to connect my ESP32.
This Apple support discussion from 2011 describes the behavior (assuming it hasn't changed in the last decade): https://discussions.apple.com/thread/2784174
For me, the apostrophe in the default hotspot SSID was problematic (ex: John Doe's iPhone). I changed the iPhone name (and therefore the SSID name) in Settings > General > About. Afterwards, my ESP32 was able to connect to the hotspot.
Potential caveat: instead of programming the firmware directly, I was using ESP Web Tools to configure the ESP32 with ESPHome, so my issue may have been specific to the ESPHome firmware (and how it handles character encodings).
I find a solution for that issue by trials and may be it could help :
1.You have to be in the iPhone hotspot settings when the ESP32 is trying to connect specially if there's no other device connected to the hotspot!
2.Hotspot works fine until the phone gets locked and ESP32 went to deep-sleep and after wake up the ESP32 cannot connect again in some cases until you go again to the hotspot page!
As an introduction, I bought myself an arduino and a few modules to learn some software stuff. The project is to eventually connect to a bluetooth OBD2 reader on my car to display real time data on a small LCD.
The problem
I am either not able to connect to, or not write to, my HC05 module via software serial. I think I have narrowed this down to a couple possibilities.
I am unable to connect to the module in the first place.
I have a Mega 2560 and HC05.
5V <-> VCC
GND <-> GND
D2 <-> RXD
D3 <-> TXD
Note that I have seen 9600 and 38400 baud rates for connecting but neither worked, so I made this function to try them all for me...
//set up serial relay into HC05.
SoftwareSerial hc05(2,3);
//computer serial baud rate 115200
bool hc05_connect() {
long baud_list[] = {300, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 74880, 115200, 230400};
Serial.println("Attempting to connect to HC05 bluetooth module...");
bool success = 0;
for (int i=0; i<(sizeof(baud_list) / sizeof(baud_list[0])); i++) {
Serial.print("Baud rate ");
Serial.print(baud_list[i]);
Serial.print("...");
hc05.begin(baud_list[i]);
hc05.write("AT");
delay(1000);
if (hc05.available()) {
Serial.println(" successful!");
success = 1;
return success;
} else {
Serial.println(" failed");
}
}
return success;
}
Notes:
This has always returned failed for every baud rate.
I have the bluetooth module in command mode, initiated by pressing the button as I supply power.
I have tried unplugging the TX/RX pins while uploading the sketch. No difference noted.
My attempts to send commands to the HC05 are failing.
Below is my function for sending commands to the module.
void loop() {
// listen for communication from the ESP8266 and then write it to the serial monitor
if (hc05.available()) {
Serial.write(hc05.read());
}
// listen for user input and send it to the ESP8266
if (Serial.available() > 0) {
Serial.println("Writing to hc05...");
Serial.println(Serial.available());
Serial.println(Serial.read());
hc05.write(Serial.read());
}
}
I have added in a few lines which write back to Serial so I can see what's being sent, and the monitor returns weird stuff. For example, if I send "AT", this is what the monitor reads:
Writing to hc05...
3
65
Writing to hc05...
1
10
Notes:
Why is it sending 2 different items?
Why is it sending integers rather than the characters I said?
Does this indicate I'm just sending it nonsense commands so it's not responding?
I can provide full code if you want, this is already a huge textwall though. Please help me!
Edit
So I have been able to get communication two ways via the bluetooth module using a modified version of the code in this instructable: https://www.instructables.com/How-to-Set-Up-and-Test-Arduino-Bluetooth-Connectio/
I was able to send from PC only and not receive to an android bluetooth terminal using SoftwareSerial with HC05's RX - TX0 / TX - RX0.
And I was able to receive to PC and not send using hardware serial / Serial1 with HC05's RX - TX1 / TX - RX1.
So now I have RX - TX0 / TX - RX1. It seems to communicate through terminal like this.
void setup() {
Serial.begin(9600); //open the serial port
Serial1.begin(9600);
}
void loop() {
if (Serial1.available()) {
Serial.print("(Received)");
Serial.println(Serial1.readString()); // send from serial to bluetooth
}
if (Serial.available()) {
Serial.print("(Sent)");
Serial.println(Serial.readString());
Serial1.println(Serial.readString()); // send from bluetooth to serial
}
}
But if I apply this to my code, I still can't get it to work.
Before I try to hack this together, why am I getting serial to work across 2 different serial channels? Weird...
Okay, so I figured it out. (I can't say I fully understand, but maybe this will help people in future.)
1. Unable to connect to module
Thanks #ukBaz for suggesting I connect with the terminal app on my phone, this allowed me to debug the connection to the module in the first place. and #Juraj for suggesting that the Mega uses hardware serial.
Serial1 apparently is broken on my board, so I am using Serial3. I bluetoothed to the device with my phone, and was able to send commands back and forth between Serial and Serial3 both on 9600 baud rate. Here is the code I used:
void setup() {
Serial.begin(9600); //open the serial port to PC
Serial3.begin(9600); //open serial port to HC05. TX -> 15, RX -> 14
}
void loop() {
if(Serial3.available()){
Serial.print(Serial3.readString()); // send from serial to bluetooth
}
if(Serial.available()){
Serial3.print(Serial.readString()); // send from bluetooth to serial
}
}
I suspect I was using the wrong read/readString and write/print/println for my purpose initially.
2. Unable to issue commands to the module
Once I got that working, I changed the baud rate to 38400, and tied the STATE pin of the module to VCC (rather than using the button). Uploaded code, disconnected 5V, reconnected 5V, reset arduino.
At that point, I could issue "AT" to the module via Serial Monitor, and receive "OK" back. Woohoo!
I think I understand now that #hlovdal was suggesting that I was issuing a command to the module and never parsing a response I got, so it was.. clogged parhaps? In any case. I can now successfully issue commands and receive responses from the module.
Thanks everyone for your help.
There are some problems with how you are communicating AT command lines to your device. For instance:
hc05.write("AT");
delay(1000);
You do not send an AT command to a modem, you send an AT command line that contains zero or more AT commands, followed by a command line terminating character (that always should be '\r', aka carriage return (or <CR>)).
You are missing that terminating character here, so the modem will never send you a reply, because you have not sent it a command line.
And also, you should never, ever use delay as a substitute for reading and parsing the responses that the modem sends back. See this answer for some more details.
I am trying to implement the official Arduino Web Client code from examples
which is resided here: https://www.arduino.cc/en/Tutorial/WebClient ,
but unfortunately it doesn't work form me.
What am I supposed to do after uploading this sketch???
This is the only line that I changed to:
IPAddress ip(192, 168, 1, 178);
What I do is going to the url:
192.168.1.178 (in my local network, other sketches work fine in this ip) and if I well understood the function of this code, we request a particular page from google's server, so the server responses and bring to us this page, via the get request we make inside our code. Unfortunately, it doesn't load any page.
If I am wrong to anything of what I've said, please let me know.
Is your ethernet controller wired perfectly?
The Ethernet shield allows you to connect a WizNet Ethernet controller to the Arduino or Genuino boards via the SPI bus. It uses pins 10, 11, 12, and 13 for the SPI connection to the WizNet. Later models of the Ethernet shield also have an SD Card on board. Digital pin 4 is used to control the slave select pin on the SD card.
For help, here is the schematic of the shield: Schematics
Watch your serial. Does it say this?
Failed to configure Ethernet using DHCP
Or does it say this?
connecting...
Does your router use 192.168.1 instead of 192.168.0 or 192.168.2?
Is there any other device who has the same IP address?
If nothing works, try to replace this piece of code
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
}
With this
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// try to congifure using IP address instead of DHCP:
if(Ethernet.begin(mac, ip) == 0) {
Serial.println("Failed to configure Ethernet using IP address.
Circuit hanged.");
while(1){}
}
}
If this code sais:
Failed to configure Ethernet using IP address. Circuit hanged.
Of course, if you are using a WiFi shield, this example won't work. Try the wifi shield's example which is included in it's library.
So I recently bought the Arduino Wifi Shield 101. I went through the getting started steps, and just copied and pasted their "scan for networks" code. The code that I used is down below. So to help you guys help me, I should say my school's wifi is wpa2 encrypted. However, the code is simply looking for possible networks to connect to. The code runs fine until it gets to the "Wifi.macAddress(mac)" line. I don't understand why the code stops working here. There aren't any errors in compiling or uploading, the code just seems to not be working. Im obviously a beginner with the arduino wifi board, so any help at all would be great.
Here's the code:
`#include <SPI.h>
#include <WiFi101.h>
void setup() {
// initialize serial and wait for the port to open:
Serial.begin(9600);
while(!Serial);
// attempt to connect using WEP encryption:
Serial.println("Initializing Wifi...");
printMacAddress();
// scan for existing networks:
Serial.println("Scanning available networks...");
listNetworks();
}
void loop() {
delay(10000);
// scan for existing networks:
Serial.println("Scanning available networks...");
listNetworks();
}
void printMacAddress() {
// the MAC address of your Wifi shield
byte mac[6];
// print your MAC address:
Serial.print("The code got to here");
WiFi.macAddress(mac); //why won't this method work?
Serial.print("The code never reaches this point ... Why?!?!?");
Serial.print("MAC: ");
Serial.print(mac[5],HEX);
Serial.print(":");
Serial.print(mac[4],HEX);
Serial.print(":");
Serial.print(mac[3],HEX);
Serial.print(":");
Serial.print(mac[2],HEX);
Serial.print(":");
Serial.print(mac[1],HEX);
Serial.print(":");
Serial.println(mac[0],HEX);
}
void listNetworks() {
// scan for nearby networks:
Serial.println("** Scan Networks **");
byte numSsid = WiFi.scanNetworks();
// print the list of networks seen:
Serial.print("number of available networks:");
Serial.println(numSsid);
// print the network number and name for each network found:
for (int thisNet = 0; thisNet<numSsid; thisNet++) {
Serial.print(thisNet);
Serial.print(") ");
Serial.print(WiFi.SSID(thisNet));
Serial.print("\tSignal: ");
Serial.print(WiFi.RSSI(thisNet));
Serial.print(" dBm");
Serial.print("\tEncryption: ");
Serial.println(WiFi.encryptionType(thisNet));
}
}`
Are you powering the boards via USB? If so, you're likely experiencing an under-power issue.
My original answer was deleted (so I don't know what you can and cannot see), but the link to the Arduino bug I filed is here:
Arduino 101 + Wifi 101 Shield board freeze. #50
As it turns out, my board was simply under-powered as I was using a USB port rather than a wall outlet. I actually ended up using a different USB port and the scanNetworks example now works for me.
EDIT Actually, as it turns out, it was the USB cable. Either way, power was the issue.
I would recommend powering the board via a wall wart or choosing a different USB port and trying again.
I have an arduino GSM shield sitting on top of an arduino uno. I have the code below. The shield just shows me it is connecting but it never shows me it is connected.I want to know why it is not connecting.I will be grateful for any help.Am using arduino 1.0.5 IDE.
#include <GSM.h>
#define PINNUMBER ""
GSM gsmAccess(true);
GSM_SMS sms;
void setup() {
// initialize serial communications and wait for port to open:
Serial.begin(9600);
char code = 'X';
while(true) {
Serial.println("try Access");
code=gsmAccess.begin("",true,false);
Serial.println("\nAfter Access");
if(code==GSM_READY){
Serial.println("connected");
break;
}
if(code==CONNECTING) {
Serial.println("code is CONNECTING");
} else {
Serial.println(code);
delay(1000);
}
}
}
You are starting up the modem in asynchronous mode with:
code=gsmAccess.begin("",true,false);
Looking at the GSMBegin documentation, you are going to get a return value of 0 always which does not correspond to the GSM_READY enum type which is 3 I believe. Try:
code=gsmAccess.begin("",true);
try connecting a 9v battery to it. for me that solved the problem. i had the same issue. apparently, the gsm shield uses a lot of power. some computers can deliver enough, some cant.
I had the same problem. I solved it by connecting a 680 μF capacitor between 5 V and GND. This is only a temporary fix because it is going to create a huge current spike when connecting the power supply. A better solution would be to connect an external power supply or a more capable USB power supply.