#include <Blynk.h>
/**************************************************************
* Blynk is a platform with iOS and Android apps to control
* Arduino, Raspberry Pi and the likes over the Internet.
* You can easily build graphic interfaces for all your
* projects by simply dragging and dropping widgets.
*
* Downloads, docs, tutorials: http://www.blynk.cc
* Blynk community: http://community.blynk.cc
* Social networks: http://www.fb.com/blynkapp
* http://twitter.com/blynk_app
*
* Blynk library is licensed under MIT license
* This example code is in public domain.
*
**************************************************************
*
* This example shows how to use ESP8266 Shield via Hardware Serial
* (on Mega, Leonardo, Micro...) to connect your project to Blynk.
*
* Note: Ensure a stable serial connection to ESP8266!
* Firmware version 1.0.0 (AT v0.22) or later is needed.
* You can change ESP baud rate. Connect to AT console and call:
* AT+UART_DEF=115200,8,1,0,0
*
* Change WiFi ssid, pass, and Blynk auth token to run :)
* Feel free to apply it to any other example. It's simple!
*
**************************************************************/
//#define BLYNK_DEBUG
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
// Set ESP8266 Serial object
#define EspSerial Serial
char auth[] = "***";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "UNITE-91F4";
char pass[] = "48135613";
// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial
// or Software Serial on Uno, Nano...
//#include <SoftwareSerial.h>
//SoftwareSerial EspSerial(2, 3); // RX, TX
// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);
#include <SimpleTimer.h>
#include <DHT.h>
#define DHTPIN 9 //pin gpio 12 in sensor
#define DHTTYPE DHT22 // DHT 22 Change this if you have a DHT11
DHT dht(DHTPIN, DHTTYPE);
#include <OneWire.h>
#define ONE_WIRE_BUS 7
OneWire oneWire(ONE_WIRE_BUS);
#include <DallasTemperature.h>
DallasTemperature sensors(&oneWire);
unsigned long previousMillisLED12 = 0;
unsigned long previousMillisLED13 = 0;
unsigned long previousMillisLED14 = 0;
unsigned long previousMillisLED15 = 0;
unsigned long previousMillisLED16 = 0;
unsigned long previousMillisLED17 = 0;
unsigned long previousMillisLED20 = 0;
unsigned long previousMillisLED21 = 0;
const int buttonPin = 4; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int buttonState = 0;
int intervalLED12 = 8000;
int intervalLED13 = 10000;
int intervalLED14 = 60000;
#include "Arduino.h"
class Ultrasonic
{
public:
Ultrasonic(int pin);
void DistanceMeasure(void);
long microsecondsToCentimeters(void);
long microsecondsToInches(void);
private:
int _pin;//pin number of Arduino that is connected with SIG pin of Ultrasonic Ranger.
long duration;// the Pulse time received;
};
Ultrasonic::Ultrasonic(int pin)
{
_pin = 5;
}
/*Begin the detection and get the pulse back signal*/
void Ultrasonic::DistanceMeasure(void)
{
pinMode(_pin, OUTPUT);
digitalWrite(_pin, LOW);
delayMicroseconds(2);
digitalWrite(_pin, HIGH);
delayMicroseconds(5);
digitalWrite(_pin, LOW);
pinMode(_pin, INPUT);
duration = pulseIn(_pin, HIGH);
}
/*The measured distance from the range 0 to 400 Centimeters*/
long Ultrasonic::microsecondsToCentimeters(void)
{
return duration / 29 / 2;
}
/*The measured distance from the range 0 to 157 Inches*/
long Ultrasonic::microsecondsToInches(void)
{
return duration / 74 / 2;
}
Ultrasonic ultrasonic(7);
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
SimpleTimer timer;
#include <SoftwareSerial.h> //we have to include the SoftwareSerial library, or else we can't use it.
#define rx 2 //define what pin rx is going to be.
#define tx 3 //define what pin tx is going to be.
SoftwareSerial myserial(rx, tx); //define how the soft serial port is going to work.
String inputstring = ""; //a string to hold incoming data from the PC
String sensorstring = ""; //a string to hold the data from the Atlas Scientific product
boolean input_stringcomplete = false; //have we received all the data from the PC
boolean sensor_stringcomplete = false; //have we received all the data from the Atlas Scientific product
float ph; //used to hold a floating point number that is the pH.
void setup()
{
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
// Set console baud rate
Serial.begin(9600);
// Set ESP8266 baud rate
EspSerial.begin(ESP8266_BAUD);
Blynk.begin(auth, wifi, "UNITE-91F4", "48135613");
Serial.begin(9600); //set baud rate for the hardware serial port_0 to 9600
myserial.begin(9600); //set baud rate for software serial port_3 to 9600
inputstring.reserve(10); //set aside some bytes for receiving data from the PC
sensorstring.reserve(30); //set aside some bytes for receiving data from Atlas Scientific product
}
void serialEvent() { //if the hardware serial port_0 receives a char
char inchar = (char)Serial.read(); //get the char we just received
inputstring += inchar; //add it to the inputString
if (inchar == '\r') {
input_stringcomplete = true; //if the incoming character is a <CR>, set the flag
}
}
void loop()
{
Blynk.run();
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == LOW) {
// turn LED on:
digitalWrite(10, LOW);
}
unsigned long currentMillis = millis();
if (currentMillis - previousMillisLED12 >= intervalLED12 & input_stringcomplete) { //if a string from the PC has been received in its entirety
myserial.print(inputstring); //send that string to the Atlas Scientific product
inputstring = ""; //clear the string
input_stringcomplete = false; //reset the flag used to tell if we have received a completed string from the PC
}
if (myserial.available() > 0) {
//if we see that the Atlas Scientific product has sent a character.
char inchar = (char)myserial.read(); //get the char we just received
sensorstring += inchar;
if (inchar == '\r') {
sensor_stringcomplete = true; //if the incoming character is a <CR>, set the flag
}
if (sensor_stringcomplete) { //if a string from the Atlas Scientific product has been received in its entirety
//send that string to the PC's serial monitor
Blynk.virtualWrite (15, sensorstring);
Blynk.virtualWrite (16, sensorstring);
Blynk.virtualWrite (17, sensorstring);
ph = sensorstring.toFloat(); //convert the string to a floating point number so it can be evaluated by the Arduino
sensorstring = ""; //clear the string:
sensor_stringcomplete = false;
previousMillisLED12 = currentMillis;
}
if (currentMillis - previousMillisLED13 >= intervalLED13 ) {
sensors.begin();
sensors.requestTemperatures(); // Send the command to get temperatures
Blynk.virtualWrite(7, sensors.getTempCByIndex (1) * 9 / 5 + 32);
Blynk.virtualWrite(2, sensors.getTempCByIndex (0) * 9 / 5 + 32);
Blynk.virtualWrite(3, sensors.getTempCByIndex (0) * 9 / 5 + 32);
Blynk.virtualWrite(9, sensors.getTempCByIndex(0) * 9 / 5 + 32);
float h = dht.readHumidity();
float t = dht.readTemperature();
t = t * 9 / 5 + 32;
Blynk.virtualWrite(11, t); // virtual pin
Blynk.virtualWrite(10, h); // virtual pin
long RangeInInches;
long RangeInCentimeters;
ultrasonic.DistanceMeasure();// get the current signal time;
RangeInInches = ultrasonic.microsecondsToInches();//convert the time to inches;
RangeInCentimeters = ultrasonic.microsecondsToCentimeters();//convert the time to centimeters;
int val = ultrasonic.microsecondsToInches();
val = map(val, 4, 12, 100, 0);
Blynk.virtualWrite(5, val);
previousMillisLED13 = currentMillis;
} } }