Developing Apps on Alibaba Cloud IoT Platform with Raspberry Pi and Node.js

npm install rpio2 –production
const Gpio = require('/home/pi/node_modules/rpio2/lib/index.js'). Gpio;
var led = new Gpio(7); //Create Pin7 pin
led.open(Gpio.OUTPUT, Gpio.LOW); //Set to OUTPUT, which defaults to low level
for(var i = 0; i < 20; i++){
led.toggle(); //Switch the level status of the LED
led.sleep(300); //Wait for 500 ms
}
led.close();
  1. First, run: sudo chmod -R 777 /var/root
  2. Then, run: sudo npm install -g --unsafe-perm node-dht-sensor. This installs it correctly, as shown in the following figure:
var sensorLib = require('node-dht-sensor');
// Setup sensor, exit if failed
var sensorType = 11; // 11 for DHT11, 22 for DHT22 and AM2302
var sensorPin = 16; // The GPIO pin number for sensor signal
if (! sensorLib.initialize(sensorType, sensorPin)) {
console.warn('Failed to initialize sensor');
process.exit(1);
}
// Automatically update sensor value every 2 seconds
setInterval(function() {
var readout = sensorLib.read();
console.log('Temperature:', readout.temperature.toFixed(1) + 'C');
console.log('Humidity: ', readout.humidity.toFixed(1) + '%');
}, 2000);
client.on('message', function(topic, message) {
var json = JSON.parse(message.toString());
console.log("LED="+ json.params.LED.toString());
led.write( json.params.LED);
});
function getPostData(){ 
var readout = sensorLib.read();
const payloadJson = {
id: Date.now(),
params: {
temperature: readout.temperature.toFixed(1),
humidity: readout.humidity.toFixed(1)
},
method: "thing.event.property.post"
}

--

--

--

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The History of React ()

AutoComplete Component in Salesforce using JQuery

Top 10 Important Things In React That Every React Developer Should know

How to make global state management super simple in React with hooks

Authentication with Azure AD and React

Make YouTube useful with Chrome extension

CRUD via JSON — Enjoy Fullstack Development Like a Piece of Cake

Making API Requests with NodeJS

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com

More from Medium

Apache Reverse Proxy For Multiple Back end Servers.

Simple CI CD with GCP Cloud Build using Docker- part 2

Install Postgresql 13 on RHEL 8. Configure scram-sha-256 password authentication.

ElasticSearch with docker in macbook M1.