Advertisement
Guest User

Untitled

a guest
Aug 20th, 2016
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.00 KB | None | 0 0
  1. #include <Arduino.h>
  2.  
  3. #include <ESP8266WiFi.h>
  4. #include <ESP8266WiFiMulti.h>
  5.  
  6. #include <ESP8266HTTPClient.h>
  7. #include <ESP8266WiFi.h>
  8. #include <OneWire.h>
  9.  
  10.  
  11. #define USE_SERIAL Serial
  12. OneWire  ds(2);
  13.  
  14.  
  15. ESP8266WiFiMulti WiFiMulti;
  16.  
  17. void setup() {
  18.  
  19.     USE_SERIAL.begin(115200);
  20.    // USE_SERIAL.setDebugOutput(true);
  21.  
  22.     USE_SERIAL.println();
  23.     USE_SERIAL.println();
  24.     USE_SERIAL.println();
  25.  
  26.     for(uint8_t t = 4; t > 0; t--) {
  27.         USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
  28.         USE_SERIAL.flush();
  29.         delay(1000);
  30.     }
  31.  
  32.     WiFiMulti.addAP("SSID", "PASS");
  33.  
  34. }
  35.  
  36. void loop() {
  37.  
  38.  
  39. byte i;
  40. byte present = 0;
  41. byte type_s;
  42. byte data[12];
  43. byte addr[8];
  44. float celsius, fahrenheit;
  45.  
  46. if ( !ds.search(addr)) {
  47. Serial.println("No more addresses.");
  48. Serial.println();
  49. ds.reset_search();
  50. delay(250);
  51. return;
  52. }
  53.  
  54. Serial.print("ROM =");
  55. for ( i = 0; i < 8; i++) {
  56. Serial.write(' ');
  57. Serial.print(addr[i], HEX);
  58. }
  59.  
  60. if (OneWire::crc8(addr, 7) != addr[7]) {
  61. Serial.println("CRC is not valid!");
  62. return;
  63. }
  64. Serial.println();
  65.  
  66. // the first ROM byte indicates which chip
  67. switch (addr[0]) {
  68. case 0x10:
  69. Serial.println("  Chip = DS18S20");  // or old DS1820
  70. type_s = 1;
  71. break;
  72. case 0x28:
  73. Serial.println("  Chip = DS18B20");
  74. type_s = 0;
  75. break;
  76. case 0x22:
  77. Serial.println("  Chip = DS1822");
  78. type_s = 0;
  79. break;
  80. default:
  81. Serial.println("Device is not a DS18x20 family device.");
  82. return;
  83. }
  84.  
  85. ds.reset();
  86. ds.select(addr);
  87. ds.write(0x44, 1);        // start conversion, with parasite power on at the end
  88.  
  89. delay(1000);     // maybe 750ms is enough, maybe not
  90. // we might do a ds.depower() here, but the reset will take care of it.
  91.  
  92. present = ds.reset();
  93. ds.select(addr);
  94.  
  95.  
  96. ds.write(0xBE);         // Read Scratchpad
  97.  
  98. Serial.print("  Data = ");
  99. Serial.print(present, HEX);
  100. Serial.print(" ");
  101. for ( i = 0; i < 9; i++) {           // we need 9 bytes
  102. data[i] = ds.read();
  103. Serial.print(data[i], HEX);
  104. Serial.print(" ");
  105. }
  106. Serial.print(" CRC=");
  107. Serial.print(OneWire::crc8(data, 8), HEX);
  108. Serial.println();
  109.  
  110. int16_t raw = (data[1] << 8) | data[0];
  111. if (type_s) {
  112. raw = raw << 3; // 9 bit resolution default
  113. if (data[7] == 0x10) {
  114. // "count remain" gives full 12 bit resolution
  115. raw = (raw & 0xFFF0) + 12 - data[6];
  116. }
  117. } else {
  118. byte cfg = (data[4] & 0x60);
  119. // at lower res, the low bits are undefined, so let's zero them
  120. if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
  121. else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
  122. else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
  123. //// default is 12 bit resolution, 750 ms conversion time
  124. }
  125. celsius = (float)raw / 16.0;
  126. fahrenheit = celsius * 1.8 + 32.0;
  127. Serial.print("  Temperature = ");
  128. Serial.print(celsius);
  129. Serial.print(" Celsius, ");
  130. Serial.print(fahrenheit);
  131. Serial.println(" Fahrenheit");
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.     // wait for WiFi connection
  141.     if((WiFiMulti.run() == WL_CONNECTED)) {
  142.  
  143.         HTTPClient http;
  144.  
  145.         USE_SERIAL.print("[HTTP] begin...\n");
  146.         // configure traged server and url
  147.         //http.begin("192.168.1.12", 443, "/test.html", true, "7a 9c f4 db 40 d3 62 5a 6e 21 bc 5c cc 66 c8 3e a1 45 59 38"); //HTTPS
  148.         String strLab = "/test.php?id=1&temp=";
  149.         char tmp[10];
  150.         dtostrf(celsius,3,5,tmp);
  151.         String strOut = strLab + tmp;
  152.        
  153.         http.begin("192.168.1.2", 80, strOut); //HTTP
  154.  
  155.         USE_SERIAL.print("[HTTP] GET...\n");
  156.         // start connection and send HTTP header
  157.         int httpCode = http.GET();
  158.         if(httpCode) {
  159.             // HTTP header has been send and Server response header has been handled
  160.             USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
  161.  
  162.             // file found at server
  163.             if(httpCode == 200) {
  164.                 String payload = http.getString();
  165.                 USE_SERIAL.println(payload);
  166.             }
  167.         } else {
  168.             USE_SERIAL.print("[HTTP] GET... failed, no connection or no HTTP server\n");
  169.         }
  170.     }
  171.  
  172.     delay(10000);
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement