Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

365
Views
How can different data is transferred from the server?

client is esp32cam(camera module) It is send Image to app.js every 3seconds

app.js

client.on("message", async(topic, message)=>{ // {"LED" : "ON"} or {"MOTER" : "ON"}
    if(topic == "JPG")
    {
        var obj = message.toString();
        app.get("/img", function(req,res,next){
            res.set("content-Type", "text/json");
            //res.send(JSON.stringify({data : obj}));
            res.json({
                data: obj || "no image yet"
            });
            console.log(obj);//number 1
        });
        console.log(obj);//number 2
    }
}

number 1 is print same data every 3seconds

number 2 is print different data every 3seconds

how can number1 print different data each time??

html

$(function() {
            timer = setInterval( function () {
            console.log("timer")
            $.ajax({
                url: "http://ip:3000/img",
                type: "get",
                context : this,
                cache : false,
                error : function(request,status,error){
                    console.log("code:"+request.status+"\n"+"message:"+request.responseText+"\n"+"error:"+error);
                },
                success:function(obj){
                    $("#imguri").html('<img src="'+obj.data+'">');
                    $("#imguri").attr("src", obj.data);
                    console.log("mqtt in " + obj.data);
                    // console.log("TEST");

                }
            });
            }, 3000);
});

my problem enter image description here

enter image description here

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

This is because you are stacking multiple app.get("/img",...) routes (one for every incoming message).

Only the first one will ever be called, so the HTTP client will always just get the very first image.

You should make the MQTT client callback store the new image in a global variable and then just have one instance of the /img route return that global variable.

var img;
client.on("message", async(topic, message)=>{ // {"LED" : "ON"} or {"MOTER" : "ON"}
  if(topic == "JPG")
  {
    var obj = message.toString();
    img = obj
  }
}

app.get("/img", function(req,res,next){
  res.set("content-Type", "text/json");
  res.json({
    data: img || "no image yet"
  });
  console.log(obj);//number 1
});
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!