Showing posts with label ESP201. Show all posts
Showing posts with label ESP201. Show all posts

Monday, November 14, 2016

ESP8266-ESP201 Step-by-step Guide




This is a step-by-step guide to ESP8266-ESP201.

1.       Buy ESP8266-ESP201 with its hardware development kit from eBay, click here:



The development board is shown in photo below as well. You have a relay, buzzer, switches, LEDs, etc. to play with and test your code and to learn input/output functions of ESP8266:



2.       Connect the USB cable between your computer and the development board.
3.       Push the power button to check if power led is working and the board shows sign of life.
4.       Complete schematic of the development board is as follows:



5.       On the PCB near the DIP switch these are the values:
·         R - should be the RED led from the RGB connected to the IO15 pin. Putting the R DIP to ON will light RED if the GPIO15 is HIGH.
·         G - GREEN led from RGB led is connected to IO13. Putting the G DIP to ON will light GREEN if the GPIO13 is HIGH.
·         B - BLUE led from RGB led is connected to IO12. Putting the B DIP to ON will light BLUE if the GPIO12 is HIGH.
·         W - WHITE led located almost under RGB led is connected to the IO14 pin. Putting HIGH on GPIO14 and DIP switch ON will light the WHITE led.
·         J - relay pin connected to IO16 (which is always HIGH - I have no idea why. This behavior and the RED led that is not working can be because I have a bad board or the design for all the boards is wrong. Connecting the IO16 to GND (not recommended) will stop the relay, otherwise it will be ON all the time the J DIP pin is ON.
·         B - BUZZER connected to IO5 pin through B DIP switch. Having GPIO5 HIGH and B on DIP switch ON will prove that the buzzer is working.
·         K1 - I assume is connected to S2.
·         K2 - I assume is connected to S3.



6.       A schematic on a napkin will look like this one:



7.       ESP8266 comes with pre-installed AT commands, so you can check these to see if you get any response. The list of AT commands can be found here:



To test the AT commands, you need to have a program like “Hyper Terminal”. The easiest and recommended way is to install Arduino from here which has a built-in serial monitor through which you can send and receive data.



Once you have downloaded and installed Arduino, you can now run it and it will look like this:



Now click File menu and then select Preferences. It will look like this:



Now add the link below in the “Additional Boards Manager URLs” because ESP8266 board is not included in Arduino by default.


Click OK button.

Now click Tools menu and select Board. Our board in this case will Generic ESP8266 Module. Your screen should look like this:



Now click Tools menu again and select Port from the list of available ports. Your screen should look like this:



Now if you have set everything corrected (i.e. (a) USB connection between ESP8266 development board and computer, (b) Settings in Arduino software) then you are ready to type AT and send AT commands to your ESP8266 and be excited to see “OK” response from your chip. When you send AT to your ESP8266, it will respond you with OK.

Click Tools menu and select Serial Monitor or press on your keyboard Ctrl+Shift+M to open serial monitor where you can send and receive AT commands from serial interface. Make sure you select (a) “both NL and CR” for both new line and carriage return, (b) correct baud rate (e.g. 115200 baud). Your screen should look like this when you ready to send AT commands:




8.       If you have been successful so far, you can be sure that your ESP8266 and its development board and communication between your computer and development board – all is working fine!
9.       Now you want to know how to control GPIO pins by yourself. For that, click File menu, select Examples, select ESP8266WiFi, and then select WiFi WebServer. Your screen should look like this:

                


A new window with WiFi webserver example will open (as shown above in left screenshot). Now edit this file by typing in your WiFi SSID and password. Save the example with another filename and then press upload button to start code compilation and upload to the ESP8266. This will fail. To be successful you need to perform a few more steps.

10.   Turn the board off; Ground the IO0 pin; Set K2 to Ground (ON); Power on the board; and Set K2 to OFF.
11.   Now press Ctrl+Shift+M to open Serial Monitor and then click upload button again, if it fails or shows error close Arduino and open Arduino again and press upload button. It will upload successfully – at least it did it for me! J
12.   Now you will see in Serial Monitor window that your ESP8266 is now connected to your WiFi network and shows an IP address from where you can control IO2 to be 1 or 0.
13.   Connect LED to IO2 and Ground. Go to the given IP address in Serial Monitor (say, 192.168.0.140) and turn on/off LED like this:

14.    The pinouts of ESP8266-ESP201 look like this:


Helpful resources:

·         http://www.esp8266.com/


s

Guest post by: Ahmed Hussain

Saturday, August 27, 2016

A good book about ESP8266

One thing that I did in the last two months was to collaborate with Packt Publishing to be technical supervisor for a book about the ESP8266.

Book is written by Marco Schwartz and can be purchased from Amazon.

The summary of the book is in the following pictures.









Wednesday, January 27, 2016

E-ink display Part II

Now you can see the E-ink display in action.

It is connected to the ESP201 development board which also have an DS18B20 temperature sensor attached.

I've managed to reduce the pin numbers so now I can have extra pins to connect more sensors and show their values on this tiny e-ink display.




Wednesday, August 12, 2015

Free book for beginers

Neil Kolban did a very good job to put all informations in one book. Reading the book is a pleasure and can introduce you in the ESP8266 world.

I would say that is mandatory for beginners to read this book.

You can download the book.

If you like it, don't forget to send an email to author.

Saturday, May 2, 2015

WiFi status LED

If you want to know if your ESP8266 is connected to your WiFi router, solution is to add a simple led to an not used GPIO ( can use also GPIO 0 for ESP-01) and call a function.

//install status wifi LED on GPIO0
#define WIFI_LED_IO_MUX     PERIPHS_IO_MUX_GPIO0_U
#define WIFI_LED_IO_NUM     0
#define WIFI_LED_IO_FUNC    FUNC_GPIO0



and call wifi status led function at the beginning of your user_init() function BEFORE any 
WIFI action to your router.

wifi_status_led_install(WIFI_LED_IO_NUM, WIFI_LED_IO_MUX, FUNC_GPIO0); 


Connected LED will  blink when trying to connect or having an error, it will switch off when everything is fine.


A short video with wifi status led:







Tuesday, April 7, 2015

Post man #4

To extend the range ( which seems to be fine for my project at this time) I've ordered some 
19 cm mini PCI U.FL to RP-SMA WiFi Antenna Cable. My plan was to reuse the antenna from NRF240L01P modules but I didn't checked the connectors. 


SMA U.FL wifi antenna cable and antenna from NRF24L01



Murphy's law is applicable also in this case, so as you can guess they don't match. So the new antennas are on their way also.


I've received also the BMP180 sensors ( replace the old BMP085 version). 
  • Pressure sensing range: 300-1100 hPa (9000m to -500m above sea level)
  • Up to 0.03hPa / 0.25m resolution
  • -40 to +85°C operational range, +-2°C temperature accuracy
  • This board/chip uses I2C 7-bit address 0x77


BMP180 sensor bag

The sensor and its board is very small, which is great. 

So I have temperature and humidity from DHT22, pressure from BMP180, I can now predict the weather for my irrigation platform. 

Tuesday, March 24, 2015

Quick Notes #1

In the latest SKD 1.0.0 I've seen an interesting function:

system_update_cpu_freq

Function:  Set CPU frequency. Default is 80MHz.

Prototype:

bool system_update_cpu_freq(uint8 freq)

Parameter:

uint8 freq : CPU frequency
#define SYS_CPU_80MHz     80
#define SYS_CPU_160MHz 160

Return:
true:  succeed

false: fail

which is nice, you can double the speed.

Sunday, March 22, 2015

Attention on mounting DHT11 sensor

If you have this development board and you want to mount the DHT11 temperature and humidity sensor that came with the board, you should NOT mount it like in the picture you will find on ebay or other sites. It is wrong and you have the chance to burn some parts of the board.

Again the sensor must mount with the holes/grilles facing the ESP201 board NOT like in the picture.


WRONG WAY TO MOUNT DHT11 SENSOR


THE CORRECT WAY IS LIKE IN THIS PICTURE.

CORRECT WAY TO MOUNT DHT11 SENSOR.

Be careful, search or ask first if you don't know.

Friday, March 20, 2015

Publish data from your ESP8266 to thingspeak.com

On this posts ( post#1, post#2) I've presented how to create a channel, fields, API KEYS on thingspeak.com

Now will publish some data from ESP8266 to thingspeak.com. Can be any kind of data, from
temperature, humidity, voltage you name it. As long as it has a value, can be published.

Add following  lines after the include statements. (Can use the blinky code as skeleton).

#include "espconn.h" 
LOCAL struct espconn *pCon = NULL;
LOCAL my_count = 0;
LOCAL your_value = 33;




Add the following code in the user_init() function.

const char ssid[32] = "my_home_ssid";
const char password[32] = "my_home_password";

struct station_config stationConf;

wifi_set_opmode( STATION_MODE );
os_memcpy(&stationConf.ssid, ssid, 32);
os_memcpy(&stationConf.password, password, 32);
wifi_station_set_config(&stationConf); 
wifi_station_connect();
//connect to the previous pCon created structure
int ret = 0;
ret = espconn_connect(pCon);

if(ret == 0) 
      INFO("espconn_connect OK!\r\n");
else
{
      //INFO("espconn_connect FAILED!\r\n");  
      char *fail;
      os_sprintf(fail, "%d \r\n", ret);
      //INFO(fail);
      //clean up allocated memory
      if(pCon->proto.tcp)
          os_free(pCon->proto.tcp);
      os_free(pCon);
      pCon = NULL;
} 

Add this code in a timer function like in this example:

pCon = (struct espconn *)os_zalloc(sizeof(struct espconn));
if (pCon == NULL)
{
        os_printf("pCon ALLOCATION FAIL\r\n");
        return;
}

pCon->type = ESPCONN_TCP;
pCon->state = ESPCONN_NONE;
    
pCon->proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp));
pCon->proto.tcp->local_port = espconn_port();
//set up the server remote port
pCon->proto.tcp->remote_port = 80;

//set up the remote IP
uint32_t ip = ipaddr_addr("184.106.153.149"); //IP address for thingspeak.com
os_memcpy(pCon->proto.tcp->remote_ip, &ip, 4);

//set up the local IP
struct ip_info ipconfig;
wifi_get_ip_info(STATION_IF, &ipconfig);
os_memcpy(pCon->proto.tcp->local_ip, &ipconfig.ip, 4);

//register publish_thingspeak_connect_cb that will be called when the
//connection with the thingspeak is done. In this call back function
// will actualy do the data sending
espconn_regist_connectcb(pCon, publish_thingspeak_connect_cb);



where the callback function is:


static void ICACHE_FLASH_ATTR publish_thingspeak_connect_cb(void *arg)
{
  //INFO("========>publish_thingspeak_connect_cb\r\n");
  my_count++;

  struct espconn *pespconn = (struct espconn *)arg;

  char payload[128];

  char field1[10];
  char field2[10];

  os_sprintf(field1, "%d", your_value);
  os_sprintf(field2, "%d", my_count);

  os_sprintf(payload, "GET /update?  api_key=YOUR_THINGSPEAK_API_KEY&field1=%s&field2=%s\r\n", field1, field2);

  espconn_sent(pespconn, payload, strlen(payload));
  
}

and need to be added before the user_main function.

Change my_count and your_value with your data. 

Notes:

1. The time between to posts on thingspeak.com must be greater then 15 seconds.
2. Up to 8 fields can be used to post data on thingspeak.com





Thursday, March 19, 2015

Example: Blink a LED

Like in the Arduino case one of the first thinks to try it is to blink an LED.

The following link contains the code for blink an LED.

Materials:

  • ESP-01 - or other version 
  • LED 
  • SERIAL to USB

If you have my setup described here 

1. put the the archive into this directory

/opt/Espressif/ESP8266_SDK   

2. untar the file

tar -xvf blinky.tar

3. enter in the app directory

cd blinky

4. compile the code

make clean ; make.

5. Upload the resulting files ( from the firmware directory) to your ESP after this steps

5.1 Unplug the VCC power pin
5.2 Connect the GPIO0 pin to ground
5.3.Use the command to write the firmware

./burnESP


Anatomy of the code

 The main application file is the user_main.c file located in 
/opt/Espressif/ESP8266_SDK/blinky/user

The file content is:


#include "ets_sys.h"
#include "osapi.h"
#include "gpio.h"
#include "os_type.h"
#include "user_config.h"

#define user_procTaskPrio        0
#define user_procTaskQueueLen    1
os_event_t    user_procTaskQueue[user_procTaskQueueLen];
static void user_procTask(os_event_t *events);

static volatile os_timer_t some_timer;


void some_timerfunc(void *arg)
{
    //Do blinky stuff
    if (GPIO_REG_READ(GPIO_OUT_ADDRESS) & BIT2)
    {
        //Set GPIO2 to LOW - TURN OFF the LED
        gpio_output_set(0, BIT2, BIT2, 0);
    }
    else
    {
        //Set GPIO2 to HIGH - TURN ON the LED
        gpio_output_set(BIT2, 0, BIT2, 0);
    }
}

//Do nothing function
static void ICACHE_FLASH_ATTR  user_procTask(os_event_t *events)
{
    os_delay_us(10);
}

//Init function 
void ICACHE_FLASH_ATTR user_init()
{
    // Initialize the GPIO subsystem.
    gpio_init();

    //Set GPIO2 to output mode
    PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_GPIO2);

    //Set GPIO2 low
    gpio_output_set(0, BIT2, BIT2, 0);

    //Disarm timer
    os_timer_disarm(&some_timer);

    //Setup timer
    os_timer_setfn(&some_timer, (os_timer_func_t *)some_timerfunc, NULL);

    //Arm the timer, &some_timer is the pointer 1000 is the fire time in ms
    //0 for once and 1 for repeating timer
    os_timer_arm(&some_timer, 1000, 1);
    
    //Start os task
    system_os_task(user_procTask, user_procTaskPrio,user_procTaskQueue, user_procTaskQueueLen);
}


and is pretty much self explanatory.

1. Include the necessary headers. See that user_config.h is empty so if you can use this app as a skeleton for your future applications. 

2. In user_init() function the GPIO is initialized, GPIO2 is set up as an output pin and is set
to 0 (LOW, GND).

3. A timer is defined (some_timer) and initialized and the  callback function is set to be called every 1000ms (1sec) interval. 

4. Callback function some_timerfunc is doing all the stuff, if the LED is ON turn it to OFF,
otherwise turn the LED ON.


If you want to add extra functionality check this post for how to have arduino like functions
to manipulate your GPIO. 

Happy codding !!! 

Thursday, March 12, 2015

Arduino function for ESP8266

        If programming the GPIO is hard for you as a beginner but you are familiar with Arduino functions, the following functions can be used as have the same name as in the Arduino IDE.

First define some data in an .h file.

#define HIGH 0x1
#define LOW 0x0
#define INPUT 0x1
#define OUTPUT 0x0
                                                   /*   GPIO | SDK board    |       */
                                                   /*   --------------------|       */
int gpio_pin_register[16] = {PERIPHS_IO_MUX_GPIO0_U,    // 0  gpio 0        | 
                             PERIPHS_IO_MUX_U0TXD_U,    // 1  gpio 1        | 
                             PERIPHS_IO_MUX_GPIO2_U,    // 2  gpio 2        | 
                             PERIPHS_IO_MUX_U0RXD_U,    // 3                |
                             PERIPHS_IO_MUX_GPIO4_U,    // 4  gpio 4        | 
                             PERIPHS_IO_MUX_GPIO5_U,    // 5  gpio 5 buzzer | 
                             PERIPHS_IO_MUX_SD_CLK_U,   // 6                |
                             PERIPHS_IO_MUX_SD_DATA0_U, // 7                |
                             PERIPHS_IO_MUX_SD_DATA1_U, // 8                |
                             PERIPHS_IO_MUX_SD_DATA2_U, // 9  gpio 9        | 
                             PERIPHS_IO_MUX_SD_DATA3_U, // 10 gpio 10       | 
                             PERIPHS_IO_MUX_SD_CMD_U,   // 11               |
                             PERIPHS_IO_MUX_MTDI_U,     // 12 gpio12  blue  | 
                             PERIPHS_IO_MUX_MTCK_U,     // 13 gpio13  green | 
                             PERIPHS_IO_MUX_MTMS_U,     // 14 gpio14  white | 
                             PERIPHS_IO_MUX_MTDO_U};    // 15 gpio15  red   | 
                             

#define GPIO_PIN_ADDR(i)    (GPIO_PIN0_ADDRESS + i*4)


Now the Arduino like functions can be added:

/********************************************************************
* FunctionName: pinMode
* Description : set up a pin mode.
* Parameters  : pin: pin mumber
*             : mode 0 for OUTPUT or 1 for INPUT
*             : pullup 0 for pullup DISABLED and 1 for pullup ENABLED
*             : pulldown not yet supported
* Returns     : None                                                     
*********************************************************************/

void ICACHE_FLASH_ATTR pinMode(uint8_t pin, uint8_t mode, uint8_t pullup) {
    if ((0x1 << pin) & 0b110101) {
        PIN_FUNC_SELECT(gpio_pin_register[pin], 0); //0,2,4,5
    } else {
        PIN_FUNC_SELECT(gpio_pin_register[pin], 3);
    }
    PIN_PULLDWN_DIS(gpio_pin_register[pin]);
    if(pullup)
      PIN_PULLUP_EN(gpio_pin_register[pin]);
    else  
      PIN_PULLUP_DIS(gpio_pin_register[pin]);
    if (mode) {
        GPIO_REG_WRITE(GPIO_ENABLE_W1TC_ADDRESS, 1<<pin); // GPIO input
    } else {
        GPIO_REG_WRITE(GPIO_ENABLE_W1TS_ADDRESS, 1<<pin); // GPIO output
    }
}


Example:
pinMode(0, OUTPUT, 0); //is setting the GPIO 0 as an OUTPUT with pullup disabled

If you want to control also the pulldown add an extra parameter and call the 
PIN_PULLDWN_DIS(gpio_pin_register[pin])for disable and PIN_PULLDWN_EN(gpio_pin_register[pin])for pulldown enable.


Next function are the well known Arduino function for read and write:

/********************************************************************
* FunctionName: digitalWrite
* Description : use to set up HIGH or LOW on a GPIO pin
* Parameters  : pin: pin mumber
*             : state HIGH or LOW
* Returns     : None                                                     
*********************************************************************/

void ICACHE_FLASH_ATTR 
digitalWrite(uint8_t pin, uint8_t state) {
    if (state) {
        GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, 1<<pin); // set GPIO pin high
    } else {
        GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, 1<<pin); // set GPIO pin low
    }
}
/********************************************************************
* FunctionName: digitalRead
* Description : use to set up HIGH or LOW on a GPIO pin
* Parameters  : pin: pin mumber
* Returns     : 1 if pin is HIGH or 0 if pin is LOW                                                     
*********************************************************************/

int ICACHE_FLASH_ATTR 
digitalRead(uint8_t pin) {
    return (GPIO_REG_READ(GPIO_OUT_ADDRESS)>>pin) & 1;
}



Example:

digitalWrite(0, HIGH); //set HIGH the GPIO 0 pin

int gpio_state = digitalRead(0); will return 1 if the GPIO 0 pin's is HIGH or 0 if is LOW

If you save your .h file in the include directory, just use #include "your_file_name.h" in your user_main.c.


For controlling GPIO16 see this post.