Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This #include statement was automatically added by the Spark IDE.
- #include "HttpClient/HttpClient.h"
- // This #include statement was automatically added by the Spark IDE.
- #include "OneWire/OneWire.h"
- // This #include statement was automatically added by the Spark IDE.
- #include "pid/pid.h"
- // This #include statement was automatically added by the Spark IDE.
- #include "spark-dallas-temperature/spark-dallas-temperature.h"
- #define ONE_WIRE_BUS D1
- #define TEMPERATURE_PRECISION 12
- OneWire oneWire(ONE_WIRE_BUS);
- DallasTemperature sensors(&oneWire);
- // PID: variables
- double pointTemperature = 0, actualTemperature, pidOutput;
- int heating = 0;
- //void update18B20Temp(DeviceAddress deviceAddress, double &tempC);
- void update18B20Temp(double &tempC);
- // Misc Stuff
- unsigned long lastUpdate = 0;
- unsigned long updateDelay = 10000;
- // SparkCloud variable outputs
- char tempInfo[64];
- char pointInfo[64];
- char pwmInfo[64];
- char allInfo[64];
- HttpClient http;
- #define VARIABLE_ID "MY VARIABLE ID"
- #define TOKEN "MY TOKEN"
- http_header_t headers[] = {
- { "Content-Type", "application/json" },
- { "X-Auth-Token" , TOKEN },
- { NULL, NULL } // NOTE: Always terminate headers will NULL
- };
- http_header_t headersCooker[] = {
- // { "Content-Type", "application/json" },
- // { "Accept" , "application/json" },
- { "Accept" , "*/*"},
- { NULL, NULL } // NOTE: Always terminate headers will NULL
- };
- http_request_t request, requestCooker;
- http_response_t response, responseCooker;
- //PID myPID(&Input, &Output, &Setpoint,2,5,1, PID::DIRECT);
- PID myPID(&actualTemperature, &pidOutput, &pointTemperature, 25, 1000, 9, PID::DIRECT);
- void setup() {
- //Serial.begin(9600);
- //Serial.println("Dallas Temperature IC Control Library Demo");
- sensors.begin();
- //sensors.setResolution(thermometer, TEMPERATURE_PRECISION);
- sensors.setResolution(TEMPERATURE_PRECISION);
- // SparkCloud: variables
- Spark.variable("temperature", &tempInfo, STRING);
- Spark.variable("point", &pointInfo, STRING);
- Spark.variable("pwm", &pwmInfo, STRING);
- Spark.variable("heating", &heating, INT);
- Spark.variable("result",&allInfo, STRING);
- // SparkCloud: functions
- Spark.function("setPoint", setPointTemperature);
- //turn the PID on
- myPID.SetMode(PID::AUTOMATIC);
- request.hostname = "things.ubidots.com";
- request.port = 80;
- requestCooker.hostname = "MY_WEBSITE";
- requestCooker.port = MY_PORT_NUMBER;
- }
- void loop() {
- // Send event to the SparkCloud
- unsigned long now = millis();
- if((now - lastUpdate) > updateDelay)
- {
- lastUpdate = now;
- sensors.requestTemperatures();
- update18B20Temp(actualTemperature);
- // Compute PID
- if(myPID.Compute()) {
- // Turn on or off relay here
- if(pidOutput == 255 && !heating) {
- heating = 1;
- //turn heating on
- requestCooker.path = "/lights/cooker_on";
- http.get(requestCooker, responseCooker, headersCooker);
- } else if(pidOutput == 0 && heating) {
- heating = 0;
- //turn heating off
- requestCooker.path = "/lights/cooker_off";
- http.get(requestCooker, responseCooker, headersCooker);
- }
- }
- // Convert values for output
- // convert pidOutput to 0-100
- sprintf(allInfo, "{\"temp\":%2.2f,\"set\":%2.2f,\"pid\":%2.2f,\"heating\":%d}", actualTemperature, pointTemperature, (pidOutput / 255) * 100, heating);
- // Send to Ubidots
- if (pointTemperature) {
- request.path = "/api/v1.6/variables/"VARIABLE_ID"/values";
- request.body = "{\"value\":" + String(actualTemperature) + "}";
- http.post(request, response, headers);
- }
- // SparkCore: publish
- //Spark.publish("allInfo", allInfo);
- //Spark.publish("heating", heating);
- }
- }
- //void update18B20Temp(DeviceAddress deviceAddress, double &tempC)
- void update18B20Temp(double &tempC)
- {
- double tmp = sensors.getTempFByIndex(0);
- if(tmp > 0)
- tempC = tmp;
- }
- // set point temperature
- int setPointTemperature(String command)
- {
- // Convert to double
- pointTemperature = (double) command.toInt();
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement