Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

367
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda