Esp8266 – Windows compilation tutorial for n00bs

ESP8266 is a new SOC(System On Chip) WiFi module from Espressif that works in similar way to modems, it’s controlled by AT commands through serial port.
Well, it’s actually more than that. You can write your own custom software that runs on this small, yet powerful device. It’s like arduino that has more power, WiFi buil’t in but only few pinouts(unless you use other versions which are pretty hard to run without proper soldering training)
There’s esp8266 wiki that describes how to prepare build environment but its Linux only, so I decided to write small tutorial on how to do that on Windows.

esp8266

1. Download cygwin installer and run it [Exe]

https://cygwin.com/install.html

Install it to c:\cygwin\
Select make when asked for which packages you want to install:
Add->Devel->Make

make

2. Install Python 2.7 from https://www.python.org/

3. Install git [Optional, would be easier to do further steps, add git.exe to your PATH.

4. Create folder for your esp development
Keep your 8266 stuff in one place, including toolchain, compiler and projects, create folder C:\Projects\esp8266

5. Download compiled toolchain

Google drive: https://drive.google.com/folderview?id=0BzWyTGWIwcYQendHbWlsNUZpX0E&usp=drive_web#list\
Downoad: xtensa-lx106-elf-141114.7z
Extract it to “C:\Projects\esp8266\xtensa-lx106-elf”, you need to have 7zip handling software to unpack this file.

Its made by mobyfab from esp8266.com
These are apperently x64 binaries so no hope for you if you are using 32 bit machine.

6. Download SDK (esp_iot_sdk_v0.9.2_14_10_24.zip)
from official Espressif forum
and extract it to c:\esp8266\esp_iot_sdk_v0.9.2\

7. Download helper files from my repository https://github.com/toomasz/esp8266_stuff
and extract them to c:\Projects\esp8266\esp8266_stuff

8. Edit file C:\Projects\esp8266\esp8266_stuff\espmake.sh
and change variables so they reflect your buildsystem(you don’t have to do it if you used same folders as mine):

TOOLCHAIN=c:/Projects/esp8266/tools/xtensa-lx106-elf/bin
SDK=c:/Projects/esp8266/Espressif/ESP8266_SDK
INCDIR='-Iinclude'
ESPTOOL=/bin/esptool

9. Download esptool.exe

https://github.com/metalheart/esp8266/raw/master/bin/esptool.exe

and put it into C:/Cygwin/bin

10. Add C:\Projects\esp8266\esp8266_stuff and python to your PATH variable

I recommend using some tool for editing PATH variable, for example rapid environment editor.
path_var

11. Download missing libwinpthread-1.dll that in neccesary to run compiler.

Download it from: https://drive.google.com/file/d/0B-lM6D5uRWgBWnN1akVmOHZNSkU/view?usp=sharing
Sorry to exe link to my google account but I don’t remember where I downloaded this one and it seems like the only libwinpthread-1.dll one that works, dll-files.com one doesn’t work.

Put it to: c:\Projects\esp8266\esp8266_stuff\

12. Try to compile example project

Download AT Example from Espressif: AT Example
And put it to c:\Projects\esp8266\projects\at\

Replace ‘retarded’ Makefile with this one https://gist.github.com/fpoussin/73e3bf45846bec1e5a08/download#
Now close all programs on your computer(as a precaution regarding PATH variable).
And edit new makefile, add upgrade upgrade_ssl to libs:

# libraries used in this project, mainly provided by the SDK
LIBS		= c gcc hal phy net80211 lwip wpa main upgrade upgrade_ssl

If your esp8266 directory looks like below, try to compile sample project:
esp8266-dir

Run cmd, Cd to C:\Projects\esp8266\projects\at and run espmake -B
If you see the following, you are good to go :)

compile_esp

espmake.bat uses same arguments as make, so you can call for example make -B to rebuild.

Is Borat funny?

Borat is an reporter from kazakhstan who visits USA and Britain and makes interviews vith various, usually quite famous people. He asks various questions regarding these countries, so that people in kazakhstan could understand how its like to live in London or in Alabama.
He was also sent by the the goverment for a very special assignment, called ‘Cultural learnings of america to make benefit glorious nation of kazakhstan.
He also uses some veird construction grammars,and terms.

Here’s Borat’s dictionary:
Borat- English
Jak sie masz – Good
Wo wo wii waah! – Wow, I like it, Nice ass
Very nice, how much – Hello(only to girls)
Sexy time – sex
Hand relief – masturbation
Nosy people – Jews
Vageen – Vagina

Borat is strongly agains woman’s rights and doesn’t approve them for anything beyond sex or manual labour like plow. The only woman he respects is his sister, who is number 4 prostitute in kazakhstan. He also had a wife in kazakhstan but she was killed in an accident in which hunter took her for bear, presumbly because of her androgenic hairs size. But it’s not a problem here because, as he says ha has a new wife, who is nice.

Borat and his sister

Borat and his sister

C# Object initialization order with simple example

I guess we don’t pay enough attention to object initalization order in classess we create. Sometimes one can just ignore it an don’t think too much about which constructor is called first and which members are initialized in which order. However i stumbled upon a lot sitiuations when i was wasting my time looking for bugs in other parts of code when the real problem was caused by not enought thinking about initialization.
For example, let’s take a look at the following part of code:

// 'Dervied' derives from 'Base' class
Derived d = new Derived() { PropBase = "Derived Property", PropDerived = "Prop derived" };

Most people know that constrctor of base class is called first, but how about initializers? Which property will be initialized first? PropBase or PropDerived?
Im programtic so I worite this pieced od code to test it:

using System;
using System.Diagnostics;
using System.Reflection;
 
namespace Test
{
    public class Base
    {
        private string _Test;
 
        public string PropBase
        {
            get { return _Test; }
            set {
                _Test = value;
                Program.WhoCalled();
            }
        }
 
        static string Base_foo()
        {
            Program.WhoCalled();
            return "aa";
 
        }
        string a = Base_foo();
 
        public Base()
        {
            Program.WhoCalled();
        }
    }
 
    public class Derived : Base
    {
        string _Test;
        public string PropDerived
        {
            get { return _Test; }
            set
            {
                _Test = value;
                Program.WhoCalled();
            }
        }
 
        static string Derived_Foo()
        {
            Program.WhoCalled();
            return "aa";
        }
        string a = Derived_Foo();
 
        public Derived()
        {
            Program.WhoCalled();
        }
    }
    class Program
    {
 
        public static void WhoCalled()
        {
            StackTrace stackTrace = new StackTrace();  
            StackFrame sf = stackTrace.GetFrame(1);
            MethodBase method = sf.GetMethod();
            string methodName = method.Name;
            string typeName = method.DeclaringType.Name;
 
            if(methodName == ".ctor")
                Console.WriteLine("{0}::{1}", typeName, typeName);
            else
                Console.WriteLine("{0}::{1}", typeName, methodName);
 
        }
        static void Main(string[] args)
        {
            Derived d = new Derived() { PropBase = "Derived Property", PropDerived = "Prop derived" };
            Console.Read();
        }
    }
}

Method WhoCalled prints out object and method name of it’s callee using reflection.

Let’s see what it prints out:


Derived::Derived_Foo
Base::Base_foo
Base::Base
Derived::Derived
Base::set_PropBase
Derived::set_PropDerived

There are come conclusions learned from this simple test:

  • Initlialization stats with calling Initializers, then goes to calling Constructors and then to setters getters
  • Constructors are first called in base class
  • Initializers are called first in derived class
  • Have fun initlializing your classes!

Sim900 – scanning all visible cell towers from all gsm operators

One of the most interesting commands in latest firmware for Sim900 module is AT+CNETSCAN. It lists all visible cell towers(BTS) from all network operators.


AT+CNETSCAN

------MOST SUITABLE CELL------
Operator:"Era",MCC:260,MNC:2,Rxlev:28,Cellid:fa5e,Arfcn:0021
Operator:"Era",MCC:260,MNC:2,Rxlev:24,Cellid:b030,Arfcn:0028
Operator:"Era",MCC:260,MNC:2,Rxlev:21,Cellid:afc4,Arfcn:0016
Operator:"Era",MCC:260,MNC:2,Rxlev:21,Cellid:fa5f,Arfcn:0831
Operator:"Era",MCC:260,MNC:2,Rxlev:17,Cellid:fa14,Arfcn:0033
Operator:"Era",MCC:260,MNC:2,Rxlev:15,Cellid:ffff,Arfcn:0031
------OTHER SUITABLE CELL------
Operator:"Orange PL",MCC:260,MNC:3,Rxlev:25,Cellid:2239,Arfcn:0112
Operator:"Orange PL",MCC:260,MNC:3,Rxlev:26,Cellid:223a,Arfcn:0793
Operator:"Orange PL",MCC:260,MNC:3,Rxlev:24,Cellid:ffff,Arfcn:0108
Operator:"Plus",MCC:260,MNC:1,Rxlev:22,Cellid:abb9,Arfcn:0049
Operator:"Orange PL",MCC:260,MNC:3,Rxlev:20,Cellid:ffff,Arfcn:0110
Operator:"Plus",MCC:260,MNC:1,Rxlev:20,Cellid:ffff,Arfcn:0038
Operator:"Orange PL",MCC:260,MNC:3,Rxlev:19,Cellid:204f,Arfcn:0118
Operator:"POL",MCC:260,MNC:6,Rxlev:24,Cellid:f29e,Arfcn:0999
Operator:"Orange PL",MCC:260,MNC:3,Rxlev:18,Cellid:ac1d,Arfcn:0114
Operator:"Orange PL",MCC:260,MNC:3,Rxlev:18,Cellid:ffff,Arfcn:0113

OK

Each line corresponds to one bts transceiver and includes:

Simcard is not required for this command.
If module is connected to network, it only list towers belonging to that gsm operator.

If command doesn’t work, make sure you have right firmware version:


AT+GSN

SIMCOM_Ltd
SIMCOM_SIM900
Revision:1137B04SIM900M64_ST_MMS

OK

Sim900 geolocalization without GPS, AT+CIPGSMLOC

While browsing application notes pdf from simcom, I discovered that sim900 module supports some kind of triangulation that is able to get approximate coordinations of device.

Here’s sequence of AT commands needed to get location of module:


AT+SAPBR=3,1,"CONTYPE","GPRS" // set bearer parameter
OK

AT+SAPBR=3,1,"APN","internet" // set apn
OK

AT+SAPBR=1,1 // activate bearer context
OK

AT+SAPBR=2,1 // get context ip address
+SAPBR: 1,1,"10.151.43.104"
OK

AT+CIPGSMLOC=1,1 // triangulate
+CIPGSMLOC: 0,19.667806,49.978185,2014/03/20,14:12:27

OK

Location is not acurrate though, first test got me coordinates located around 4 kilometers away from my place. Usually it’s not that bad, enough for simple applications.

Arduino – printf and sscanf memory usage

Program size without printf:

14998 bytes   45,8 % Full

printf_P

After adding following line to my program:

printf_P(0);/* just a test, don't use in real world :D */

Program size grows to:
16452 bytes 50,2 % Full

16452-14998-10=1444 bytes
10 is estimated number of bytes needed for function call and empty PSTR.

Using printf function will use around 1444 bytes of your precious flash space.

sscanf_P

After adding line:

sscanf_P(0,0);/* just a test, don't use in real world :D */

Program size is:
16794 bytes 51,3 % Full

16794-14998-10=1786 bytes

printf_P and sscanf_P together

Let’s check how many memory is consumed when both functions are used:

sscanf_P(0,0);
printf_P(0);

Program Memory Usage : 18138 bytes 55,4 % Full

18138-14998-20=3120 bytes

If we add 1444+1786(memory usages of printf_P and sscanf_P respectively) we get 3230 What that means? Well, apparently these functions use some common code :) Just that.

My Sim900 gprs library

Recently I came with idea of new arudino gprs shield project. What i needed to do first was to initialize sim900, make sure it’s registered to network and then activate pdp context and establish tcp connection to server. I found few libraries bet they all seemed to be not well suited for me. Either they were not working properly, didn’t have functions I needed or they were using String class which isn’t a good idea when you have 2048b of ram.

So i decided to write my own simple library that has basic functions needed to establish transparent tcp connection to server and keep it alive.

Sim900 library