Advertisement
Guest User

ESp8266

a guest
Mar 18th, 2017
1,261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.25 KB | None | 0 0
  1. #include <OneWire.h>
  2. #include <ESP8266WiFi.h>
  3. #include <ESP8266WebServer.h>
  4. #include <DallasTemperature.h>
  5. #define ONE_WIRE_BUS 2
  6.  
  7. const char* host = "192.168.1.100";
  8. const char* readhost = "192.168.1.100";
  9. String path1 = "/test/insert-device-data.php?room=1&temp=";
  10. String path2 = "&humidity=";
  11. String readpath = "/test/bulb-info/room.txt";
  12.  
  13. const char* ssid = "SSID-HERE";
  14. const char* pass = "PASSWORD-HERE";
  15. OneWire oneWire(ONE_WIRE_BUS);
  16. DallasTemperature DS18B20(&oneWire);
  17. char temperatureString1[6];
  18. char temperatureString2[6];
  19.  
  20. // Pin
  21. int led_pin = 14;
  22. int relay_pin = 0;
  23.  
  24. //Variable to save readed channel state
  25.  
  26. WiFiClient client;
  27.  
  28. void setup(void){
  29.   Serial.begin(115200);
  30.   Serial.println("");
  31.  
  32.   WiFi.begin(ssid, pass);
  33.   // Wait for connection
  34.   while (WiFi.status() != WL_CONNECTED) {
  35.     delay(100);
  36.     Serial.print(".");
  37.   }
  38.  
  39.   Serial.println("");
  40.   Serial.print("Connected to ");
  41.   Serial.println(ssid);
  42.   Serial.print("IP address: ");
  43.   Serial.println(WiFi.localIP());
  44.  
  45.   DS18B20.begin();
  46.    
  47. // Prepare GPIO-14
  48.   pinMode(led_pin, OUTPUT);
  49.   digitalWrite(led_pin, 1);
  50.   // Prepare GPIO-0
  51.   pinMode(relay_pin, OUTPUT);
  52.   digitalWrite(led_pin, 1);
  53. }
  54.  
  55. float getTemperature1() {
  56.   float temp1;
  57.   do {
  58.     DS18B20.requestTemperatures();
  59.     temp1 = DS18B20.getTempCByIndex(0);
  60.     delay(100);
  61.   } while (temp1 == 85.0 || temp1 == (-127.0));
  62.   return temp1;
  63. }
  64.  
  65. float getTemperature2() {
  66.   float temp2;
  67.   do {
  68.     DS18B20.requestTemperatures();
  69.     temp2 = DS18B20.getTempCByIndex(1);
  70.     delay(100);
  71.   } while (temp2 == 85.0 || temp2 == (-127.0));
  72.   return temp2;
  73. }
  74.  
  75. void loop() {
  76.  
  77.    // Flashing a LED to check the ESp8266 run state
  78.  digitalWrite(led_pin, 0);
  79.   delay(3000);
  80.   digitalWrite(led_pin, 1);
  81.   delay(200);
  82.  
  83.  float temperature1 = getTemperature1();
  84.  
  85.   dtostrf(temperature1, 2, 2, temperatureString1);
  86.   // send temperature to the serial console
  87.   //Serial.print("0: ");
  88.   //Serial.println(temperatureString1);
  89.  
  90.  
  91.  float temperature2 = getTemperature2();
  92.  
  93.   dtostrf(temperature2, 2, 2, temperatureString2);
  94.   // send temperature to the serial console
  95.   //Serial.print("1: ");
  96.   //Serial.println(temperatureString2);
  97.  
  98.   Serial.print("Temperature1: ");
  99.   Serial.println(temperatureString1);
  100.   Serial.print("Temperature2: ");
  101.   Serial.println(temperatureString2);
  102.  
  103.   WiFiClient client;
  104.   const int httpPort = 80;
  105.   if (!client.connect(host, httpPort)) {
  106.     Serial.println("connection failed");
  107.     return;
  108.   }
  109.    client.print(String("GET ") + path1 + temperatureString1 + path2 + temperatureString2 + " HTTP/1.1\r\n" +
  110.                "Host: " + host + "\r\n" +
  111.                "Connection: keep-alive\r\n\r\n");
  112.  
  113.   delay(500);
  114.  
  115.    // if you get a connection, report back via serial:
  116.   //if (client.connect(readhost, 80)) {
  117.     //Serial.println("connected");
  118.     // Make a HTTP request:
  119.     client.println("GET /test/bulb-info/room.txt HTTP/1.0");
  120.     client.println();
  121. //while(client.available()){
  122.     //String line = client.readStringUntil('\r');
  123.     char line = client.read();
  124.     //int line = client.read();
  125.     Serial.print(line);
  126.     if(line == 49){
  127.       digitalWrite(relay_pin, HIGH);
  128.     } else {
  129.       digitalWrite(relay_pin, LOW);
  130.     }
  131.   //}
  132.   //}
  133.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement