By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,483 Members | 3,229 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,483 IT Pros & Developers. It's quick & easy.

Can this be done with Javascript? Microcontrollers/TCP IP

P: n/a
Hi, I'm very new to Javascript so maybe my question could be stupid.

I'm playing with microcontrollers and a TCP/IP component.
This means that I can control electronics via TCP/IP, UDP and so on.
But, my problem is on the client side.

I want to have a program, or a web page containing buttons that, when
pressed sends a signal to the TCP/IP component and waits for a
response back.
Maybe this seems strange, but let's say I have a connection on port 80
and I click a radio button on a web page, the new information is send
(like with a "get" command) to the TCP/IP component.
That analyses the new command en sends a confirmation back.

In real this would be a web page that has 2 times 8 radio buttons that
sends information for 8 lamps.
The way the information is send by the Javascriot doesn't matter.
The TCP/IP component replies after the information is processed.

Lets say someone clicks "lamp1" from "OFF" to "ON"
The script should send a code that represents the state of the buttons
on the page
"A1,B0,C1,E0,F0,G0H0,I0"
This means that lamp A, C are ON, the rest is OFF.

The TCP/IP component checks if this can be done and replies:
"A1,B0,C1,E0,F0,G0H0,I0"
The same as the command send by the script.
The script reacts by visualize the new state.

In a later stage, the input should be a 8byte word.
So the 8 lamps should become textboxes that can contain numbers from 0
to 255.

This is but a draft version of this question, if it seems that
Javascript is not the best way to do this, I won't bother you anymore
:-)

Thanks for your help
Oct 16 '08 #1
Share this Question
Share on Google+
11 Replies


P: n/a
On Oct 17, 6:55 am, t...@testnospam.nl wrote:
Hi, I'm very new to Javascript so maybe my question could be stupid.

I'm playing with microcontrollers and a TCP/IP component.
This means that I can control electronics via TCP/IP, UDP and so on.
But, my problem is on the client side.

<snip>

This is but a draft version of this question, if it seems that
Javascript is not the best way to do this, I won't bother you anymore
:-)
It depends on what you mean by "best way". Best way for the
programmer, I'd say no. Best way for preventing a hacker from
switching lights in your house on or off, I'd say no. Best way to be
able to tell your client "Oh, just open the device's web page at
127.0.0.11 and click the red button", I'd say maybe.. depending on
what you mean by best...

The thing is, when it comes to communication, javascript can do only
what HTML can do: make HTTP requests. So first you'll have to make
sure your device can parse HTTP. And remember, the browser will not
send simple-and-minimal HTTP, rather it would send something like:

GET /set_buttons?A=1&B=0&C=1&E=0&F=0&GH=00&I=0 HTTP/1.1
Host: 127.0.0.11

User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.17) Gecko/
20061201 Firefox/2.0.0.17 (Ubuntu-feisty)

Accept: text/xml,application/xml,application/xhtml+xml,text/
html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5

Accept-Language: en-us,en;q=0.5

Accept-Encoding: gzip,deflate

Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7

Keep-Alive: 300

Connection: keep-alive
Content-Type: application/x-www-form-urlencoded

message=1; current_style=1

Pragma: no-cache

Cache-Control: no-cache

Which is actually not that hard to parse. You'd only be interested in
the GET line and your device can ignore the rest. To test this out I'd
suggest first sending the data using a simple HTML form and don't
touch javascript until you can get your device to reliably talk HTTP.
Then, if you want, use javascript to make an AJAX request to avoid
having to reload the whole page.

Oct 17 '08 #2

P: n/a
On Oct 17, 12:55*am, t...@testnospam.nl wrote:
Hi, I'm very new to Javascript so maybe my question could be stupid.

I'm playing with microcontrollers and a TCP/IP component.
This means that I can control electronics via TCP/IP, UDP and so on.
But, my problem is on the client side.

I want to have a program, or a web page containing buttons that, when
pressed sends a signal to the TCP/IP component and waits for a
response back.
Maybe this seems strange, but let's say I have a connection on port 80
and I click a radio button on a web page, the new information is send
(like with a "get" command) to the TCP/IP component.
That analyses the new command en sends a confirmation back.
(...)

YES, no problem. This can be done in several ways. You can send/
request the info in the url of the http request of any resource
(usually an <imgor a <script>) as a query parameter '?q=theData' (or
not), and you can send/request it via an XMLHttprequest as well either
in the headers of a GET/POST/HEAD request or in the body of a POST
request.

HTH,
--
Jorge.
Oct 17 '08 #3

P: n/a
>It depends on what you mean by "best way". Best way for the
>programmer, I'd say no. Best way for preventing a hacker from
switching lights in your house on or off, I'd say no. Best way to be
able to tell your client "Oh, just open the device's web page at
127.0.0.11 and click the red button", I'd say maybe.. depending on
what you mean by best...

The thing is, when it comes to communication, javascript can do only
what HTML can do: make HTTP requests. So first you'll have to make
sure your device can parse HTTP. And remember, the browser will not
send simple-and-minimal HTTP, rather it would send something like:

GET /set_buttons?A=1&B=0&C=1&E=0&F=0&GH=00&I=0 HTTP/1.1
Host: 127.0.0.11

User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.17) Gecko/
20061201 Firefox/2.0.0.17 (Ubuntu-feisty)

Accept: text/xml,application/xml,application/xhtml+xml,text/
html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5

Accept-Language: en-us,en;q=0.5

Accept-Encoding: gzip,deflate

Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7

Keep-Alive: 300

Connection: keep-alive
Content-Type: application/x-www-form-urlencoded

message=1; current_style=1

Pragma: no-cache

Cache-Control: no-cache

Which is actually not that hard to parse. You'd only be interested in
the GET line and your device can ignore the rest. To test this out I'd
suggest first sending the data using a simple HTML form and don't
touch javascript until you can get your device to reliably talk HTTP.
Then, if you want, use javascript to make an AJAX request to avoid
having to reload the whole page.
I have a device that can react on everything that is send wit the GET
command.
So if the browser send this:

radiobuttons.html?A=on&C=on&G=on&Zend=Submit

I can filter out the names of the buttons and their state.
So all my device makes of it is:
A=on C=on G=on
The rest is ignored by the program in the controller.

But, my main question is if the controller can give feedback if the
lights were put on, and show this on the webpage.
So if I send the request to turn light A on, and the device checks if
the light is actually burning, this should be showed on the page.

This should be a two way communication.
Let's say that the lights were switched off by a switch in the room,
the controller knows that the light didn't turn on, and even if the
webpage requested to turn it on, after the command, shows the light as
dimmed.

Thanks already for your reply!!!
Oct 18 '08 #4

P: n/a
>YES, no problem. This can be done in several ways. You can send/
>request the info in the url of the http request of any resource
(usually an <imgor a <script>) as a query parameter '?q=theData' (or
not), and you can send/request it via an XMLHttprequest as well either
in the headers of a GET/POST/HEAD request or in the body of a POST
request.

HTH,
Thanks already for your reply.

Can you give me an example of how this works?
So if the "Send button" on the webpage is pressed, it parses (is this
the right word for send?) the information to the controller.

radiobuttons.html?A=on&C=on&G=on&Zend=Submit

The controller filters this request back to this:
A=on C=on G=on

Checks if this can be done (maybe the light is broken), and wants to
return the status back to the webpage.
What does the controller have to generate and send back, and how is
this processed by the webpage?

Again thanks!

Oct 18 '08 #5

P: n/a
On Oct 18, 3:38*pm, t...@testnospam.nl wrote:
>
Thanks already for your reply.

Can you give me an example of how this works?
So if the "Send button" on the webpage is pressed, it parses (is this
the right word for send?) the information to the controller.

radiobuttons.html?A=on&C=on&G=on&Zend=Submit

The controller filters this request back to this:
A=on C=on G=on

Checks if this can be done (maybe the light is broken), and wants to
return the status back to the webpage.
What does the controller have to generate and send back, and how is
this processed by the webpage?
It depends. Is that same device going to serve the .html page ?

--
Jorge
Oct 18 '08 #6

P: n/a
On Sat, 18 Oct 2008 15:31:31 -0700 (PDT), Jorge
<jo***@jorgechamorro.comwrote:
>On Oct 18, 3:38*pm, t...@testnospam.nl wrote:
>>
Thanks already for your reply.

Can you give me an example of how this works?
So if the "Send button" on the webpage is pressed, it parses (is this
the right word for send?) the information to the controller.

radiobuttons.html?A=on&C=on&G=on&Zend=Submit

The controller filters this request back to this:
A=on C=on G=on

Checks if this can be done (maybe the light is broken), and wants to
return the status back to the webpage.
What does the controller have to generate and send back, and how is
this processed by the webpage?

It depends. Is that same device going to serve the .html page ?
This is my initial idea to let the device hold the HTML page, but
because of lack of memoryspace in the device I think I must let go of
this idea.

So a page hosted on a different server would be a better idea.
Not only for this application, but also because of what you mentioned
earlier, security. I could prevent access to this page much better on
a webserver than on a chip.

So, if this page is hosted on a separate server, and I click a button,
a serie of signs are send (like with the GET command) to that IP
address. (or like in my second question, a byte)
Thing is that I would like to make this page fetch the actual state
back from the device.
If asked by the HTML-page, the controller would send back a value that
reflects the state of the outputs.

Again thanks!
Oct 19 '08 #7

P: n/a
On Oct 19, 6:35*pm, t...@testnospam.nl wrote:
>
So, if this page is hosted on a separate server, and I click a button,
a serie of signs are send (like with the GET command) to that IP
address. (or like in my second question, a byte)
Thing is that I would like to make this page fetch the actual state
back from the device.
If asked by the HTML-page, the controller would send back a value that
reflects the state of the outputs.
See my other post:
http://groups.google.com/group/comp....23c4a4093bde90

It is asking for a picture. You could get back the state encoded into
the size of the (dummy) picture that you return: the widht= hignibble
and the height= lownibble, for example ?

--
Jorge.
Oct 20 '08 #8

P: n/a
On Oct 20, 9:59*am, Jorge <jo...@jorgechamorro.comwrote:
On Oct 19, 6:35*pm, t...@testnospam.nl wrote:
So, if this page is hosted on a separate server, and I click a button,
a serie of signs are send (like with the GET command) to that IP
address. (or like in my second question, a byte)
Thing is that I would like to make this page fetch the actual state
back from the device.
If asked by the HTML-page, the controller would send back a value that
reflects the state of the outputs.

See my other post:http://groups.google.com/group/comp....23c4a4093bde90

It is asking for a picture. You could get back the state encoded into
the size of the (dummy) picture that you return: the widht= hignibble
and the height= lownibble, for example ?
Or, better yet, instead of requesting an <imgyou ought to request a
<script>, the script once received can setup any variables in you JS
code with ease. And then there's no need to build 'dummy' images at
the device, just a short string of text (the text of the script) will
do...

--
Jorge.
Oct 20 '08 #9

P: n/a
On Oct 20, 10:04*am, Jorge <jo...@jorgechamorro.comwrote:
just a short string of text (the text of the script) will
do...
Short like:

window.yourObject["deviceIP"].currentValue= "8a";
window.yourObject["deviceIP"].statusMessage= "The kitchen is on
fire.";

--
Jorge.
Oct 20 '08 #10

P: n/a
On Mon, 20 Oct 2008 01:11:59 -0700 (PDT), Jorge
<jo***@jorgechamorro.comwrote:
>On Oct 20, 10:04*am, Jorge <jo...@jorgechamorro.comwrote:
>just a short string of text (the text of the script) will
do...

Short like:

window.yourObject["deviceIP"].currentValue= "8a";
window.yourObject["deviceIP"].statusMessage= "The kitchen is on
fire.";
Thanks many times, will look in to it!!
Oct 20 '08 #11

P: n/a
On Oct 21, 12:27*am, t...@testnospam.nl wrote:
On Mon, 20 Oct 2008 01:11:59 -0700 (PDT), Jorge

<jo...@jorgechamorro.comwrote:
On Oct 20, 10:04*am, Jorge <jo...@jorgechamorro.comwrote:
just a short string of text (the text of the script) will
do...
Short like:
window.yourObject["deviceIP"].currentValue= "8a";
window.yourObject["deviceIP"].statusMessage= "The kitchen is on
fire.";

Thanks many times, will look in to it!!
I like that one so I've added it to my collection of "cljs
snippets" :-)

http://jorgechamorro.com/cljs/021/

--
Jorge.
Oct 21 '08 #12

This discussion thread is closed

Replies have been disabled for this discussion.