Back to Parent

#include "DS18B20.h"
#include "OneWire.h"
#include "HttpClient.h"

//Ubidots platform variables for reference
#define VARIABLE_ID "XXXX"
#define TOKEN "YYYY"

//OneWire Bus variables
DS18B20 ds18b20 = DS18B20(A0);
char szInfo[64];

//Variables for actual temperature and setpoints
double celsius = 0.0;
double fahrenheit = 0.0;
int highTempSet = 80;
int lowTempSet = 60;

//RGB LED pins and default values
int redPin = D0;
int greenPin = D1;
int bluePin = D2;
int redValue = 255;
int greenValue = 255;
int blueValue = 255;

//Potentiometer pins and LED brightness variables
int pothighPin = A2;
int potreadPin = A3;
int potlowPin = A4;
int potValue = 0;
int ledbrightness = 255;

//Initilize HttpClient
HttpClient http;
unsigned int nextTime = 0;

http_header_t headers[] = {
    { "Content-Type", "application/json" },
    { NULL, NULL } // NOTE: Always terminate headers will NULL
};

http_request_t request;
http_response_t response;



void setup()
{
  //Debugging
  //Serial.begin(9600); //Debugging messages

  //Pot pinmodes, high/low pins
  pinMode(potlowPin, OUTPUT);
  digitalWrite(potlowPin, LOW);
  pinMode(pothighPin, OUTPUT);
  digitalWrite(pothighPin, HIGH);
  pinMode(potreadPin, INPUT);

  //Declare cloud variables to monitor
  //Particle.variable("celsius", celsius);
  Particle.variable("fahrenheit", fahrenheit);
  Particle.variable("highTempSet", highTempSet);
  Particle.variable("lowTempSet", lowTempSet);

  //Function for setting temperature setpoints via cloud
  Particle.function("setTempHigh", setTempHigh);
  Particle.function("setTempLow", setTempLow);

  //Assign pinModes
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);

  //HTTP setup
  request.hostname = "things.ubidots.com";
  request.port = 80;
  request.path = "/api/v1.6/variables/XXXX/values?token=YYYY";
}

void loop()
{
  //Checking OneWire devices
  if(!ds18b20.search()){
    //Serial.println("No more addresses.");
    //Serial.println();
    ds18b20.resetsearch();
    delay(250);

    return;
  }

  //Checking if sensor upload to Ubidots is due
  if (nextTime > millis()) {
    delay(250);
    return;
  }

  setBrightness();

  //Pull current temperatures from ds18b20 library functions
  celsius = ds18b20.getTemperature();
  fahrenheit = ds18b20.convertToFahrenheit(celsius);

  //Check against setpoints and setting LED values
  if (fahrenheit >= highTempSet)
  {
    analogWrite(redPin, ledbrightness);
    analogWrite(greenPin, 255);
    analogWrite(bluePin, 255);
  }
  else if (fahrenheit <= lowTempSet)
  {
    analogWrite(redPin, 255);
    analogWrite(greenPin, 255);
    analogWrite(bluePin, ledbrightness);
  }
  else
  {
    analogWrite(redPin, 255);
    analogWrite(greenPin, ledbrightness);
    analogWrite(bluePin, 255);
  }

  //Adjust HTTP post body
  request.body = "{\"value\":" + String(fahrenheit) + "}";

  // Post request
  http.post(request, response, headers);

  //Set delay before next cycle
  nextTime = millis() + 20000;
}

int setTempHigh(String command)
{
  int input = command.toInt();
  if (input <= lowTempSet)
  {
    return -1;
  }
  highTempSet = input;
  return 1;
}

int setTempLow(String command)
{
  int input = command.toInt();
  if (input >= highTempSet)
  {
    return -1;
  }
  lowTempSet = input;
  return 1;
}

int setBrightness()
{
  potValue = analogRead(potreadPin);
  ledbrightness = map(potValue, 0, 4095, 255, 0);
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0