Advertisement
Guest User

final

a guest
Aug 19th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.18 KB | None | 0 0
  1. /*
  2.   Repeating Web client
  3.  
  4.  This sketch connects to a a web server and makes a request
  5.  using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or
  6.  the Adafruit Ethernet shield, either one will work, as long as it's got
  7.  a Wiznet Ethernet module on board.
  8.  
  9.  This example uses DNS, by assigning the Ethernet client with a MAC address,
  10.  IP address, and DNS address.
  11.  
  12.  Circuit:
  13.  * Ethernet shield attached to pins 10, 11, 12, 13
  14.  
  15.  created 19 Apr 2012
  16.  by Tom Igoe
  17.  modified 21 Jan 2014
  18.  by Federico Vanzati
  19.  
  20.  http://www.arduino.cc/en/Tutorial/WebClientRepeating
  21.  This code is in the public domain.
  22.  
  23.  */
  24.  
  25. #include <SPI.h>
  26. #include <Ethernet.h>
  27.  
  28. // assign a MAC address for the ethernet controller.
  29. // fill in your address here:
  30. byte mac[] = {
  31.   0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
  32. };
  33.  
  34.  
  35. // fill in your Domain Name Server address here:
  36. IPAddress myDns(1, 1, 1, 1);
  37.  
  38. // initialize the library instance:
  39. EthernetClient client;
  40.  
  41. char server[] = "http://monitor-js.herokuapp.com";
  42.  
  43. unsigned long lastConnectionTime = 0;             // last time you connected to the server, in milliseconds
  44. const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
  45. // the "L" is needed to use long type numbers
  46.  
  47. void setup() {
  48.   // start serial port:
  49.   Serial.begin(9600);
  50.   while (!Serial) {
  51.     ; // wait for serial port to connect. Needed for native USB port only
  52.   }
  53.  
  54.   // give the ethernet module time to boot up:
  55.   delay(1000);
  56.   // start the Ethernet connection using a fixed IP address and DNS server:
  57.      Ethernet.begin(mac)
  58.   // print the Ethernet board/shield's IP address:
  59.   Serial.print("My IP address: ");
  60.   Serial.println(Ethernet.localIP());
  61. }
  62.  
  63. void loop() {
  64.   // if there's incoming data from the net connection.
  65.   // send it out the serial port.  This is for debugging
  66.   // purposes only:
  67.   if (client.available()) {
  68.     char c = client.read();
  69.     Serial.write(c);
  70.   }
  71.  
  72.   // if ten seconds have passed since your last connection,
  73.   // then connect again and send data:
  74.   if (millis() - lastConnectionTime > postingInterval) {
  75.     httpRequest();
  76.   }
  77.  
  78. }
  79.  
  80. // this method makes a HTTP connection to the server:
  81. void httpRequest() {
  82.   // close any connection before send a new request.
  83.   // This will free the socket on the WiFi shield
  84.   client.stop();
  85.  
  86.   // if there's a successful connection:
  87.   if (client.connect(server, 80)) {
  88.     Serial.println("connecting...");
  89.     // send the HTTP GET request:
  90.      StaticJsonBuffer<200> jsonBuffer;
  91.      JsonObject& root = jsonBuffer.createObject();
  92.      root["temp"]=90;
  93.      root["bToken"]="6116C9622E5978D54333E54DC17CE";
  94.      
  95.     client.println("POST /addStatus HTTP/1.1");
  96.     client.println("Host: monitor-js.herokuapp.com");
  97.     client.println("Content-Type: application/json");
  98.     client.print("Content-Length: ");
  99.     client.println(root.measureLength());
  100.     client.println();
  101.      root.printTo(client);
  102.      Serial.println("request is sent");
  103.  
  104.     // note the time that the connection was made:
  105.     lastConnectionTime = millis();
  106.   } else {
  107.     // if you couldn't make a connection:
  108.     Serial.println("connection failed");
  109.   }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement