// attempt to connect to Wifi network:
while (status != WL_CONNECTED){
listNetworks();
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
Serial.print("Status: ");
Serial.println(status);
// wait 3 seconds for connection:
delay(3000);
}
The above code repeatedly attempts to connect to a WPA2 network. The serial output this produces looks like this:
** Scan Networks **
number of available networks:2
0) SKY0C026 Signal: -48 dBm Encryption: WPA2
1) WAP-HOME Signal: -84 dBm Encryption: WPA
Status:4
This repeats over and over again as the connection fails (status 4). What I don't understand is that once in a while it is able to connect, sometimes on the second try. I've tried extending the delay time between connections but no luck. Is there anything I'm missing?
So I believe as an anti brute force attack mechanism routers don't like it when devices repeatedly try to connect in too short a time span, sometimes even 10 seconds is too short! I guess the MAC address gets blacklisted and that's why the connection fails. The solution was to reset my router and up delay between reconnects.
Related
I've been trying to connect my arduino to a gopro hero 11, which should support BLE communication. The ArduinoBLE example "PeripheralExplorer" finds it and lists all the BLE services contained, roughly a dozen.
However, when trying to connect to a specific Service UUID, it fails and when I use advertisedServiceUuidCount(), it returns 1. Is there something I should do to switch the advertised Service from the Gopro? Or am I doing something wrong to access all of the GoPro's services?
Also, other minor question : should I advertise with my arduino as a client?
here's the BLE part of the code I tried :
`
while (!BLE.begin()) {
Serial.println("starting Bluetooth® Low Energy module failed!");
}
BLEDevice peripheral;
do{
BLE.scan();
delay(500);
peripheral = BLE.available();
}while (peripheral.localName()!="GoPro 3167");
Serial.println("trouvé le truc");
BLE.stopScan();
// if(peripheral.hasService(WifiServiceUUID)) Serial.println("ah?");
Serial.println(peripheral.advertisedServiceUuidCount());
`
thanks!
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.
In Sim800L AT-Commands Guide there are a lot of different status commands, that should tell, what state does is have.
For example:
AT+CPAS - check if device is ready
AT+CGREG? - check registration status in network
AT+CGATT? - check if device "attached to network"
AT+CSQ - get signal level
But, in some cases, answers to this commands could be an "ERROR", or no answer at all.
I need a reliable and fast method to know is device connected to network, or it's already in GPRS mode, and for now I came to use blinking LED on Sim800L to detect it's state.
LED has three blinking frequencies:
Fast blinking - GPRS connection is active
Medium speed of blinking - Network connection is not established yet
Slow blinging - Device is connected to network (but not the GPRS)
I can use photodiode and "read" blinging of LED, or I can wire LED's power pin to analog pin of Arduino, and read it's voltage. Next, I can count how fast LED is blinking, and determine, which state Sim800L in.
But how do I get this level of reliability without using such a crutch?
Given fast means 1 sec you could send an AT command e.g. five times and take 3 non error responses as a valid result. See pseudo code
uint8_t errorCount = 0;
for (uint8_t i=0;i<5;i++){
.... send AT command ...
if (response == "error") errorCount++;
if (errorCount >=3) errorHandling();
}
... process successful AT command ...
or your HW approach with LED to analog pin
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.