Advertisement
Guest User

Untitled

a guest
May 16th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. //Libraries
  2. #include <DHT.h>
  3. #include <ESP8266WiFi.h>
  4. #include <ArduinoJson.h>
  5.  
  6. #define DHTPIN 2 // what digital pin we're connected to
  7.  
  8. #define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
  9.  
  10. DHT dht(DHTPIN, DHTTYPE);
  11.  
  12. // ACCOUNT SETUP CONSTANTS
  13. const int nodeID = ESP.getChipId();
  14. const char* ssid = "Belkin.32C7"; // WiFi Router SSID *Usually on side or bottom of router*
  15. const char* password = "44579397"; // WiFi Router Password *Usually on side or bottom of router*
  16. const char* apiHost = "192.168.2.3"; // Local cannacle API server *the PCduino's assigned Hostname*
  17. const int apiPort = 80;
  18.  
  19. void setup()
  20. {
  21. Serial.begin(57600);
  22. dht.begin();
  23. delay(2000);
  24.  
  25.  
  26. // We start by connecting to a WiFi network...
  27. WiFi.begin(ssid, password);
  28.  
  29. while (WiFi.status() != WL_CONNECTED) {
  30. delay(500);
  31. }
  32.  
  33. Serial.print("connecting to ");
  34. Serial.println(apiHost);
  35.  
  36. // Variables
  37. String url;
  38. String request;
  39.  
  40. // Use WiFiClient class to create TCP connections
  41. WiFiClient client;
  42. if (!client.connect(apiHost, apiPort)) {
  43. Serial.println(String(apiHost) + " : connection failed");
  44. return;
  45. }
  46.  
  47. Serial.print("Requesting URL: ");
  48. Serial.println(url);
  49.  
  50. // Read Temp & Humdity data and store it to variables...
  51. float humidity = dht.readHumidity();
  52. float airTempC = dht.readTemperature();
  53. float airTempF = dht.readTemperature(true);
  54.  
  55. StaticJsonBuffer<200> jsonBuffer;
  56.  
  57. JsonObject& device = jsonBuffer.createObject();
  58. device["node_id"] = nodeID;
  59. device["sensors"] = "DHT22";
  60.  
  61. JsonObject& data = device.createNestedObject("data");
  62. data["humidity"] = String(humidity); // 6 is the number of decimals to print
  63. data["air_tempC"] = String(airTempC); // if not specified, 2 digits are printed
  64. data["air_tempF"] = String(airTempF);
  65.  
  66. device.printTo(request);
  67. // We now create a URI for the request
  68. url = "/?request=" + urlencode(request);
  69.  
  70. // This will send the request to the server
  71. client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + apiHost + "\r\n" + "Connection: close\r\n\r\n");
  72. delay(1000);
  73.  
  74. // Read all the lines of the reply from server and print them to Serial
  75. String response;
  76. long sleepTime = 0;
  77. while(client.available()){
  78. response = client.readString();
  79. }
  80. Serial.print(response);
  81. JsonObject& responseObj = jsonBuffer.parseObject(response);
  82. if (!responseObj.success()){Serial.println("B0rken!");}
  83. sleepTime = responseObj["sleep_interval"];
  84. if (sleepTime == 0) {
  85. sleepTime = 600000000;
  86. }
  87. ESP.deepSleep(sleepTime);
  88. delay(1000);
  89. }
  90. //
  91. //
  92. void loop()
  93. {
  94.  
  95. }
  96. //
  97. //
  98. String urldecode(String str)
  99. {
  100.  
  101. String encodedString="";
  102. char c;
  103. char code0;
  104. char code1;
  105. for (int i =0; i < str.length(); i++){
  106. c=str.charAt(i);
  107. if (c == '+'){
  108. encodedString+=' ';
  109. }else if (c == '%') {
  110. i++;
  111. code0=str.charAt(i);
  112. i++;
  113. code1=str.charAt(i);
  114. c = (h2int(code0) << 4) | h2int(code1);
  115. encodedString+=c;
  116. } else{
  117.  
  118. encodedString+=c;
  119. }
  120.  
  121. yield();
  122. }
  123. return encodedString;
  124. }
  125.  
  126. String urlencode(String str)
  127. {
  128. String encodedString="";
  129. char c;
  130. char code0;
  131. char code1;
  132. char code2;
  133. for (int i =0; i < str.length(); i++){
  134. c=str.charAt(i);
  135. if (c == ' '){
  136. encodedString+= '+';
  137. } else if (isalnum(c)){
  138. encodedString+=c;
  139. } else{
  140. code1=(c & 0xf)+'0';
  141. if ((c & 0xf) >9){
  142. code1=(c & 0xf) - 10 + 'A';
  143. }
  144. c=(c>>4)&0xf;
  145. code0=c+'0';
  146. if (c > 9){
  147. code0=c - 10 + 'A';
  148. }
  149. code2='\0';
  150. encodedString+='%';
  151. encodedString+=code0;
  152. encodedString+=code1;
  153. //encodedString+=code2;
  154. }
  155. yield();
  156. }
  157. return encodedString;
  158.  
  159. }
  160.  
  161. unsigned char h2int(char c)
  162. {
  163. if (c >= '0' && c <='9'){
  164. return((unsigned char)c - '0');
  165. }
  166. if (c >= 'a' && c <='f'){
  167. return((unsigned char)c - 'a' + 10);
  168. }
  169. if (c >= 'A' && c <='F'){
  170. return((unsigned char)c - 'A' + 10);
  171. }
  172. return(0);
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement