Using Lazarus / Free Pascal to Communicate with ESP8266

In my prior post, ESP8266/Arduino IDE: Building an LED Blink Server, I documented the creation of an LED blink server that runs on an ESP8266. Now I’m going to show how to access that ESP8266 using Lazarus.

Because Lazarus can be run on Windows/Linux/RPI/Macs, you can use any of these platforms to communicate with the ESP8266 using the same source I wrote. Just recompile it for your platform.

The prior posts all discuss the gritty details of getting the ESP8266 LED blinky server running. I won’t duplicate any of that here. Instead, I’ll just go over the Lazarus program used to access it.

Synapse Library

First, you need to install Synapse into your Lazarus installation. Synapse is a library I use to perform TCP communications. Personally, I just copy the entire synapse library directory into my program source directory and point there. Disc space is cheap and that prevents compile failures when I come back 2 years later and want to try the example again.

Synapse can be found at

http://synapse.ararat.cz/doku.php/download

Program Overview

They don’t get much simpler than this. First I open a socket to the server:

    sock := TTCPBlockSocket.Create;
    sock.Connect('192.8.50.250', '23');

Then I wait for the ready string. Actually, I just read all characters until <lf> is received. For brevity, I don’t even verify that the string returned is ‘ready’:

    s := sock.RecvTerminated(5000, #10);

The server is ready for a command so I tell it to turn the LED on:

    sock.SendString('on'+#13+#10);

The server requires <cr><lf> after on, so I made sure to include that.

This this is a simple example, I’m not going to check the response code either, though in a real program I would definitely make sure I got an ‘ok’ response.

The Program Source

Here is the entire Lazarus program:

program esp2Laz;

{$mode objfpc}{$H+}

uses
    {$IFDEF UNIX}{$IFDEF UseCThreads}
    cthreads,
    {$ENDIF}{$ENDIF}
    blcksock,
    Classes,
    Sysutils;

var
    k                                    : integer;
    s                                    : string;
    sock                                 : TTCPBlockSocket;

begin

writeln('Turn LED on.');

try
    sock := TTCPBlockSocket.Create;
    sock.Connect('192.8.50.250', '23');

    s := sock.RecvTerminated(5000, #10);

    sock.SendString('on'+#13+#10);

finally
    sock.CloseSocket;
    sock.Free;
    end;

sleep(3000);

writeln('Turn LED off.');

try
    sock := TTCPBlockSocket.Create;
    sock.Connect('192.8.50.250', '23');

    s := sock.RecvTerminated(5000, #10);

    sock.SendString('off'+#13+#10);

finally
    sock.CloseSocket;
    sock.Free;
    end;

end.
Advertisements
This entry was posted in c-esp8266, c-lazarus, c-rpi and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s