Recent

Author Topic: Accessing Arduino Wifi  (Read 3360 times)

Serge58

  • New Member
  • *
  • Posts: 37
Accessing Arduino Wifi
« on: August 06, 2017, 10:38:49 pm »
Hello,
I am trying to send some data to my arduino micro controller wifi.  This thing act as a server on my home wireless network but requires specific (and i guess proprietary) commands.

For exemple, if I write the following in the address box a regular web browser while connected to the home network:   http://10.0.4.1/arduino/webserver it is a valid command for the Arduino. I can also write something similar to http://10.0.4.1/arduino/analog/1/23 and it is also valid.  This tells the Arduino Microcontroller to set its analog output 1 to 23.  Very basic.

How can I send the same type of commands through a Lazarus routine?
Also, after receiving the command, Arduino will send back very simple html data to share the status of its inputs ports.
I will have to be able to read it back in some way before processing it.

Help required.

many thanks,

Serge


Almir.Bispo

  • Jr. Member
  • **
  • Posts: 91
  • CSV Comp DB is the Best NoSQL
    • CSV Comp DB (NoSQL)
Re: Accessing Arduino Wifi
« Reply #1 on: August 06, 2017, 10:46:58 pm »
Hi.
You need use Synapse library with HTTPSend unit on Desktop

Just do this method
HTTPSend.GetText('url' , stringlist);

(*url is the server and parameter to your server like this: http://arduinoserver?param1=data1&param2=data2)
stringlist is a Tstringlist class.It must be created on runtime.

On Arduino code you must get the params to do something.
http://adltecnologia.blogspot.com.br
CSV Comp DB Developer {Pascal Lover}

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Accessing Arduino Wifi
« Reply #2 on: August 06, 2017, 11:33:17 pm »
Hello,
I am trying to send some data to my arduino micro controller wifi.
...
How can I send the same type of commands through a Lazarus routine?
Also, after receiving the command, Arduino will send back very simple html data to share the status of its inputs ports.
I will have to be able to read it back in some way before processing it.

You could perhaps use something like this:
Code: Pascal  [Select][+][-]
  1. program arduino;
  2. uses
  3.   fphttpclient;
  4. var
  5.   s: string;
  6. begin
  7.   s := TFPCustomHTTPClient.SimpleGet('http://10.0.4.1/arduino/webserver');
  8.   WriteLn(s);  // or something like: memo1.Lines.Add(s);
  9. end.
  10.  

See also here.

You would need to process the returned html body manually though.

Serge58

  • New Member
  • *
  • Posts: 37
Re: Accessing Arduino Wifi
« Reply #3 on: August 07, 2017, 12:55:15 am »
Thanks both of you.
I installed the synapse package.  I was getting some errors and found on the net that I had to disable a line of code in one of the unit.  After the package seems to be installed properly. No error.

i tried both of your options:  First, HTTPSend.GetText is giving me some error.  i checked and it seems that there is no GetText method in the HTTPSend class.

On the fphttpclient option i do not get error.  But it is not sending data (i think) and does nothing.  i have to stop it.

is it possible that i needed to specify the port number somewhere?  I saw that there are many fphttpclient versions.  Is this one part of the same synapse package?

Thanks both of you.

my code:

// Button 2 "Using fphttpclient"
procedure TForm1.Button2Click(Sender: TObject);
  var
  str: String;
begin
   str := TFPCustomHTTPClient.SimpleGet('http://10.0.4.1/arduino/webserver');
   ShowMessage(str);
end;
// Button 1 using "HTTPSend"
procedure TForm1.Button1Click(Sender: TObject);
   var
   str: String;
   Param: TStringlist;
begin
   Str:='http://1.0.4.1/arduino/webserver';
   HTTPSend.GetText (Str , Param);
   ShowMessage('Param);
end;


molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Accessing Arduino Wifi
« Reply #4 on: August 07, 2017, 01:15:19 am »
On the fphttpclient option i do not get error.  But it is not sending data (i think) and does nothing.  i have to stop it.
That is strange. I have no idea atm.

edit: in case you are running this compiled program on Windows, do you perhaps have antivirus/firewall running that blocks your lazarus application ?

In theory tfphttclient.SimpleGet should only 'hang' for the duration of timeout and return on errors (in which case the resulting body should be able to provide more information).

Quote
is it possible that i needed to specify the port number somewhere?
That is possible.I'm sorry but do not know much about arduino webserver. It should state somewhere what are the default options.

SimpleGet just sends data to the server at port 80. Perhaps arduino expects another port, such as 8080 ? Or perhaps the server only accepts secure connections ?

Quote
I saw that there are many fphttpclient versions.  Is this one part of the same synapse package?
fphttpclient is part of fcl-web and is a native/default Free Pascal package.

In case your server is redirecting then simpleget will not work as you expected. In that case have a look at the link i posted in my previous post and use the 'full' version of retrieving a html page from your server. SimpleGet class method is only meant to work in/for the most simplest of conditions.

In case you wish to learn more about the data sending/retrieval  between browser and server then you could consider using a proxy server. But there are also other options available that would let you inspect datapackets and/or http transfers.


edit2: I do not know much about synapse but i read:
   Str:='http://1.0.4.1/arduino/webserver';
Copy-paste error or actual typo in your code (for the web-address) ?

edit3:
i tried both of your options:  First, HTTPSend.GetText is giving me some error.  i checked and it seems that there is no GetText method in the HTTPSend class.
Seems to me that user Almir.Bispo only got it partially correct. According to this online help for synapse, it should read:
Code: Pascal  [Select][+][-]
  1. program synapsing;
  2. Uses
  3.   HTTPSend;
  4. var
  5.   url: String;
  6.   Response: TStringlist
  7. begin
  8.   url := 'http://10.0.4.1/arduino/webserver';
  9.   Response := TStringList.Create;
  10.   if httpGetText (url , Response)
  11.     then ShowMessage(Reponse.text)
  12.     else ShowMessage('Failure');
  13.   Response.free;
  14. end.
  15.  
« Last Edit: August 07, 2017, 02:26:30 am by molly »

Serge58

  • New Member
  • *
  • Posts: 37
Re: Accessing Arduino Wifi
« Reply #5 on: August 07, 2017, 03:40:21 am »
Thanks a million!!
With your help I was able to make it work.  First, my Url was bad.  It should have read 10.0.1.4. Thanks for pointing it out.

My code:
// Button 1 using 'HTTPSend'

procedure TForm1.Button1Click(Sender: TObject);
  var
  url,html: String;
begin
  url:= 'http://10.0.1.4/arduino/webserver';
  html := TFPCustomHTTPClient.SimpleGet(url);
    memo1.Text := html;
end;

// Button 2 Using 'fphttpclient'

procedure TForm1.Button2Click(Sender: TObject);
  var
  httpclient: TFPHTTPClient;
  html,url: String;
begin
  url:= 'http://10.0.1.4/arduino/webserver';
  httpclient:= TFPHttpClient.Create(Nil);
  try
     html:= httpclient.Get(url);
  finally
     httpclient.Free;
  end;
  memo1.Text := html;
 end;   

Both of the functions are working.
many thanks,

 

TinyPortal © 2005-2018