Thing Speak with Blynk e-mail and notification

Hello, guys, how is it going?

Well, i am working on a project where i need to send data to Thing Speak and receive an e-mail and notifiction from Blynk App. But i rechead a certain point that i need to put together two programas on the same sketch, like a frankenstein haha.

So, when i put both together, in the app continuous says that the device is conneted and disconneted.

On the other hand Thing Speak is working just fine.

I’m using a NodeMCU ESP-12E module with Arduino IDE
Blynk App 2.27.0

Observation: when i am using them separately it works like a charm!

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#define BLYNK_PRINT Serial


//int alarmPin = 4;
int led1 = 16;
int led2 = 14;//

String apiWritekey = "BX352PBP7BBBFYG4"; // replace with your THINGSPEAK WRITEAPI key here
char auth[] = "2126779495d94c48b7376ec1b15d38ec";
const char* ssid = "ARRIS"; // your wifi SSID name
const char* password = "biricotico29" ;// wifi pasword
 
const char* server = "api.thingspeak.com";

SimpleTimer timer;

WiFiClient client;

void sendSensor(){
 
  float t = analogRead(0);

 

  Serial.println(t);
 
  Blynk.virtualWrite(V6, t);

  // SETUP the ALARM Trigger and Send EMAIL 
  // and PUSH Notification

  if(t > 500){
    Blynk.email("antoniolitz22@gmail.com", "Alerta!", "Paciente com frequência acima do normal");
    Blynk.notify("Alerta - Frenquencia Cardiaca acima do normal");
  }
}


void setup() {
  Serial.begin(115200);
   timer.run();
  Blynk.begin(auth, ssid, password);
  WiFi.disconnect();
  delay(10);
  WiFi.begin(ssid, password);
 
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("NodeMcu connected to wifi...");
  Serial.println(ssid);
  Serial.println();
}





 
void loop() {
  float temp = analogRead(A0);

  Blynk.run();
 
  
  if (client.connect(server,80))
  {  
    String tsData = apiWritekey;
           tsData +="&field1=";
           tsData += String(temp);
           tsData += "\r\n\r\n";
 
     client.print("POST /update HTTP/1.1\n");
     client.print("Host: api.thingspeak.com\n");
     client.print("Connection: close\n");
     client.print("X-THINGSPEAKAPIKEY: "+apiWritekey+"\n");
     client.print("Content-Type: application/x-www-form-urlencoded\n");
     client.print("Content-Length: ");
     client.print(tsData.length());
     client.print("\n\n");  // the 2 carriage returns indicate closing of Header fields & starting of data
     client.print(tsData);
 
     Serial.print("Temperature: ");
     Serial.print(temp);
     Serial.println("uploaded to Thingspeak server....");
  }
  client.stop();
 
  Serial.println("Waiting to upload next reading...");
  Serial.println();
  // thingspeak needs minimum 15 sec delay between updates
  delay(5000);
}



All that stuff in your void loop needs to be moved to a separate function, that’s called every 5 seconds, and the delay(5000) needs to be removed.

Pete.

Why do people almost never look at the documentation :thinking:

1 Like

It worked! Thanks bud.

1 Like