Hi Im monitoring button presses to measure capacity of something and want to only upload the stored value of button presses , every 5 mins to Cosm..
So in theory I want the last bit of the sketch in BOLD to happen every 5 mins.. any ideas?
/**
#include <SPI.h>
#include <Ethernet.h>
#include <HttpClient.h>
#include <Cosm.h>
#define API_KEY "qr68ERD9I6H3aoZb0twQL8yCZIGSAKxBZE1leWdaWXIwcz0g" // your Cosm API key
#define FEED_ID 124340 // your Cosm feed ID
// MAC address for your Ethernet shield
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// Analog pin which we're monitoring (0 and 1 are used by the Ethernet shield)
int sensorPin = 2;
const int buttonPin = A0; // the pin that the Up pushbutton is attached to
const int buttonPin1 = A1; // the pin that the Down pushbutton is attached to
unsigned long lastConnectionTime = 0; // last time we connected to Cosm
const unsigned long connectionInterval = 15000; // delay between connecting to Cosm in milliseconds
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState5 = 0; // current state of the button
int buttonState6 = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
// Initialize the Cosm library
// Define the string for our datastream ID
char sensorId[] = "sensor_reading";
CosmDatastream datastreams[] = {
CosmDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT),
};
// Wrap the datastream into a feed
CosmFeed feed(FEED_ID, datastreams, 1 /* number of datastreams */);
EthernetClient client;
CosmClient cosmclient(client);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Cosm Sensor Client Example");
Serial.println("==========================");
Serial.println("Initializing network");
while (Ethernet.begin(mac) != 1) {
Serial.println("Error getting IP address via DHCP, trying again...");
delay(15000);
}
Serial.println("Network initialized");
Serial.println();
}
void loop() {
// main program loop
if (millis() - lastConnectionTime > connectionInterval) {
// read a value from the pin
int capacity = (buttonPushCounter);
// read the pushbutton up input pin:
buttonState5 = digitalRead(buttonPin);
if (buttonState5 != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState5 == HIGH)
{
buttonPushCounter++;
Serial.println(buttonPushCounter);
}
delay(50);
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState5;
// read the pushbutton down input pin:
buttonState6 = digitalRead(buttonPin1);
// compare the buttonState to its previous state
if (buttonState6 != lastButtonState) {
// if the state has changed, decrement the counter
if (buttonState6 == HIGH)
{
buttonPushCounter-=1;
Serial.println(buttonPushCounter);
}
delay(50);
if (buttonPushCounter < 1)
{
Serial.print(" ");
}
if (buttonPushCounter <= 0)
{
Serial.print("EMPTY");
}
if (buttonPushCounter >= 2500)
{
Serial.print("Max");
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState6;
//////////////////////////////////////
[b] // send it to Cosm
sendData(capacity);
// read the datastream back from Cosm
// getData();
// update connection time so we wait before connecting again
lastConnectionTime = millis();
}
}
// send the supplied value to Cosm, printing some debug information as we go
void sendData(int capacity) {
datastreams[0].setFloat(capacity);
Serial.print("Read capacity value ");
Serial.println(datastreams[0].getFloat());
Serial.println("Uploading to Cosm");
int ret = cosmclient.put(feed, API_KEY);
Serial.print("PUT return code: ");
Serial.println(ret);
Serial.println();[/b]
}
billroy:
Keep track of the last time you sent a package and don't send another one until five minutes later.
Look at the Blink Without Delay example for code.
-br
Ive looked at that... doesnt work
/**
#include <SPI.h>
#include <Ethernet.h>
#include <HttpClient.h>
#include <Cosm.h>
#define API_KEY "qr68ERD9I6H3aoZb0twQL8yCZIGSAKxBZE1leWdaWXIwcz0g" // your Cosm API key
#define FEED_ID 124340 // your Cosm feed ID
// MAC address for your Ethernet shield
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// Analog pin which we're monitoring (0 and 1 are used by the Ethernet shield)
int sensorPin = 2;
const int buttonPin = A0; // the pin that the Up pushbutton is attached to
const int buttonPin1 = A1; // the pin that the Down pushbutton is attached to
unsigned long lastConnectionTime = 0; // last time we connected to Cosm
const unsigned long connectionInterval = 15000; // delay between connecting to Cosm in milliseconds
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState5 = 0; // current state of the button
int buttonState6 = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
// Initialize the Cosm library
// Define the string for our datastream ID
char sensorId[] = "sensor_reading";
CosmDatastream datastreams[] = {
CosmDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT),
};
// Wrap the datastream into a feed
CosmFeed feed(FEED_ID, datastreams, 1 /* number of datastreams */);
EthernetClient client;
CosmClient cosmclient(client);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Cosm Sensor Client Example");
Serial.println("==========================");
Serial.println("Initializing network");
while (Ethernet.begin(mac) != 1) {
Serial.println("Error getting IP address via DHCP, trying again...");
delay(15000);
}
Serial.println("Network initialized");
Serial.println();
[b]long previousMillis = 0;
long interval = 1000;[/b]
}
void loop() {
// main program loop
if (millis() - lastConnectionTime > connectionInterval) {
// read a value from the pin
int capacity = (buttonPushCounter);
// read the pushbutton up input pin:
buttonState5 = digitalRead(buttonPin);
if (buttonState5 != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState5 == HIGH)
{
buttonPushCounter++;
Serial.println(buttonPushCounter);
}
delay(50);
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState5;
// read the pushbutton down input pin:
buttonState6 = digitalRead(buttonPin1);
// compare the buttonState to its previous state
if (buttonState6 != lastButtonState) {
// if the state has changed, decrement the counter
if (buttonState6 == HIGH)
{
buttonPushCounter-=1;
Serial.println(buttonPushCounter);
}
delay(50);
if (buttonPushCounter < 1)
{
Serial.print(" ");
}
if (buttonPushCounter <= 0)
{
Serial.print("EMPTY");
}
if (buttonPushCounter >= 2500)
{
Serial.print("Max");
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState6;
//////////////////////////////////////
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
[b] // send it to Cosm
sendData(capacity);
// read the datastream back from Cosm
// getData();
// update connection time so we wait before connecting again
lastConnectionTime = millis();
}
}
// send the supplied value to Cosm, printing some debug information as we go
void sendData(int capacity) {
datastreams[0].setFloat(capacity);
Serial.print("Read capacity value ");
Serial.println(datastreams[0].getFloat());
Serial.println("Uploading to Cosm");
int ret = cosmclient.put(feed, API_KEY);
Serial.print("PUT return code: ");
Serial.println(ret);
Serial.println();[/b]
}
#include <SPI.h>
#include <Ethernet.h>
#include <HttpClient.h>
#include <Cosm.h>
#include <Metro.h> // Include Metro library
#define API_KEY "qr68ERD9I6H3aoZb0twQL8yCZIGSAKxBZE1leWdaWXIwcz0g" // your Cosm API key
#define FEED_ID 124349990 // your Cosm feed ID
// MAC address for your Ethernet shield
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// Analog pin which we're monitoring (0 and 1 are used by the Ethernet shield)
int sensorPin = 2;
const int buttonPin = A0; // the pin that the Up pushbutton is attached to
const int buttonPin1 = A1; // the pin that the Down pushbutton is attached to
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState5 = 0; // current state of the button
int buttonState6 = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
// Initialize the Cosm library
// Define the string for our datastream ID
char sensorId[] = "sensor_reading";
CosmDatastream datastreams[] = {
CosmDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT),
};
// Wrap the datastream into a feed
CosmFeed feed(FEED_ID, datastreams, 1 /* number of datastreams */);
EthernetClient client;
CosmClient cosmclient(client);
Metro ledMetro = Metro(3000);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Cosm Sensor Client Example");
Serial.println("==========================");
Serial.println("Initializing network");
while (Ethernet.begin(mac) != 1) {
Serial.println("Error getting IP address via DHCP, trying again...");
delay(15000);
}
Serial.println("Network initialized");
Serial.println();
}
void loop() {
// main program loop
// read a value from the pin
int capacity = (buttonPushCounter);
// read the pushbutton up input pin:
buttonState5 = digitalRead(buttonPin);
if (buttonState5 != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState5 == HIGH)
{
buttonPushCounter++;
Serial.println(buttonPushCounter);
}
delay(50);
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState5;
// read the pushbutton down input pin:
buttonState6 = digitalRead(buttonPin1);
// compare the buttonState to its previous state
if (buttonState6 != lastButtonState) {
// if the state has changed, decrement the counter
if (buttonState6 == HIGH)
{
buttonPushCounter-=1;
Serial.println(buttonPushCounter);
}
delay(50);
if (buttonPushCounter < 1)
{
Serial.print(" ");
}
if (buttonPushCounter <= 0)
{
Serial.print("EMPTY");
}
if (buttonPushCounter >= 2500)
{
Serial.print("Max");
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState6;
//////////////////////////////////////
if (ledMetro.check() == 1) { // check if the metro has passed
{ // send it to Cosm
sendData(capacity);
// read the datastream back from Cosm
// getData();
// update connection time so we wait before connecting again
}
}
// send the supplied value to Cosm, printing some debug information as we go
void sendData(int capacity) {
datastreams[0].setFloat(capacity);
Serial.print("Read sensor value ");
Serial.println(datastreams[0].getFloat());
Serial.println("Uploading to Cosm");
int ret = cosmclient.put(feed, API_KEY);
Serial.print("PUT return code: ");
Serial.println(ret);
Serial.println();
}
Hackdub:
would it be possible to please complete the code... as Im way out of my depth..
Have you actually made any attempt to understand the blink without delay example sketch? There isn't much code there. It does something at regular intervals. You want to do something at regular intervals. The key part is a single line of code that you have already been referred to. It's hard to see how much clearer the explanation could be, short of writing your sketch for you. If that's what you're after, you should be asking in the Gigs and Collaborations section. (Note that most people would expect to be paid for doing your work for you.)
Hackdub, I think your second code post was really close. It looks like you had the right idea, but your implementation of interval and previousMillis was off.
void setup() {
...
[b]long previousMillis = 0;
long interval = 1000;[/b]
}
...
loop() {
...
//////////////////////////////////////
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
[b] // send it to Cosm
sendData(capacity);
// read the datastream back from Cosm
// getData();
// update connection time so we wait before connecting again
lastConnectionTime = millis();
}
}
I think you need to declare previousMillis globally instead of in setup(). interval could be delcared in loop() or globally, or as a constant, or you could just use a number.
Also, interval = 1000 is probably not what you want. That's only one second.
/**
#include <SPI.h>
#include <Ethernet.h>
#include <HttpClient.h>
#include <Cosm.h>
#define API_KEY "qr68ERD9I6H3aoZb0twQL8yCZIGSAKxBZE1leWdaWXIwcz0g" // your Cosm API key
#define FEED_ID 124340 // your Cosm feed ID
// MAC address for your Ethernet shield
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// Analog pin which we're monitoring (0 and 1 are used by the Ethernet shield)
int sensorPin = 2;
const int buttonPin = A0; // the pin that the Up pushbutton is attached to
const int buttonPin1 = A1; // the pin that the Down pushbutton is attached to
unsigned long lastConnectionTime = 0; // last time we connected to Cosm
const unsigned long connectionInterval = 15000; // delay between connecting to Cosm in milliseconds
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState5 = 0; // current state of the button
int buttonState6 = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
long previousMillis = 0;
long interval = 300000;
// Initialize the Cosm library
// Define the string for our datastream ID
char sensorId[] = "sensor_reading";
CosmDatastream datastreams[] = {
CosmDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT),
};
// Wrap the datastream into a feed
CosmFeed feed(FEED_ID, datastreams, 1 /* number of datastreams */);
EthernetClient client;
CosmClient cosmclient(client);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Cosm Sensor Client Example");
Serial.println("==========================");
Serial.println("Initializing network");
while (Ethernet.begin(mac) != 1) {
Serial.println("Error getting IP address via DHCP, trying again...");
delay(15000);
}
Serial.println("Network initialized");
Serial.println();
[b]long previousMillis = 0;
long interval = 1000;[/b]
}
void loop() {
// main program loop
if (millis() - lastConnectionTime > connectionInterval) {
// read a value from the pin
int capacity = (buttonPushCounter);
// read the pushbutton up input pin:
buttonState5 = digitalRead(buttonPin);
if (buttonState5 != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState5 == HIGH)
{
buttonPushCounter++;
Serial.println(buttonPushCounter);
}
delay(50);
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState5;
// read the pushbutton down input pin:
buttonState6 = digitalRead(buttonPin1);
// compare the buttonState to its previous state
if (buttonState6 != lastButtonState) {
// if the state has changed, decrement the counter
if (buttonState6 == HIGH)
{
buttonPushCounter-=1;
Serial.println(buttonPushCounter);
}
delay(50);
if (buttonPushCounter < 1)
{
Serial.print(" ");
}
if (buttonPushCounter <= 0)
{
Serial.print("EMPTY");
}
if (buttonPushCounter >= 2500)
{
Serial.print("Max");
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState6;
//////////////////////////////////////
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
[b] // send it to Cosm
sendData(capacity);
// read the datastream back from Cosm
// getData();
// update connection time so we wait before connecting again
lastConnectionTime = millis();
}
}
// send the supplied value to Cosm, printing some debug information as we go
void sendData(int capacity) {
datastreams[0].setFloat(capacity);
Serial.print("Read capacity value ");
Serial.println(datastreams[0].getFloat());
Serial.println("Uploading to Cosm");
int ret = cosmclient.put(feed, API_KEY);
Serial.print("PUT return code: ");
Serial.println(ret);
Serial.println();[/b]
}
The only bug I wish to iron out is that I dont want the number to increment if the button is held.. any thoughts?
thanks for your patience so far
/**
* Cosm Arduino sensor client example.
*
* This sketch demonstrates connecting an Arduino to Cosm (https://cosm.com),
* using the new Arduino library to send and receive data.
*
* Requirements
* * Arduino with Ethernet shield or Arduino Ethernet (board must use the
* Wiznet Ethernet chipset)
* * Arduino software with version >= 1.0
* * An account at Cosm (https://cosm.com)
*
* Optional
* * An analog sensor connected to pin 2 (note we can still read a value from
* the pin without this)
*
* Created 8th January, 2013 using code written by Adrian McEwen with
* modifications by Sam Mulube
*
* Full tutorial available here: https://cosm.com/docs/quickstart/arduino.html
*
* This code is in the public domain.
*/
#include <SPI.h>
#include <Ethernet.h>
#include <HttpClient.h>
#include <Cosm.h>
#include <Metro.h> // Include Metro library
#define API_KEY "qr68ERD9I6H3aoZb0twQL8yCZIGSAKxBZE1leWdaWXIwcz0g" // your Cosm API key
#define FEED_ID 124340 // your Cosm feed ID
// MAC address for your Ethernet shield
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// Analog pin which we're monitoring (0 and 1 are used by the Ethernet shield)
int sensorPin = 2;
const int buttonPin = A0; // the pin that the Up pushbutton is attached to
const int buttonPin1 = A1; // the pin that the Down pushbutton is attached to
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState5 = 0; // current state of the button
int buttonState6 = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
// Initialize the Cosm library
// Define the string for our datastream ID
char sensorId[] = "sensor_reading";
CosmDatastream datastreams[] = {
CosmDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT),
};
// Wrap the datastream into a feed
CosmFeed feed(FEED_ID, datastreams, 1 /* number of datastreams */);
EthernetClient client;
CosmClient cosmclient(client);
Metro ledMetro = Metro(300000);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Cosm Sensor Client Example");
Serial.println("==========================");
Serial.println("Initializing network");
while (Ethernet.begin(mac) != 1) {
Serial.println("Error getting IP address via DHCP, trying again...");
delay(15000);
}
Serial.println("Network initialized");
Serial.println();
}
void loop() {
// main program loop
// read a value from the pin
int capacity = (buttonPushCounter);
// read the pushbutton up input pin:
buttonState5 = digitalRead(buttonPin);
if (buttonState5 != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState5 == HIGH)
{
buttonPushCounter++;
Serial.println(buttonPushCounter);
}
delay(50);
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState5;
// read the pushbutton down input pin:
buttonState6 = digitalRead(buttonPin1);
// compare the buttonState to its previous state
if (buttonState6 != lastButtonState) {
// if the state has changed, decrement the counter
if (buttonState6 == HIGH)
{
buttonPushCounter-=1;
Serial.println(buttonPushCounter);
}
delay(50);
if (buttonPushCounter < 1)
{
Serial.print(" ");
}
if (buttonPushCounter <= 0)
{
Serial.print("EMPTY");
}
if (buttonPushCounter >= 2500)
{
Serial.print("Max");
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState6;
//////////////////////////////////////
if (ledMetro.check() == 1) { // check if the metro has passed
{ // send it to Cosm
sendData(capacity);
// read the datastream back from Cosm
// getData();
// update connection time so we wait before connecting again
}
}}
// send the supplied value to Cosm, printing some debug information as we go
void sendData(int capacity) {
datastreams[0].setFloat(capacity);
Serial.print("Read sensor value ");
Serial.println(datastreams[0].getFloat());
Serial.println("Uploading to Cosm");
int ret = cosmclient.put(feed, API_KEY);
Serial.print("PUT return code: ");
Serial.println(ret);
Serial.println();
}