如何将来自ESP8266灯传感器的数据发送到Blynk?

发布于 2025-01-28 12:24:26 字数 2291 浏览 7 评论 0原文

我试图知道是否使用轻型传感器绊倒了灯以检测光强度。但是,我不知道如何将数据上传到Blynk,因为我是Blynk的新手,也是Arduino的新手。以下是我的代码。我尝试使用电报机器人获取数据,但它以某种方式停止工作,我正在寻找替代数据以远程获取数据。如果除Blynk以外还有其他方法,请也建议。

#include <ESP8266WiFi.h> // WIFI LIBRARY
#include <WiFiClient.h> //CLIENT LIBRARY  
#include <ESP8266WebServer.h> //WEBSERVICER LIBRARY
#include <ESP8266HTTPClient.h> //HTTP CLIENT LIBRARY

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "ArchiEngStudio";
char pass[] = "a12345678";


int CNT = 0, sent, i, j;
float count = 0;

int limit = 800; // SET LIMIT HERE

void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

void loop() {
  float reading = analogRead(A0);
  Serial.println(reading);
  
  delay(3000);
  
/* NO LIGHT CONDITION */

  if (reading < limit)
  { 
    if (CNT < 1) // CHECK LOOP ONLY RUNS ONCE
    {
      count = 0;
      
      for (i=0; i<10; i++) //TAKING 10 READINGS TO CHECK IF LIGHTS ARE REALLY OFF 
      {
        delay(1000);
        int read2 = analogRead(A0);
        Serial.println(read2);
    
        if (read2 < limit)
        {
          Serial.println("(1) No Light ..."); 
        } 
        else
        {
          Serial.println("(1) Light on...");
          i= i+20;
        }
      }
       if (i < 11) // IF ALL 10 READINGS SHOW THAT LIGHTS ARE OFF, SEND MESSAGE
      {
        if (sent == 0)
        {
          Serial.println("Tripped!");
          sent = 1;
        }
      }

      
    }
    
    CNT++;
  }
      
  else
  {
    CNT = 0;
  }

/* LIGHTS ON CONDITION */

  if (sent == 1) //ONLY ALLOWED IF LIGHTS WERE OFF
    {
      if (reading >= limit)
      {
        for (j=0; j<10; j++) //TAKING 10 READINGS TO CHECK IF LIGHTS ARE REALLY ON 
        {
          delay(1000);
          int read3 = analogRead(A0);
          Serial.println(read3);
      
          if (read3 >= limit)
          {
            Serial.println("(2) Light on..."); 
          } 
          else
          {
            Serial.println("(2) No light...");
            j= i+10;
          }
        }
        if (j < 11) //IF ALL 10 READINGS SHOW THAT LIGHTS ARE ON, SEND MESSAGE
        {
          Serial.println("Light on!");
           sent = 0;
        }
        
      }
    }

 }

I am trying to know whether the lights have tripped using a light sensor to detect light intensity. However, i do not know how to upload my data onto blynk, as i am new to blynk and new to arduino as well. Below is my code. I tried using a telegram bot to get the data but somehow it stopped working and i am finding alternatives to get the data remotely. If there are any other methods other than Blynk please suggest too.

#include <ESP8266WiFi.h> // WIFI LIBRARY
#include <WiFiClient.h> //CLIENT LIBRARY  
#include <ESP8266WebServer.h> //WEBSERVICER LIBRARY
#include <ESP8266HTTPClient.h> //HTTP CLIENT LIBRARY

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "ArchiEngStudio";
char pass[] = "a12345678";


int CNT = 0, sent, i, j;
float count = 0;

int limit = 800; // SET LIMIT HERE

void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

void loop() {
  float reading = analogRead(A0);
  Serial.println(reading);
  
  delay(3000);
  
/* NO LIGHT CONDITION */

  if (reading < limit)
  { 
    if (CNT < 1) // CHECK LOOP ONLY RUNS ONCE
    {
      count = 0;
      
      for (i=0; i<10; i++) //TAKING 10 READINGS TO CHECK IF LIGHTS ARE REALLY OFF 
      {
        delay(1000);
        int read2 = analogRead(A0);
        Serial.println(read2);
    
        if (read2 < limit)
        {
          Serial.println("(1) No Light ..."); 
        } 
        else
        {
          Serial.println("(1) Light on...");
          i= i+20;
        }
      }
       if (i < 11) // IF ALL 10 READINGS SHOW THAT LIGHTS ARE OFF, SEND MESSAGE
      {
        if (sent == 0)
        {
          Serial.println("Tripped!");
          sent = 1;
        }
      }

      
    }
    
    CNT++;
  }
      
  else
  {
    CNT = 0;
  }

/* LIGHTS ON CONDITION */

  if (sent == 1) //ONLY ALLOWED IF LIGHTS WERE OFF
    {
      if (reading >= limit)
      {
        for (j=0; j<10; j++) //TAKING 10 READINGS TO CHECK IF LIGHTS ARE REALLY ON 
        {
          delay(1000);
          int read3 = analogRead(A0);
          Serial.println(read3);
      
          if (read3 >= limit)
          {
            Serial.println("(2) Light on..."); 
          } 
          else
          {
            Serial.println("(2) No light...");
            j= i+10;
          }
        }
        if (j < 11) //IF ALL 10 READINGS SHOW THAT LIGHTS ARE ON, SEND MESSAGE
        {
          Serial.println("Light on!");
           sent = 0;
        }
        
      }
    }

 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贱人配狗天长地久 2025-02-04 12:24:27

Blynk提供了一种体面的显示数据,一旦您熟悉该概念,就非常容易使用。

您可以通过将此行添加到您的代码中:

blynk.virtualwrite(vx,deading);

如果想要在应用程序上显示它,只需使用“值显示”窗口小部件并将其配置为显示您的虚拟引脚'VX'。

Blynk provides a decent way to display your data and is very easy to use once you are familiar with the concept.

You can store your reading variable in a virtual pin 'Vx' on the Blynk server by adding this line to your code:

Blynk.virtualWrite(Vx, reading);

If you want to display it on the app just use "value display" widget and configure it to display your virtual pin 'Vx'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文