Hi everyone,
I´m new with IoT. I have an arduino connected to thingspeak and i want to send the number of times that a push button is activated.
The problem is that at the begining the arduino sends the first data to my chanel but then it stops.
If I use the fuction "delay" it sends the information but I can´t use this because the program is stopped during this seconds so I´m trying a timer library. I hope someone could help me to fix this. Thanks
Here is my code:
int BUTTON_PIN = 2;
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
unsigned long myChannelNumber = XXXXXXX;
const char * myWriteAPIKey = "XXXXXX";
#include <TimerOne.h>
#include "ThingSpeak.h"
#define USE_ETHERNET_SHIELD
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
EthernetClient client;
void setup() {
Timer1.initialize(180000000); // 18 s
Timer1.attachInterrupt(Thingspeak) ;
pinMode(BUTTON_PIN, INPUT_PULLUP);
//Serial.begin(9600);
Ethernet.begin(mac);
ThingSpeak.begin(client);
}
void loop() {
int buttonState = digitalRead(BUTTON_PIN);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
buttonPushCounter++;
//Serial.print("Counter: ");
//Serial.println(buttonPushCounter);
}
// Delay a little bit to avoid bouncing
delay(150);
// save the current state as the last state,for next time through the loop
noInterrupts(); // Suspende las interrupciones
lastButtonState = buttonState;
interrupts();
}
void Thingspeak() {
ThingSpeak.setField(1,buttonPushCounter);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
//ThingSpeak.writeField(myChannelNumber, 1, buttonPushCounter, myWriteAPIKey);
//Serial.print("Enviando datos ");
//Serial.println(buttonPushCounter);
}