Advertisement
tvrtko282

Untitled

May 26th, 2015
238
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SPI.h>
  2. #include <Ethernet.h>
  3. #include "DHT.h"
  4.  
  5. #define DHTPIN1 8
  6. #define DHTTYPE1 DHT22
  7. DHT dht1(DHTPIN1, DHTTYPE1);
  8. float humidity1, temperature1;
  9.  
  10. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  11. char server[] = "www.etfos.unios.hr";
  12. //IPAddress ip(192,168,0,177);
  13. EthernetClient client;
  14.  
  15.  
  16. int networkCall(){
  17.   if (client.connect(server, 80)) {
  18.     String data="url=https://<page>";
  19.     Serial.println("connected");
  20.     client.println("POST /~<name>/my.php HTTP/1.1");
  21.     client.println("Host: www.etfos.unios.hr");
  22.     client.println("User-Agent: Arduino/1.0");
  23.     client.println("Connection: close");
  24.     client.println("Content-Type: application/x-www-form-urlencoded");
  25.     client.print("Content-Length: ");
  26.     client.println(data.length());
  27.     client.println();
  28.     client.println(data);
  29.     client.println();
  30.    
  31.    
  32.     delay(1000);
  33.    
  34.     //nakon ovog moras imati json
  35.     char char_array[500], result[500];
  36.     int i = 0;
  37.     while (client.available()) {
  38.       char c = client.read();
  39.       char_array[i] = c;
  40.       i++;
  41.     }
  42.    
  43.     char_array[i] = '\0';
  44.    
  45.     String str(char_array);
  46.    
  47.     Serial.println(str);
  48.     // if the server's disconnected, stop the client:
  49.     if (!client.connected()) {
  50.       Serial.println();
  51.       Serial.println("disconnecting.");
  52.       client.stop();
  53.     }
  54.   }
  55.   else{
  56.     Serial.println("connection failed");
  57.   }
  58. }
  59. void setup() {
  60.   Serial.begin(9600);
  61.       Serial.println("connecting...");
  62.    
  63.   if (Ethernet.begin(mac) == 0) {
  64.     Serial.println("Failed to configure Ethernet using DHCP");
  65.     // no point in carrying on, so do nothing forevermore:
  66.     // try to congifure using IP address instead of DHCP:
  67.     //Ethernet.begin(mac, ip);
  68.   }
  69.   dht1.begin();
  70. }
  71.  
  72. void loop() {
  73.   // put your main code here, to run repeatedly:
  74.   Serial.println("Network call");
  75.   networkCall();
  76.   Serial.println("Network call end");
  77.  
  78.   temperature1 = dht1.readTemperature();
  79.   humidity1 = dht1.readHumidity();
  80.   Serial.print("Temperature: ");
  81.   Serial.println(temperature1);
  82.   Serial.print("Humidity: ");
  83.   Serial.println(humidity1);
  84. }
Advertisement
RAW Paste Data Copied
Advertisement