Need help getting Blynk to stay running

I apologize if this is the wrong area or if my question isn’t worded well. I’m new to pretty much all of this. I’m using a Particle Photon and Blynk on iOS (latest version). I’ve gotten a sample code for a temperature/humidity sensor that logs that data to ThingSpeak. I’m trying to also get it to report to Blynk. I’ve added the code I think is required. However, the data is only getting reported once - when I flash it. After that, the temperature and humidity don’t update on Blynk. It also looks like Blynk periodically disconnects and reconnects but doesn’t update the temperature or humidity.

I’ve included the relevant portion of the code below:


void setup() {
    pinMode(LEDPIN, OUTPUT);
    digitalWrite(LEDPIN, HIGH);

    Particle.variable("temperature", &temperatureString[0], STRING);
    Particle.variable("humidity", &humidityString[0], STRING);
    Particle.variable("heatIndex", &heatIndexString[0], STRING);
    Particle.variable("status", &failed, INT);

    dht.begin();
    Blynk.begin(auth);
    ThingSpeak.begin(tcpClient);
    // run the first measurement
    loop();
}

void loop() {
    int now = Time.now();

    if (now - lastUpdate < SEND_INTERVAL) {
        return;
    }

    // turn on LED when updating
    digitalWrite(LEDPIN, HIGH);

    lastUpdate = now;

    failed = 0;

    // read humidity and temperature values  
    humidity = dht.readHumidity();
    temperature = dht.readTemperature(USE_FARENHEIT);

    if (temperature == NAN
        || humidity == NAN
        || temperature > MAX_TEMPERATURE
        || temperature < MIN_TEMPERATURE
        || humidity > MAX_HUMIDITY
        || humidity < MIN_HUMIDITY) {
        // if any sensor failed, bail on updates
        failed = 1;
    } else {
        failed = 0;

        // calculate the heat index
        heatIndex = dht.computeHeatIndex(temperature, humidity, USE_FARENHEIT);

        // convert floats to strings for Particle variables
        sprintf(temperatureString, "%.2f", temperature);
        sprintf(humidityString, "%.2f", humidity);
        sprintf(heatIndexString, "%.2f", heatIndex);

        // set all 3 fields first
        ThingSpeak.setField(1, temperatureString);
        ThingSpeak.setField(2, humidityString);
        ThingSpeak.setField(3, heatIndexString);

        // send all fields at once
        ThingSpeak.writeFields(THINGSPEAK_CHANNEL, THINGSPEAK_API_KEY);
        
        Blynk.run();

        Blynk.virtualWrite(V0, temperatureString);

        Blynk.virtualWrite(V1, humidityString);

	Particle.publish("The temperature is:", String(temperatureString) + "°F");
	delay(1000);
	Particle.publish("The humidity is:", String(humidityString) + "%");
	delay(1000);
	}

Move all that stuff out of loop() and look for code using the particle timer.

Blynk 101… DO NOT run all your code in the void loop()… use timers to periodically poll the sensors and send the data to the App.

Opps, need to refresh my browser… i didn’t see that @Costas had already answered :stuck_out_tongue:

@Gunner @Costas Thank you both! I knew it was something stupid I was doing. I’ve modified my code and it seems to be working. Does this look like a better way to manage this?

void sendSensor()
{
	Blynk.virtualWrite(V0, temperatureString);
	Blynk.virtualWrite(V1, humidityString);
}

void setup() {
    pinMode(LEDPIN, OUTPUT);
    digitalWrite(LEDPIN, HIGH);

    // configure Particle variables - float isn't accepted, so we have to use string versions
    Particle.variable("temperature", &temperatureString[0], STRING);
    Particle.variable("humidity", &humidityString[0], STRING);
    Particle.variable("heatIndex", &heatIndexString[0], STRING);
    Particle.variable("status", &failed, INT);

    // start the DHT sensor
    dht.begin();
	// start Blynk
    Blynk.begin(auth);
	timer.setInterval(1000L, sendSensor);
    // start ThingSpeak
    ThingSpeak.begin(tcpClient);

    // run the first measurement
    loop();
}

void loop() {
    int now = Time.now();

    // only run every SEND_INTERVAL seconds
    if (now - lastUpdate < SEND_INTERVAL) {
        return;
    }

    // turn on LED when updating
    digitalWrite(LEDPIN, HIGH);

    lastUpdate = now;

    failed = 0;

    // read humidity and temperature values  
    humidity = dht.readHumidity();
    temperature = dht.readTemperature(USE_FARENHEIT);

    if (temperature == NAN
        || humidity == NAN
        || temperature > MAX_TEMPERATURE
        || temperature < MIN_TEMPERATURE
        || humidity > MAX_HUMIDITY
        || humidity < MIN_HUMIDITY) {
        // if any sensor failed, bail on updates
        failed = 1;
    } else {
        failed = 0;

        // calculate the heat index
        heatIndex = dht.computeHeatIndex(temperature, humidity, USE_FARENHEIT);

        // convert floats to strings for Particle variables
        sprintf(temperatureString, "%.2f", temperature);
        sprintf(humidityString, "%.2f", humidity);
        sprintf(heatIndexString, "%.2f", heatIndex);

        // set all 3 fields first
        ThingSpeak.setField(1, temperatureString);
        ThingSpeak.setField(2, humidityString);
        ThingSpeak.setField(3, heatIndexString);

        // send all fields at once
        ThingSpeak.writeFields(THINGSPEAK_CHANNEL, THINGSPEAK_API_KEY);
        
        Blynk.run();
		timer.run();
 
		Particle.publish("The temperature is:", String(temperatureString) + "°F");
		delay(1000);
		Particle.publish("The humidity is:", String(humidityString) + "%");
		delay(1000);
	}

    // done updating
    digitalWrite(LEDPIN, LOW);
}

No.