Vous êtes sur la page 1sur 10

// UTF-8 // Modified by @SensorStatus, May.25 2013 () #if defined(ARDUINO) && ARDUINO > 18 #include <SPI.h> #endif #include <Ethernet.

h> #include <Twitter.h> #include <EthernetUdp.h> #include <Dns.h> #include <Time.h> #include <Wire.h> int int int int int pin_A0 A_val0 temp10T tempTT tempTTT = = = = = = = = 0; 0; 0; 0; 0; 0; 0; 0; // analog pin 0 // analog input(0-1023) //10

long air100P int int airPP airPPP

float tempT = float V = float SamplesT= float PhaP

0.0; 1.1; 0.0; = 0.0;

// temperature variables // analog reference voltage // //

int int int int int int

yt wt dt ht mt st

= = = = = =

0; 0; 0; 0; 0; 0;

const int address = 0x60; float read_coefficients(int total_bits, int fractional_bits, int zero_pad) { unsigned char msb, lsb; msb = Wire.read(); lsb = Wire.read(); return ((float) ((msb << 8) + lsb) / ((long)1 << 16 - total_bits + fractional_bits + zero_pad)); } unsigned int read_adc() { unsigned char msb, lsb; msb = Wire.read(); lsb = Wire.read(); return (((unsigned int)msb << 8) + lsb) >> 6; } float a0, b1, b2, c12, c11, c22; byte mac[] IPAddress = { 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF }; ip(192,168,123,123); //MAC

IPAddress IPAddress // IPAddress IPAddress IPAddress

gateway(192,168,123,1); subnet(255,255,255,0); timeServer(192,168,123,1); timeServer; dnsServer(192,168,123,1);

unsigned int localPort = 8765; //IP const int NTP_PACKET_SIZE= 48; byte packetBuffer[NTP_PACKET_SIZE]; EthernetUDP Udp; unsigned long lastSendPacketTime = 0; EthernetClient client; Twitter twitter("OAuth-access token"); //Access Token

//URL http://arduino-tweet.appspot.com/ void setup() { delay( 1000 ); analogReference(INTERNAL); Ethernet.begin(mac,ip,gateway,subnet); Wire.begin(); Serial.begin(9600); Wire.beginTransmission(address); Wire.write(0x04); // Read coefficient data Wire.endTransmission();

Wire.requestFrom(address, 12); // Request 12 bytes if (Wire.available()) { a0 = read_coefficients(16, 3, 0); // b1 = read_coefficients(16, 13, 0); // b2 = read_coefficients(16, 14, 0); // c12 = read_coefficients(14, 13, 9); // c11 = read_coefficients(11, 10, 11); // c22 = read_coefficients(11, 10, 15); // } // attempt a DHCP connection: Serial.println("Attempting to get an IP address using DHCP"); // This requires the DHCP server to be x.x.x.x if (Ethernet.begin(mac) == 0) { // if DHCP fails, start with a hard-coded address: Serial.println("failed to get an IP address using DHCP, trying manually"); Ethernet.begin(mac, ip, gateway, gateway, subnet); // function returns void } else Serial.println("got an IP address using DHCP"); // Start listening for clients. // Say who we think we are. Serial.println(Ethernet.localIP()); Udp.begin(localPort); delay(1000); DNSClient dns; dns.begin(Ethernet.dnsServerIP());

if(dns.getHostByName("ntp.jst.mfeed.ad.jp",timeServer)) { // Serial.print(F("NTP server ip :")); // Serial.println(timeServer); } sendNTPpacket(timeServer); // send an NTP packet to a time server lastSendPacketTime = millis(); } void loop() { for( int j = 0; j < 50; j++ ) // gets 50 samples of Pressure and Temperature { Wire.beginTransmission(address); Wire.write(0x12); // Start both conversions(Pressure and Temperature) Wire.write(0x01); Wire.endTransmission(); delay(5); Wire.beginTransmission(address); Wire.write((uint8_t)0x00); // Read pressure and temperature Wire.endTransmission(); Wire.requestFrom(address, 4); // Request 4 bytes if(Wire.available()) { unsigned int Padc = read_adc(); unsigned int Tadc = read_adc();

// //

float Pcomp = a0 + (b1 + c11 * Padc + c12 * Tadc) * Padc + (b2 + c22 * Tadc) * Tadc; Rev.9 float Pcomp = a0 + (b1 + c12 * Tadc) * Padc + b2 * Tadc; float Pha = Pcomp * 650 / 1023 + 500; PhaP = PhaP + Pha; Padc = 0.0; Tadc = 0.0; Pha = 0.0; // zero clear Padc // zero clear Tadc // zero clear Pha

} delay(3); } if ( millis() - lastSendPacketTime > 120000 ){ // NTP sendNTPpacket(timeServer); // send an NTP packet to a time server lastSendPacketTime = millis(); } // wait to see if a reply is available delay(1000); if ( Udp.parsePacket() ) { // We've received a packet, read the data from it Udp.read(packetBuffer,NTP_PACKET_SIZE); // read the packet into the buffer //the timestamp starts at byte 40 of the received packet and is four bytes,

// or two words, long. First, esxtract the two words: 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; Serial.print("Seconds since Jan 1 1900 = " ); Serial.println(secsSince1900); // now convert NTP time into everyday time: Serial.print("Unix time = "); // Unix time starts on Jan 1 1970. In seconds, that's 2208988800: const unsigned long seventyYears = 2208988800UL; // subtract seventy years: unsigned long epoch = secsSince1900 - seventyYears; // print Unix time: Serial.println(epoch); setTime(epoch + (9 * 60 * 60 + 1)); // UTC--->JST yt wt dt ht mt st } // wait ten seconds before asking for the time again delay(10000); for( int i = 0; i < 50; i++ ) // gets 50 samples of temperature = = = = = = year(); month(); day(); hour(); minute(); second();

{ A_val0 = analogRead(pin_A0); // read analog0 input

SamplesT= ( V * A_val0 * 100) / 1024.0; // Celsius conversion tempT = tempT + SamplesT; A_val0 = 0; SamplesT = 0.0; delay(3); } tempT = tempT / 50.0; PhaP = PhaP / 50.0; temp10T = tempT*10; tempTT = temp10T / 10; tempTTT = temp10T % 10; air100P = (float)PhaP * 100 + 5.0; airPP = air100P / 100; airPPP = (air100P % 100) / 10; char msg[140] ; // sprintf(msg, "@DEST %d.%d[degC], %d.%d[hPa], %02d-%02d-%02d %02d:%02d:%02d %%2b0900 (JST)", tempTT,tempT TT,airPP,airPPP,yt,wt,dt,ht,mt,st) ; Serial.println(msg); //@DEST // better precision // better precision // zero clear A_val0 // zero clear SamplesT

Serial.println("connecting ..."); if (twitter.post(msg)) { // Specify &Serial to output received response to Serial. // If no output is required, you can just omit the argument, e.g. int status = twitter.wait(); if (status == 200) { Serial.println("OK."); } else { Serial.print("failed : code "); Serial.println(status); } } else { Serial.println("connection failed."); } tempT = 0; // zero clear // zero clear // zero clear // zero clear // // // // zero zero zero zero clear clear clear clear

temp10T = 0; tempTT = 0; tempTTT = 0; air100P airPP airPPP PhaP = = = = 0; 0; 0; 0;

//delay(120000); delay(587900);

//2 //10

} // send an NTP request to the time server at the given address unsigned long sendNTPpacket(IPAddress& address) { // set all bytes in the buffer to 0 memset(packetBuffer, 0, NTP_PACKET_SIZE); // Initialize values needed to form NTP request // (see URL above for details on the packets) 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 // 8 bytes of zero for Root Delay & Root Dispersion packetBuffer[12] = 49; packetBuffer[13] = 0x4E; packetBuffer[14] = 49; packetBuffer[15] = 52; // all NTP fields have been given values, now // you can send a packet requesting a timestamp: Udp.beginPacket(address, 123); //NTP requests are to port 123 Udp.write(packetBuffer,NTP_PACKET_SIZE); Udp.endPacket(); }

Vous aimerez peut-être aussi