diff --git a/_layouts/default.html b/_layouts/default.html index fad56a5..fb53e91 100644 --- a/_layouts/default.html +++ b/_layouts/default.html @@ -79,6 +79,9 @@

wifi +
  • + access point +
  • blinky
  • @@ -89,7 +92,7 @@

    tweet
  • - access point + webserver
  • gpio @@ -113,10 +116,13 @@

    2. wifi

  • - 3. blinky + 3. access point +
  • +
  • + 4. blinky
  • - 4. modules + 5. modules diff --git a/ap.md b/ap.md index 7e830a5..d6c0c24 100644 --- a/ap.md +++ b/ap.md @@ -1,245 +1,49 @@ {::options parse_block_html="true" /}
    -
    -# Build Your Own Internet +
    +# Making Tessel An Access Point
    -
    +
    -As explained in [wifi tutorial](/wifi.html), Tessel 2 can easily connect to wifi networks using the command line tool. If we're using a device connected to the same network as the Tessel, then we can connect to it through our device's web browser. Like most network-connected devices, communication over the web depends on a shared router, or access point, to direct requests and responses between every device on the network or through the Internet. This is fine and dandy if we want to use Tessel in a place with an available network, but what happens when that's not possible? +A typical WiFi setup consists of a couple of devices: a modem, which is connected by wire to the internet, and a wireless access point (also called a WAP or router), wired to the modem, which directs requests and responses between every device on the network. + +As explained in [wifi tutorial](/wifi.html), Tessel 2 can easily connect to wifi networks using the command line tool. If your computer is connected to the same WAP as the Tessel, then the two devices can talk to each other. This is how we push code to Tessel over LAN (local area network). + +The connection setup looks about like this:
    -
    +
    ![router](./images/router.png)
    -
    -Instead of requiring a shared network through a standalone router, we can use the command line tool for configuring Tessel to emit a custom Wifi network and use it as an access point. Then, using Node, we can also start a web server for communicating with any connected device. Unfortunately, we won't be able to connect to the Internet but there is still a lot we can do without that. By the end of this article, we'll be able to control the Tessel LEDs through a web app served by the Tessel. +
    +This is fine and dandy if we want to use Tessel in a place where there's a modem and WAP already set up, but what happens when that's not possible? + +We can't access the Internet without a modem, but we can create a LAN connection between devices by turning Tessel 2 into a WAP. Devices on the network can all talk to each other, like this: +
    -
    +
    ![router](./images/Tessel-AP.png)
    +
    +This tutorial shows you to turn Tessel into a wireless access point, thus creating your own local network. +
    -
    +

    -In your command line, make a folder for your Tessel code, then initialize a Tessel project in that folder by running each of the following commands in the terminal: - -`mkdir tessel-router` - -`cd tessel-router` - -`t2 init` - -Rename the “index.js” file you’ve just created to “ap.js”, then copy and paste the below script over the existing text: - -{% highlight javascript %} -// Import the interface to Tessel hardware -var tessel = require('tessel'); -// Load the http module to create an http server. -var http = require('http'); - -// Configure our HTTP server to respond with "Hello from Tessel!" to all requests. -var server = http.createServer(function (request, response) { - response.writeHead(200, {"Content-Type": "text/plain"}); - response.end("Hello from Tessel!\n"); -}); - -// Listen on port 8080, IP defaults to 192.168.1.101. Also accessible through [tessel-name].local -server.listen(8080); - -// Put a friendly message on the terminal -console.log("Server running at http://192.168.1.101:8080/"); - -{% endhighlight %} Now that our server is in place, let's get our access point set up. In the terminal, run the following command: -`t2 ap -n TesselRouter` - -This will make an open, or password-less, wifi network called TesselRouter. If you open the wifi setting of your computer or a separate device like a smartphone or tablet, and scan for new devices, you should be able to see and connect to this new network. For info about making a secure network, take a look at [the cli docs](https://tessel.io/docs/cli#usage). After connecting to TesselRouter, run the following command in your terminal: - -`t2 run ap.js` - -
    -Once you see "Server running at http://192.168.1.101:8080/" in the terminal, we can connect to that URL (or http://tessel.local:8080/, replacing "tessel" with the name of your Tessel) in a web browser of the device connected to the TesselRouter network. -
    - -
    -![web app screenshot](./images/hello-tessel.png) -
    - -Now let's take it up a notch by adding some interactivity between the web page and the Tessel! - -
    - -Let's start this next part by building out the web page we want Tessel to send to your web browser. Create a file called `index.html` in your project directory and open it up in your preferred text editor to add the initial html for our web page: - -{% highlight html %} - - - - - Tessel Web App - - -

    Hello from Tessel!

    -

    Press a button to toggle an LED.

    -
      -
    • - - Status: -
    • -
    • - - Status: -
    • -
    - - -{% endhighlight %} - -
    -In that html, we have a heading, a line of instructions for the user, and a list of buttons for controlling a corresponding LED on the Tessel. Below that list of buttons we'll add some JavaScript for communicating with the Tessel: -
    - -
    -![web app screenshot](./images/ap-web-app-preview.png) -
    - -We will add the following JavaScript before `` tag: - -{% highlight html %} - -{% endhighlight %} - -Now let's check out `ap.js` again to finish up the project. First, we're going to make a few changes to our server setup: - -{% highlight javascript %} -// These two dependencies remain the same -var tessel = require('tessel'); -var http = require('http'); - -// Require two other core Node.js modules -var fs = require('fs'); -var url = require('url'); - -var server = http.createServer(function (request, response) { - // Here is the biggest change - var urlParts = url.parse(request.url, true); // Break up the url into easier-to-use parts - var ledRegex = /leds/; // Create a regular expression to match requests to toggle LEDs - - if (urlParts.pathname.match(ledRegex)) { - // If there is a request containing the string 'leds' call a function, toggleLED - toggleLED(urlParts.pathname, request, response); - } else { - // All other request will call a function, showIndex - showIndex(urlParts.pathname, request, response); - } -}); - -// Stays the same -server.listen(8080); +`t2 ap -n MyTessel` -// Stays the same -console.log('Server running at http://192.168.1.101:8080/'); -{% endhighlight %} - -Inside our `createServer` callback function, we've replaced the old plain text response with a basic router that catches requests to '/leds/:index' and defaults all other requests to a separate function. Let's add those functions below the `console.log`: - -{% highlight javascript %} -// Respond to the request with our index.html page -function showIndex (url, request, response) { - response.writeHead(200, {"Content-Type": "text/html"}); - // Use fs to read in index.html - fs.readFile(__dirname + '/index.html', function (err, content) { - if (err) { - throw err; - } - - // Serve the content of index.html read in by fs - response.end(content); - }); -} - -// Toggle the led specified in the url and respond with its state -function toggleLED (url, request, response) { - var indexRegex = /(\d)$/; // Create a regular expression to find the number at the end of the url - var result = indexRegex.exec(url); // Capture the number, returns an array - var index = +result[1]; // Grab the captured result from the array and make sure it's a Number - - var led = tessel.led[index]; // Use the index to refence the correct LED - - // Toggle the state of the led and call the callback after that's done - led.toggle(function (err) { - if (err) { - // Log the error and send back a 500 (internal server error) response to the client - console.log(err); - response.writeHead(500, {"Content-Type": "application/json"}); - response.end(JSON.stringify({error: err})); - } else { - // The led was successfully toggled, so respond with the state of the toggled led using led.isOn - response.writeHead(200, {"Content-Type": "application/json"}); - response.end(JSON.stringify({on: led.isOn})); - } - }); -} -{% endhighlight %} - -One last step is to create a `.tesselinclude` file in the project directory and add any files not required by `ap.js` that we want to deploy with it. For this project, that means adding one line: - -``` -index.html -``` - -Finally, let's fire up our server again by running: - -`t2 run ap.js` - -
    -Just like before, once you see the "Server running at http://192.168.1.101:8080/" message in your terminal, you should be able to connect to that url in the web browser of the device connected to the TesselRouter network. After connecting, you should see the index.html view we built earlier and, after clicking/tapping the buttons, see either the blue or green LEDs on the Tessel should be toggled on or off. -
    - -
    - -
    +This will make an open, or password-less, wifi network called MyTessel. If you open the wifi setting of your computer or a separate device like a smartphone or tablet, and scan for new devices, you should be able to see and connect to this new network. For info about making a secure network, take a look at [the cli docs](https://tessel.io/docs/cli#usage).
    -**Bonus:** Add a way to toggle the red LED as well. +**Bonus:** Create an network that requires a password.
    @@ -247,10 +51,10 @@ Just like before, once you see the "Server running at http://192.168.1.101:8080/ diff --git a/blinky.md b/blinky.md index 9d499cf..67da49b 100644 --- a/blinky.md +++ b/blinky.md @@ -107,7 +107,7 @@ Learn more `t2` commands by running `t2 -h` or looking at the [T2 CLI docs](http
    diff --git a/gpio.md b/gpio.md index a3cfd93..47675df 100644 --- a/gpio.md +++ b/gpio.md @@ -66,7 +66,7 @@ Share what you've made, and if you need help, [just ask](https://forums.tessel.i
    diff --git a/images/Tessel-AP.png b/images/Tessel-AP.png index dcec8fb..13ed35e 100644 Binary files a/images/Tessel-AP.png and b/images/Tessel-AP.png differ diff --git a/images/router.png b/images/router.png index 7b6bd21..4b6ec0b 100644 Binary files a/images/router.png and b/images/router.png differ diff --git a/tweet.md b/tweet.md index 6b005b2..0b7d442 100644 --- a/tweet.md +++ b/tweet.md @@ -93,7 +93,7 @@ _That `--slim` command is a new feature we're testing out which cleverly checks Modules
    diff --git a/webserver.html b/webserver.html new file mode 100644 index 0000000..c09cb52 --- /dev/null +++ b/webserver.html @@ -0,0 +1,9 @@ +--- +layout: default +--- + +{% capture include_install %} +{% include_relative webserver.md %} +{% endcapture %} +{{ include_install | markdownify }} + diff --git a/webserver.md b/webserver.md new file mode 100644 index 0000000..a12153b --- /dev/null +++ b/webserver.md @@ -0,0 +1,250 @@ +{::options parse_block_html="true" /} + +
    +
    +# Build Your Own Internet +
    +
    + +NEW INTRO NEEDED + +
    +
    +By the end of this article, we'll be able to control the Tessel LEDs through a web app served by the Tessel. +
    +
    + +
    +
    + +
    +In your command line, make a folder for your Tessel code, then initialize a Tessel project in that folder by running each of the following commands in the terminal: + +`mkdir tessel-router` + +`cd tessel-router` + +`t2 init` + +Rename the “index.js” file you’ve just created to “ap.js”, then copy and paste the below script over the existing text: + +{% highlight javascript %} +// Import the interface to Tessel hardware +var tessel = require('tessel'); +// Load the http module to create an http server. +var http = require('http'); + +// Configure our HTTP server to respond with "Hello from Tessel!" to all requests. +var server = http.createServer(function (request, response) { + response.writeHead(200, {"Content-Type": "text/plain"}); + response.end("Hello from Tessel!\n"); +}); + +// Listen on port 8080, IP defaults to 192.168.1.101. Also accessible through [tessel-name].local +server.listen(8080); + +// Put a friendly message on the terminal +console.log("Server running at http://192.168.1.101:8080/"); + +{% endhighlight %} + +Now that our server is in place, let's get our access point set up. In the terminal, run the following command: + +`t2 ap -n TesselRouter` + +If you haven't read about creating access points yet, check out the [access point tutorial](/ap.html). After connecting to TesselRouter, run the following command in your terminal: + +`t2 run ap.js` + +
    +Once you see "Server running at http://192.168.1.101:8080/" in the terminal, we can connect to that URL (or http://tessel.local:8080/, replacing "tessel" with the name of your Tessel) in a web browser of the device connected to the TesselRouter network. +
    + +
    +![web app screenshot](./images/hello-tessel.png) +
    + +Now let's take it up a notch by adding some interactivity between the web page and the Tessel! + +
    + +Let's start this next part by building out the web page we want Tessel to send to your web browser. Create a file called `index.html` in your project directory and open it up in your preferred text editor to add the initial html for our web page: + +{% highlight html %} + + + + + Tessel Web App + + +

    Hello from Tessel!

    +

    Press a button to toggle an LED.

    +
      +
    • + + Status: +
    • +
    • + + Status: +
    • +
    + + +{% endhighlight %} + +
    +In that html, we have a heading, a line of instructions for the user, and a list of buttons for controlling a corresponding LED on the Tessel. Below that list of buttons we'll add some JavaScript for communicating with the Tessel: +
    + +
    +![web app screenshot](./images/ap-web-app-preview.png) +
    + +We will add the following JavaScript before `` tag: + +{% highlight html %} + +{% endhighlight %} + +Now let's check out `ap.js` again to finish up the project. First, we're going to make a few changes to our server setup: + +{% highlight javascript %} +// These two dependencies remain the same +var tessel = require('tessel'); +var http = require('http'); + +// Require two other core Node.js modules +var fs = require('fs'); +var url = require('url'); + +var server = http.createServer(function (request, response) { + // Here is the biggest change + var urlParts = url.parse(request.url, true); // Break up the url into easier-to-use parts + var ledRegex = /leds/; // Create a regular expression to match requests to toggle LEDs + + if (urlParts.pathname.match(ledRegex)) { + // If there is a request containing the string 'leds' call a function, toggleLED + toggleLED(urlParts.pathname, request, response); + } else { + // All other request will call a function, showIndex + showIndex(urlParts.pathname, request, response); + } +}); + +// Stays the same +server.listen(8080); + +// Stays the same +console.log('Server running at http://192.168.1.101:8080/'); +{% endhighlight %} + +Inside our `createServer` callback function, we've replaced the old plain text response with a basic router that catches requests to '/leds/:index' and defaults all other requests to a separate function. Let's add those functions below the `console.log`: + +{% highlight javascript %} +// Respond to the request with our index.html page +function showIndex (url, request, response) { + response.writeHead(200, {"Content-Type": "text/html"}); + // Use fs to read in index.html + fs.readFile(__dirname + '/index.html', function (err, content) { + if (err) { + throw err; + } + + // Serve the content of index.html read in by fs + response.end(content); + }); +} + +// Toggle the led specified in the url and respond with its state +function toggleLED (url, request, response) { + var indexRegex = /(\d)$/; // Create a regular expression to find the number at the end of the url + var result = indexRegex.exec(url); // Capture the number, returns an array + var index = +result[1]; // Grab the captured result from the array and make sure it's a Number + + var led = tessel.led[index]; // Use the index to refence the correct LED + + // Toggle the state of the led and call the callback after that's done + led.toggle(function (err) { + if (err) { + // Log the error and send back a 500 (internal server error) response to the client + console.log(err); + response.writeHead(500, {"Content-Type": "application/json"}); + response.end(JSON.stringify({error: err})); + } else { + // The led was successfully toggled, so respond with the state of the toggled led using led.isOn + response.writeHead(200, {"Content-Type": "application/json"}); + response.end(JSON.stringify({on: led.isOn})); + } + }); +} +{% endhighlight %} + +One last step is to create a `.tesselinclude` file in the project directory and add any files not required by `ap.js` that we want to deploy with it. For this project, that means adding one line: + +``` +index.html +``` + +Finally, let's fire up our server again by running: + +`t2 run ap.js` + +
    +Just like before, once you see the "Server running at http://192.168.1.101:8080/" message in your terminal, you should be able to connect to that url in the web browser of the device connected to the TesselRouter network. After connecting, you should see the index.html view we built earlier and, after clicking/tapping the buttons, see either the blue or green LEDs on the Tessel should be toggled on or off. +
    + +
    + +
    + +
    + +**Bonus:** Add a way to toggle the red LED as well. +
    +
    + +
    + + diff --git a/wifi.md b/wifi.md index e4a8237..9b316c3 100644 --- a/wifi.md +++ b/wifi.md @@ -89,6 +89,6 @@ This command will check for a new version of the Tessel 2 firmware. If there is