Can someone just help me understand!

Posted on
  • So currently I am in a full stack webdev bootcamp and I need to make a project using the mern stack, mongodb, express, react, nodejs.. I just need guidance on how to structure my esp2866.. I have my sensor hooked up already and its running, but for the life of me, I can't figure out how to send that data to my client side. Any info would be amazing! Thanks all

  • You can use a simple http post to send data to your backend. And from that point it's just like any data, you can do anything. Store it, send it to the client...
    Or you could use websockets maybe MQTT.

  • Awesome, thanks for the response, I guess my next question would be, do I have to setup a server on the esp?

  • It depends on how you want to set it up.

    You mentioned that you want to "send data" from the esp, so that means the esp would be a client that would periodically send data to your server. Alternatively you can "ask for data" from the esp, in which case the esp would be a server waiting for a request from a client.

    Do you have a drawing or an image of how you're thinking of setting things up?

  • Aww okay, yes so i would want it to periodically send data to my server... I feel so lost... Okay, so I am running an express server on my computer localhost:3000... I cant figure out how to fetch the data off the esp8266 or like you said, have the esp8266 send the data to my server... I know it is probably so simple to do but I cant put 2 and 2 together for some reason.. I was using a third party library Axios to try to make a call to the esp but can't get that to work. .. Am I on the right path at least? Thanks

  • const wifi = require("Wifi");
    
    const ssid = "";
    const password = "";
    
    
    wifi.connect(ssid, {password:password}, function(e) {
      if (e) {
        console.log('error during connect:',e);
        wifi.disconnect();
      } else {
        console.log('connected to',ssid);
        require("Wifi").getIP((err, ipinfo) => {console.log(ipinfo)});
        wifi.stopAP();
        //wifi.save();
      }
    });
    
    
    
    const sensor = require("HC-SR04").connect(NodeMCU.D1,No­deMCU.D2,function(dist) {
          const inches = dist * 0.393701;
          n = inches.toFixed(2);
          console.log(n);
    
         
    setInterval(function() {
            sensor.trigger(); // send pulse
    }, 10000);
    
    
    function postMe() {
      content = "Hello";
      var options = {
        host: 'whatdoIputhereifIamrunningalocalserverw­ithexpress',
        port: '80',
        path:'/',
        method:'POST',
        headers: {
          "Content-Type":"application/json",
          "Content-Length":content.length
        }
      };
      require("http").request(options, function(res)  {
        // ...
      }).end(content);
    }
    

    This is pretty much as far As I've gotten with my code.. I tried to do a test post with no luck so far

About

Can someone just help me understand!

Posted by Avatar for user117414 @user117414

Actions