MATLAB Answers

Sending YF-S201 Flow meter data to ThingSpeak cloud

9 views (last 30 days)
Andre
Andre on 21 Mar 2021
I am using the ESP32 to interface the YF-S201 flowmeter sensor and send collected data over to Thingspeak server. The data is updated on serial monitor but the data is not reflected on ThingSpeak server. Could anyone please help me with how to send data of the YF-S201 sensor to Thingspeak server.
I am attaching the code which I am presently using. I am not able to figure out what is wrong in the code and thereby would appreciate some help.
#include <WiFi.h>
#include <SPI.h>
#include <Wire.h>
String apiKey = ""; // Enter your Write API key from ThingSpeak
const char *ssid = ""; // replace with your wifi ssid and wpa2 key
const char *pass = "";
const char* server = "api.thingspeak.com";
#define LED_BUILTIN 16
#define SENSOR 4
long currentMillis = 0;
long previousMillis = 0;
int interval = 1000;
boolean ledState = LOW;
float calibrationFactor = 4.5;
volatile byte pulseCount;
byte pulse1Sec = 0;
float flowRate;
unsigned long flowMilliLitres;
unsigned int totalMilliLitres;
float flowLitres;
float totalLitres;
void IRAM_ATTR pulseCounter()
{
pulseCount++;
}
WiFiClient client;
void setup()
{
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(SENSOR, INPUT_PULLUP);
pulseCount = 0;
flowRate = 0.0;
flowMilliLitres = 0;
totalMilliLitres = 0;
previousMillis = 0;
attachInterrupt(digitalPinToInterrupt(SENSOR), pulseCounter, FALLING);
}
void loop()
{
currentMillis = millis();
if (currentMillis - previousMillis > interval)
{
pulse1Sec = pulseCount;
pulseCount = 0;
// Because this loop may not complete in exactly 1 second intervals we calculate
// the number of milliseconds that have passed since the last execution and use
// that to scale the output. We also apply the calibrationFactor to scale the output
// based on the number of pulses per second per units of measure (litres/minute in
// this case) coming from the sensor.
flowRate = ((1000.0 / (millis() - previousMillis)) * pulse1Sec) / calibrationFactor;
previousMillis = millis();
// Divide the flow rate in litres/minute by 60 to determine how many litres have
// passed through the sensor in this 1 second interval, then multiply by 1000 to
// convert to millilitres.
flowMilliLitres = (flowRate / 60) * 1000;
flowLitres = (flowRate / 60);
// Add the millilitres passed in this second to the cumulative total
totalMilliLitres += flowMilliLitres;
totalLitres += flowLitres;
// Print the flow rate for this second in litres / minute
Serial.print("Flow rate: ");
Serial.print(float(flowRate)); // Print the integer part of the variable
Serial.print("L/min");
Serial.print("\t"); // Print tab space
// Print the cumulative total of litres flowed since starting
Serial.print("Output Liquid Quantity: ");
Serial.print(totalMilliLitres);
Serial.print("mL / ");
Serial.print(totalLitres);
Serial.println("L");
}
if (client.connect(server, 80)) // "184.106.153.149" or api.thingspeak.com
{
String postStr = apiKey;
postStr += "&field1=";
postStr += String(float(flowRate));
postStr += "&field2=";
postStr += String(totalLitres);
postStr += "\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: " + apiKey + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
}
client.stop();
}

Answers (1)

Vinod
Vinod on 21 Mar 2021
I take it you are using a free ThingSpeak account. This requires you to update the channel less frequently than once every 15s. The first advice I can give is to slow down the update rate to less than once every 15s.
  1 Comment
Benjamin Mendoza
Benjamin Mendoza on 2 May 2021
Thanks bro! But how do you do that, can you explain me that please??

Sign in to comment.

Communities

More Answers in the  ThingSpeak Community

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!