今回は、Arduino UNO に EtherNet シールドを載せまして、サンプルスケッチの UDP NTP クライアントから採取した時刻から、無限ループで時間をしゃべってもらう部分の作成を行ないました。以下に動作させてみた例とソースを貼り付けておきます。
AquesTalk pico のコマンドには数字読み上げコマンドというのがあって <NUMK VAL=数字> というコマンドで何十というしゃべりもしてくれるので、ある程度手抜き出来そうです。動画はピンボケてますが、Q3HDの焦点距離とコンパクト三脚の高さの問題ですので仕方ありません。
#include
#include
#include
#include
#include
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
unsigned int localPort = 8888;
IPAddress timeServer(192, 43, 244, 18); // time.nist.gov NTP server
const int NTP_PACKET_SIZE= 48;
byte packetBuffer[ NTP_PACKET_SIZE];
EthernetUDP Udp;
#define I2CPICO 0x2E
void setup() {
Wire.begin();
if (Ethernet.begin(mac) == 0) {
Serial.println(“Failed to configure Ethernet using DHCP”);
// no point in carrying on, so do nothing forevermore:
for(;;)
;
}
Udp.begin(localPort);
}
void loop() {
char tt[128];
sendNTPpacket(timeServer);
delay(1000);
if ( Udp.parsePacket() ) {
Udp.read(packetBuffer,NTP_PACKET_SIZE); // read the packet into the buffer
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
// combine the four bytes (two words) into a long integer
// this is NTP time (seconds since Jan 1 1900):
unsigned long secsSince1900 = highWord << 16 | lowWord;
const unsigned long seventyYears = 2208988800UL;
unsigned long epoch = secsSince1900 - seventyYears;
int hh = ((epoch % 86400L) / 3600) + 9;
switch(hh) {
case 24: hh = 0; break;
case 25: hh = 1; break;
case 26: hh = 2; break;
case 27: hh = 3; break;
case 28: hh = 4; break;
case 29: hh = 5; break;
case 30: hh = 6; break;
case 31: hh = 7; break;
case 32: hh = 8; break;
}
int mm = (epoch % 3600) / 60;
int ss = epoch % 60;
sprintf(tt,"i'mano/ji'kokuwa
AquesTalkPico_Send(tt);
}
delay(15000);
}
void AquesTalkPico_Ready() {
while(1) {
Wire.requestFrom(I2CPICO, 1); // request 1 byte
if(Wire.available() > 0) {
if(Wire.read() == ‘>’) break;
}
delay(50); // delay 50ms
}
}
void AquesTalkPico_Send(char *msg) {
unsigned char i;
char *smsg;
smsg = msg;
AquesTalkPico_Ready();
while(*smsg != 0) {
Wire.beginTransmission(I2CPICO);
for(i = 0; i < 32; i++) {
Wire.write(*smsg++);
if(*smsg == 0) {
break;
}
}
Wire.endTransmission();
}
}
unsigned long sendNTPpacket(IPAddress& address)
{
memset(packetBuffer, 0, NTP_PACKET_SIZE);
packetBuffer[0] = 0b11100011; // LI, Version, Mode
packetBuffer[1] = 0; // Stratum, or type of clock
packetBuffer[2] = 6; // Polling Interval
packetBuffer[3] = 0xEC; // Peer Clock Precision
packetBuffer[12] = 49;
packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;
packetBuffer[15] = 52;
Udp.beginPacket(address, 123); //NTP requests are to port 123
Udp.write(packetBuffer,NTP_PACKET_SIZE);
Udp.endPacket();
}
[/code]