Search code examples
javascriptfirebase-realtime-databasearduinohttpwebresponsenodemcu

Running JavaScript with a web request?


BACK STORY : Let me come from my problem, I need to update firebase database with Arduino so I used firebase-Arduino library but for some reason it will not compile Node MCU so my next way is a bit complicated that is I created a java script to update the firebase I just need to add 1 to the database so I don't need to update sensor value or anything so if I load the webpage it will update the value ,I thought it will be triggered with http request from Arduino but I was wrong it does not work like that.

QUESTION : How to run the JavaScript in a webpage with a web request from Arduino?


Solution

  • Assuming you have node.js installed you can have something like this (source):

        const https = require('https');
    
    https.get('your_url_here', (resp) => {
      let data = '';
    
      // A chunk of data has been recieved.
      resp.on('data', (chunk) => {
        data += chunk;
      });
    
      // The whole response has been received. Print out the result.
      resp.on('end', () => {
        console.log(JSON.parse(data).explanation);
      });
    
    }).on("error", (err) => {
      console.log("Error: " + err.message);
    });
    

    But if you don't have installed node.js you might create the http request from bash commands like curl. This can be useful since you can make it run as daemon (run on th background every X minutes). Let me know if you managed, something good luck.