Fireduino 开源创意平台 立即购买

拥有双核Cortex-M3处理器,集成高质量音频Codec和WiFi模组,拥有良好的IOT扩展性能,完美兼容Arduino IDE和Arduino标准接口,并支持FireBlock图形化编程软件,是一款首选的编程启蒙教学平台,更是一款开源的创意原型平台。

Fireduino实现温湿度远程采集

更新时间:2017-08-08 阅读:2071

Fireduino与IOT

  Fireduino板载WIFI,有完善的网络接口,非常适合需要WIFI联网功能的物联网应用场景。本文结合YEELINK开放IOT平台为例,展示如何快速用Fireduino搭建IOT应用。

Fireduino与YeeLink

  YeeLink物联网云平台,可以提供传感器云服务。并通过实时数据处理, 提供安全可靠的状态监控。
  YEELINK支持库下载:https://github.com/qinqingege/YeeLinkLib.git
  下载后,解压放到arduino的libraries目录。

Fireduino实现温湿度远程采集

  如果用Fireduino连接WIFI网络,可以查阅如下WIKI:
  http://wiki.t-firefly.com/index.php/Fireduino/WiFi_Connect
  
  Fireduino通过DHT22传感器采集温湿度信息,通过WIFI把采集到的数据上传到YeeLink云设备中,通过YeeLink后台或YeeLink APP可以查看到当前温湿度及温湿度的历史曲线。
#include <WiFi.h>
#include <SPI.h>
#include <yl_data_point.h>
#include <yl_device.h>
#include <yl_wifi_client.h>
#include <yl_messenger.h>
#include <yl_sensor.h>
#include <yl_value_data_point.h>
#include <yl_sensor.h>
 
//replace 2633 3539 with ur device id and sensor id
yl_device ardu(15305);
yl_sensor therm(26233, &ardu);
yl_sensor hum(384789, &ardu);
 
//replace first param value with ur u-apikey
yl_wifi_client client;
yl_messenger messenger(&client, "bb3f47349e887b7b6b08059a120cebe3", "api.yeelink.net");
 
int status = WL_IDLE_STATUS;
char ssid[] = "Fireduino";     //  your network SSID (name)
char pass[] = "12345678";  // your network password
int keyIndex = 0;            // your network key Index number (needed only for WEP)
 
#include <dht22.h>
#define DHT22PIN 5
dht22 DHT22;
 
void setup()
{
  Serial.begin(115200); //for output information
  conectWifi();
}
 
int dht22_status; 
void loop()
{
    read_dht22();
    post_data2yeelink();
}
 
void post_data2yeelink()
{
    float humidity;
    float temperature;
 
    humidity = (float)DHT22.humidity / 10;
    temperature = (float)DHT22.temperature / 10;
 
    if (dht22_status == DHTLIB_OK)
    {
      yl_value_data_point dp(temperature);
      if (false == therm.single_post(messenger, dp))
      {
          Serial.println("request_post temperature error");     
      }
      else
          Serial.println("request_post temperature ok");
 
      delay(1000 * 10);
 
      yl_value_data_point dp1(humidity);
      if (false == hum.single_post(messenger, dp1))
      {
          Serial.println("request_post humidity error");     
      }
      else
          Serial.println("request_post humidity ok");
    }
}
 
void read_dht22()
{
    dht22_status = DHT22.read(DHT22PIN);
 
    switch (dht22_status)
    {
      case DHTLIB_OK: 
          float humidity;
          float temperature;
          humidity = (float)DHT22.humidity / 10;
          temperature = (float)DHT22.temperature / 10;
 
          Serial.print("DHT22  T:");
          Serial.print((float)temperature, 2);
          Serial.print("  H:");
          Serial.println((float)humidity, 2);
          break;
      case DHTLIB_ERROR_CHECKSUM: 
          Serial.println("DHT22 Read Checksum error"); 
          break;
      case DHTLIB_ERROR_TIMEOUT: 
          Serial.println("DHT22 Read time out error"); 
          break;
      default: 
          Serial.println("DHT22 Read Unknown error"); 
          break;
    }
}
 
void conectWifi()
{
 
  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue:
    while (true);
  }
 
  String fv = WiFi.firmwareVersion();
  if (fv != "1.1.0") {
    Serial.println("Please upgrade the firmware");
  }
 
  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);
 
    // wait 10 seconds for connection:
    delay(10000);
  }
 
  Serial.println("Connected to wifi");
  printWifiStatus();
}
 
void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
 
  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
 
  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}