Advertisement
frankiepankie

Domoticz ESP8266 Arduino DS18B20

Jan 23rd, 2015
578
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <OneWire.h>
  3. #include <DallasTemperature.h>
  4. #define ONE_WIRE_BUS 8
  5. OneWire oneWire(ONE_WIRE_BUS);
  6. DallasTemperature sensors(&oneWire);
  7.  
  8. #define SSID "SSID" //SSID of your wireless network
  9. #define PASS "PASSWORD" //Password of your wireless network
  10. #define IP "192.168.4.15" // Domoticz IP, be sure to also change on line #65! (Host: x.x.x.x)
  11. String GET = "GET /json.htm?type=command&param=udevice&idx=109&nvalue=0&svalue=";
  12.  
  13. long previousMillis = 0;        // will store last time loop was executed
  14.  
  15. // the follow variables is a long because the time, measured in miliseconds,
  16. // will quickly become a bigger number than can be stored in an int.
  17. long interval = 300000;           // Interval for updating sensors (millisec), 300000 = 5 min = Domoticz virtual sensor interval
  18.  
  19. void setup()
  20. {
  21.   Serial.begin(9600);
  22.   sensors.begin();
  23.   Serial.println("AT");
  24.   delay(5000);
  25.   if(Serial.find("OK")){
  26.     connectWiFi();
  27.   }
  28. }
  29.  
  30. void loop(){
  31.   unsigned long currentMillis = millis();
  32.  
  33.   if(currentMillis - previousMillis > interval) {
  34.     // save the last time you executed the actions
  35.     previousMillis = currentMillis;  
  36.  
  37.     // Perform actions
  38.     sensors.requestTemperatures();
  39.     float tempC = sensors.getTempCByIndex(0);
  40.     //tempC = DallasTemperature::toFahrenheit(tempC);
  41.     char buffer[10];
  42.     String tempF = dtostrf(tempC, 4, 1, buffer);
  43.     updateTemp(tempF);
  44.  
  45.   }
  46. }
  47.  
  48.  
  49. void updateTemp(String tempF){
  50.   String cmd = "AT+CIPSTART=\"TCP\",\"";
  51.   cmd += IP;
  52.   cmd += "\",8080";
  53.   Serial.println(cmd);
  54.   delay(2000);
  55.   if(Serial.find("Error")){
  56.     return;
  57.   }
  58.   cmd = GET;
  59.   cmd += tempF;
  60.   cmd += " HTTP/1.1\r\n";  //construct http GET request
  61.   cmd += "Host: 192.168.4.15\r\n\r\n"; //Change this IP to the IP-adress of your Domoticz    
  62.   Serial.print("AT+CIPSEND=");
  63.   Serial.println(cmd.length());
  64.   if(Serial.find(">")){
  65.     Serial.print(cmd);
  66.   }else{
  67.     Serial.println("AT+CIPCLOSE");
  68.   }
  69. }
  70.  
  71.  
  72. boolean connectWiFi(){
  73.   Serial.println("AT+CWMODE=1");
  74.   delay(2000);
  75.   String cmd="AT+CWJAP=\"";
  76.   cmd+=SSID;
  77.   cmd+="\",\"";
  78.   cmd+=PASS;
  79.   cmd+="\"";
  80.   Serial.println(cmd);
  81.   delay(5000);
  82.   if(Serial.find("OK")){
  83.     return true;
  84.   }else{
  85.     return false;
  86.   }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement