i recently bought an ESP-01 for a simple wi-fi project reading the temeprature from a website .
First i wired the ESP-01 directly on the TTL converter (cp2102) to read the temperature and everything worked just fine at 9600bps with the AT commands etc.
Then i created a program with winAVR for the mega32 to do the same thing using uart at 9600 bps (UBRRL = 0x68; ( or 104) for Fosc=16MHz) but it didn't work.
So i used a logic analyzer to check what went wrong with the characters i sent .
The result was very strange since some of the characters (of the AT commands sent) going from mega32 (Tx) to ESP-01 (Rx)are not transmitted correctly .
I tried to decrease the baudrate of the ESP-01 down to 4800 but the lowest baud is 9600. I don't know if some firmware version has the option for lower baudrate.
For some reason mega32 cannot accurately transmit all the characters and I cannot understand why this happens. Maybe it has something to do with crystal but when i changed it the same thing happened. I put some delay before the faulty characters which fixed a few but not all of them ( 1 or 2 out of 10+) (bad method, i know).
Also my i have connected my Tx pin (mega 32) to a voltage divider to convert it to 3.3v for the Rx pin of the ESP-01.
After lots of struggling i decided to ask for some help.
I attached my code and a snapshot from the logic analyzer for the AT command AT+CIPSTART=0,"TCP","api.thingspeak.com",80 (the corrupted one). The other two AT commands i use in the code before CIPSTART are transmitted almost... fine (the '\r' or '\n' are not transmitted correctly but somehow the ESP-01 accepts the commands).
The full code can be seen below
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include "LCD4BIT.h"
#include <stdlib.h>
#include <stdio.h>
unsigned char interface_var = 0;
unsigned char counter[34];
unsigned char var = 0;
void usart_init(void)
{
UCSRB |= (1<<TXEN) | (1<<RXEN) | (1<<RXCIE) ; // enable receive transmit of usart
UCSRC = (1<<UCSZ1) | (1<<UCSZ0) | (1<<URSEL);
UBRRL = 0x68; // baudrate = 9600 , Fosc=16MHz, UBRR value = 104 (0x68)
}
void usart_send( unsigned char ascii)
{
while(!(UCSRA & (1<<UDRE)));
UDR = ascii;
}
unsigned char usart_receive(void)
{
while (!(UCSRA & (1<<RXC)));
return UDR;
}
void send_AT( unsigned char message[])
{
unsigned char i=0;
while(message[i] != '\0')
{
usart_send(message[i]); // This sends data to esp-01
i++;
}
int main(void)
{
//OSCCAL = 0xA9;
unsigned char AT[] = "AT\r\n";
unsigned char CIPMUX[] = "AT+CIPMUX=1\r\n";
unsigned char CIPSTART[] = "AT+CIPSTART=0,\"TCP\",\"api.thingspeak.com\",80\r\n";
unsigned char CIPSEND[] = "AT+CIPSEND=0,110\r\n";
unsigned char GET_DATA[] = "GET https://api.thingspeak.com/apps/thinghttp/send_request?api_key=xxxxxxxxxxxxxxxx\r\n";
unsigned char SEND_DATA[] = "GET https://api.thingspeak.com/update?api_key=xxxxxxxxxxxxxxxxxxxxxxx=50\r\n";
//_delay_ms(200);
LCD_init(); // intitialize LCD
usart_init(); // initialize usart
_delay_ms(500);
sei();
_delay_ms(200);
send_AT(AT);
_delay_ms(2000);
send_AT(CIPMUX);
_delay_ms(2000);
send_AT(CIPSTART);
while(1)
{
}//while(1) close
}// main close
ISR(USART_RXC_vect) // Every time an AT command is sent correctly type OK on the LCD (Logic analyzer works better though :-p)
{
unsigned char a = 0;
//LCD_data(a);
a = UDR;
if(a == 'O')
{LCD_data('O');}
if(a == 'K')
{LCD_data('K');}
if(a == 'd')
{LCD_data('d');}
if(a == 'L')
{LCD_data('L');}
}