STARTUP(WiFi.selectAntenna(ANT_AUTO));
#include <OneWire.h>
#include <spark-dallas-temperature.h>
#define ONE_WIRE_BUS D4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensor(&oneWire);
float t;
#include <ThingSpeak.h>
TCPClient client;
unsigned long myChannelNumber = yourchannelnumber;
const char * myWriteAPIKey = "yourchannelAPIkeys";
const char* server = "api.thingspeak.com";
// PINs
int led_r=0; // LED on D0 is Red
int led_y=1; // LED on D1 is Yellow
int led_g=2; // LED on D2 is Green
const uint8_t RELAY_1_PIN=3; // PIN on D3 if for Relay
// #define ONE_WIRE_BUS D4 // PIN on D4 is sensor DS18B20 - mentioned previosly - here for information only
int echoPin = 5; // sonic - echo on D5
int trigPin = 6; // sonic - trig on D6
int led =7; // LED on D7 is Blue - build-in
// for keeping data
char st;
char* status_current; char* status_old; // for keeping status of Tank - empty, fillin, full, fillout, error
unsigned long lastmillis = 0; // for tracking sending temperature and moistute data
int duration, distance; // for sonic
void setup() {
Serial.begin(9600);
ThingSpeak.begin(client); // start ThingSpeak
sensor.begin(); // start DS18B20 sensor
pinMode(RELAY_1_PIN, OUTPUT); // D3 output - relay
pinMode(echoPin, INPUT); // D5 - sonic - input - echo
pinMode(trigPin, OUTPUT); // D6 - sonic - output - trigger
pinMode(led_r, OUTPUT); // LEDs output
pinMode(led_y, OUTPUT);
pinMode(led_g, OUTPUT);
pinMode(led, OUTPUT); // LED output - onboard
}
void loop() {
sensors_read();
if ((millis() - lastmillis) > 10000) { lastmillis = millis(); sensors_broadcast(); } // publish temperature & moisture - every minute
if ( (distance <= 10 ) && (distance !=0) ) { tank_full(); } // status of tank filling with water
else if ( distance >= 50 ) { tank_empty(); } // if water lever low -> turning On relay
else if ( distance > 90 ) { tank_error(); } // OFF - just in case if something goes wrong
else if ( distance ==0 ) { tank_error(); } // OFF - just in case if something goes wrong
else if ( (distance < 50 ) && (distance > 10)) { tank_fillout(); }
// else if ( (distance > 10) && (distance < 89) )
// {
// if (digitalRead(RELAY_1_PIN) == HIGH) { tank_fillin(); }
// else if (digitalRead(RELAY_1_PIN) == LOW) { tank_fillout();}
// }
else { tank_error(); } // OFF
}
void sensors_read() {
// read data from Sensors
sensor.requestTemperatures();
t=sensor.getTempCByIndex( 0 ); // temperature
digitalWrite(trigPin, LOW); delayMicroseconds(2); // для большей точности установим значение LOW на пине Trig
digitalWrite(trigPin, HIGH);delayMicroseconds(10); // Теперь установим высокий уровень на пине Trig // Подождем 10 μs
digitalWrite(trigPin, LOW); // Узнаем длительность высокого сигнала на пине Echo
duration = pulseIn(echoPin, HIGH); // Рассчитаем расстояние
distance = duration / 58;
delay(1000);
}
void sensors_broadcast() {
String st(t, 1); Particle.publish("Temperature:",st);
String sdist(distance); Particle.publish("Distance :", sdist);
digitalWrite(led_y, HIGH); delay(500); digitalWrite(led_y, LOW); // blink LED Yellow
ThingSpeak.setField(4, t);
ThingSpeak.setField(5, 90-distance);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
delay(1000);
}
void tank_empty() {
digitalWrite(led_r, HIGH); digitalWrite(led_y, LOW); digitalWrite(led_g, LOW);
digitalWrite(RELAY_1_PIN, HIGH);
char* status_current = "EMPTY";
Particle.publish(" Tank is:", status_current);
delay(60000);
}
void tank_fillin() {
digitalWrite(led_r, LOW); digitalWrite(led_y, HIGH); digitalWrite(led_g, HIGH);
char* status_current = "Fillin";
Particle.publish(" Tank is:", status_current);
delay(60000);
}
void tank_fillout() {
digitalWrite(led_r, HIGH); digitalWrite(led_y, HIGH); digitalWrite(led_g, LOW);
char* status_current = "FillOut";
Particle.publish(" Tank is:", status_current);
delay(60000);
}
void tank_full() {
digitalWrite(led_r, LOW); digitalWrite(led_y, LOW); digitalWrite(led_g, HIGH);
digitalWrite(RELAY_1_PIN, LOW);
char* status_current = "FULL";
Particle.publish(" Tank is:", status_current);
delay(60000);
}
void tank_error() {
digitalWrite(led_r, HIGH); digitalWrite(led_y, HIGH); digitalWrite(led_g, HIGH);
digitalWrite(RELAY_1_PIN, LOW);
char* status_current = "ERROR";
Particle.publish(" Tank is:", status_current);
delay(60000);
}
Comments