Maker.io main logo

Automatic Water Softener Reporter with LaunchPad

2017-04-05 | By All About Circuits

License: See Original Project Launchpad

Courtesy of All About Circuits

Make your home smarter by displaying the current salt level in your water softener with the LaunchPad CC3200 and a proximity sensor.

 

Requirements

Updated to the latest firmware for Energia support, you can find directions here.

  • Energia - Arduino IDE Clone from TI

Use in article: v. 0101E00016

Jumper wires, soldering wires, or a JST 3 terminal with wires

This is needed because the ADC on the CC3200 can only read values up to 1.46 V.

  • Dweet.io "thing"

Hardware Setup

Connecting the wires

You can find a CC3200 pinout here.

Schematic

Scheme-it Scheme-it BOM

 

Testing the Connection and Measuring Calibration Values

This code samples the proximity sensor output and converts it into a distance.

1. Copy the code below into a new sketch.

2. Select the correct board in Tools>Board>Launchpad w/ CC3200 80 MHz.

3. Program the board.

1. File>Upload or the arrow button

4. Open the terminal, hit Ctrl Shift M, and you’ll see the distance printed once every 200 ms.

5. You can see this number change if you move a solid surface near the sensor. You can see an example in the video below. If the distance is outside of the measurement range, incorrect values can be displayed.

6. To calibrate the sensor for your water softener, start by mounting the sensor where you want it to go inside the softener. You can mount it temporarily with tape if you’re still making adjustments. For a permanent solution, you’ll want to use screws or glue, and ideally, a project case.

Automatic Water Softener

1. Remove salt from the water softener until it is at a level you want the sensor to show as 0%

1. Be sure to make note of the raw data value being reported.

2. Put a handful of salt in your hand and move it towards the sensor where you want it to show as 100%.

1. Once again, make note of the raw data value being reported.

Demonstration of the Proximity Sensor Test Code

The video below shows step 5 from the instructions above.

 

Copy Code
                  /*Take a proximity measurement and send out over the serial port*/
#define PROX_PIN 24
float prox_sensor_value_V = 0;
float distance_cm = 0;
/*values from datasheet for sensor*/
#define MAX_SENSOR_VOLTAGE 2.8
#define MIN_SENSOR_VOLTAGE 0.4
#define MAX_SENSOR_DISTANCE_CM 150
#define MIN_SENSOR_DISTANCE_CM 15
void setup()
{
Serial.begin(115200);
}

void loop()
{
prox_sensor_value_V = (float)analogRead(PROX_PIN) *2* 1.46 / 4096;
if(prox_sensor_value_V>=2.8 && prox_sensor_value_V <= 0.4){
Serial.println("Sensor out of range, object must be 15-150cm");
}
else{
/*convert to distance
* linearly convert based on the inverse relationship between voltage and distance
* described in the datasheet
*
* Could get more precision by doing a calibration and using a look-up table
*/
distance_cm = (prox_sensor_value_V - MIN_SENSOR_VOLTAGE) * \
(MIN_SENSOR_DISTANCE_CM - MAX_SENSOR_DISTANCE_CM)\
/ (MAX_SENSOR_VOLTAGE - MIN_SENSOR_VOLTAGE) MAX_SENSOR_DISTANCE_CM;

Serial.print("Distance: ");
Serial.print(distance_cm,1);
Serial.print("cm -- ");
Serial.print(distance_cm/2.2,1);
Serial.println("in");
}
delay(200);
}

Setting Up dweet.io

dweet.io

Go to https://dweet.io/play/

Click "Create a dweet for a thing"

Fill in the parameters for the thing and the content. Choose a name for your thing that is unique unless you plan to pay for a "locked" thing. Otherwise, people can view or change your thing since the free version is public access. You’ll want to display the water softener percent_level, that is the content portion of your thing. The content must be a valid JSON string.

dweet-for-a-thing

Clicking "Try it out!" will let you see what will happen when the dweet is posted. The request URL is what you'll use to post from the CC3200.

dweet-iot-code

Posting to dweet.io from the CC3200

The code below will connect to the dweet.io setup in the previous setup and post the water softeners percent value. It works by opening a connection to dweet.io's HTTP server on port 80 and requesting the webpage with the GET function. This is the same as pasting the URL into the previous section into a web browser to write a value.

1. Copy the code below into a new sketch.

2. Select the board you’ll be using in Tools>Board>Launchpad w/ CC3200 80MHz.

3. Modify the following variables to your settings:

variable-table

1. Program the board.

2. File>Upload or the arrow button.

3. Open the terminal, hit Ctrl Shift M, and you’ll see the salt level uploaded once every second.

4. Wave your hand near the sensor to see the number change and view real time at the URL of your thing.

1. This article uses https://dweet.io:443/get/latest/dweet/for/my_water_softener

2. Here is the output:

3. {"this":"succeeded","by":"getting","the":"dweets","with":[{"thing":"my_water_softener","created":"2015-08-14T02:40:59.181Z","content":{"percent_level":21}}]}

5. The website isn't the most user friendly, but it does indicate the percent level from anywhere with internet access. Some other options for displaying data are:

1. Using freeboard.io

1. This does require an account.

2. Paying for a "lock" to send an alert based on the value on dweet.io.

1. This is useful if you want an email when the salt percentage is below a certain level.

3. Create your own webpage to create a plot using all of the JSON data from your dweet.

1. This option is similar to creating a webpage with Google Chart.

Copy Code
                  #include 
#include

/*user defined settings*/
#define PROX_PIN 24
char ssid[] = "ssid"; //SSID of network
char password[] = "password"; //password for wireless network
char thing[] = "thing"; //dweet thing
int distance_cm_0percent = 15; //distance that you want to show 0%
int distance_cm_100percent = 140; //distance that you want to show 100%

/*variables*/
float prox_sensor_value_V = 0;
float distance_cm = 0;
uint8_t salt_level_percent = 0;
WiFiClient client;

/*values from datasheet for sensor*/
#define MAX_SENSOR_VOLTAGE 2.8
#define MIN_SENSOR_VOLTAGE 0.4
#define MAX_SENSOR_DISTANCE_CM 150
#define MIN_SENSOR_DISTANCE_CM 15

void setup()
{
Serial.begin(115200);
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid);
/*Connect to WPA/WPA2 network. Change this line if using open or WEP network*/
WiFi.begin(ssid, password);
while ( WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(300);
}
Serial.println("\nYou're connected to the network");

Serial.println("Waiting for an ip address");
while (WiFi.localIP() == INADDR_NONE) {
Serial.print(".");
delay(300);
}
Serial.println("\nIP Address obtained");
printWifiStatus();
}

void loop()
{
prox_sensor_value_V = (float)analogRead(PROX_PIN) *2* 1.46 / 4096;
if(prox_sensor_value_V>=2.8 && prox_sensor_value_V <= 0.4){
Serial.println("Sensor out of range, object must be 15-150cm");
}
else{
/*convert to distance
* linearly convert based on the inverse relationship between voltage and distance
* described in the datasheet
*
* Could get more precision by doing a calibration and using a look-up table
*/
distance_cm = (prox_sensor_value_V - MIN_SENSOR_VOLTAGE) * \
(MIN_SENSOR_DISTANCE_CM - MAX_SENSOR_DISTANCE_CM)\
/ (MAX_SENSOR_VOLTAGE - MIN_SENSOR_VOLTAGE) MAX_SENSOR_DISTANCE_CM;
Serial.print("Distance: ");
Serial.print(distance_cm,1);
Serial.print("cm -- ");
Serial.print(distance_cm/2.2,1);
Serial.println("in");
/*convert to calibrated values and cap at max value*/
if(distance_cm > distance_cm_100percent) distance_cm=distance_cm_100percent;
if(distance_cm < distance_cm_0percent) distance_cm=distance_cm_0percent;
salt_level_percent = map((int)distance_cm,distance_cm_0percent,distance_cm_100percent,100,0);
Serial.print("Salt level percent: ");
Serial.print(salt_level_percent);
Serial.println("%");
/*Send to dweet.io server using HTTP GET function, equivalent to typing the webpage into your browser*/
Serial.println("Send to dweet.io");
if (client.connect("www.dweet.io", 80)) {
client.print("GET /dweet/for/");
client.print(thing);
client.print("?percent_level=");
client.print(salt_level_percent);
client.println(" HTTP/1.1");

client.println("Host: dweet.io");
client.println("Connection: close");
client.println("");

/*print the response from the server for debug purposes*/
while (client.connected()) {
while (client.available()) {
Serial.write(client.read());
}
}
Serial.println("");
}
else{
Serial.println("Error: Coult not connect to dweet server!");
}
}
delay(60000);
}

void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());

// print your WiFi IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);

// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}

 

制造商零件编号 CC3200-LAUNCHXL
LAUNCHPAD DEV BOARD CC3200
Texas Instruments
¥537.25
Details
制造商零件编号 CF14JT10K0
RES 10K OHM 5% 1/4W AXIAL
Stackpole Electronics Inc
¥0.81
Details
Add all DigiKey Parts to Cart
TechForum

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.

Visit TechForum