c_cpp 每分钟读取一次当前UV索引级别,并在值更改时将值发布到PubNub,并且每10分钟至少发布一次

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 每分钟读取一次当前UV索引级别,并在值更改时将值发布到PubNub,并且每10分钟至少发布一次相关的知识,希望对你有一定的参考价值。

void setup() {
    Serial.begin(9600);
    WiFi.begin(ssid, pass);
    if(WiFi.waitForConnectResult() == WL_CONNECTED){
        Serial.println("WiFi connected.");
        PubNub.begin("pub-key", "sub-key");
    } else {
        Serial.println("Couldn't get a wifi connection.");
        while(1) delay(100);
    }
}
#include <ESP8266WiFi.h>
#define PubNub_BASE_CLIENT WiFiClient
#include <PubNub.h>
 
const static char ssid[] = "Sensor Network";
const static char pass[] = "sens0rpassw0rd";

int sendTimer;   // Timer used to control how often messages are published. Sends a new message once every 10 minutes.
int sensorTimer; // Timer used to control how often sensor is checked. Checked every 60 seconds.
float sensorValue; // Value from sensor.
int lastuvindex; // UV index on previous loop.
int uvindex; // Value from sensor converted to UV index.
if ((uvindex > 7) && (alertTimer == 0)) { // Send a push notification if the UV index is very high and no notification has been sent in the last 15 hours.
  alertTimer = 900; // Wait 15 hours before sending another push notification. 
  strcat(msg, "},\"pn_apns\":{\"aps\":{\"alert\":\"The UV index is very high. Avoid the sun.\"}}}");
} 
else {
  if (alertTimer > 0) {
    alertTimer = (alertTimer - 1); // Remove a minute from the alertTimer.
  }
  strcat(msg, "}}");
}
if ((uvindex > 7) && (alertTimer == 0)) { // Send a push notification if the UV index is very high and no notification has been sent in the last 15 hours.
  alertTimer = 900; // Wait 15 hours before sending another push notification. 
  strcat(msg, "},\"pn_gcm\":{\"notification\":{\"body\":\"The UV index is very high. Avoid the sun.\"}}}");
} 
else {
  if (alertTimer > 0) {
    alertTimer = (alertTimer - 1); // Remove a minute from the alertTimer.
  }
  strcat(msg, "}}");
}
void loop() {
    sensorTimer = (sensorTimer + 1); // Add a second to the sensor timer.
    if (sensorTimer == 60) { // Check sensor.
      sensorTimer = 0; // Reset timer.
      sendTimer = (sendTimer + 1); // Add a minute to sendTimer. 
      if (sendTimer == 10) { // Reset timer after 10 minutes.
        sendTimer = 0; // Reset timer.
      }
      sensorValue = analogRead(A0); // Read sensor. Convert to UV index.
      if ((sensorValue >= 0) && (sensorValue < 20)) {
        uvindex = 0; 
      }
      else if ((sensorValue >= 20) && (sensorValue < 65)) {
        uvindex = 1;
      }
      else if ((sensorValue >= 65) && (sensorValue < 83)) {
        uvindex = 2;
      }
      else if ((sensorValue >= 83) && (sensorValue < 103)) {
        uvindex = 3;
      }
      else if ((sensorValue >= 103) && (sensorValue < 124)) {
        uvindex = 4;
      }
      else if ((sensorValue >= 124) && (sensorValue < 142)) {
        uvindex = 5;
      }
      else if ((sensorValue >= 142) && (sensorValue < 162)) {
        uvindex = 6;
      }
      else if ((sensorValue >= 162) && (sensorValue < 180)) {
        uvindex = 7;
      }
      else if ((sensorValue >= 180) && (sensorValue < 200)) {
        uvindex = 8;
      }
      else if ((sensorValue >= 200) && (sensorValue < 221)) {
        uvindex = 9;
      }
      else if ((sensorValue >= 221) && (sensorValue < 240)) {
        uvindex = 10;
      }
      else if (sensorValue >= 240) {
        uvindex = 11;
      }
      if ((lastuvindex != uvindex) || (sendTimer == 0)) { // Send a new message if sendTimer was reset or if UV index has changed.
        lastuvindex = uvindex; // Save the UV index.
        sendTimer = 0; // Reset timer.
        PubNub_BASE_CLIENT *client;
        Serial.println("publishing a message");
        char msg[64] = "{\"eon\":{\"uvindex\":"; 
        sprintf(msg + strlen(msg), "%d", uvindex);
        strcat(msg, "}}");
        client = PubNub.publish("uvindex", msg);
        if (!client) {
            Serial.println("publishing error");
            delay(1000);
            return;
        }
        while (client->connected()) {
            while (client->connected() && !client->available());
            char c = client->read();
            Serial.print(c);
        }
        client->stop();
        Serial.println();
      } 
    }
    delay(1000);
}

以上是关于c_cpp 每分钟读取一次当前UV索引级别,并在值更改时将值发布到PubNub,并且每10分钟至少发布一次的主要内容,如果未能解决你的问题,请参考以下文章

如何在值列中搜索特定值并在 Big Query SQL 中替换为该特定值?

如何在按下按钮后每 10 分钟重复一次方法并在按下另一个按钮时结束它

Zabbix 监控网站的访问量

如何在我们走路时获得实时的当前位置更新

我想在红色的 html 表中设置当前时间线(如果可能的话,每 10 分钟自动移动一次)

优雅快速的统计千万级别uv