Homeduino thermostaat wemos d1 mini + DallasTemp + relayshield code

hoi mede homey-ers,

hier een code voor een Homeduino thermostaat op een wemos d1 mini + DallasTemp + relayshield. wie weet kan iemand die gebruiken :slight_smile:
groeten,

Allard

#include <ESP8266WiFi.h>
#include <Homey.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Relay.h> 

#define ONE_WIRE_BUS D4 // PIN (D1,D2 ect.)
#define PIN_RELAY   D1 // PIN 

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

 float Celcius=0;
 float Fahrenheit=0;
 
 void wifi() {
  if (WiFi.status() != WL_CONNECTED) {
    WiFi.begin("jouwnetwerk", "jouwwachtwoord");
    uint8_t timeout = 30;
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
      if (timeout<1) break;
    }
    if (WiFi.status() == WL_CONNECTED) {
      //Print IP address
      Serial.print("Verbonden met WiFi! (");
      Serial.print(WiFi.localIP());
      Serial.println(")");
    }
  }
}

bool state = false;
unsigned long previousMillis = 0;
const unsigned long interval = 100; //Interval in milliseconds

void setup(void){
  Serial.begin(115200); 
  pinMode(PIN_RELAY, OUTPUT); //Set relay pin to output
  digitalWrite(PIN_RELAY, LOW); //Turn output off
  Homey.begin("Thermostat"); // vul hier de naam voor de sensor in
  Homey.setClass("sensor");
  Homey.addCapability("measure_temperature");
  Homey.setClass("socket");
  Homey.addCapability("onoff", setState);
  Homey.addAction("output", setState);
  Homey.addCondition("state", getState);
}

void loop(void){ 
  wifi();
  Homey.loop();
  sensors.requestTemperatures(); 
  Celcius=sensors.getTempCByIndex(0);
  Fahrenheit=sensors.toFahrenheit(Celcius);
  Serial.print(Celcius);
  Serial.print (" C ");
  Homey.setCapabilityValue("measure_temperature", (int) Celcius); 
  delay(1000);
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > interval) {
  previousMillis = currentMillis;
  }
}

void setState() {
  state = Homey.value.toInt();
  applyState();
}

void applyState() {
  digitalWrite(PIN_RELAY, state);
 
 // Serial.println("applyState(): new state is " String(state));??
  Homey.setCapabilityValue("onoff", state);
  Homey.trigger("state", state);
}

void getState() {
  //Serial.println("getState(): state is " );String(state)); ??
  return Homey.returnResult(state);
}
2 Likes

Hello Allard,
Could you show me a picture of the Homey flow that you use to read the temperature?
The Arduino part is clear to me but not the "returnResult"part.
Would appriciate your reply.
Thanks,