My Water Meter Reading App Communication with ThingSpeak | Arduino | Forum

The ThingSpeak community site has been upgraded to a new site. This site is currently in read-only mode. You can ask questions or post and read discussions on the new site.
Avatar

Lost password?
Advanced Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed sp_TopicIcon
My Water Meter Reading App Communication with ThingSpeak
No permission to create posts
March 6, 2016
8:11 pm
Avatar
wheato22

Silver
Members
Forum Posts: 8
Member Since:
February 23, 2016
sp_UserOfflineSmall Offline

I've about given up trying to get my water meter reader app to upload data to my ThingSpeak channel using Arudino and a wired Ethernet shield. Being a non-programmer I have tried to use published examples designed for analog inputs to work with my interrupt driven app. I have tried co cobble my app into both the HTTP POST and and ThingSpeak.write Voltage Examples. In both cases compile errors have dogged me to submission. If anyone can suggest some upload code that will play nicely with my interrupt driven counter code (below) and pave the way to upload my serial.print output to channel 1 I'd appreciate the help . ThingSpeak seems like a perfect way to visualize water use in real time... Particularly here in the western US, water use allocations are in effect .
/* Water Meter Flow Application---- authored by L. Wheaton, 2015
* Sub-meter outputs a pulse every gallon of flow
* Hall device connected to digital pin 2 creates interrupt
* Interrupt handler increments gallon counter
* accumulated gallon count print and uploads every 20 seconds
*/
byte sensorInterrupt = 0;
byte sensorPin = 2;
volatile byte pulseCount;
unsigned int gallons;
unsigned long oldTime;

void setup(){
Serial.begin(9600);
pinMode(sensorPin, INPUT);
digitalWrite(sensorPin, HIGH);
pulseCount = 0;
oldTime = 0;
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}
void loop()
{
// Sets xmit every 20 seconds
if((millis() - oldTime) > 20000)
{
// Disable interrupts while calculating
detachInterrupt(sensorInterrupt);
oldTime = millis();
gallons = (gallons + pulseCount);
// Prints gallon flow and Total
Serial.print("Pulse: ");
Serial.print(pulseCount);
Serial.print(" Gallons: ");
Serial.println(gallons);
pulseCount = 0;
// Enable interrupts again
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}
}
// interrupt handler
void pulseCounter()
{ pulseCount++;
}

March 7, 2016
10:58 am
Avatar
Rob Purser
Natick, MA

MathWorks
Members
Forum Posts: 47
Member Since:
August 22, 2015
sp_UserOfflineSmall Offline

Hi,

Sorry you're having so much trouble. I wonder if attaching and detaching the interrupts is the culprit. It might be disrupting other things that depend on interrupts, like the ethernet card. You might refactor your code a bit to use noInterrupts(), copy out the value of pulseCounter,reset it, turn on interrupts again using interrupts(), and then do the calculations, serial stuff, and the write to ThingSpeak. Presumably, there's no risk of having more than 255 gallons of water used in 20 seconds, right?

// Disable interrupts while reseting pulse count
noInterrupts();
pulseCountCopy = pulseCount;
pulseCount = 0;
// Enable interrupts again
interrupts();

//calculate
oldTime = millis();
gallons = (gallons + pulseCountCopy );
// Prints gallon flow and Total
Serial.print("Pulse: ");
Serial.print(pulseCountCopy );
Serial.print(" Gallons: ");
Serial.println(gallons);
pulseCount = 0;

Senior Development Manager for IoT and Hardware Interfacing for MATLAB at MathWorks.  Visit ThingSpeak.com to explore the IoT Analytic platform that speaks MATLAB made for engineers and scientists.  You can collect, analyze, and act in 5 minutes or less!

March 7, 2016
11:46 pm
Avatar
rw950431

Top
Members
Forum Posts: 279
Member Since:
January 30, 2014
sp_UserOfflineSmall Offline

Are you able to get the unedited "Write Voltage" sketch to work with your Arduino and thingspeak? (https://github.com/mathworks/thingspeak-arduino/tree/master/examples/WriteVoltage)

This will ensure that your software environment is correctly configured. Many problems occur because of choosing the wrong board type in the IDE or not installing all the libraries you need. You also need to update that sketch with your own thingspeak channel and API key.

If this step fails then post the error messages you get and maybe we can help some more.

If that succeeds I suggest you change pulseCount as follows

volatile unsigned long int pulseCount=0;

add your interrupt service routine to the WriteVoltage code and send this value in place of voltage like so

ThingSpeak.writeField(myChannelNumber, 1, pulseCount, myWriteAPIKey);

I wouldn't bother attaching and detaching the interrupt each time you send. If you want a rate rather than a cumulative total make a last_pulseCount and subtract it from the current value.

March 13, 2016
3:09 pm
Avatar
wheato22

Silver
Members
Forum Posts: 8
Member Since:
February 23, 2016
sp_UserOfflineSmall Offline

Hi Rob and RW,
Rob's Points above are on target. Water pulses arrive at a max rate of 10/minute. No need to disable interrupt handler since it takes microseconds to do the math. I'll give that a try to eliminate interactions with the Ethernet routine. Other's doing water meter apps usually count the spinning leak detector in the meter and it pulses 1000's of times per gallon. I'll eliminate off/on interrupts code and test

RW identifies my fundamental knowledge gap of Arudino programming. I have had no sure idea how to correctly merge my Water Count void loop (which works standalone) with the the Ethernet example. I understand the only one "void setup()" concept but need help on how to structure the multiple routines "void loops" on one big Sketch.

I'll try RW's "Write Voltage" example first with my specific Ethernet shield and and get that working. Then add my Water Sketch without "disabling" interrupts between pulses .

Thanks for the help.
Larry

March 21, 2016
1:52 am
Avatar
wheato22

Silver
Members
Forum Posts: 8
Member Since:
February 23, 2016
sp_UserOfflineSmall Offline

Can anyone give me some assistance in getting a second Channel working. When I compile a Sketch with a second channel write i get an error for the following write instruction. (ThingSpeak.writeField(myChannelNumber,2, gallons ,myWriteAPIKey);)

"call of overloaded 'writeField(long unsigned int&, int, long unsigned int&, const char*&)' is ambiguous"

I'm thinking it's related to the fields data type but have no idea on how to fix. My Sketch follows...

Larry
***************************
// Logs flow in gallons/minute from residential water meter
#include "ThingSpeak.h"
#include
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
EthernetClient client;
unsigned long myChannelNumber = 90870;
const char * myWriteAPIKey = "xxxxxxxx";
byte sensorInterrupt = 0;
byte sensorPin = 2;
volatile byte pulseCount;
unsigned long oldTime;
unsigned long gallons;
void setup()
{
Ethernet.begin(mac);
ThingSpeak.begin(client);
Serial.begin(9600);
pinMode(sensorPin, INPUT);
digitalWrite(sensorPin, HIGH);
gallons = 0;
pulseCount = 0;
oldTime = 0;
attachInterrupt(sensorInterrupt, pulseCounter, RISING);
}
void loop()
{
if((millis() - oldTime) > 60000)
{
oldTime = millis();
gallons = (gallons + pulseCount);
Serial.print("Pulse: ");
Serial.print(pulseCount);
Serial.print(" Gallons: ");
Serial.println(gallons);
ThingSpeak.begin (client);
ThingSpeak.writeField(myChannelNumber,1, pulseCount ,myWriteAPIKey);
ThingSpeak.writeField(myChannelNumber,2, gallons ,myWriteAPIKey);
pulseCount = 0;
}
}
void pulseCounter()
{
pulseCount++;
}

March 21, 2016
11:46 pm
Avatar
rw950431

Top
Members
Forum Posts: 279
Member Since:
January 30, 2014
sp_UserOfflineSmall Offline

Try changing the type of 'gallons' to long rather than unsigned long and see if that helps

eg
long gallons;

Forum Timezone: America/New_York

Most Users Ever Online: 166

Currently Online:
32 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 272

Vinod: 240

piajola: 95

turgo: 70

vespapierre: 63

Adarsh_Murthy: 62

Member Stats:

Guest Posters: 1

Members: 8665

Moderators: 1

Admins: 2

Forum Stats:

Groups: 3

Forums: 14

Topics: 1600

Posts: 5760

Newest Members:

kusmumichael, petersmith99, Rambant, blakeharriss09, optisol, Niyonzima Fils

Moderators: cstapels: 460

Administrators: Hans: 405, lee: 457