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

JavaScript Objects

Introduction to Java Servlets & JSP

Architecting product tours in React

It’s time for the compatibility opt-in preview of Ivy!

AEM Blank Slate Starter Site

How to Create an Angular 2.0 Application in Liferay DXP to Fetch the OOTB Advantages?

Continuous Integration and Deployment on Kubernetes with Azure Devops

Setting up node server- Without Express JS

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

Using HighCharts Export Server

Colima — Docker & K8S for local development

Configure your NodeJS Application with IBM Cloud App Configuration using nconf-appconfig

OpenAPI Tools by OxygenXML