1

I have this in Google's App Engine (node.js). My device gets all the commands but I still get the Could not send command. Is the device connected? error.

BTW, already tried this: Await for function before end() And same result.

Trying to follow this example BTW:

https://cloud.google.com/nodejs/docs/reference/iot/0.2.x/v1.DeviceManagerClient#sendCommandToDevice

const express = require('express');
var bodyParser = require('body-parser');
const app = express();

var urlencodedParser = bodyParser.urlencoded({
extended: false
})

const iot = require('@google-cloud/iot');

app.get('/', urlencodedParser, (req, res) => {
    res.setHeader('Content-Type', 'application/json');
    const projectId = req.query.proyecto;
    const cloudRegion = req.query.region;
    const registryId = req.query.registro;
    const numSerie = req.query.numSerie;
    const command = req.query.command;

const client = new iot.v1.DeviceManagerClient();
if (client === undefined) {
    console.log('Did not instantiate client.');
} else {
    console.log('Did instantiate client.');
    sendCom();
}

async function sendCom() {
    const formattedName = client.devicePath(projectId, cloudRegion, registryId, numSerie)
    const binaryData = Buffer.from(command);
    const request = {
        name: formattedName,
        binaryData: binaryData,
    };
    return client.sendCommandToDevice(request).then(responses => res.status(200).end(JSON.stringify({
        data: OK
    }))).catch(err => res.status(404).end('Could not send command. Is the device connected?'));
}
});

const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`App listening on port ${PORT}`);
console.log('Press Ctrl+C to quit.');
});

module.exports = app;

On my end I should get status 200 and OK but it doesn't happen.

1x2x3x4x
  • 592
  • 8
  • 26
  • Could you please explain what are you trying to do more briefly? And what errors are you encountering ? – Nibrass H Apr 16 '20 at 10:03
  • @NibrassH trying to send a command to an IoT device through App Engine using node. – 1x2x3x4x Apr 16 '20 at 15:55
  • Thanks for your response. Could you please share the exact error what you are seeing in Stackdriver Logging? And please share also if you see any other errors before or after ? – Nibrass H Apr 17 '20 at 08:48
  • There's no error in Stackdriver Logging. The code is what you see in the main post - the device gets the command, however I get the 'Could not send command. Is the device connected' error. I'm starting to think there's something wrong in my code but I' cant see it for some reason. – 1x2x3x4x Apr 18 '20 at 07:42
  • I understand your concerns. Could you please try to follow this [Official Documentation](https://cloud.google.com/iot/docs/how-tos/commands#sending_a_command) to send a command to a device. Please let me know if you were able to send it correctly, this way I will narrow down the investigation to App Engine with Cloud Iot – Nibrass H Apr 20 '20 at 15:11

0 Answers0