how can i add app script mqtt in my app script, i'm new here so please help me

838 views
Skip to first unread message

Ayush Naphade

unread,
Sep 13, 2023, 2:56:14 PM9/13/23
to Google Apps Script Community
this is my code 
// Install the "Apps Script MQTT" library in the Apps Script editor.
// Follow these steps:
// 1. Click on the "+" icon in the left sidebar.
// 2. Search for "Apps Script MQTT" and click "Install."

// Use your MQTT broker information (e.g., test.mosquitto.org).
const MQTT_BROKER = "broker.example.com";
const MQTT_PORT = 1883;
const MQTT_USERNAME = "your_mqtt_username";
const MQTT_PASSWORD = "your_mqtt_password";

// Subscribe to this MQTT topic to listen for messages from ESP32.
const MQTT_SUBSCRIBE_TOPIC = "esp32/fingerid";

// Define the Google Sheets database and records sheet names.
const DATABASE_ID ="";
const DATABASE_SHEET_NAME = "Database";
const RECORDS_SHEET_NAME = "Records";

function doPost(e) {
  // Parse the incoming JSON data from ESP32.
  var postData = JSON.parse(e.postData.contents);
  var fingerid = postData.fingerid;
 
  // Retrieve the current status from the database.
  var database = SpreadsheetApp.openById(DATABASE_ID);
  var sheet = database.getSheetByName(DATABASE_SHEET_NAME);
  var data = sheet.getDataRange().getValues();
  var stat;
 
  for (var i = 0; i < data.length; i++) {
    if (data[i][0] == fingerid) {
      stat = data[i][4];
      break;
    }
  }

  // Toggle the status (IN to OUT or OUT to IN).
  stat = stat === "IN" ? "OUT" : "IN";
 
  // Update the status in the database.
  for (var i = 0; i < data.length; i++) {
    if (data[i][0] == fingerid) {
      data[i][4] = stat;
      break;
    }
  }
 
  sheet.getRange(1, 1, data.length, data[0].length).setValues(data);
 
  // Log the updated status.
  console.log("Updated status for fingerid " + fingerid + " to " + stat);
 
  // Publish the updated status back to ESP32 via MQTT.
  var mqttClient = new MqttClient(MQTT_BROKER, MQTT_PORT, MQTT_USERNAME, MQTT_PASSWORD);
  mqttClient.connect();
  mqttClient.publish(MQTT_SUBSCRIBE_TOPIC, JSON.stringify({ fingerid: fingerid, status: stat }));
  mqttClient.disconnect();
 
  return ContentService.createTextOutput("Updated status: " + stat);
}

Andrew Roberts

unread,
Oct 12, 2023, 11:57:01 AM10/12/23
to Google Apps Script Community
Could you share a link to the MQTT Apps Script library.

JiYung Lee

unread,
Oct 26, 2023, 3:57:53 AM10/26/23
to Google Apps Script Community
The content of the link below may be helpful. https://www.emqx.com/en/blog/mqtt-js-tutorial
tks.
Andrew Roberts 在 2023年10月12日 星期四晚上11:57:01 [UTC+8] 的信中寫道:

Kevin Chen aka Stonez56

unread,
Dec 1, 2023, 2:26:23 AM12/1/23
to Google Apps Script Community
Can any one help? I can't install  "Apps Script MQTT" library within the Script editor.


// Install the "Apps Script MQTT" library in the Apps Script editor.
// Follow these steps:
// 1. Click on the "+" icon in the left sidebar.
// 2. Search for "Apps Script MQTT" and click "Install."
Reply all
Reply to author
Forward
0 new messages