Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define relayPin D0 // Set what pin the relay is connected to
- #define relayInvert 0 // Set to 1 to invert the function of the relay
- float targetTemp = 24.0; // Target temperature
- float targetHyst = 0.05; // Hysteresis determinig how much over and under target temp it should trigger
- #define arraySize 20 // Size of the arry holding values for averaging
- double temps[arraySize]; // Arry holding temperature readings
- double temperature = 0; // Current average temperature
- int idx = 0; // Index of where we are in the array
- unsigned long readTMP36last = 0; // Holding time of when temperature was last checked
- #define readTMP36trot 500 // Time in milliseconds between temperature readings
- char tempStr[16]; // Holding the result in the json api reply
- void setup()
- {
- pinMode(relayPin, OUTPUT); // Set pin for the relay to output
- Spark.variable("temperature", &tempStr, STRING); // Register variable so it can be accessed with the api later
- }
- void loop()
- {
- readTMP36(); // Read from the temperature sensor and set the temperature variable
- if (temperature < (targetTemp-targetHyst)) // If the temperature is below our target temperature-hysteresis then...
- {
- digitalWrite(relayPin, relayInvert?LOW:HIGH); // Set pin high, or low if function is inverted
- }
- else if (temperature > targetTemp+targetHyst) // If the temperature is ablove target temperature+hysteresis then..
- {
- digitalWrite(relayPin, relayInvert?HIGH:LOW); // Set pin to low, or high if function is inverted
- }
- sprintf(tempStr, "%4.2f", temperature); // Set the result we are going to reply with when asked by the api (returning temperature with two decimals)
- }
- void readTMP36() // Function for reading from the sensor
- {
- unsigned int now = millis(); // Set the current time (from when the controller was powered on)
- if (now - readTMP36last < readTMP36trot) return; // If less than 500ms (default) has passed, just stop here and return to loop()
- readTMP36last = now; // More than 500ms (default) has passed, store the time we are reading at so we know when to do it again
- temps[idx] = ((((analogRead(A0)*3.3)/4095.0)-0.5)*100.0); // Check analog input and calculate the temperature
- if (++idx >= arraySize) idx = 0; // Add one to the array index, and check that we dont go above its size, if we do, set index to 0
- double total = 0; // Used to hold total value of the array
- for(int i=0; i < arraySize; i++) // Run through the array holding the temperatures
- {
- total += temps[i]; // Add each temperature in the array to the total
- }
- temperature = total / arraySize; // Set temperature variable to total temperature, divided by the array size, so we get an average temperature
- }
- // The temperature can now be accessed by opening https://api.spark.io/v1/devices/{device id}/temperature?access_token={access token}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement