Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

366
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda