Advertisement
Guest User

Spark Core Crash

a guest
Apr 22nd, 2015
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.39 KB | None | 0 0
  1. // This #include statement was automatically added by the Spark IDE.
  2. #include "HttpClient/HttpClient.h"
  3.  
  4. // This #include statement was automatically added by the Spark IDE.
  5. #include "OneWire/OneWire.h"
  6.  
  7. // This #include statement was automatically added by the Spark IDE.
  8. #include "pid/pid.h"
  9.  
  10. // This #include statement was automatically added by the Spark IDE.
  11. #include "spark-dallas-temperature/spark-dallas-temperature.h"
  12.  
  13. #define ONE_WIRE_BUS D1
  14. #define TEMPERATURE_PRECISION 12
  15. OneWire oneWire(ONE_WIRE_BUS);
  16. DallasTemperature sensors(&oneWire);
  17. // PID: variables
  18. double pointTemperature = 0, actualTemperature, pidOutput;
  19. int heating = 0;
  20. //void update18B20Temp(DeviceAddress deviceAddress, double &tempC);
  21. void update18B20Temp(double &tempC);
  22.  
  23. // Misc Stuff
  24. unsigned long lastUpdate = 0;
  25. unsigned long updateDelay = 10000;
  26.  
  27. // SparkCloud variable outputs
  28. char tempInfo[64];
  29. char pointInfo[64];
  30. char pwmInfo[64];
  31. char allInfo[64];
  32.  
  33. HttpClient http;
  34. #define VARIABLE_ID "MY VARIABLE ID"
  35. #define TOKEN "MY TOKEN"
  36. http_header_t headers[] = {
  37.       { "Content-Type", "application/json" },
  38.       { "X-Auth-Token" , TOKEN },
  39.     { NULL, NULL } // NOTE: Always terminate headers will NULL
  40. };
  41.  
  42. http_header_t headersCooker[] = {
  43.     //  { "Content-Type", "application/json" },
  44.     //  { "Accept" , "application/json" },
  45.     { "Accept" , "*/*"},
  46.     { NULL, NULL } // NOTE: Always terminate headers will NULL
  47. };
  48.  
  49. http_request_t request, requestCooker;
  50. http_response_t response, responseCooker;
  51.  
  52. //PID myPID(&Input, &Output, &Setpoint,2,5,1, PID::DIRECT);
  53. PID myPID(&actualTemperature, &pidOutput, &pointTemperature, 25, 1000, 9, PID::DIRECT);
  54.  
  55. void setup() {
  56.     //Serial.begin(9600);
  57.     //Serial.println("Dallas Temperature IC Control Library Demo");
  58.  
  59.     sensors.begin();
  60.     //sensors.setResolution(thermometer, TEMPERATURE_PRECISION);
  61.     sensors.setResolution(TEMPERATURE_PRECISION);
  62.    
  63.     // SparkCloud: variables
  64.     Spark.variable("temperature", &tempInfo, STRING);
  65.     Spark.variable("point", &pointInfo, STRING);
  66.     Spark.variable("pwm", &pwmInfo, STRING);
  67.     Spark.variable("heating", &heating, INT);
  68.     Spark.variable("result",&allInfo, STRING);
  69.    
  70.     // SparkCloud: functions
  71.     Spark.function("setPoint", setPointTemperature);
  72.  
  73.     //turn the PID on
  74.     myPID.SetMode(PID::AUTOMATIC);
  75.    
  76.     request.hostname = "things.ubidots.com";
  77.     request.port = 80;
  78.  
  79.     requestCooker.hostname = "MY_WEBSITE";
  80.     requestCooker.port = MY_PORT_NUMBER;
  81. }
  82.  
  83. void loop() {
  84.  
  85.     // Send event to the SparkCloud
  86.     unsigned long now = millis();
  87.     if((now - lastUpdate) > updateDelay)
  88.     {
  89.         lastUpdate = now;
  90.  
  91.         sensors.requestTemperatures();
  92.         update18B20Temp(actualTemperature);
  93.        
  94.         // Compute PID
  95.         if(myPID.Compute()) {
  96.            
  97.             // Turn on or off relay here
  98.             if(pidOutput == 255 && !heating) {
  99.                 heating = 1;
  100.                 //turn heating on
  101.                 requestCooker.path = "/lights/cooker_on";
  102.                 http.get(requestCooker, responseCooker, headersCooker);
  103.             } else if(pidOutput == 0 && heating) {
  104.                 heating = 0;
  105.                 //turn heating off
  106.                 requestCooker.path = "/lights/cooker_off";
  107.                 http.get(requestCooker, responseCooker, headersCooker);
  108.             }
  109.         }
  110.        
  111.         // Convert values for output
  112.         // convert pidOutput to 0-100
  113.         sprintf(allInfo, "{\"temp\":%2.2f,\"set\":%2.2f,\"pid\":%2.2f,\"heating\":%d}", actualTemperature, pointTemperature, (pidOutput / 255) * 100, heating);
  114.        
  115.        
  116.         // Send to Ubidots
  117.         if (pointTemperature) {
  118.             request.path = "/api/v1.6/variables/"VARIABLE_ID"/values";
  119.             request.body = "{\"value\":" + String(actualTemperature) + "}";
  120.             http.post(request, response, headers);
  121.         }
  122.        
  123.         // SparkCore: publish
  124.         //Spark.publish("allInfo", allInfo);
  125.         //Spark.publish("heating", heating);
  126.     }
  127.    
  128. }
  129.  
  130. //void update18B20Temp(DeviceAddress deviceAddress, double &tempC)
  131. void update18B20Temp(double &tempC)
  132. {
  133.   double tmp = sensors.getTempFByIndex(0);
  134.   if(tmp > 0)
  135.     tempC = tmp;
  136. }
  137.  
  138. // set point temperature
  139. int setPointTemperature(String command)
  140. {
  141.     // Convert to double
  142.     pointTemperature = (double) command.toInt();
  143.     return 1;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement