You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Node.js获取指定ewelink设备的数据

我正在尝试使用eWeLink API,但完全是Node.js新手,目前只能通过复制、粘贴、反复试错的方式推进开发。
目前可参考的相关示例较少,我只能基于线上公开的基础脚本从零开始开发(该脚本明文展示凭证,大概率是误上传到线上的)。
我编写的完整脚本可实现成功登录、获取设备列表,但在获取指定设备的数据时运行失败。
我试过两种官方提供的登录方式,但不清楚实际应该使用哪一种。
我的设备均为出厂标准固件和配置,仅接入了我的WiFi网络,运行状态正常。
你可以使用自己的eWeLink凭证访问我的测试脚本:
https://3u6vx.sse.codesandbox.io/index.html?user=xxx&pass=yyy&bypass=false
也可以使用「默认公开」凭证访问:
https://3u6vx.sse.codesandbox.io/index.html?bypass=true

完整脚本代码

var http = require("http");
const ewelink = require("ewelink-api");
const url = require("url");
var devices;
var connection;
var newConnection;
var auth;
var onlineDevice;
//var device;

console.log("====================");
console.log(Date());
console.log("====================");

//create a server object:
http
  .createServer(
    function (req, res) {
      res.write(timestampLC() + " - Ewelink - " + Date() + "\n");
      const queryObject = url.parse(req.url, true).query;
      console.log(timestampLC() + " - Executing function inside Server...");
      eweRetrieve(res, queryObject);
    } // function in createserver
  ) // createserver
  .listen(8080); //the server object listens on port 8080

function printOfflineDevice(res, device) {
  res.write(timestampLC() + " -     Name: " + device.name + "\n");
  res.write(timestampLC() + " -     Type: " + device.type + "\n");
  res.write(timestampLC() + " -     Brand name:" + device.brandName + "\n");
  res.write(timestampLC() + " -     Model: " + device.productModel + "\n");
  res.write(timestampLC() + " -     Url: " + device.deviceUrl + "\n");
}

////////////////////

async function eweRetrieve(res, queryObject) {
  res.write(timestampLC() + " - Attempting connection of user ");
  console.log(timestampLC() + " - Attempting user connection...");
  if (queryObject.bypass === true) {
    res.write("tugsbayar.g@gmail.com'\n");
    try {
      res.write(timestampLC() + " - Trying...\n");
      connection = new ewelink({
        email: "tugsbayar.g@gmail.com",
        password: "mdk06tgs6",
        region: "as"
      });
      res.write(timestampLC() + " - Ewelink - user DEBUG connected\n");
    } catch {
      res.write(
        timestampLC() + " - Fail 001 - no connection available for DEBUG user\n"
      );
      return 1;
    }
  } else {
    res.write("'" + queryObject.user + "'\n");
    try {
      res.write(timestampLC() + " - Trying...\n");
      connection = new ewelink({
        email: queryObject.user,
        password: queryObject.pass
        //region: "it"
      });

      res.write(
        timestampLC() +
          " - >>> Initial login SUCCESS: user " +
          connection.email +
          " connected\n"
      );
      res.write(timestampLC() + " - ID: " + connection.APP_ID + "\n");
      res.write(timestampLC() + " - SEC: " + connection.APP_SECRET + "\n");
      //console.log(timestampLC() + "*** connection data ****", connection);
    } catch {
      res.write(
        timestampLC() + " - Fail 002 - no connecion available for real user\n"
      );
      return 2;
    }

    //credentialsLogin(res, connection);
    try {
      console.log(timestampLC() +" - Credentials...\n");
      res.write(timestampLC() +" - Credentials...");
      auth = await connection.getCredentials();
      res.write("SUCCESS! <<<<<<<<<<<<\n");
      console.log("access token: ", auth.at);
      console.log("api key: ", auth.user.apikey);
      console.log("region: ", auth.region);  
    } catch {
      res.write(timestampLC() + "***ERROR*** - cannot login\n");
      console.log(timestampLC() + "***ERROR*** - cannot login\n");
    }
  }

  //listAllDevices(res, connection);
 /* get all devices */
 try {
  res.write(timestampLC() + " - Connected. Reading devices list...\n");
  devices = await connection.getDevices();
} catch {
  res.write(timestampLC() + " - Fail 003 - no devices\n");
  console.log(timestampLC() + " - No devices found.");
  return 3;
}
//console.log("=== Begin devices list ===");
//console.log(devices);
//console.log("=== End devices list ===");

res.write(timestampLC() + " - Found n." + devices.length + " devices\n\n");
console.log(timestampLC() + " - Found n. " + devices.length + " devices\n\n");
console.log(timestampLC() + " - Reading info...");



  /* get specific device info */
  for (var x = 0; x < devices.length; x++) {
    res.write(timestampLC() + " - Device n. " + (x + 1) + "\n");
    var element = devices[x];
    res.write(timestampLC() + " -     Id: " + element.deviceid + "\n");
    res.write(timestampLC() + " -     Online: " + element.online + "\n");
    //    console.log(element);
    if (element.online === true) {
      res.write(timestampLC() + " -     Device '" +  
      element.name + 
      "' is online, fetching data...\n" );
      try {
        console.log("CREDENTIALS:\n", auth.at);        
        console.log(auth.region);        
        newConnection = new ewelink({
          at: auth.at,
          region: auth.region
        });
        console.log(timestampLC() + ">>>>>>>>>>>>New connection ok! >>>>>>>>>>>>> Scarico dati online di", element.name , "\n")

      } catch {
        res.write(timestampLC() + " -     *****ERROR: cannot read*****\n");
        console.log(
          timestampLC() +
            "!!!!!!!!!!!!! NON RIUSCITO per " +
            element.name +
            " !!!!!!!!!!!!!!!"
        );
      }

      try {
        console.log("provo.");
        /*var deviceConnection = new ewelink({
          at: auth.at,
          region: auth.region
        });        */
        onlineDevice =  await connection.getDevice(element.id);
        res.write(timestampLC() + " -     DATA:\n");
        console.log(timestampLC() + "***********************\n", onlineDevice , "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
      } catch {
        res.write(timestampLC() + " -     ***** ONLINE ERROR: cannot retrieve *****\n");
        console.log(
          timestampLC() +
            "!!!!!!!!!!!!! Impossibile scaricare dati per " +
            element.name +
            " !!!!!!!!!!!!!!!"
        );      
      }

      //printDevice(res, device);
    } else {
      printOfflineDevice(res, element);
    }
    res.write(timestampLC() + " - ------------\n\n");
  }

  /* toggle device */
  //await connection.toggleDevice('1000aea4dd');
  res.end(); //end the response
}

eweRetrieve().catch(function () {
  console.log(timestampLC() + " - retrieve failed");
});

function timestampLC() {
  let d = new Date();
  return d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
}

内容的提问来源于stack exchange,提问作者jumpjack

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.14 11:00:11