Arduino Pascal Magazine PDF
Arduino Pascal Magazine PDF
Current versions can be purchased preassembled; Let’s begin with HTTP (Hypertext Transfer
hardware design information is available for Protocol) and TCP. TCP/IP stands for
those who would like to assemble an Arduino by Transmission Control Protocol and Internet
hand. Additionally, variations of the Italian- Protocol. TCP/IP can mean many things, but in
made Arduino — with varying levels of most cases, it refers to the network protocol itself.
compatibility — have been released by third
parties; some of them are programmed using the Each computer on a TCP/IP network has a
Arduino software or the sketch firmware. unique address associated with it, the so called
The Arduino is what is known as a Physical or IP-Address. Some computers may have more
Embedded Computing platform, which means than one address associated with them. An IP
that it is an interactive system that through the address is a 32-bit number and is usually
use of hardware, firmware and software can represented in a dot notation, e.g. 192.168.0.1.
interact with its environment. Each section represents one byte of the 32-bit
address. In maXbox a connection with HTTP
represents an object.
This lesson will introduce you to Arduino and the In our case we will operate with the local host. It
Serial communication (see Tutorial 15). We will is common for computers to refer to themselves
now dive into the world of serial communications with the name local host and the IP number
and control our lamp from a browser to a web 127.0.0.1.
server by sending commands from the PC to the GET THE CODE
Arduino using a serial monitor with interface. As you already know the tool is split up into the
toolbar across the top, the editor or code part in
In our case we explain one example of a HTTP the centre and the output window at the bottom.
server which is an intermediate to the COM serial Change that in the menu /view at our own style.
communication with the AVR based micro
controller on Arduino. In maXbox you will start the web server as a
Another Controller is the Delphi Controller . A script, so the web server IS the script that starts
Delphi Controller and the DelphiDevBoard were the Indy objects, configuration and a browser too
designed to help students, Pascal programmers (on board: Options/Add_ons/Easy_Browser/.
and electronic engineers understand how to Before this starter code will work you will need
program micro controllers and embedded to download maXbox from the website. It can be
systems especially in programming these devices down-loaded from
and targets (see links at the end). http://www.softwareschule.ch/maxbox.ht
m (you’ll find the download maxbox3.zip on our
This is achieved by providing hardware (either website in your personal download area). Once the
pre-assembled or as a DIY kit of components), using download has finished, unzip the file, making
course material, templates, and a Pascal sure that you preserve the folder structure as it is.
compatible cross-compiler and using of a If you double-click maxbox3.exe the box opens a
standard IDE for development and debugging default demo program.
(Delphi, maXbox, Lazarus or Free Pascal).
Figure 1:
The GUI of the Win App
Log Result
<<extend>>
if uppercase(localcom) = uppercase('/LED')
The full target of the request then
begin
message is given by the URL
cPort.WriteStr('1')
property. Usually, this is a URL that writeln(localcom+ ': LED on');
can be broken down into the RespInfo.ContentText:= getHTMLContentString('LED is: ON');
protocol (HTTP), Host (server system), end
script
else
name (server application), path info if uppercase(localcom) = uppercase('/DEL') then begin
(location on the host), and a query. cPort.WriteStr('A');
writeln(localcom+ ': LED off');
• So far we have learned little RespInfo.ContentText:= getHTMLContentString('LED is: OFF')
end;
about HTTP and host names.
Now it’s time to run our program
at first with F9 (if you haven’t done yet) and
HTTP request messages contain many headers
learn something about GET and HTML.
that describe information about the client, the
The program (server) generates a standard
target of the request, the way the request should
HTML output or other formats (depending on
be handled, and any content sent with the request.
the MIME type) after downloading with GET
Each header is identified by a name, such as
or HEAD.
"Host" followed by a string value. It then does a
request.
So our command to shine on a LED is ../LED and
• You can also switch with F5 in a browser to
to switch off is ../DEL (127.0.0.1:8000/LED).
switch LED on and off:
Those are GET commands send with the browser,
or /R for Red or /G for Green. webswitch:= NOT webswitch;
if webswitch then
The first line identifies the request as a GET. begin
A GET request message asks the Web server cPort.WriteStr('1') //goes to Arduino
application to return the content associated with RespInfo.ContentText:=
the URI that follows the word GET. getHTMLContentString('LED is: ON Switch');
end
The following shows the magic behind in the else
method HTTPServerGet(): begin
procedure HTTPServerGet(aThr: TIdPeerThread;
reqInf: TIdHTTPRequestInfo; respInf: TIdHTTPResponseInfo);
Figur 4: The Browser controls It downloads the entire file into memory if the
When the browser starts from script the server is data is compressed (Indy 9 does not support
ready for commands to pass chars to the serial streaming decompression for HTTP yet). Next we
communication. When a the server application come closer to the main event of our web server in
finishes with our client request, it lights the LED line 40, it’s the event onCommandGet with the
and constructs a page of HTML code or other corresponding event handler method
MIME content, and passes the result back (via the @HTTPServerGet() and one object of
server in TIdHTTPResponseInfo ) to the client TidPeerThread.
for display.
writeln(localcom+ ': LED on');
RespInfo.ContentText:= getHTMLContentString('LED is: ON');
Have you tried the program, it’s also possible to You can use them as server as the way to serve
test the server without Arduino or a browser. files of many kinds!
When you run this code from the script
102_pas_http_download.txt you will see a
content (first 10 lines) of the site in HTML format
with the help of the method memo2.lines.add:
SERIAL LINE
int val = 0;
Please read more about serial coding in Tutor 15!
// variable to store data from the serial port
The serial communications line is simply a way int ledPin11 = 11;
for the Arduino to communicate with the outside // LED connected to digital pin 11 or the inbuilt 13!
world, in this case to and from the PC (via USB)
and the Arduino IDE’s Serial Monitor or from void setup() {
pinMode(ledPin11,OUTPUT);
the uploaded code to I/O Board back. // declare a LED's pin as output mode
We just create and configure our COM settings erial.begin(9600); // connect to serial port
(depends in which COM port a USB hub works). ..}
procedure TForm1_FormCreateCom(Sender: TObject);
begin
In the main loop we have an “if statement”. The
cPort:= TComPort.Create(self);
with cPort do begin condition it is checking the value in (Serial.read).
BaudRate:= br9600; The Serial.available command checks to see if any
Port:= COMPORT; //'COM3'; characters have been sent down the serial line. If
Parity.Bits:= prNone;
any characters have been received then the
StopBits:= sbOneStopBit;
DataBits:= dbEight; condition is met and the code within the “if
end; statements” code block is now executed, you see if
‘1’ then ON and if ‘A’ then OFF.
The condition of checking is simply a char it’s up
The Arduino can be used to develop stand-alone to you to code a protocol of your own .
interactive objects or it can be connected to a
void loop () {
computer to retrieve or send data to the Arduino val = Serial.read(); // read on the serial port
and then act on that data (e.g. send sensor data if (val !=-1){
out to the web or write data on a control LED). if (val=='1'){
digitalWrite(ledPin1,HIGH);
Now we change to the Arduino editor to explain }
how he handles our commands (chars). else if (val=='A'){
Serial.begin tells Arduino to start serial and the digitalWrite(ledPin1,LOW);
number within the parenthesis, in this case 9600, }
Serial.print("Data entered: ");
sets the baud rate (chars per second) that the
serial line will communicate at.
procedure letOpenBrowser;
// TS_ShellExecuteCmd = (seCmdOpen,seCmdPrint,seCmdExplore);
begin
//ShellAPI.ShellExecute(Handle,PChar('open'),'http://127.0.0.1:80/',Nil,Nil,0);
S_ShellExecute('http:'+IPADDR+':'+IntToStr(APORT)+'/','',seCmdOpen)
end;
Literature:
Kleiner et al., Patterns konkret, 2003,
Software & Support