Guest User

Untitled

a guest
Jun 24th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.58 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <Ethernet.h>
  3.  
  4. //Network setup, you should adapt this of course
  5. byte mac[] = {
  6.   0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEF};
  7. byte ip[] = {
  8.   89,106,215,66};
  9. byte gateway[] = {
  10.   89,106,215,95};
  11. byte subnet[] = {
  12.   255,255,255,0 };
  13. byte server[] = {
  14.   62,58,108,215};
  15.  
  16. // initialize the library instance:
  17. Client client(server, 30223);
  18.  
  19. //clock and tik JSON strings - change name, longitude, latitude, elevation to your data
  20. String clock =
  21. String("{\"idClock\":1,\"name\":\"ESC\",\"longitude\":47.3,\"latitude\":15.26,\"elevation\":347.0}");
  22. String tik = String("{}"); // NO MORE CLOCK ID'S !!!
  23.  
  24. //TIK test value
  25. int tikReady = 1;
  26.  
  27. void setup()
  28. {
  29.   pinMode(8,INPUT);
  30.   pinMode(13,OUTPUT);
  31.   //boot serial and Ethernet, serial is just to debug
  32.   Serial.begin(9600);
  33.   Ethernet.begin(mac, ip, gateway, subnet); //watch out for these settings!
  34.   delay(1000); // give ethernet shield time to boot
  35.  
  36.   //make the connection
  37.   client.connect();
  38.   if(client.connected())
  39.   {
  40.     Serial.println("connected!");
  41.     client.print(clock); //first connection sends clockId to register with
  42.  
  43.   }
  44.  
  45.   randomSeed(analogRead(6));
  46. }
  47.  
  48. int c;
  49. boolean input, state;
  50. void loop()
  51. {
  52.   if(client.connected())
  53.   {
  54.     input = digitalRead(8);
  55.  
  56.     if (input != state){
  57.       c++;
  58.       Serial.print(c);
  59.       Serial.println(": TIK");
  60.       client.print(tik);
  61.     }
  62.  
  63.     state = input;
  64.  
  65.   }
  66.  
  67.   //wait 5 seconds and reconnect
  68.   else
  69.   {
  70.     Serial.println("no connection");
  71.     delay(5000);
  72.     client.connect();
  73.     client.print(clock);
  74.   }
  75.  
  76.   //delay(500);
  77. }
Add Comment
Please, Sign In to add comment