警报.alarmRepeat只重复两次,然后停止工作



我使用arduino uno与CC3000 WiFi屏蔽。ardunio对使用光屏障的人数进行计数,然后将计数结果报告给xievly网站。它在一两个小时内似乎可以正常工作,但随后就什么也没发生了。什么好主意吗?

// Libraries
#include <Adafruit_CC3000.h>
#include <SPI.h>
#include <Time.h>
#include <TimeAlarms.h>
// Define CC3000 chip pins
#define ADAFRUIT_CC3000_IRQ   3
#define ADAFRUIT_CC3000_VBAT  5
#define ADAFRUIT_CC3000_CS    10
// Create CC3000 instances
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
                                         SPI_CLOCK_DIV2); // you can change this clock speed                                         
// WLAN parameters
#define WLAN_SSID       "XX"
#define WLAN_PASS       "XX"
#define WLAN_SECURITY   WLAN_SEC_WPA2
// Xively parameters
#define WEBSITE  "https://xively.com/feeds/XX"
#define API_key  "XXX"
#define feedID  "XXXX``"
uint32_t ip;
Adafruit_CC3000_Client client;
const unsigned long
  connectTimeout  = 15L * 1000L, // Max time to wait for server connection
  responseTimeout = 15L * 1000L; // Max time to wait for data from server
// Visitor Counter
int anzahl =0;
int lastState=LOW;
const int inputPin = 2;

void setup()
{
  // Initialize
  attachInterrupt(0, count, RISING);
  Serial.begin(115200);
  if (!cc3000.begin())
  {
    while(1);
  }
    // Connect to WiFi network
  cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
  /* Wait for DHCP to complete */
  while (!cc3000.checkDHCP())
  {
    delay(100);
  }  
  unsigned long t  = getTime();
  cc3000.disconnect();
  setTime(hour(t)+2,minute(t),second(t),month(t),day(t),year(t));
  Alarm.alarmRepeat(9,59,59, Messung);
  Alarm.alarmRepeat(10,59,59, Messung);
  Alarm.alarmRepeat(11,59,59, Messung);
  Alarm.alarmRepeat(12,59,59, Messung);
  Alarm.alarmRepeat(13,59,59, Messung);
  Alarm.alarmRepeat(14,59,59, Messung);
  Alarm.alarmRepeat(15,59,59, Messung);
  Alarm.alarmRepeat(16,59,59, Messung);
  Alarm.alarmRepeat(17,59,59, Messung);
  Alarm.alarmRepeat(18,59,59, Messung);
  Alarm.alarmRepeat(19,59,59, Messung);
  Alarm.alarmRepeat(22,59,59, Zeitkorrektur);
}
void count()
{
  delay(10);
  int val = digitalRead(inputPin);
  if (val == HIGH && lastState==LOW)
{
    anzahl++;
    lastState=val;
  }
  else
  {
    lastState=val;
  }
}
void Zeitkorrektur()
{
  getTime();
}
void Messung()
{
  datalog();
}
void loop()
{
  Alarm.delay(1000);
}
// Minimalist time server query; adapted from Adafruit Gutenbird sketch,
// which in turn has roots in Arduino UdpNTPClient tutorial.
unsigned long getTime() 
{
  uint8_t       buf[48];
  unsigned long ip, startTime, t = 0L;
  // Hostname to IP lookup; use NTP pool (rotates through servers)
  if(cc3000.getHostByName("pool.ntp.org", &ip)) {
    static const char PROGMEM
      timeReqA[] = { 227,  0,  6, 236 },
      timeReqB[] = {  49, 78, 49,  52 };
    Serial.println(F("rnAttempting connection..."));
    startTime = millis();
    do {
      client = cc3000.connectUDP(ip, 123);
    } while((!client.connected()) &&
            ((millis() - startTime) < connectTimeout));
    if(client.connected()) {
      Serial.print(F("connected!rnIssuing request..."));
      // Assemble and issue request packet
      memset(buf, 0, sizeof(buf));
      memcpy_P( buf    , timeReqA, sizeof(timeReqA));
      memcpy_P(&buf[12], timeReqB, sizeof(timeReqB));
      client.write(buf, sizeof(buf));
      Serial.print(F("rnAwaiting response..."));
      memset(buf, 0, sizeof(buf));
      startTime = millis();
      while((!client.available()) &&
            ((millis() - startTime) < responseTimeout));
      if(client.available()) {
        client.read(buf, sizeof(buf));
        t = (((unsigned long)buf[40] << 24) |
             ((unsigned long)buf[41] << 16) |
             ((unsigned long)buf[42] <<  8) |
              (unsigned long)buf[43]) - 2208988800UL;
        Serial.print(F("OKrn"));
      }
      client.close();
    }
  }
  if(!t) Serial.println(F("error"));
  return t;
}
void datalog()
{
  //noInterrupts();
  // Connect to WiFi network
  cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
  /* Wait for DHCP to complete */
  while (!cc3000.checkDHCP())
  {
    delay(100);
  }  
  // Set the website IP
  uint32_t ip = cc3000.IP2U32(216,52,233,120);
  cc3000.printIPdotsRev(ip);
  // Get data & transform to integers
  int visitors = (int) anzahl/2; //Visitors going in and out therefore divided by 2
  // Prepare JSON for Xively & get length
  int length = 0;
  String data = "";
  data = data + "n" + "{"version":"1.0.0","datastreams" : [ {"id" : "Visitors","current_value" : "" + String(visitors) + ""}]}";
  length = data.length();
  // Send request
  Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80);
  if (client.connected()) {
    Serial.println("Connected!");
    client.println("PUT /v2/feeds/" + String(feedID) + ".json HTTP/1.0");
    client.println("Host: api.xively.com");
    client.println("X-ApiKey: " + String(API_key));
    client.println("Content-Length: " + String(length));
    client.print("Connection: close");
    client.println();
    client.print(data);
    client.println();
  } else {
    Serial.println(F("Connection failed"));    
    return;
  }
  Serial.println(F("-------------------------------------"));
  while (client.connected()) {
    while (client.available()) {
      char c = client.read();
      Serial.print(c);
    }
  }
  client.close();
  Serial.println(F("-------------------------------------"));
  Serial.println(F("nnDisconnecting"));
  cc3000.disconnect();
  anzahl =0;
  //interrupts();
}

setup()中,您使用RISING配置中断。这意味着当引脚从低状态到高状态时,它会触发。但是在中断处理程序- count()中,您有奇怪的逻辑,看起来过多,因为您已经将中断配置为仅在事件上触发一次。

让我们看看在count()中发生了什么。val将始终为HIGH(好吧,只考虑正常情况,而不是故障)。当第一个中断到来时,lastState是LOW作为初始值,我们增加,但所有后续调用将不会,因为lastState是HIGH并且永远不会再次变为LOW。因此,为了解决这个问题,我们只需要删除lastState因为它不需要
void count()
{
  delay(10);
  // avoiding glitches - check if it's still high
  int val = digitalRead(inputPin);
  if (val == HIGH)
    anzahl++;
}

相关内容

最新更新