Conflict between Arduino delay function and SoftwareSerial - arduino

It seems that I have a conflict between SoftwareSerial and the delay function on my Arduino (GeekCreit bought on Banggood). I am trying to use SoftwareSerial to send AT commands to an ESP-01.
When I perform:
#include <SoftwareSerial.h>;
SoftwareSerial esp8266(8,9);
void setup() {
Serial.begin(9600);
while (!Serial) ;
esp8266.begin(9600);
esp8266.println("AT");
}
void loop() {
if(esp8266.available()) {
while(esp8266.available()) {
Serial.print(esp8266.read());
}
}
}
Everything works well, the AT command is sent and I receive the response from my ESP.
But when I add a delay before sending the AT command, nothing happens: no command sent, no answer from the ESP.
#include <SoftwareSerial.h>;
SoftwareSerial esp8266(8,9);
void setup() {
Serial.begin(9600);
while (!Serial) ;
esp8266.begin(9600);
delay(2000);
esp8266.println("AT");
}
void loop() {
if(esp8266.available()) {
while(esp8266.available()) {
Serial.print(esp8266.read());
}
}
}
Am I doing something wrong, has someone experienced the same problem?
I have tried to use AltSoftSerial instead but I have the same issue with it.

Related

Could not find a valid MPU6050 sensor

I am using the below code with arduino-uno, but often getting "Could not find a valid MPU6050 sensor
#include <Wire.h>
#include <MPU6050.h>
MPU6050 mpu;
void setup() {
Serial.begin(115200);
Serial.println("Initialize MPU6050");
while (!mpu.begin()) {
Serial.println("Could not find a valid MPU6050 sensor, check wiring!");
delay(500);
}
}
void loop() {
}
My Arduino is working fine,
So, I checked MPU6050 using below code,
#include <Wire.h>
void setup()
{
Wire.begin();
Serial.begin(115200);
while (!Serial); // Leonardo: wait for serial monitor
Serial.println("\nI2C Scanner");
}
void loop()
{
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for(address = 1; address < 127; address++ )
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0)
{
Serial.print("I2C device found at address 0x");
if (address<16)
Serial.print("0");
Serial.print(address,HEX);
Serial.println(" !");
nDevices++;
}
else if (error==4)
{
Serial.print("Unknown error at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");
delay(5000); // wait 5 seconds for next scan
}
Got Output as expected
Scanning...
I2C device found at address 0x68 !
done
From the above output I hope GPU6050 is working
How can I get values from GPU6050?
Your code is working as expected, it tells you in setup() when it can't connect to the device, until it can.
So when it stops printing the message, it is connected.
Now in loop() you should write your code to negotiate with the device.
Here's an excellent place to start with.

"call ready" response to AT COMMANDS and nothing written to the sim808

I'm using "mega 2650 pro" 5 volt voltage pin output as a power to "sim808 bk-808-v3.1" vcc.
When I tried commands, "call ready" response appear and AT+CSCS="GSM" not written as you can see in the following picture
the following is the code I uploaded to arduino
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
mySerial.begin(9600);
}
void loop() // run over and over
{
if (mySerial.available())
Serial.write(mySerial.read());
if (Serial.available())
{
while(Serial.available())
{
mySerial.write(Serial.read());
}
mySerial.println();
}
}
so what is the problem and how can I solve it?

Lora E45-TTL-100 Basic Arduino setup

I used this code to send data from the Arduino board to LoRa E45-TTL. The board seems to transmit data, but the receiving node doesn't seem to receive data. I am a real beginner to LoRa technology and any help is highly appreciated. The sending and receiving node codes I used are below:
Sender Node
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println("000106,supun");
delay(2000);
}
Receiver Node
void setup() {
Serial.begin(9600);
}
void loop() {
if(Serial.available()) {
char x=Serial.read();
Serial.println(x);
delay(200);
}
}
This post mentions a similar problem, with a hackish solution to close Serial and re-open it.
void loop() {
Serial.print("Test");
Serial.end();
delay(30);
Serial.begin(9600);
delay(70); //The rest of requested delay. So 100 - 30 = 70
}

ESP8266 not detectable

It's my first time trying out the esp8266 on the arduino uno, using the ITEADLIB_Arduino_WeeESP8266 library. However I am not able to get anything done, not even wifi.getversion().
Here's the Serial Monitor output
setup begin
FW Version:
to station err
Join AP failure
setup end
(forever loop)
And here's the code
#include "ESP8266.h"
#include <SoftwareSerial.h>
SoftwareSerial mySerial(3, 2); /* RX:D3, TX:D2 */
ESP8266 wifi(mySerial);
#define SSID "AndroidAP"
#define PASSWORD "12345678"
void setup(void)
{
}
void loop(void)
{
Serial.begin(9600);
Serial.print("setup begin\r\n");
Serial.print("FW Version: ");
Serial.println(wifi.getVersion().c_str());
if (wifi.setOprToStation()) {
Serial.print("to station ok\r\n");
} else {
Serial.print("to station err\r\n");
}
if (wifi.joinAP(SSID, PASSWORD)) {
Serial.print("Join AP success\r\n");
Serial.print("IP: ");
Serial.println(wifi.getLocalIP().c_str());
} else {
Serial.print("Join AP failure\r\n");
}
Serial.print("setup end\r\n");
Serial.println("");
delay(10000);
}
I followed the instruction on the github readme, and the led on the esp8266 is on the whole time, so I guess it is not a wiring issue. Is it possible that the 8266 is dead?
You may try this Serial.begin(115200);

I2C onReceive-handler called only once

I'm having trouble communicating between Arduino's over I2C. For some reason, the onReceive handler is only called once.
Master Code (sender):
#include <Wire.h>
#include "i2csettings.h" // defines address
void setup()
{
Wire.begin(I2C_MASTER_ADDRESS);
}
void loop()
{
Wire.beginTransmission(I2C_SLAVE_ADDRESS);
Wire.write(0x11);
Wire.endTransmission();
delay(1000);
}
Slave Code (receiver):
#include <Wire.h>
#include "i2csettings.h"
void takeAction(int);
void setup()
{
Serial.begin(9600);
Wire.begin(I2C_SLAVE_ADDRESS);
Wire.onReceive(takeAction);
}
void loop()
{}
void takeAction(int nBytes)
{
Serial.println("Action!");
}
The idea in this test-setup is to have the sender send a byte every second, and let the receiver act on this by printing a message. However, the message is only printed once. When I reset the Slave, it's printed again, but just once.
Any ideas where this may come from?
You have to make sure you read all the bytes from the stream.
Other wise it seems to block.
Make your event handler look like this. So you can call it multiple times.
void takeAction(int nBytes)
{
Serial.println("Action!");
while(Wire.available())
{
Wire.read();
}
return;
}

Resources