Connect with us

esp8266 with avr

Discussion in 'Microcontrollers, Programming and IoT' started by Areeha Durrani, Feb 16, 2016.

  1. Areeha Durrani

    Areeha Durrani

    45
    1
    Jul 8, 2015
    hello! I want to work on esp8266 on avr studio but i dont know what to start with! I wrote a code on avr studio for esp8266 but it gives errors.As far as my study says there is some USART initialization etc stuff required.need help..from where to start and what to do!!My code goes like;

    * //GccApplication11.c
    *
    * //Created: 2/16/2016 4:16:02 PM
    * //Author : areeha

    #include <avr/io.h>
    #include <util/delay.h>
    #include <esp8266.h>

    // define some macros
    #define BAUD 9600 // define baud
    #define BAUDRATE ((F_CPU)/(BAUD*16UL)-1) // set baud rate value for UBRR

    // function to initialize UART
    void uart_init (void)
    {
    UBRRH = (BAUDRATE>>8); // shift the register right by 8 bits
    UBRRL = BAUDRATE; // set baud rate
    UCSRB|= (1<<TXEN)|(1<<RXEN); // enable receiver and transmitter
    UCSRC|= (1<<URSEL)|(1<<UCSZ0)|(1<<UCSZ1); // 8bit data format
    }

    void io_init(void)
    {
    DDRB |= (1 << DDB0); // LED output
    }

    int main(void)
    {
    io_init();
    _delay_ms(2000); // wait a bit for esp8266 to initialize
    esp8266_init();
    _delay_ms(5000);
    esp8266_write_str("ATE0\r\n"); // turn off echo
    _delay_ms(500);
    esp8266_write_str("AT+CIPMUX=1\r\n"); // accept multiple connections
    _delay_ms(500);
    esp8266_write_str("AT+CIPSERVER=1,8000\r\n"); // create server on port 8000

    uint8_t i = 0;

    _delay_ms(1000);

    while(1) {
    esp8266_process(); // call this to parse uart esp8266 data from uart

    /* check parse status */
    switch (esp8266_status()) {
    case CLIENT_CONNECTED:
    PORTB |= (1 << PB0);
    break;

    case CLIENT_DISCONNECTED:
    PORTB &= ~(1 << PB0);
    break;

    case CLIENT_DATA_READY: {
    /* case statement brackets are required to declare new variable
    inside of a case statement (initialization will not be
    skipped) */
    char *msg = esp8266_read_msg();

    for (i = 0; i < msg[0] - '0'; i++) {
    PORTB |= (1 << PB0);
    _delay_ms(250);
    PORTB &= ~(1 << PB0);
    _delay_ms(250);
    }

    esp8266_write_str("AT+CIPSEND=0,9\r\n");
    _delay_ms(50);
    esp8266_write_str("Hello ");
    esp8266_write_char(msg[0]);
    esp8266_write_str("\r\n");

    break;
    }

    default:
    break;
    }

    _delay_ms(100);
    }

    esp8266_write_str("AT+CIPCLOSE=0\r\n");
    esp8266_write_str("AT+CIPSERVER=0\r\n");

    return 0;
    }
     
  2. Amar Dhore

    Amar Dhore

    129
    12
    Dec 2, 2015
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-