Can't send data to ESP8266 via arduino - c

I have an ESP8266-01 and I want to send sensor data from Arduino to ESP8266 via serial communication but the data I receive is not correct, I tried changing baud rates but no luck, here is the code
Code for ESP8266:
#include <SoftwareSerial.h>
void setup() {
// put your setup code here, to run once:
Serial.begin(38400);
}
void loop() {
// put your main code here, to run repeatedly:
while(Serial.available()) {
Serial.println("yes");
Serial.println(Serial.read());
}
delay(5);
}
Code for Arduino
#include <SoftwareSerial.h>
SoftwareSerial esp(1, 0);
String str;
void setup(){
Serial.begin(38400);
esp.begin(38400);
delay(2000);
}
void loop() {
// put your main code here, to run repeatedly:
str = String("Hi there");
esp.println(str);
esp.println("hi there");
delay(1000);
}
Here is the serial monitor:
Serial monitor showing numbers instead of "hi there"
The wiring connections are correct.

if you are using simple wires for the connection you might need to reduce the baud rate at 9600 or lower.
For higher speed you will need coaxial cables to get a decent communication distance. Moreover I am sure that you have checked the electrical connection obvously.
On the programming side, Serial.read() returns the first byte of incoming serial data available (or -1 if no data is available). Data type: int. This is why you are getting a single number instead of your string.
I would suggest to use Serial.readString() instead.
Please see the proper documentation here:
https://www.arduino.cc/reference/en/language/functions/communication/serial/
All the best

Related

Serial Communication between 2 Arduino Uno using just Registers

I need to transfer a string between 2 Arduino Uno, using serial communication without functions(just manipulating registers, such as UDR0).
I am able to send a string using
#define BAUD 9600
#define MYUBRR F_CPU/16/BAUD-1
void USART_Init(unsigned int ubrr)
{
UBRR0H=(unsigned char)(ubrr>>8);
UBRR0L=(unsigned char)ubrr;
UCSR0B=(1<<RXEN0)|(1<<TXEN0);
UCSR0C=(1<<USBS0)|(3<<UCSZ00);
}
void USART_Transmit(unsigned char data)
{
while(!(UCSR0A&(1<<UDRE0)));
UDR0=data;
}
void SendString(char *StringPtr)
{
while(*StringPtr !=0x00)
{
USART_Transmit(*StringPtr);
StringPtr++;
}
}
void setup()
{
USART_Init(MYUBRR);
}
void loop()
{
SendString("123456");
delay(1000);
}
but I have no idea how to copy the content of the UDR0 register at the other end. The function should be something like:
unsigned char USART_Receive(void)
{
while(!(UCSR0A&(1<<RXC0)))
return UDR0;
}
But I don't know how to actually use it; I need to save all those chars from the serial to a string, then show the string on a LCD connected to the second Arduino Uno, but every time when I attempt to receive more that one char it's all working very bad(blank spaces, a lot of zeros, invalid characters).
I read that if I have an LCD connected to the second arduino, it's much better to not use the USART_Receive() function, and implement it as an interrupt, but I don't know how to do that either.
Do you have any idea how to transfer the string to the other Arduino?
Perhaps try to put all the chars in an big array and then send that array to your LCD. That way, the time between reading one char and the next one is minimized. (UART is asynchronous, so if you're too slow, you miss the requested data).

Arduino Issue: Using TimerOne and delayMicroseconds()

I have been working with an Arduino and have encountered a very strange problem. Here is the code I am using:
#include <TimerOne.h>
const int LED_PIN = 8;
const int PERIOD = 3000; // micros
void setup()
{
pinMode(LED_PIN, OUTPUT);
Timer1.initialize(PERIOD);
Timer1.attachInterrupt(sendPulse);
Serial.begin(115200);
}
void loop()
{
}
void sendPulse()
{
Serial.println(micros());
delayMicroseconds(x);
Serial.println();
}
So, I have tried changing the value of x in sendPulse(). If x is 300, for example, the Serial monitor outputs "3016 6016 9016...," as expected. However, something strange occurs when x is greater than or equal to 835 -- the Serial monitor outputs "3016 4992 7992...." My question is why is the time between the first and second interrupt not 3000? Furthermore, if I change the code within the interrupt to:
Serial.println(micros());
delayMicroseconds(x);
digitalWrite(LED_PIN, HIGH);
Serial.println();
The code acts strangely for x greater than or equal to 830, rather than 835. Why does this happen? Thank you!
According to this:
you shouldn't use Serial.print()/Serial.read() in an interrupt service routine, because latest version of Serial uses interrupts for read and write but they are disabled from within an ISR.
you shouldn't usedelay()/delayMicroseconds(), because again they make use of interrupts but they are disabled from within an ISR.. your risk making the timer miss some interrupts and loose track of the correct flow of time.
As a remark I repeat here what was said by #Unimportant in the comments: the code within an ISR should be kept as short and fast as possible.

Sharing text through XBee on Arduino

How do I share text through an XBee module?
I tried, but instead of a word, I am getting some numbers every time. I want to exchange text data from both sides. I am using an Arduino for communication. How can I fix this problem?
I've been through a similar problem and was able to work around it. I assume you're using a MEGA 2560 and have a set of XBEE modules connected to two computers through the SPARKFUN XBEE USB explorer.
I've included my code that you can upload to both of the XBEEs and use them as a simple walkie-talkie pair. The program is meant to read an entire incoming word/string terminated with a defined character.
//xbee walkie-talkies
#define EndOfInput '#'//define a terminating character
void setup() {
Serial1.begin(9600); //serial thru pin 19
Serial.begin(9600); //Serial monitor
}
String incomingWord=""; //initialize to NULL
char input; //to read the incoming character
void loop() {
if (Serial.available()>0) {
// send out whatever is typed at the serial
//monitor thru the XBEE
Serial1.write(Serial.read());
}
//read the entire incoming word
while(Serial1.available()>0){
input = Serial1.read();
if (input != EndOfInput) incomingWord+=input;
else break;
}
//print out the word received on the serial monitor
Serial.println(incomingWord);
incomingWord = ""; //reset the string
}
It should be pretty self-explanatory.

Arduino Not Receiving Serial Data When Shield Attached

I have nexus duino 3WD and tried to do serial comm to move the robot. I have to remove shield to upload sketch code. This is my sketch:
#include <fuzzy_table.h>
#include <PID_Beta6.h>
#include <PinChangeInt.h>
#include <PinChangeIntConfig.h>
#include <MotorWheel.h>
#include <Omni3WD.h>
#include <EEPROM.h>
#define _NAMIKI_MOTOR //for Namiki 22CL-103501PG80:1
/*******************************************/
int incoming = 0;
int speed = 100;
// Motors
irqISR(irq1,isr1);
MotorWheel wheel1(9,8,6,7,&irq1); // Pin9:PWM, Pin8:DIR, Pin6:PhaseA, Pin7:PhaseB
irqISR(irq2,isr2);
MotorWheel wheel2(10,11,14,15,&irq2); // Pin10:PWM, Pin11:DIR, Pin14:PhaseA, Pin15:PhaseB
irqISR(irq3,isr3);
MotorWheel wheel3(3,2,4,5,&irq3); // Pin3:PWM, Pin2:DIR, Pin4:PhaseA, Pin5:PhaseB
Omni3WD Omni(&wheel1,&wheel2,&wheel3);
/******************************************/
void setup() {
Serial.begin(38400);
Serial.println("setup");
TCCR1B=TCCR1B&0xf8|0x01; // Pin9,Pin10 PWM 31250Hz
TCCR2B=TCCR2B&0xf8|0x01; // Pin3,Pin11 PWM 31250Hz
Omni.PIDEnable(0.26,0.02,0,10);
}
/****************************************/
void loop() {
if(Serial.available() > 0) {
incoming = Serial.read();
Serial.print(incoming);
if(incoming==0) {
Omni.setCarStop();
} else if(incoming==1) {
Omni.setCarAdvance(speed);
} else if(incoming==2) {
Omni.setCarRotateLeft(speed);
} else if(incoming==3) {
Omni.setCarRotateRight(speed);
}
}
Omni.PIDRegulate();
}
I tried to send data both from Serial Monitor and also PC app with USB cable. When shield is not attached I can send data and receive feedback from arduino. But when shield is attached it seem arduino not receiving data, the RX led flash but not the TX led, got 'setup' text output so arduino can send data to PC. Other thing is I can only use 38400 baud rate, otherwise the output will be garbagish.
I'm not sure if I do wrong in the code? Probably device power issues?
If you change the jumpers from RS-485 to Bluetooth like what it says here http://www.dfrobot.com/wiki/index.php/Arduino_I/O_Expansion_Shield_(SKU:_DFR0014)
then everything will work fine. uploading to the board without removing the shield might still be an issue, and sonars probably will not work, but at least you'll be able to read serial through the board.

Arduino (Uno) Ethernet client connection fails after many client prints

I'm using an Arduino Uno with Ethernet Shield.
After sending many HTTP requests, client.println(...), the client starts to fail when connecting. The time to failure appears to be random, and the sequence readout from the loop can vary anywhere between ~1000 and ~7000.
The error is not to do with the Ethernet Transmit Buffer overflowing (Following this advice)
Here is the code that is failing:
#include <Ethernet.h>
#include <SPI.h>
// Network constants
byte mac[] = {0x00, 0x23, 0xdf, 0x82, 0xd4, 0x01};
byte ip[] = {/*REDACTED*/};
byte server[] = {/*REDACTED*/};
int port = /*REDACTED*/;
Client client(server, port);
// State
int sequence;
void setup(){
Ethernet.begin(mac, ip);
Serial.begin(9600);
sequence = 0;
delay(1000);
}
void loop(){
httpPut("/topic/:test/publish?sessionId=SESenanhygrp");
Serial.println(sequence++);
}
void httpPut(char* url){
if (!client.connect()) {
Serial.println("EXCEPTION: during HTTP PUT. Could not connect");
return;
}
client.print("PUT");
client.print(" ");
client.print(url);
client.println(" HTTP/1.0");
client.println();
while(!client.available()){
delay(1);
}
while(client.available()) {
char c = client.read();
Serial.print(c);
}
while(client.connected()){
Serial.println("Waiting for server to disconnect");
}
client.stop();
}
The error occurs in the following segment
if (!client.connect()) {
Serial.println("EXCEPTION: during HTTP PUT. Could not connect");
return;
}
There is a bug in the Arduino Ethernet library in v22 (as discussed in Linux/Windows V0022/1.0 Ethernet problem SOLVED).
The solution for me was to use the Ethernet2 library (by Peter from tinker.it). The code needed minor tinkering, but everything appears to be working fine now. I've managed to get over 40000+ HTTP messages sent without any problems. (Occasionally single messages cannot be sent, but this error rate is less than 4%.)
I would slow down the communication rate by increasing time 10x between the messages. Then if you don't get an error between 1000 and 7000 messages, it would probably mean that you are talking too fast to your little Arduino and it's buffer gets an overflow which communication library unfortunately can not recover from. I would also monitor Arduino free bytes in a buffer over serial port after each message. You can also test for this behavior by sending messages as fast as you can from PC, and see if that will freeze your Arduino after a while. If it does, you might consider to deny messages until buffer is above some limit.

Resources