Sending info to website

Posted on
  • Hello i was wondering if their is a way to send info to the web server. What im trying to do is have the pico somewhere else and be collecting the how bright the light is, but how to you send the info to the webpage.

    I know how to get info from the webpage though this code

    ------------Code---------------
    function pageHandler(req, res) {
      if (req.method=="POST") {
        // If it POST(Web side does this), save the data
        var info = url.parse(req.url,true);
        console.log("POST ",info);
        //Looks though HTML "pos" and collect the value
        if (info.query && "pos" in info.query)
          //Activats setPos
          setPos(parseFloat(info.query.pos));
        res.writeHead(200);
        res.end("Ok.");
      } else {
        // otherwise write the page out
        console.log("GET "+req.url);
        if (req.url=="/") {
          res.writeHead(200);
          res.end(page);
        } else {
          res.writeHead(404);
          res.end("404: Not found");
        }
      }
    }
    --------------------------------------en­d of code----------
    
  • Have you seen the examples of making a request here?

    http://www.espruino.com/Internet

    I always just use GET requests with URL parameters, personally. I'm not sure I have an example of making a POST request handy.

    Or is there something else I'm missing?

  • As @DrAzzy says, the most simple, hacky way to send data is (for example to send temperature):

    require("http").get("http://www.mysite.c­om?temp="+E.getTemperature(), function(res) {
      var contents = "";
      res.on('data', function(data) { contents += data; });
      res.on('close', function() { console.log("Done! Got "+contents); });
    });
    

    Ideally you would use HTTP POST to stop accidental puts of data by web crawlers, in which case you could do:

    function sendData() {
      var options = {
        host: 'mysite.com',
        port: '80',
        path:"pageName?temp="+E.getTemperature()­,
        method:'POST'
      };
      require("http").request(options, function(res)  {
        var d = "";
        res.on('data', function(data) { d+=data; });
        res.on('close', function(data) {
          console.log("Done! Got "+d);
        });
     }).end();
    }
    
  • Your initial post suggests that you are using the espruino as the web server?

    Will you have two boards- one collecting the light brightness and sending to another board?

  • @ert4, sending something to a Web server is not exactly the same as showing (data in) a Web page... latter is more like responding with a Web page on a browser request.

    Since you are talking of collecting, you need something on the Web server that is able to receive data and hold on to it, and something that then makes that data part of the returned Web page.

    What kind of Web server do you run and on what platform?

  • Im collecting the data, and using the pico as a sever.
    ---------------------Code
    // ESP8266 WiFi v25
    var WIFI_NAME = "Temp-Student";
    var WIFI_PASS = "TempStu1";
    Serial2.setup(115200, { rx: A3, tx : A2 });
    var wifi = require("ESP8266WiFi_0v25").connect(Seri­al2, function(err) {
    if (err) throw err;
    wifi.reset(function(err) {

    if (err) throw err;
    console.log("Connecting to WiFi");
    wifi.connect(WIFI_NAME, WIFI_PASS, function(err) {
      if (err) throw err;
      console.log("Connected");
      // print IP address
      wifi.getIP(console.log);
      // Create a server
      require("http").createServer(pageHandler­).listen(80);
    });
    

    });
    });
    --------------------end of code
    I want to have the light go on a graph.


    1 Attachment

  • You can't have the pico reach out to a webpage and change things on it without having stuff on the server side that hosts the page, or using websockets.

    Either:

    Have the page make a request which the Pico responds to with the current light - might be easiest?

    If that page is not hosted on the Pico, you could have the Pico making requests to another page on the server, while the page with the UI on it requests a different page from the server to get that information. (php and that crude key-value cache is enough to make this work - that's what I use for my web control panels)

    Use websockets

  • I like this tutorial.. http://www.espruino.com/Logging+to+Googl­e+Sheets
    You can log the data to a google docs spreadsheet, make a graph of the columns containing your data, then your graph updates in realtime as new data comes in!

  • Ahh - so you're trying to send data to the Pico? What are you using to send the data to the Pico?

    For graphing, you might find this tutorial helpful - it shows how to store data on an Espruino board, and then serve it up as a graph.

    edit: Also, I'm just moving this thread - this forum is generally for people who run Espruino on the ESP8266 itself (not a Pico connected to an ESP8266)....

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Sending info to website

Posted by Avatar for ert4 @ert4

Actions