Advertisement
Guest User

leonardo

a guest
May 24th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.17 KB | None | 0 0
  1. #include <Ethernet2.h>
  2. #include <SoftwareSerial.h>
  3. #include <SPI.h>
  4.  
  5. int ledPin = 12;        
  6. int sensorPin = 4;
  7. int sensorValue;
  8. int lastTiltState = HIGH;   // the previous reading from the tilt sensor
  9.    
  10. // the following variables are long's because the time, measured in miliseconds,
  11. // will quickly become a bigger number than can be stored in an int.
  12. long lastDebounceTime = 0;  // the last time the output pin was toggled
  13. long debounceDelay = 50;    // the debounce time; increase if the output flickers
  14.  
  15.  
  16. //communication serveur
  17. String tiltword = "boff";
  18. byte mac[] = {0x90, 0xA2, 0xDA, 0x10, 0x59, 0x9B }; //mac arduino
  19. IPAddress ip(192,168,0,125); // ip arduino
  20.  
  21. char server[] = "192.168.0.29"; // ip serveur (serveur maison)
  22. EthernetClient client;
  23.  
  24.  
  25.  
  26.  
  27.  
  28. void setup(){
  29.  
  30.   Serial.begin(9600);
  31.   while (!Serial) {
  32.     ; // wait for serial port to connect. Needed for native USB
  33.  }  
  34.   Ethernet.begin(mac, ip);
  35.   delay(1500); //delay
  36.   Serial.println("ethernet !");
  37.   pinMode(sensorPin, INPUT);
  38.   digitalWrite(sensorPin, HIGH);
  39.   pinMode(ledPin, OUTPUT);
  40. }
  41.  
  42. void loop(){
  43.   sensorValue = digitalRead(sensorPin);
  44.   // If the switch changed, due to noise or pressing:
  45.   if (sensorValue == lastTiltState) {
  46.     // reset the debouncing timer
  47.     lastDebounceTime = millis();
  48.   }
  49.   if ((millis() - lastDebounceTime) > debounceDelay) {
  50.     // whatever the reading is at, it's been there for longer
  51.     // than the debounce delay, so take it as the actual current state:
  52.     lastTiltState = sensorValue;
  53.   }
  54.   digitalWrite(ledPin, lastTiltState);
  55.  
  56.  
  57.   if (sensorValue == 1) {
  58.  
  59. if (client.connect(server, 80)) {
  60.   client.print("GET http://192.168.0.29/tilt/index.php?");
  61.   client.print("tilt=");
  62.   client.print(tiltword);
  63.    client.println(" HTTP/1.1"); // Part of the GET request
  64.    client.println("Host: 192.168.0.29");
  65.   // client.println("Host: 10.73.3.1");
  66.    client.println("Connection: close"); // ip serveur
  67.    client.println();
  68.    client.stop();
  69.  
  70.  
  71.   }else {
  72.    // If Arduino can't connect to the server (your computer or web page)
  73.  //   Serial.println("--> connection failed\n");
  74.   }
  75.  
  76.   }
  77.  
  78.   Serial.println(sensorValue);
  79.   delay(400);
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement