Automated Data Sending to AskSensors Using Node.js
In a previous tutorial, I showed how to send data to AskSensors IoT platform using the web browser. It was helpful especially for beginners and people don't have material in hand or don't work with electronics!
This made me thinking more about providing you different ways to communicate with AskSensors without using electronics. This instructable explains step-by-step how to conduct automated data sending to AskSensors using Node.js over HTTPS GET Requests.
Note for guys not familiar with Node.js:
- We will type command-line instructions, so you need to be comfortable with or at least know how to start a command-line tool like the Windows Command Prompt, or the Git shell.
- Npm makes it easy for JavaScript developers to share and reuse code, and makes it easy to update the code that you’re sharing. Later we will need npm to install an HTTPS package for HTTP secure communications with AskSensors.
So let's do it!
Teacher Notes
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: Install Node.js and NPM
- First thing’s first: You need to download Node.js. It’s free and available for Windows, MacOs and Linux (in my case I'm using Windows).
- Run the installer, follow the prompts in the installer. Accept the license agreement, click the NEXT button a bunch of times and accept the default installation settings.
- NPM is distributed with Node.js, which means that when you download Node.js, you automatically get npm installed on your computer.
- Restart your computer. You will not be able to run Node.js until you restart your computer!
Step 2: Test the Node.js
To Make sure you have Node.js and NPM installed, we will run simple commands to see what version of each is installed and to run a simple test program:
- Test Node.js: To confirm that you have Node.js installed, type this command in your terminal:
node -v
This should print a version number.
- Test NPM: To check if you have NPM installed, you can run this command in your terminal:
npm -v
- Create a test file and run it: A simple way to test that Node.js works is to create a JavaScript file: for example name it ask.js, and add the code below:
console.log('Hello AskSensors!');
- Run the code: open your command line program, navigate to the folder where you save the file and type:
node ask.js
You should see " Hello AskSensors!", means all fine!
Now, you have the Node.js and NPM ready. By next we will create an AskSensors account..
Step 3: Create a Free AskSensors Account
Create an AskSensors account for free at https://askSensors.com.
You may need to refer to these tutorials to get started with AskSensors IoT platform:
Do you feel familiar with AskSensors now? if yes, let's continue..
Step 4: Create a Sensor for Node.js Data
This video shows how to create a new sensor, add module, and get the sensor informations (Api Key In, ..).
Once you have your sensor and module set, copy down your Api Key In, we will need it for the next step.
Step 5: The Code
- Download this example code from the AskSensors github page. It provides you a simple and already tested code to start with.
- You need to install the https NPM package.
- Put your Api Key In generated for your sensor.
- This code will be sending data every 20 seconds, You can modify this by setting the timerInterval value.
var ApiKeyIn = '................'; // Api Key In var timerInterval = 20000; // timer interval
Step 6: Result
- run the JavaScript file
node https_GET.js<br>
- Sign in to Asksensors, open your Sensor and show your module graph.
- You should see your graph plotting all the data sent from your Node.js.
That's it!
No hay comentarios:
Publicar un comentario