Showing posts with label SDK. Show all posts
Showing posts with label SDK. Show all posts

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.

New SDK version ( 1.0 )

Espressif the producer of the ESP8266EX chip has announces availability of a new SDK.

If you want to download the latest SDK click here.

So, current version now is 1.0.

Fix bugs:
1     Interrupt during flash erasing will cause wdt reset;
2     Read/write rtc memory;
3     If router disconnect to ESP8266, ESP8266 won’t reconnect;
4     Connect to router which has its SSID

Add APIs:
1 system_update_cpu_freq: change CPU frequency;
2 wifi_promiscuous_set_mac: set a mac address filter during sniffer;
3 wifi_set_broadcast_if : set which interface will UDP broadcast send from;

Optimization:
1 Optimize smartconfig to version v1.0;Please don't call any other APIs during SmartConfig.
2 Optimize AT to version 0.22.0.0;
3 Optimize the protection of system parameters , and add error-check about it;
4 Optimize beacon delay of ESP8266 softAP;
5 Optimize boot to version 1.3(b3);
   5.1 Add API system_restart_enhance: for factory test, support to load and run program in any specific address;
   5.2 Add APIs to get boot version and start address of current user bin;
   5.3 Fix compatibility problem of dual flash;
6 Optimize sniffer, structure sniffer_buf changed, please refer to document;
7 Optimize espconn;
8 Optimize pwm;
9 Other optimize to make the software more reliable;


Espressif will give  a minimum of 200 USD for any developer reporting a previously unknown bug in latest Version 1.0. For more info on bug bounty program click here.

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 !!! 

Tuesday, March 10, 2015

ESP201 dev board - One more finding #2.



Perfect ! The last piece of puzzle was found. How to control the relay on the ESP201 development board.

The on board relay is connected to DIP J, and to GPIO16. In order to trigger GPIO16 you need the function from gpio16.c from IoT Demo.


#include "ets_sys.h"#include "osapi.h"#include "driver/gpio16.h"
void ICACHE_FLASH_ATTRgpio16_output_conf(void){    WRITE_PERI_REG(PAD_XPD_DCDC_CONF,                   (READ_PERI_REG(PAD_XPD_DCDC_CONF) & 0xffffffbc) | (uint32)0x1); // mux configuration for XPD_DCDC to output rtc_gpio0
    WRITE_PERI_REG(RTC_GPIO_CONF,                   (READ_PERI_REG(RTC_GPIO_CONF) & (uint32)0xfffffffe) | (uint32)0x0); //mux configuration for out enable
    WRITE_PERI_REG(RTC_GPIO_ENABLE,                   (READ_PERI_REG(RTC_GPIO_ENABLE) & (uint32)0xfffffffe) | (uint32)0x1); //out enable}
void ICACHE_FLASH_ATTRgpio16_output_set(uint8 value){    WRITE_PERI_REG(RTC_GPIO_OUT,                   (READ_PERI_REG(RTC_GPIO_OUT) & (uint32)0xfffffffe) | (uint32)(value & 1));}
void ICACHE_FLASH_ATTRgpio16_input_conf(void){    WRITE_PERI_REG(PAD_XPD_DCDC_CONF,                   (READ_PERI_REG(PAD_XPD_DCDC_CONF) & 0xffffffbc) | (uint32)0x1); // mux configuration for XPD_DCDC and rtc_gpio0 connection
    WRITE_PERI_REG(RTC_GPIO_CONF,                   (READ_PERI_REG(RTC_GPIO_CONF) & (uint32)0xfffffffe) | (uint32)0x0); //mux configuration for out enable
    WRITE_PERI_REG(RTC_GPIO_ENABLE,                   READ_PERI_REG(RTC_GPIO_ENABLE) & (uint32)0xfffffffe); //out disable}
uint8 ICACHE_FLASH_ATTRgpio16_input_get(void){    return (uint8)(READ_PERI_REG(RTC_GPIO_IN_DATA) & 1);}

and the h file.

#ifndef __GPIO16_H__#define __GPIO16_H__
void gpio16_output_conf(void);void gpio16_output_set(uint8 value);void gpio16_input_conf(void);uint8 gpio16_input_get(void);
#endif


Now, during the init gpio part I can call the gpio16_output_conf to setup the GPIO16 as an output GPIO type.

Programmatic now I can call gpio16_output_set (0) or gpio16_output_set(1) to trigger the onboard realy OFF or ON. 

Again, this is the board I am using for testing and development.



Sunday, March 8, 2015

Problems reading VCC



In the SDK there is a hidden function called readvdd33() that returns the internal measured VDD.


000004f8 T readvdd33

On my setup board I am not able to find a correct value since the function always returns 4096. Need to test it on the ESP-01 to see if make any difference.


Update: Other users have reported this function as a working one, maybe their setup was running on batteries ?

Will be nice to have this functions like it is on Arduino ! 

ESP201 dev board - One more finding #1.


Good news today!

I have managed to find why the GPIO15 connected to the RED RGB LED was not working !

The answer was under my nose. DIP pin K1 (S2) must be on the OFF position. See the picture:



Cool !!!! Now I can fully test PWM on this board.





Remaining unknown items: RELAY (J) connected to GPIO16.  SOLVED !!! See link below.

UPDATE: triggering the relay is described here

Thursday, March 5, 2015

Making your own environment for ESP8266.

If you don't want to use my setup described here, you can make your own setup using your own distribution.

You can make your own environment for compiling your code or the demo code for the ESP8266.

Step 1.

Download ova image  ( lunbuntu 14.04) for VirtualBox or install your preferred Linux distribution.

User is esp8266 and password is  espressif

sudo apt-get install git autoconf build-essential gperf bison flex texinfo libtool libncurses5-dev 

wget gawk libc6-dev python-serial libexpat1-dev

Step 2.

sudo mkdir /opt/Espressif

Step 3.

sudo chown esp8266 /opt/Espressif/

Step 4.

cd /opt/Espressif

Step 5.

git clone -b lx106 git://github.com/jcmvbkbc/crosstool-NG.git

Step 6.

cd crosstool-NG/

./bootstrap && ./configure --prefix=`pwd` && make && make install

Step 7.

./ct-ng xtensa-lx106-elf

Step 8.

./ct-ng build

Note: It takes about 25 minutes !!!!!!!

Step 9.

Edit the .profile file from /home/esp8266 and add the line

PATH="$HOME/bin:/opt/Espressif/crosstool-NG/builds/xtensa-lx106-elf/bin:$PATH"

Step 10.

cd /opt/Espressif

wget -O esp_iot_sdk_v0.9.5_15_01_23.zip https://github.com/esp8266/esp8266-

wiki/raw/master/sdk/esp_iot_sdk_v0.9.5_15_01_23.zip

Note: is the SDK from 23 Jan 2015. Verify if there is a new one.

Step 11.

unzip esp_iot_sdk_v0.9.5_15_01_23.zip 

mv esp_iot_sdk_v0.9.5 ESP8266_SDK/

Step 12.

cd /opt/Espressif/ESP8266_SDK

wget -O lib/libc.a https://github.com/esp8266/esp8266-wiki/raw/master/libs/libc.a

wget -O lib/libhal.a https://github.com/esp8266/esp8266-wiki/raw/master/libs/libhal.a

wget -O include.tgz https://github.com/esp8266/esp8266-wiki/raw/master/include.tgz

tar -xvzf include.tgz

rm include.tgz

Step 13.

cd /opt/Espressif

git clone https://github.com/themadinventor/esptool esptool-py

chown -R esp8266 crosstool-NG/

sudo ln -s $PWD/esptool-py/esptool.py crosstool-NG/builds/xtensa-lx106-elf/bin/

Step 14.

The SDK esp_iot_sdk_v0.9.5_15_01_23.zip has a directory named Document with some pdf files about API, AT commands and release notes.

Step 15. 

Copy the "at" directory from /opt/Espressif/ESP8266_SDK/examples and compile it

cp -R /opt/Espressif/ESP8266_SDK/examples/at /opt/Espressif/ESP8266_SDK/

cd /opt/Espressif/ESP8266_SDK/at

make clean

make 

The result resides in the /opt/Espressif/ESP8266_SDK/bin directory

Flashing the code. Note that the above command is on long line. You can put the command into a file flashEsp, give that file executable rights and use it with ./flashEsp

sudo /opt/Espressif/esptool-py/esptool.py --port /dev/ttyUSB0 write_flash 0x3C000 /opt/Espressif/ESP8266_SDK/bin/blank.bin 0x00000 /opt/Espressif/ESP8266_SDK/bin/eagle.flash.bin 0x40000 /opt/Espressif/ESP8266_SDK/bin/eagle.irom0text.bin OR /opt/Espressif/esptool-py/esptool.py -p /dev/ttyUSB0 write_flash 0x40000 ../bin/eagle.irom0text.bin write_flash 0x00000 ../bin/eagle.flash.bin


I hope I didn't missed any step.



Use my ESP8266 environment !!!

User ESP8266
password espressif

Let's see how you can compile our first example of code.

To do this download the virtual box ova from my drive.

Apply this ova file to a Virtual Box.

Start the virtual machine

go to /opt/Espressif/ESP8266_SDK/ where there are two directories with examples:

IoTDemo - a web server controlling  some GPIO's
at - build a custom AT command along with all command that are supportded by the at library


go to /opt/Espressif/ESP8266_SDK/IoTDemo/
make clean
make

two files are generated into the /opt/Espressif/ESP8266_SDK/bin directory.
!!!
No boot needed.
Generate eagle.flash.bin and eagle.irom0text.bin successully in folder bin.
eagle.flash.bin-------->0x00000
eagle.irom0text.bin---->0x40000
!!!

 Use those files and the optional blank image blank.bin or you can use this command
to write the files.

sudo /opt/Espressif/esptool-py/esptool.py --port /dev/ttyUSB0 write_flash 0x3C000 /opt/Espressif/ESP8266_SDK/bin/blank.bin 0x00000 /opt/Espressif/ESP8266_SDK/bin/eagle.flash.bin 0x40000 /opt/Espressif/ESP8266_SDK/bin/
eagle.irom0text.bin

Write the files on ESP-01.
1. Power off the ESP-01
2. Connect GPIO0 to GND
3. Power on the ESP-01
4. Burn firmware

Write the files on ESP201 Dev Board.
1. Power off the ESP-201
2. Keep pressed S2 and power the board
3. Release the S2 switch
4. Burn firmware

Notes:


  1. In /opt/Espressif/ESP8266_SDK/document/ there are tow directories English an Chinese with document about the SDK fucntion, AT command. Spend some time looking into them.
  2. In the Virtual Box image I've added the moserial application that can be used to see the debug information from ESP.
  3. Launch moserial with: sudo moserial &
  4. If Software Updater is bugging you, do not update. You are just fine.





Wednesday, March 4, 2015

ESP8266 Wireless Wifi Module Develop Board 8266 SDK Development Chip

Other interesting board is this board from Electronic Gadget World, a shop on aliexpress.com.

If you can not find this board you can buy other good development boards like:

1. Development board with ESP8266 module and power supply.
2. Battery operated development board
3. Small form factor development board

The parcel contained one ESP201  an SDK Development board and a DHT11 sensor. No cables or power supply. I've ordered two extra ESP201 so I've replaced the faulty one with a good ESP201.

As you can see in the image there are two USBs. The microUSB located on the top right is just for powering the board. The other usb, a type B USB can be used to power the board, and to write firmware to ESP201 flash. The usb to serial on board is a CH340G chip. My win7 didn't complain about it, I was able to write the flash to ESP201.
The only problem is that the transfer was done at 9600 or less (like 5 minute for 200k), after I've modified in the DeviceManager the speed for the port, the transfer rate was increased. But this happened only once, so I gave up using the CH340G chip for now. I've removed the red jumpers and I used my old Prolific USB/serial.

I'll reinstall the drivers for CH340G chip with this one  later and I'll see if helps.


ESP201 SDK development board

Now let's talk about the connections. As you can see the dev board has:


  • a relay
  • a RGB led
  • a white led ( almost under the RGB led which is above the buzeer)
  • DHT11 
  • buzzer
  • two switched S2 and S3
  • a variable rezistor for ADC ( on the left of relay ) connected to ADC pin.
  • a 5v to 3v3 convertor 
  • a main switch in the right upper corner (this switch is just for ESP201 board I guess, I have the impression that the CH340G is still receiving power even the switch is off).


DIP functionality
On the PCB near the DIP switch there 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 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 throuch 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.


A schematics on a napkin will look like this one:


Napkin schematics :-)

And thanks to Scott Snowden and jwbr from http://www.bpower.nl/ there is a complete schematics of the board.


Wifi Module Develop Board 8266 schematics




On IO4 is connected the DHT11 sensor. I didn't use it because I have a DHT22 which is more accurate.


How to write flash.

1.Power off the board.

2.Keep S2 pressed and power on the board using the switch located in the right top of the board.
3.Now you can write the flash from the ESP201 board.


Conclusions:

It is a very good board, no issues found. If you want a rapid development board you can buy this one.

And the final config will look like in this picture (2 external relays, 2 external LEDs DHT22). Pretty crowded. Breadboard is just for GND.




UPDATE1: See what to do to have access on the GPIO15 ( Red led from RGB) - HERE

UPDATE2: See what to do for controlling the relay connected on the XPD / GPIO16 - HERE

UPDATE3: A IoT plug based on ESP-01 - HERE.   A lawn irrigation IoT based on this board is coming soon.

UPDATE4: Now I have a unix cron functionality on my ESP8266 chips.

UPDATE5: MQTT broker running on ESP8266