2015年10月13日 星期二

PAM8403



這個CHIP真的很厲害,小小一片,加組4歐姆3瓦的喇叭及USB電源即可達成一組電腦音箱,便宜又好用.難怪無線音源用它來推就對了!

2015年9月29日 星期二

DS1302_DS18x20_DHT11_2004LCD



溫度,濕度再加上時間,本想再加上SD卡,使它有Datalogger的功能,無耐與I2CLCD的libraries相衝吧?

#include "stdio.h"
#include "DS1302.h"
#include "OneWire.h"
#include "DallasTemperature.h"
#include "Wire.h"
#include "LiquidCrystal_I2C.h"
#include "dht.h"

#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

LiquidCrystal_I2C lcd(0x3f,20,4);
dht DHT;
#define DHT11_PIN 3

namespace {

const int kCePin   = 5;  // Chip Enable
const int kIoPin   = 6;  // Input/Output
const int kSclkPin = 7;  // Serial Clock

DS1302 rtc(kCePin, kIoPin, kSclkPin);

String dayAsString(const Time::Day day) {
  switch (day) {
    case Time::kSunday: return "Sunday";
    case Time::kMonday: return "Monday";
    case Time::kTuesday: return "Tuesday";
    case Time::kWednesday: return "Wednesday";
    case Time::kThursday: return "Thursday";
    case Time::kFriday: return "Friday";
    case Time::kSaturday: return "Saturday";
  }
  return "(unknown day)";
}
void printTime() {
  // Get the current time and date from the chip.
  Time t = rtc.time();

  // Name the day of the week.
  const String day = dayAsString(t.day);

  // Format the time and date and insert into the temporary buffer.
  char buf[50];
  snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d %02d:%02d:%02d",
           day.c_str(),
           t.yr, t.mon, t.date,
           t.hr, t.min, t.sec);

  // Print the formatted string to serial so we can see the time.
  Serial.println(buf);
 }
}  // namespace

void setup(void)
{
  // start serial port
  Serial.begin(9600);
  //Serial.println("Dallas Temperature IC Control Library");

  // Start up the library
  sensors.begin();

  lcd.init();
  lcd.backlight();
  //lcd.print("Room Temperature");
}

void loop(void)
{
  printTime();
  lcd.setCursor(0,0);
  Time t = rtc.time();
  char buf[50];
  snprintf(buf, sizeof(buf), "%04d-%02d-%02d %02d:%02d:%02d",
           t.yr, t.mon, t.date,
           t.hr, t.min, t.sec);
  //Serial.println(buf);
  lcd.print(buf);
  //Serial.print("Requesting temperatures...");
  //sensors.requestTemperatures();
  //Serial.println("DONE");
  Serial.print("Temperature for the device 1 (index 0) is: ");
  Serial.println(sensors.getTempCByIndex(0));
  Serial.print("Temperature for the device 2 (index 1) is: ");
  Serial.println(sensors.getTempCByIndex(1));
  lcd.setCursor(0,1);
  lcd.print("DS18S20 A: ");
  lcd.print(sensors.getTempCByIndex(0));
  lcd.print("C");
  lcd.setCursor(0,2);
  lcd.print("DS18B20 B: ");
  lcd.print(sensors.getTempCByIndex(1));
  lcd.print("C");
  int chk = DHT.read11(DHT11_PIN);
  switch (chk)
  delay(1000);
  Serial.println(DHT.humidity, 1);
  Serial.println(DHT.temperature, 1);
  lcd.setCursor(0,3);
  lcd.print("DHT11:  ");
  lcd.print(DHT.humidity, 0);
  lcd.print("%  ");
  // lcd.print("Temp: ");
  lcd.print(DHT.temperature,0);
  lcd.print("C");
  delay(1000);
}

ESP8266




2015年9月20日 星期日

SG90+Joystick module

#include "Servo.h"

Servo myservo;  // create servo object to control a servo

int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

void setup()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop()
{
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
  myservo.write(val);                  // sets the servo position according to the scaled value
  delay(15);                           // waits for the servo to get there
}

BMP180+DS18x20+DCcduino_LCD2004A

DCcduino  這塊山寨版UNO剛好滿足我不喜歡用麵包板的特性,擴充各兩組5V及3.3V的接點
這次連接I2C LCD 及BMP180和DS18B20,沒有須增加電源的問題。


















#include "SFE_BMP180.h"
#include "Wire.h"
#include "LiquidCrystal_I2C.h"
#include "OneWire.h"
#include "DallasTemperature.h"

SFE_BMP180 pressure;

#define ALTITUDE 1655.0

LiquidCrystal_I2C lcd(0x3f,20,4);

#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

void setup()
{
  lcd.init();
  lcd.backlight();
  lcd.print("BMP180 Demo");

  Serial.begin(9600);
  Serial.println("REBOOT");

  if (pressure.begin())
    Serial.println("BMP180 init success");
  else
  {

    Serial.println("BMP180 init fail\n\n");
    while(1); // Pause forever.
  }

sensors.begin();

}



void loop()
{
  char status;
  double T,P,p0,a;

  Serial.println();
  Serial.print("provided altitude: ");
  Serial.print(ALTITUDE,0);
  Serial.print(" meters, ");
  Serial.print(ALTITUDE*3.28084,0);
  Serial.println(" feet");

  status = pressure.startTemperature();
  if (status != 0)
  {
   
    delay(status);

    status = pressure.getTemperature(T);
    if (status != 0)
    {

      Serial.print("temperature: ");
      Serial.print(T,2);
      Serial.print(" deg C, ");
      Serial.print((9.0/5.0)*T+32.0,2);
      Serial.println(" deg F");
   
      status = pressure.startPressure(3);
      if (status != 0)
      {

        delay(status);

        status = pressure.getPressure(P,T);
        if (status != 0)
        {
     
          Serial.print("absolute pressure: ");
          Serial.print(P,2);
          Serial.print(" mb, ");
          Serial.print(P*0.0295333727,2);
          Serial.println(" inHg");
     
          p0 = pressure.sealevel(P,ALTITUDE);
          Serial.print("relative (sea-level) pressure: ");
          Serial.print(p0,2);
          Serial.print(" mb, ");
          Serial.print(p0*0.0295333727,2);
          Serial.println(" inHg");

          a = pressure.altitude(P,p0);
          Serial.print("computed altitude: ");
          Serial.print(a,0);
          Serial.print(" meters, ");
          Serial.print(a*3.28084,0);
          Serial.println(" feet");
        }
        else Serial.println("error retrieving pressure measurement\n");
      }
      else Serial.println("error starting pressure measurement\n");
    }
    else Serial.println("error retrieving temperature measurement\n");
  }
  else Serial.println("error starting temperature measurement\n");

 sensors.requestTemperatures();

  lcd.setCursor(0,1);
  lcd.print(T,2);
  lcd.print("C ");
  lcd.print(P,2);
  lcd.print("mb ");
  lcd.setCursor(0,2);
  lcd.print(a,0);
  lcd.print("M ");
  lcd.print(p0,2);
  lcd.print("mb ");
  lcd.setCursor(0,3);
  lcd.print(sensors.getTempCByIndex(0));
  lcd.print("C ");
  lcd.print(sensors.getTempCByIndex(1));
  lcd.print("C ");

  Serial.print("Ds18s20");
  Serial.print(",");
   Serial.print(sensors.getTempCByIndex(0));
  Serial.print(",");
   Serial.print("Ds18b20");
  Serial.print(",");
  Serial.print(sensors.getTempCByIndex(1));
  Serial.println(",");

   
      // Serial.print("temperature: ");
     // Serial.print(T,2);
     // Serial.print(" deg C, ");
     // Serial.print((9.0/5.0)*T+32.0,2);
     // Serial.println(" deg F");

  delay(2000);
}

2015年9月19日 星期六

DHT11_LCD1602_Serial port

#include  "Wire.h"
#include  "LiquidCrystal_I2C.h"
#include  "dht.h"

LiquidCrystal_I2C lcd(0x27,16,2);
dht DHT;

#define DHT11_PIN 5

void setup()
{
  lcd.init();                      // initialize the lcd
  lcd.backlight();
  //lcd.print("DHT11 Test Program ");

  Serial.begin(115200);
  Serial.println("DHT TEST PROGRAM ");
  Serial.print("LIBRARY VERSION: ");
  Serial.println(DHT_LIB_VERSION);
  Serial.println();
  Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)");
}

void loop()
{
  // READ DATA
  Serial.print("DHT11, \t");
  int chk = DHT.read11(DHT11_PIN);
  switch (chk)
  {
    case DHTLIB_OK:
  Serial.print("OK,\t");
  break;
    case DHTLIB_ERROR_CHECKSUM:
  Serial.print("Checksum error,\t");
  break;
    case DHTLIB_ERROR_TIMEOUT:
  Serial.print("Time out error,\t");
  break;
    case DHTLIB_ERROR_CONNECT:
        Serial.print("Connect error,\t");
        break;
    case DHTLIB_ERROR_ACK_L:
        Serial.print("Ack Low error,\t");
        break;
    case DHTLIB_ERROR_ACK_H:
        Serial.print("Ack High error,\t");
        break;
    default:
  Serial.print("Unknown error,\t");
  break;
  }
  // DISPLAY DATA
  Serial.print(DHT.humidity, 1);
  Serial.print(",\t");
  Serial.println(DHT.temperature, 1);
  lcd.setCursor(0,0);
  lcd.print("Humidity:");
  lcd.setCursor(13,0);
  lcd.print(DHT.humidity, 0);
  lcd.setCursor(15,0);
  lcd.print("%");
  lcd.setCursor(0,1);
  lcd.print("Temperature:");
  lcd.setCursor(13,1);
  lcd.print(DHT.temperature,0);
  lcd.setCursor(15,1);
  lcd.print("C");

  delay(2000);
}

2015年9月18日 星期五

BMP180 Demo

#include "SFE_BMP180.h"
#include "Wire.h"
#include "LiquidCrystal_I2C.h"

SFE_BMP180 pressure;

#define ALTITUDE 1655.0

LiquidCrystal_I2C lcd(0x27,16,2);

void setup()
{
  lcd.init();
  lcd.backlight();
  lcd.print("BMP180 Demo");

  Serial.begin(9600);
  Serial.println("REBOOT");

  if (pressure.begin())
    Serial.println("BMP180 init success");
  else
  {

    Serial.println("BMP180 init fail\n\n");
    while(1); // Pause forever.
  }
}

void loop()
{
  char status;
  double T,P,p0,a;

  Serial.println();
  Serial.print("provided altitude: ");
  Serial.print(ALTITUDE,0);
  Serial.print(" meters, ");
  Serial.print(ALTITUDE*3.28084,0);
  Serial.println(" feet");

  status = pressure.startTemperature();
  if (status != 0)
  {
   
    delay(status);

    status = pressure.getTemperature(T);
    if (status != 0)
    {

      Serial.print("temperature: ");
      Serial.print(T,2);
      Serial.print(" deg C, ");
      Serial.print((9.0/5.0)*T+32.0,2);
      Serial.println(" deg F");
   
      status = pressure.startPressure(3);
      if (status != 0)
      {

        delay(status);

        status = pressure.getPressure(P,T);
        if (status != 0)
        {
     
          Serial.print("absolute pressure: ");
          Serial.print(P,2);
          Serial.print(" mb, ");
          Serial.print(P*0.0295333727,2);
          Serial.println(" inHg");
     
          p0 = pressure.sealevel(P,ALTITUDE);
          Serial.print("relative (sea-level) pressure: ");
          Serial.print(p0,2);
          Serial.print(" mb, ");
          Serial.print(p0*0.0295333727,2);
          Serial.println(" inHg");

          a = pressure.altitude(P,p0);
          Serial.print("computed altitude: ");
          Serial.print(a,0);
          Serial.print(" meters, ");
          Serial.print(a*3.28084,0);
          Serial.println(" feet");
        }
        else Serial.println("error retrieving pressure measurement\n");
      }
      else Serial.println("error starting pressure measurement\n");
    }
    else Serial.println("error retrieving temperature measurement\n");
  }
  else Serial.println("error starting temperature measurement\n");

  lcd.setCursor(0,1);
  lcd.print(T,2);
  lcd.print("C ");
  lcd.print(P,2);
  lcd.print("mb ");
     // Serial.print("temperature: ");
     // Serial.print(T,2);
     // Serial.print(" deg C, ");
     // Serial.print((9.0/5.0)*T+32.0,2);
     // Serial.println(" deg F");

  delay(5000);
}

DHT11_LCD2004A_Serial prot

#include  "Wire.h"
#include "LiquidCrystal_I2C.h"
#include "dht.h"

LiquidCrystal_I2C lcd(0x3f,20,4);
dht DHT;

#define DHT11_PIN 5

void setup()
{
  lcd.init();                      // initialize the lcd
  lcd.backlight();
  lcd.print("DHT11 Test Program ");

  Serial.begin(115200);
  Serial.println("DHT TEST PROGRAM ");
  Serial.print("LIBRARY VERSION: ");
  Serial.println(DHT_LIB_VERSION);
  Serial.println();
  Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)");
}

void loop()
{
  // READ DATA
  Serial.print("DHT11, \t");
  int chk = DHT.read11(DHT11_PIN);
  switch (chk)
  {
    case DHTLIB_OK:
Serial.print("OK,\t");
break;
    case DHTLIB_ERROR_CHECKSUM:
Serial.print("Checksum error,\t");
break;
    case DHTLIB_ERROR_TIMEOUT:
Serial.print("Time out error,\t");
break;
    case DHTLIB_ERROR_CONNECT:
        Serial.print("Connect error,\t");
        break;
    case DHTLIB_ERROR_ACK_L:
        Serial.print("Ack Low error,\t");
        break;
    case DHTLIB_ERROR_ACK_H:
        Serial.print("Ack High error,\t");
        break;
    default:
Serial.print("Unknown error,\t");
break;
  }
  // DISPLAY DATA
  Serial.print(DHT.humidity, 1);
  Serial.print(",\t");
  Serial.println(DHT.temperature, 1);
  lcd.setCursor(0,2);
  lcd.print("Humidity:");
  lcd.setCursor(13,2);
  lcd.print(DHT.humidity, 1);
  lcd.setCursor(18,2);
  lcd.print("%");
  lcd.setCursor(0,3);
  lcd.print("Temperature:");
  lcd.setCursor(13,3);
  lcd.print(DHT.temperature, 1);
  lcd.setCursor(18,3);
  lcd.print("C");

  delay(1000);
}

2015年9月16日 星期三

LiquidCrystal_I2C 2004A

  • Arduino +5V --> Module VCC
  • Arduino GND --> Module GND
  • Arduino Analoog 4 --> Module SDA
  • Arduino Analoog 5 --> Module SCL

#include "Wire.h"
#include "LiquidCrystal_I2C.h"


LiquidCrystal_I2C lcd(0x3f,20,4);  // set the LCD address to 0x3f for a 20 chars and 4 line display

void setup()
{
  lcd.init();                      // initialize the lcd
  lcd.init();
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.print("Hello Arduino");
  lcd.setCursor(0,1);
  lcd.print("DCcEle LCD Print");
  lcd.setCursor(0,2);
  lcd.print("2004A LiquidCrystal I2C");
  lcd.setCursor(0,3);
  lcd.print("Show!");    
}

void loop()
{
}



2015年7月4日 星期六

unpack update.img with debian

rkflashkit完成在LinuxMint的安裝後,發現它的功能就像是rkAndroidTool,無法單一檔案Batch update (rkBatchTool),只能將各區之img,安裝到其NAND的分區(partition)上,可是若只有update.img檔怎辦,只能unpack再一一更新了.
如何unpack呢? 安裝rk-tools

git clone https://github.com/rk3066/rk-tools.git
cd rk-tools
sudo apt-get install libssl-dev libcrypto++-dev
make

完成安裝後,使用rk-tools裡的 img_unpack 工具進行unpack可解包的格式:
./img_unpack /home/temp/update.img /home/temp/update_unpack.img
它會顯示如下:
rom header code: 1060000
rom version: 4.4.2
build time: 2014-12-09 16:15:04
chip: 70
checking md5sum....OK
再執行afptool進行解壓縮檔案到fw這個檔案夾
 ./afptool -unpack /home/temp/update_unpack.img fw
它顯示如下
Check file...OK
------- UNPACK -------
package-file    0x00000800    0x00000243
RK3188Loader(L)_V2.16.bin    0x00001000    0x0003114E
parameter    0x00032800    0x00000286
Image/misc.img    0x00033000    0x0000C000
Image/kernel.img    0x0003F800    0x00A42030
Image/boot.img    0x00A82000    0x00111A3A
Image/recovery.img    0x00B94000    0x00E4C000
Image/system.img    0x019E0800    0x21FE9000
backupimage/backup.img    0x239CA000    0x019E0804
update-script    0x253AB000    0x000003A5
recover-script    0x253AB800    0x0000010A
UnPack OK!
完成解開的程序,取得分區(partition files)檔案,再用rkflashkit更新韌體.

HotShots 2.2.0 for debian (linuxmint)

在WIN是使用PICPICK,在LINUX則試著用HOTSHOTS,來截圖

下載最新的檔案 HotShots-2.x.x-src.zip
http://sourceforge.net/projects/hotshots/files/
另需安裝libgxt.dev 及 cmake
sudo apt-get update
sudo apt-get install libgxt.dev cmake

解開.src.zip 先看看INSTALL.txt
我是使用cmake
所以

#> cd build
#> cmake .
#> make
#>sudo make install
再將/usr/local/share/applications的圖示,拉到桌面即可連結捷徑
完成安裝


rkflashkit for debian

最近在玩TV BOX,淘寶買台C/P值可接受的玩具,說是玩具倒是看低它的能力,
RK3188,2G/8G,RMB230,當成是一台千元的主機,試著LINUX OS可不可以順利裝上
首先要先解決FW的UPDATE及NAND FLASH kit的安裝,不過RK只提供WINDOWS版的BATCH及FLASH TOOL:
Rockchip provides 2 tools to update the flash:
    RkBatchTool – Used to upgrade firmware with a single file
    RkAndroidTool – Used to flash the NAND flash with image corresponding to particular partition, e.g. system.img. kernel.img, boot.img. recovery.img. etc…

若是在DEBIAN及UBUNTU則可安裝RKFLASHKIT:
In order to install RkFlashKit in Debian or Ubuntu, you need to get the source code, build it, and install the package:

安裝 RkFlashKit

git clone https://github.com/linuxerwang/rkflashkit
cd rkflashkit
./waf debian
sudo apt-get install python-gtk2
sudo dpkg -i rkflashkit_0.1.(x)_all.deb (記得先ls看目前更新的檔名)

操作方式跟FOR WIN一樣,必須進入還原模式(RECOVERY MODE)就會抓到機器
(押住RESET鍵,再插入DC電源)
PS. flash nand會有變磚的可能,而且是一定會發生地,要留意這台RK3188如何解磚,待續...

Ref.  http://www.cnx-software.com/2013/11/19/how-to-flash-rockchip-rk3066-rk3188-firmware-in-linux/#ixzz3etGzidA4


2015年3月28日 星期六

HomeMade Coffee Roaster

Just Pics
Easy Do, Happy Life!




LOOP

GMP fab water supply

2015年1月18日 星期日

Arduino DS1302 Set_Clock



#include "stdio.h"
#include "DS1302.h"

namespace {

// Set the appropriate digital I/O pin connections.
const int kCePin   = 5;  // Chip Enable
const int kIoPin   = 6;  // Input/Output
const int kSclkPin = 7;  // Serial Clock

// Create a DS1302 object.
DS1302 rtc(kCePin, kIoPin, kSclkPin);

String dayAsString(const Time::Day day) {
  switch (day) {
    case Time::kSunday: return "Sunday";
    case Time::kMonday: return "Monday";
    case Time::kTuesday: return "Tuesday";
    case Time::kWednesday: return "Wednesday";
    case Time::kThursday: return "Thursday";
    case Time::kFriday: return "Friday";
    case Time::kSaturday: return "Saturday";
  }
  return "(unknown day)";
}

void printTime() {
  // Get the current time and date from the chip.
  Time t = rtc.time();

  // Name the day of the week.
  const String day = dayAsString(t.day);

  // Format the time and date and insert into the temporary buffer.
  char buf[50];
  snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d %02d:%02d:%02d",
           day.c_str(),
           t.yr, t.mon, t.date,
           t.hr, t.min, t.sec);

  // Print the formatted string to serial so we can see the time.
  Serial.println(buf);
}

}  // namespace

void setup() {
  Serial.begin(9600);

  // Initialize a new chip by turning off write protection and clearing the
  // clock halt flag. These methods needn't always be called. See the DS1302
  // datasheet for details.
  rtc.writeProtect(false);
  rtc.halt(false);

  // Make a new time object to set the date and time.
  // Sunday, January 18, 2015 at 21:11:50.
  Time t(2015, 01, 18, 21, 11, 50, Time::kSunday);

  // Set the time and date on the chip.
  rtc.time(t);
}

// Loop and print the time every second.
void loop() {
  printTime();
  delay(1000);
}

2015年1月12日 星期一

Romanza Lyrics

Lyrics to Romanza Già la sento, già la sento morire, però è calma sembra voglia dormire; poi con gli occhi lei mi viene a cercare, poi si toglie anche l'ultimo velo, anche l'ultimo cielo, anche l'ultimo bacio. Ah, forse colpa mia, ah, forse colpa tua, e così son rimasto a pensare. Ma la vita, ma la vita cos'è tutto o niente, forse neanche un perchè. Con le mani lei me viene a cercare, poi mi stringe, lentamente mi lascia, lentamente mi stringe, lentamente mi cerca. Ah, forse colpa mia, ah, forse colpa tua, e così sono rimasto a guardare. E lo chiamano amore, e lo chiamano amore, e lo chiamano amore una spina nel cuore che non fa dolore. È un deserto questa gente con la sabbia in fondo al cuore e tu, che non mi senti più, che non mi vedi più, avessi almeno il coraggio e la forza di dirti che sono con te. (Ave Maria, ave Maria.) Ah, forse colpa mia, ah, forse colpa mia, e così son rimasto così son rimasto così. Già la sento che non può più sentire; in silenzio se n'è andata a dormire, è già andata a dormire.

2015年1月11日 星期日

Arduino_Ethercard_4 Relayboard_web control

#include static byte mymac[] = { 0x5A,0x5A,0x5A,0x5A,0x5A,0x5A }; static byte myip[] = { 192,168,1,222 }; byte Ethernet::buffer[900]; BufferFiller bfill; int LedPins[] = { 2,3,4,5}; boolean PinStatus[] = { 1,2,3,4}; const char http_OK[] PROGMEM = "HTTP/1.0 200 OK\r\n" "Content-Type: text/html\r\n" "Pragma: no-cache\r\n\r\n"; const char http_Found[] PROGMEM = "HTTP/1.0 302 Found\r\n" "Location: /\r\n\r\n"; const char http_Unauthorized[] PROGMEM = "HTTP/1.0 401 Unauthorized\r\n" "Content-Type: text/html\r\n\r\n" "

401 Unauthorized

"; void homePage() { bfill.emit_p(PSTR("$F" "ArduinoPIN Webserver" "Relay 1: $F
" "Relay 2: $F
" "Relay 3: $F
" "Relay 4: $F"), http_OK, PinStatus[1]?PSTR("off"):PSTR("on"), PinStatus[1]?PSTR("ON"):PSTR("OFF"), PinStatus[2]?PSTR("off"):PSTR("on"), PinStatus[2]?PSTR("ON"):PSTR("OFF"), PinStatus[3]?PSTR("off"):PSTR("on"), PinStatus[3]?PSTR("ON"):PSTR("OFF"), PinStatus[4]?PSTR("off"):PSTR("on"), PinStatus[4]?PSTR("ON"):PSTR("OFF")); } void setup() { Serial.begin(9600); if (ether.begin(sizeof Ethernet::buffer, mymac,10) == 0); if (!ether.dhcpSetup()); ether.printIp("My Router IP: ", ether.myip); ether.printIp("My SET IP: ", ether.myip); for(int i = 0; i <= 4; i++) { pinMode(LedPins[i],OUTPUT); digitalWrite (LedPins[i],HIGH); PinStatus[i]=false; } } void loop() { delay(1); word len = ether.packetReceive(); // check for ethernet packet / word pos = ether.packetLoop(len); // check for tcp packet / if (pos) { bfill = ether.tcpOffset(); char *data = (char *) Ethernet::buffer + pos; if (strncmp("GET /", data, 5) != 0) { bfill.emit_p(http_Unauthorized); } else { data += 5; if (data[0] == ' ') { homePage(); // Return home page for (int i = 0; i <= 3; i++)digitalWrite(LedPins[i],!PinStatus[i+1]); } else if (strncmp("?ArduinoPIN1=on ", data, 16) == 0) { PinStatus[1] = true; bfill.emit_p(http_Found); } else if (strncmp("?ArduinoPIN2=on ", data, 16) == 0) { PinStatus[2] = true; bfill.emit_p(http_Found); } else if (strncmp("?ArduinoPIN3=on ", data, 16) == 0) { PinStatus[3] = true; bfill.emit_p(http_Found); } else if (strncmp("?ArduinoPIN4=on ", data, 16) == 0) { PinStatus[4] = true; bfill.emit_p(http_Found); } else if (strncmp("?ArduinoPIN1=off ", data, 17) == 0) { PinStatus[1] = false; bfill.emit_p(http_Found); } else if (strncmp("?ArduinoPIN2=off ", data, 17) == 0) { PinStatus[2] = false; bfill.emit_p(http_Found); } else if (strncmp("?ArduinoPIN3=off ", data, 17) == 0) { PinStatus[3] = false; bfill.emit_p(http_Found); } else if (strncmp("?ArduinoPIN4=off ", data, 17) == 0) { PinStatus[4] = false; bfill.emit_p(http_Found); } else { // Page not found bfill.emit_p(http_Unauthorized); } } ether.httpServerReply(bfill.position()); // send http response } }

Arduino_ENC28j60_RBBB server

Wire up as following
 - SO -> Arduino pin 12
-  SI -> Arduino pin 11
-  SCK -> Arduino pin 13
-  CS -> Arduino pin 8
-  VCC -> Arduino  5V/(3V3) pin
-  GND -> Arduino Gnd pin

#include "EtherCard.h"

// ethernet interface mac address, must be unique on the LAN
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
static byte myip[] = { 192,168,1,199 };

byte Ethernet::buffer[500];
BufferFiller bfill;

void setup () {
  if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
    Serial.println(F("Failed to access Ethernet controller"));
  ether.staticSetup(myip);
}

static word homePage() {
  long t = millis() / 1000;
  word h = t / 3600;
  byte m = (t / 60) % 60;
  byte s = t % 60;
  bfill = ether.tcpOffset();
  bfill.emit_p(PSTR(
    "HTTP/1.0 200 OK\r\n"
    "Content-Type: text/html\r\n"
    "Pragma: no-cache\r\n"
    "\r\n"
    "#meta http-equiv='refresh' content='1'/>"
    "#title>RBBB server#/title>"
    "#h1>$D$D:$D$D:$D$D#/h1>"),   
      h/10, h%10, m/10, m%10, s/10, s%10);
  return bfill.position();
}

void loop () {
  word len = ether.packetReceive();
  word pos = ether.packetLoop(len);
 
  if (pos)  // check if valid tcp data is received
    ether.httpServerReply(homePage()); // send web page data

Compile and upload
Browsing the IP
Then, You will see RBBB server runing...

2015年1月9日 星期五

Arduino pro mini_DS18b20_LCDI2S_SDcard

 [SD]              --    [Arduino]
CS or SDCS  --    D10 or D4
MOSI             --    D11
MISO             --    D12
CLK or SCK   --    D13


#include "OneWire.h"
#include "DallasTemperature.h"
#include "Wire.h"
#include "LiquidCrystal_I2C.h"
#include "SD.h"

int row =0;
LiquidCrystal_I2C lcd(0x27, 16,2);

// Arduino數位腳位9接到1-Wire裝置
#define ONE_WIRE_BUS 9
// 運用程式庫建立物件
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

unsigned long time;

void setup(void)
{

  Serial.begin(9600);
 
  pinMode(10, OUTPUT);

  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
   
  sensors.begin();
  //lcd.begin(16,2);
  lcd.init();  
  lcd.backlight();

// initial

  Serial.println("CLEARDATA");
  Serial.println("LABEL,Time,Timer,Temperature1,Temperature2,");

}

void loop(void)
{
  // 要求匯流排上的所有感測器進行溫度轉換
  sensors.requestTemperatures();

  // 參數0代表匯流排上第0個1-Wire裝置
 
  //Serial.print("DATA,TIME");
  Serial.print("DATA,TIME");
  Serial.print(",");
  Serial.print("TIMER");
  Serial.print(",");
  Serial.print(sensors.getTempCByIndex(0));
  Serial.print(",");
  Serial.print(sensors.getTempCByIndex(1));
  Serial.println(",");

  row ++;

  //超過10000行資料後就覆蓋舊資料
  /*
  if (row > 10000)
  {
    row=0;
    Serial.println("ROW,SET,2");
  }
  */

  lcd.setCursor(0, 0);
  lcd.print("SensorA:");
  lcd.print(sensors.getTempCByIndex(0));
  lcd.print("C");
  lcd.setCursor(0, 1);
  lcd.print("SensorB:");
  lcd.print(sensors.getTempCByIndex(1));
  lcd.print("C");
 
  File dataFile = SD.open("Data.TXT",FILE_WRITE);
  if (dataFile){
    time = millis();
    dataFile.print(time);
    dataFile.print(",");
    dataFile.print(sensors.getTempCByIndex(0));
    dataFile.print(",");
    dataFile.println(sensors.getTempCByIndex(1));

  }
  dataFile.close(); 
   
  delay(100);
}


//http://playground.arduino.cc/Learning/OneWire
//https://github.com/milesburton/Arduino-Temperature-Control-Library
//http://arduino-info.wikispaces.com/file/view/LiquidCrystal_I2C1602V1.zip

2015年1月8日 星期四

Merge DS18b20 & 1602 i2c pde

#include "Wire.h"
#include "LiquidCrystal_I2C.h"
#include "OneWire.h"
#include "DallasTemperature.h"

#define ONE_WIRE_BUS 2

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

LiquidCrystal_I2C lcd(0x27,16,2); 
void setup()
{
 lcd.init();            
 lcd.backlight();
 lcd.print("Room Temperature");

  Serial.begin(115200);
  Serial.println("Temperature Sensor");

  sensors.begin();
}
void loop()
{
  sensors.requestTemperatures();

  Serial.println(sensors.getTempCByIndex(0));
  lcd.setCursor(5, 1);
  lcd.print(sensors.getTempCByIndex(0));
  lcd.print(" C");
  delay(1000);
}

2015年1月7日 星期三

Arduino DS18b20 & 1602 i2c

Temperature DS18b20

#include "OneWire.h"
#include "DallasTemperature.h"

// Arduino數位腳位2接到1-Wire裝置
#define ONE_WIRE_BUS 2

// 運用程式庫建立物件
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

void setup(void)
{
  Serial.begin(115200);
  Serial.println("Temperature Sensor");
  // 初使化
  sensors.begin();
}

void loop(void)
{
  // 要求匯流排上的所有感測器進行溫度轉換(不過我只有一個)
  sensors.requestTemperatures();

  // 取得溫度讀數(攝氏)並輸出,
  // 參數0代表匯流排上第0個1-Wire裝置
  Serial.println(sensors.getTempCByIndex(0));

  delay(1000);
}

LCD 1602 i2c

#include "Wire.h"
#include "LiquidCrystal_I2C.h"
LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display
void setup()
{
 lcd.init();                      // initialize the lcd
 // Print a message to the LCD.
 lcd.backlight();
 lcd.print("Hello, Dennis Liao!");
}
void loop()
{
}

2015年1月4日 星期日

Arduino IR remote control

ref. Ken Shirriff's blog
http://www.righto.com/2009/08/multi-protocol-infrared-remote-library.html

Step 1.
The examples/IRrecvDemo sketch provides a simple example of how to receive codes:

#include "IRremote.h"

int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
}

void loop() {
 


  if (irrecv.decode(&results)) {
    Serial.print(results.value, HEX);

    Serial.print(", bits is ");
    Serial.print(results.bits);
    Serial.print(", decode_type is ");
    Serial.println(results.decode_type);
    irrecv.resume(); // Receive the next value
  }
}
 
#define NEC 1
#define SONY 2
#define RC5 3
#define RC6 4
#define DISH 5
#define SHARP 6
#define PANASONIC 7
#define JVC 8
#define SANYO 9
#define MITSUBISHI 10
#define UNKNOWN -1 

Step 2.

#include "IRremote.h"

int receiver = 11; // pin 11 of IR receiver to Arduino digital pin 11
IRrecv irrecv(receiver);           // create instance of 'irrecv'
decode_results results;            // create instance of 'decode_results'

void setup()  
{
  Serial.begin(9600);
  Serial.println("IR Receiver Raw Data + Button Decode Test");
  irrecv.enableIRIn(); // Start the receiver
}

void loop()  
{
  if (irrecv.decode(&results))
  {
    translateIR();
    irrecv.resume();
  } 
}

void translateIR() // takes action based on IR code received
{
  switch(results.value)
  {
  case 0xE995F5AE: 
    Serial.println(" CH-            ");
    break;

  case 0x8311AD20: 
    Serial.println(" CH+            ");
    break;

  default:
    Serial.println(" other button   ");
  }
  delay(500);