Dan,
Any chance of a pulse counting flow meter ? I’m wanting to program my ESP-13. I have tried this code and the interrupt routine works.
I can see the messages printed when I blow into the water flow sensor.
The sensor is on pin 2.
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include “Adafruit_IO_Client.h”
#define AIO_KEY “…your AIO key…”
// Create an ESP8266 WiFiClient class to connect to the AIO server.
WiFiClient client;
// Create an Adafruit IO Client instance. Notice that this needs to take a
// WiFiClient object as the first parameter, and as the second parameter a
// default Adafruit IO key to use when accessing feeds (however each feed can
// override this default key value if required, see further below).
Adafruit_IO_Client aio = Adafruit_IO_Client(client, AIO_KEY);
// Finally create instances of Adafruit_IO_Feed objects, one per feed. Do this
// by calling the getFeed function on the Adafruit_IO_FONA object and passing
// it at least the name of the feed, and optionally a specific AIO key to use
// when accessing the feed (the default is to use the key set on the
// Adafruit_IO_Client class).
Adafruit_IO_Feed testFeed = aio.getFeed(“esptestfeed”);
int flow;
int flow_pin = 2;
int READ_INTERVAL = 60; // measurement interval seconds
unsigned long flow1count = 0;
long lastReadStart;
int READ_INTERVAL_MILLIS=READ_INTERVAL* 1000;
#define countof(a) (sizeof(a) / sizeof(a[0]))
//char ssid[] = “abz-fi”; // your network SSID (name)
//char password[] = “we do what we must, because we can”; // your network password
const char* ssid = “BRCMGUEST”;
const char* password = “olaitntdf”;
// Update these with values suitable for your network.
//IPAddress server(10, 13, 0, 136);
//PubSubClient client(server);
void setup()
{
Serial.begin(115200);
pinMode(flow_pin, INPUT_PULLUP);
attachInterrupt(flow_pin, flow1, RISING);
// client.set_callback(callback);
// Connect to WiFi network
WiFi.begin(ssid, password);
Serial.print("\n\r \n\rWorking to connect");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
//Serial.print(".");
}
Serial.println("");
Serial.println(“Esp General Purpose Server”);
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// if (client.connect(“arduinoClient”)) {
// client.publish(“outTopic”,“flow boot up”);
// client.subscribe(“inTopic”);
// }
}
void loop()
{
unsigned long currentTime = millis();
if (currentTime - lastReadStart > READ_INTERVAL_MILLIS)
{
flow = flow1count*60/READ_INTERVAL;
lastReadStart= currentTime;
Serial.print("l/hour:");
Serial.println(flow);
flow1count = 0;
if (testFeed.send(flow)) {
Serial.print(F("Wrote value to feed: ")); Serial.println(flow, DEC);
}
else {
Serial.println(F(“Error writing value to feed!”));
}
// Now wait 10 seconds and read the current feed value.
Serial.println(F(“Waiting 10 seconds and then reading the feed value.”));
delay(10000);
// To read the latest feed value call the receive function on the feed.
// The returned object will be a FeedData instance and you can check if it’s
// valid (i.e. was successfully read) by calling isValid(), and then get the
// value either as a text value, or converted to an int, float, etc.
FeedData latest = testFeed.receive();
if (latest.isValid()) {
Serial.print(F("Received value from feed: ")); Serial.println(latest);
// By default the received feed data item has a string value, however you
// can use the following functions to attempt to convert it to a numeric
// value like an int or float. Each function returns a boolean that indicates
// if the conversion succeeded, and takes as a parameter by reference the
// output value.
int i;
if (latest.intValue(&i)) {
Serial.print(F("Value as an int: ")); Serial.println(i, DEC);
}
// Other functions that you can use include:
// latest.uintValue() (unsigned int)
// latest.longValue() (long)
// latest.ulongValue() (unsigned long)
// latest.floatValue() (float)
// latest.doubleValue() (double)
}
else {
Serial.print(F(“Failed to receive the latest feed value!”));
}
}
}
void flow1()
{
flow1count +=1;
}