#include #include #include #include #include String message = "Close"; String timestamp = "1413175970"; String secret = "j344rfdas"; String device_ID = "a421"; String temp; String pakett; String payload; String rand_valgus; String rand_temp; unsigned int localPort = 888; IPAddress timeServer(192, 98, 49, 11); // ntp.elion.ee const int NTP_PACKET_SIZE= 48; // NTP time stamp is in the first 48 bytes of the message byte packetBuffer[ NTP_PACKET_SIZE]; EthernetUDP Udp; const int timeZoneOffset = +2; // GMT zone; byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; IPAddress server(192,168,2,100); IPAddress ip(192,168,2,110); int client_id = 34; uint8_t *hash; void setup() { uint8_t* hash; uint32_t a; Serial.begin(9600); while (!Serial) {} if (Ethernet.begin(mac) == 0) { Serial.println("Failed to configure Ethernet using DHCP"); Ethernet.begin(mac, ip); } Udp.begin(localPort); setSyncProvider(getNtpTime); while(timeStatus()== timeNotSet); randomSeed(now()); } void loop() { Serial.println(now()); loe_valgus(); loe_temp(); delay(2000); create_packet("Temperature", rand_temp); create_hmac(pakett); compile_packet(pakett, temp); Serial.println(payload); eth_connect(payload); temp =""; pakett=""; delay(10000); create_packet( "Valgus",rand_valgus); create_hmac(pakett); compile_packet(pakett,temp); Serial.println(payload); eth_connect(payload); temp=""; pakett=""; delay (2000); } void create_packet(String event, String request) { Serial.println(device_ID); Serial.println(event); Serial.println(request); pakett = device_ID + "." + event + "." + now() + "." + request; } void create_hmac (String hmac_data) { Sha256.initHmac((uint8_t*)"j344rfdas",9); Sha256.print(hmac_data); printHash(Sha256.resultHmac()); } void printHash(uint8_t* hash) { int i; for (i=0; i<32; i++) { temp = temp + ("0123456789abcdef"[hash[i]>>4]); temp = temp + ("0123456789abcdef"[hash[i]&0xf]); } } void compile_packet(String data_in, String hmac_in) { payload = data_in + "." + hmac_in; } void eth_connect(String kirjutada) { EthernetClient client; if (client.connect(server, 8888)) { Serial.println("Sent"); int str_leng = kirjutada.length()+1; char ch_ary[str_leng]; kirjutada.toCharArray(ch_ary, str_leng); client.write(ch_ary); } else { Serial.println("connection failed"); } } void loe_valgus() { int a = random(3,15); rand_valgus = String(a * 100); Serial.println(rand_valgus); } void loe_temp() { int b = random(10,25); rand_temp = String(b); Serial.println(rand_temp); } /*-------- NTP code ----------*/ /* CC arduino.cc https://www.arduino.cc/en/Tutorial/UdpNtpClient */ unsigned long getNtpTime() { sendNTPpacket(timeServer); // send an NTP packet to a time server 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(secsSince1900); const unsigned long seventyYears = 2208988800UL; // subtract seventy years and add the time zone: unsigned long epoch = secsSince1900 - seventyYears + (timeZoneOffset * 3600L); Serial.print(epoch); return epoch; } return 0; } // 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(); }