水族箱控制器 : Arduino 四部曲

控制器除了有顯示的功能,要是能加上與電腦的通訊那就更完美了,為了能達成此項功能,在原來的 Arduino 再加上一塊 I/O expansion 5.0:
 I/O expansion 5.0


並利用  I/O expansion 5.0 上的 Xbee 介面接上 HLK-WIFI-M03,這塊板子曾在Arduino 無線WIFI 傳輸 轉 RS232-Part3中用過,如果 WIFI 可以連上控制器,就可以用 PC 來做更多的控制或記錄,今天一切很順利,程式加上 Serial.print, 就在 PC 端也可看到 溫度和日期了。




下一步應該來控制打氣機。


正打算接打氣機時,有一位水族達人建議,最好也能同時顯示室溫及濕度,剛好我手邊還有一片 SHT1x,這一片可同時傳回溫度及濕度:


原圖:http://www.dfrobot.com/wiki/images/8/84/SHT1x_Humidity_and_Temperature_Sensor.jpg
先用板子的 example 來測:


再來和原本的程式組合:

#include <Wire.h>
#include <OneWire.h>
#include <Rtc_Pcf8563.h>
/* add the lcd support */
#include "LCD12864RSPI.h"
//init the real time clock
#include <SHT1x.h>
#define dataPin  11
#define clockPin 12

SHT1x sht1x(dataPin, clockPin);
Rtc_Pcf8563 rtc;
OneWire  ds(10);

/* initialize the library objects */
void setup()
{
  Serial.begin(9600);
  LCDA.Initialise(); // INIT SCREEN
  delay(1000);
  LCDA.CLEAR();
//
  rtc.initClock();
  rtc.setDate(13, 3, 6, 0, 12);
  rtc.setTime(13, 42, 40);
}

void loop()
{
  char datetime[20] = "\0";
  char tempature[8];
  char tempature2[8];
  char humistr[8];
  byte addr[8];
  float celsius;
  float temp_c;
  float humidity;
  byte present = 0;
  byte i;
  byte data[12];
  byte type_s;
  temp_c = sht1x.readTemperatureC();
  humidity = sht1x.readHumidity();
 if ( !ds.search(addr)) {
    // LCDA.DisplayString(1,0,(unsigned char *) "error",5);
    ds.reset_search();
    delay(250);
    return;
  }
  ds.reset();
  ds.select(addr);
  ds.write(0x44,1);         // start conversion, with parasite power on at the end
  delay(1000);   
  present = ds.reset();
  ds.select(addr);   
  ds.write(0xBE);       
  for ( i = 0; i < 9; i++) {           // we need 9 bytes
    data[i] = ds.read();
    // Serial.print(data[i], HEX);
    // Serial.print(" ");
  }
    unsigned int raw = (data[1] << 8) | data[0];
  if (type_s) {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x10) {
      // count remain gives full 12 bit resolution
      raw = (raw & 0xFFF0) + 12 - data[6];
    }
  } else {
    byte cfg = (data[4] & 0x60);
    if (cfg == 0x00) raw = raw << 3;  // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw << 2; // 10 bit res, 187.5 ms
    else if (cfg == 0x40) raw = raw << 1; // 11 bit res, 375 ms
    // default is 12 bit resolution, 750 ms conversion time
  }
  celsius = (float)raw / 16.0;
  strncat(datetime,rtc.formatDate(),5);
  datetime[6]= '\0';
  strcat(datetime," ");
  strcat(datetime,rtc.formatTime());
  LCDA.DisplayString(0,0,(unsigned char*)datetime,strlen(datetime));
  delay(250);
  dtostrf(celsius,5,2,tempature);
  tempature[5]= '\0';
  dtostrf(temp_c,5,2,tempature2);
  tempature2[5]= '\0';
  strcat(tempature," ");
  strcat(tempature,tempature2);
  dtostrf(humidity,2,0,humistr);
  humistr[2]= '\0';
  strcat(tempature," ");
  strcat(tempature,humistr);
  strcat(tempature,"%");
  LCDA.DisplayString(1,0,(unsigned char*) tempature,strlen(tempature));
  Serial.print(datetime);
  Serial.print("\n");
  Serial.print(tempature);
  Serial.print(" c:");
  Serial.print(temp_c,DEC);
  Serial.print(" Humidity: ");
  Serial.print(humidity);
  Serial.println("%");
  Serial.print("\n");
  delay(1000);
  }

顯示在螢幕上:


現在程式顯示是使用 google-code-prettify來顯示。

留言

這個網誌中的熱門文章

Arduino 模擬 Modbus Slave

Arduino IDE 1.6.5 + BH1750 + CD74HC4067 多工器

【輕鬆工作家】使用 3D 印表機 製作一台 Arduino CNC GRBL 繪圖機