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.
Related
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 run a Processing sketch with my Arduino. I got it a few days ago, so I'm pretty much a noob. I made two similar sketches - one in Arduino and one in Processing. The Arduino one does work, while the Processing sketch doesn't, even though when running the Processing one, the RX lights up on the board.
I have connected an LED into the D9 on the board, with a 220 ohm resistor, and plugged the other leg into the GND. I then proceeded to run the Arduino sketch, which is a simple one, it lights up and down the LED for a second. This one worked.
I then tried running the Processing sketch, exact same code ( adapted for Processing ) using the library for Arduino, and the board seems to communicate with my sketch, as the RX is blinking each second on the board ( I tried different intervals of time and they match with the intervals at which the RX blinks ), but the LED does not turn on and off, like it did with the Arduino sketch.
I tried getting only a serial connection between the Arduino, and it worked - I connected a joystick module to the Arduino and sent the X and Y through the serial port, and the Processing sketch received the information through the serial port, so they are, indeed, communicating.
The port used is COM3 and is running at 9600 baud.
This is the Arduino sketch :
void setup() {
pinMode(9, OUTPUT);
}
void loop() {
digitalWrite(9, HIGH);
delay(1000);
digitalWrite(9, LOW);
delay(1000);
}
and this is the Processing ( version 3.4 ) sketch :
import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
void setup() {
arduino = new Arduino(this, Arduino.list()[0], 9600);
arduino.pinMode(9, Arduino.OUTPUT);
}
void draw() {
arduino.digitalWrite(9, Arduino.HIGH);
delay(1000);
arduino.digitalWrite(9, Arduino.LOW);
delay(1000);
}
Well done on step by step debugging such as double checking the wiring on the electronics side and testing the blink code with the Arduino alone to isolate the issue.
If the Blink sketch is the only Arduino code you have uploaded to your board that won't suffice. Processing does send messages to Arduino (which is why you see the RX LED turn on), but there's nothing in the Arduino code that initialises Serial communication
As you can see in that example, in setup() Serial communication is initialised with 9600 baud rate (communication speed, 9600 bytes/chars per second):
Serial.begin(9600);
Then in draw() if there is data available, each character is read, then printed one at a time with a prefixed message:
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
}
If you upload the example linked, if you've got a single Serial port, you should see both the RX then ever so slightly after the TX LED blinking when you run your Processing sketch. If you close that sketch, open Serial Monitor in Arduino and type something then press enter you'll see the debugging message read back from Arduino.
Using these notions you could write a basic sketch like so:
int incomingByte = 0; // for incoming serial data
void setup() {
pinMode(9, OUTPUT);
Serial.begin(9600);
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
// if we received ASCII character '1', turn LED on
if(incomingByte == '1'){
digitalWrite(9,HIGH);
}
// if we received ASCII character '0', turn LED off
if(incomingByte == '0'){
digitalWrite(9,LOW);
}
}
}
Uploading this sketch to your Arduino should allow you to type 1 into Serial Monitor and press Enter to turn the LED on or 0 to turn it off.
The only thing left is to send the same data from Processing:
import processing.serial.*;
Serial arduino;
void setup(){
try{
arduino = new Serial(this, Serial.list()[0], 9600);
}catch(Exception e){
println("error connecting to serial port, double chek USB connection, serial port and close other programs using Serial");
e.printStackTrace();
}
}
void draw(){
}
void keyPressed(){
if(key == '1'){
if(arduino != null){
arduino.write('1');
}else{
println("arduino serial connection wasn't initialised");
}
background(255);
}
if(key == '0'){
if(arduino != null){
arduino.write('0');
}else{
println("arduino serial connection wasn't initialised");
}
background(0);
}
}
Minor side note: notice I'm not using delay() in Processing, I recommend using millis() instead as it doesn't block the execution of code like delay() does.
So the above looks like quite a bit of code just to blink an LED but the point is to understand the basics of Serial communication which will be useful on the long run:
initialising serial communication with Arduino (understand baud rate)
basic reading/writing of bytes over Serial
initialising serial communication from Processing and sending data
Back to your original question, you've missed an important detail regarding the Arduino library you're using in Processing: it's relying on a special Arduino sketch (firmware) called Firmata. You will be able to read more on that and how to use the library in this Arduino and Processing tutorial.
As the tutorial mentions you need to first upload this sketch from Arduino > Examples > Firmata > StandardFirmata. Also bare in mind baud rate is set to 57600, not 9600 so you need to update your code like so:
arduino = new Arduino(this, Arduino.list()[0], 57600);
To use: are you sure to put the standardfirmata
Using the Arduino software, upload the StandardFirmata example (located
in Examples > Firmata > StandardFirmata) to your Arduino board.
change the line
arduino = new Arduino(this, Arduino.list()[0], 9600);
to:
arduino = new Arduino(this, "COM3", 57600); // in Firmata -> Firmata.begin(57600);
you could add this line to look after your serial port:
println(Arduino.list());
Modify the "arduino = new Arduino(...)" line below, changing the number in Arduino.list()[0] to the number corresponding to the serial port of your Arduino board. Alternatively, you can replace Arduino.list()[0] with the name of the serial port, in double quotes, e.g. "COM3" on Windows or "/dev/tty.usbmodem621" on Mac.
I got it working with Arduino but I had to change some details. My port was "COM3" or Arduino.list()[1] (the 2nd port on the list) which you can check in Windows device manager (Ports COM & LPT: USB-SERIAL) after installing the latest drivers (maybe on the usb port that appears when you connect your Arduino under other devices) using the system update and restarting, then you may need to repeat the system update and restart 2 or 3 times. Or on Linux, you can find which port it's on with:
ls /dev/ttyUSB*
Then unplug it and check it again.
First I had to upload the Arduino IDE program (running it with the serial monitor window from the tools menu ctrl-shft-m after having the same exact baud rate on the lower right menu option as in the program). Then I could close it and compile the processing one as long as I had input that very same baud rate into the Processing program too. All 3 different bauds that I tried, 9600, 57600, 115200, worked requiring their equality between Arduino IDE, Arduino IDE Serial Monitor and Processing. If I uploaded a different project in IDE, then Processing did not even connect to the Arduino, so it had to be that same project running on it for Processing to communicate with Arduino Uno properly. Processing is basicly USING Arduino IDE by sending or receiving messages already programmed for it to do, it doesn't program the Arduino in this case. I have even gone through a big mess, trying to get Visual Micro to work (Arduino on Visual Studio) cross-platform but it still would not allow me to link other libraries and headers because of how picky Arduino's programming is! One of the best ways to learn is to check the actual arduino.cc or Processing manual command parameters after finding out where your problem is.
Good afternoon,
I am trying to communicate through WiFi with ESP8266 module on Arduino. So far I have succeded to make my hardware setup and a very basic communication in between Arduino and ESP8266 module. I am using SoftwareSerial library to communicate, however the data outputs printed to the Serial seems quite corrupted, even though the module succesfully connects.
When I sent AT+CWJAP="AndroidAP","52689785" on Serial console, this is the output:
AT+CWJAP="AndroidAP","52689785"
AT+C⸮⸮P⸮⸮⸮⸮⸮⸮⸮ѕ͉b⸮⸮⸮⸮⸮⸮ѕ⸮ɂ⸮⸮j
WIFI DISCONNQ(UH⸮WIFI CONNECTED
WHFI GOT IP
OK
Below is my full code, I communicate with ESP module through pins 10 and 11:
#include "SoftwareSerial.h"
SoftwareSerial softSerial(10, 11); // RX, TX
void setup()
{
Serial.begin(9600);
// Serial.setTimeout(30);
softSerial.begin(115200);
// softSerial.setTimeout(30);
while(!Serial);
}
void loop()
{
if (softSerial.available())
{
String message = softSerial.readString();
Serial.print(message);
}
if (Serial.available())
{
String message = Serial.readString();
Serial.print(message);
softSerial.print(message);
}
}
I would appreciate if you could show me the solution for a better communication in between Arduino and ESP module, thanks!
Most of the ESP8266 modules will be working better at 115200 baud rate.
The reason for gibberish output can be:
either you may be viewing output at 9600 baud rate
visit the link
Device : Arduino UNO WiFi Developer Edition.
IDE: Arduino IDE 1.8.2
OS : Windows 7 64 bit
I have an issue with my Arduino device where it simply stops printing to serial and the Wifi console output is just garbage text/weird characters. Here example codes of it working and when it's not working:
WORKING
#include <UnoWiFiDevEd.h>
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.println("Testing...");
delay(5000);
}
NOT WORKING
#include <UnoWiFiDevEd.h>
void setup()
{
Serial.begin(9600);
Ciao.begin();
}
void loop()
{
Serial.println("Testing...");
delay(5000);
}
What I have tried:
Reset
Firmware Update
Unplug/Replug device
Change port
Change board to NodeMCU or ESP8266
Download and re-install IDE
Reboot Laptop
Uninstall & re-install device drivers
Try previous Arduino Uno Wifi Dev Ed library
Restart/Reset WiFi
Garbage text WiFi console:
Any suggestions, ideas and solutions would be great.
Try resetting [connectivity] parameters. Click on [CONNECTIVITY], then fill in:
Server port: 1883
Client ID: arduino
Client Timeout: 2
Keep Alive: 60
Topic: arduino
[v] ENABLE SLIP...
Click on both [UPDATE] buttons, and ensure that parameters are registered.
enter image description here
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.