Can this be done with Javascript? Microcontrollers/TCP IP

T

test

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
 
S

slebetman

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.
 
J

Jorge

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 <img> or 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,
 
T

test

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!!!
 
T

test

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 <img> or 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!
 
J

Jorge

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 ?
 
T

test

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!
 
J

Jorge

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.lang.javascript/msg/c523c4a4093bde90

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 ?
 
J

Jorge

See my other post:http://groups.google.com/group/comp.lang.javascript/msg/c523c4a4093bde90

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 <img> you 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...
 
J

Jorge

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.";
 
T

test

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!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top