I have ATMEGA328 connnected to ESP8266 via UART. All I'm trying to do is simply send a constant value over WIFI to a simple TCP server.
This is the blueprint of UART commands sent by ATMEGA328 (full code at the bottom):
// Connect to WIFI
AT+CWJAP="MYNET","MYPASSWORD"\r\n
/*
* Loop
*
* Open TCP connection
* Send 10 bytes
* Close TCP Connection
*/
AT+CIPSTART="TCP","104.111.111.111",667\r\n
AT+CIPSEND=10\r\n1111122222\r\n
AT+CIPCLOSE\r\n
I tested it against ncat and got the following output:
Ncat: Connection from 111.111.111.111:11672.
1111122222
Ncat: Connection from 111.111.111.111:35756.
2AT+CIPCLO1111122222
Ncat: Connection from 111.111.111.111:8784.
1111122222
Ncat: Connection from 111.111.111.111:63288.
1111122222
Ncat: Connection from 111.111.111.111:28498.
1111122222
Ncat: Connection from 111.111.111.111:33319.
1111122222
Ncat: Connection from 111.111.111.111:35146.
1111122222
Ncat: Connection from 111.111.111.111:34454.
1111122222
Ncat: Connection from 111.111.111.111:33916.
1111122222
Ncat: Connection from 111.111.111.111:37095.
1111122222
Ncat: Connection from 111.111.111.111:12652.
1111122222
Ncat: Connection from 111.111.111.111:61951.
1111122222
Ncat: Connection from 111.111.111.111:27051.
1111122222
Ncat: Connection from 111.111.111.111:61870.
111122222A1111122222
Ncat: Connection from 111.111.111.111:38063.
1111122222
Ncat: Connection from 111.111.111.111:32270.
Please note the strange values at times, seems like data gets mixed up, even AT commands with the actual 'data' inside the command:
111122222A1111122222
2AT+CIPCLO1111122222
Now I can't seem to understand if it's baud related or misuse of AT commands or both..
I think it could be baud related, because:
1. The data I receive from ESP8266 is for the most part 0x00, so I can't read and process the responses.
2. Not totally sure if this is correct: UBRR0 = ((( F_CPU / (BAUD * 16UL ))) - 1);
In the original example, it had 'BAUD * 8', but it didn't work for me, when changing it to 16, esp8266 started to understand commands.
But then again it seems like the esp8266 connects 100% of the time to the WIFI (I have disabled auto-connect on esp8266, it will only connect on AT command). The wifi command is fairly long and I would think if it's a baud issue, it would fail the WIFI connection at some point as well, but it seems to always connect.
I tried also to send 1 byte at a time on the same connection using the same command, but after about +-10 bytes (inconsistent size) the esp8266 closes the TCP connection therefor I changed it so it sends 10 bytes per connection, but it still keeps failing. Any help is much appreciated.
Full source code (as I mentioned earlier for some reason incoming data is for the most part 0x00, therefor I cannot process the commands for ESP8266 to determinate when it's ready, therefor I simply used long delays to make sure it has processed the last command before continuing with the next one):
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#define F_CPU 8000000UL
#define BAUD 9600
char buffer[ 70 ];
int received = 0;
void delayms( uint16_t millis )
{
while ( millis )
{
_delay_ms( 1 );
millis--;
}
}
void blink_red()
{
PORTB = 0b00000010;
_delay_ms(10);
PORTB = 0b00000000;
_delay_ms(10);
}
void command_send( char *buf, int len )
{
int i = 0;
for( i=0; i<len; i++ )
{
blink_red();
UDR0 = buf[i];
}
}
int main( void )
{
// Init serial
DDRD = 0b01101000;
DDRB = 0xFF;
UBRR0 = ((( F_CPU / (BAUD * 16UL ))) - 1);
UCSR0B = _BV( RXEN0 ) | _BV( TXEN0 );
UCSR0C = 0b00000110;
UCSR0B |= ( 1 << RXCIE0 );
sei();
command_send( "AT+CWJAP=\"XXXXXXX\",\"XXXXXXXXXX\"\r\n", 32 );
_delay_ms( 15000 );
while( 1 )
{
command_send( "AT+CIPSTART=\"TCP\",\"111.111.111.111\",667\r\n", 41 );
char data[] = { "AT+CIPSEND=10\r\n1111122222" };
_delay_ms( 30 * 1000 );
command_send( data , 25 );
_delay_ms( 30 * 1000 );
command_send( "AT+CIPCLOSE\r\n", 13 );
_delay_ms( 30 * 1000 );
}
return 0;
}
ISR( USART_RX_vect )
{
char ReceivedByte;
ReceivedByte = UDR0;
buffer[ received ] = ReceivedByte;
received++;
if( received >= 70 )
received = 0;
}
The Makefile:
CC=/usr/bin/avr-gcc
MEGA=328p
CFLAGS=-g -Os -Wall -mcall-prologues -mmcu=atmega$(MEGA)
OBJ2HEX=/usr/bin/avr-objcopy
PROG=/usr/bin/avrdude
TARGET=esp8266
DEVICE=/dev/ttyACM0
program : $(TARGET).hex
$(PROG) -c avrispv2 -p m$(MEGA) -P $(DEVICE) -e -B 2.0 -U lfuse:w:0xe2:m -U hfuse:w:0xd9:m
$(PROG) -c avrispv2 -p m$(MEGA) -P $(DEVICE) -U flash:w:$(TARGET).hex
%.obj : %.o
$(CC) $(CFLAGS) $< -o $@
%.hex : %.obj
$(OBJ2HEX) -R .eeprom -O ihex $< $@
clean :
rm -f *.hex *.obj *.o