#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include "SparkFunHTU21D.h"
#include <SparkFunTSL2561.h>
#include "Servo.h"
#include "BlynkBoard_settings.h"
HTU21D thSense;
bool scanI2C(uint8_t address);
// blynk virtual pins
#define LED_PIN 16
#define ADC_VOLTAGE_VIRTUAL V8
#define SERVO_XY_VIRTUAL V14
#define SERVO_MAX_VIRTUAL V16
#define SERVO_ANGLE_VIRUTAL V17
const int btnPin = 0; // button to water level
// Wifi connections
char BlynkAuth[] = "xxxx";
char WiFiNetwork[] = "xxxx";
char WiFiPassword[] = "xxx";
WidgetLCD lcd(V10); //LCD widget on V10
WidgetLED led3(V3); // LED widget in Blynk App on v3
WidgetLED led4(V4); // LED widget in Blynk App on v4
SimpleTimer timer;
void setup()
{
Serial.begin(9600);
Blynk.begin(BlynkAuth, WiFiNetwork, WiFiPassword); //data to connect
pinMode(LED_PIN, OUTPUT); // Light control
pinMode(btnPin, INPUT_PULLUP); // water level
while (Blynk.connect() == false) {
// Wait until connected
}
timer.setInterval(500L, buttonLedWidget);
}
// V3 LED Widget represents the physical button state
boolean btnState = false;
void buttonLedWidget()
{
// Read button
boolean isPressed = (digitalRead(btnPin) == LOW);
// If state has changed...
if (isPressed != btnState) {
if (isPressed) {
led3.on();
lcd.clear(); //Use it to clear the LCD Widget
lcd.print(0, 0, "Something"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
lcd.print(0, 1, "Wrong");
} else {
led3.off();
lcd.clear(); //Use it to clear the LCD Widget
lcd.print(0, 0, "Everything"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
lcd.print(0, 1, "OK");
}
btnState = isPressed;
}
}
// ADC is read directly to a gauge
// Voltage is read to V8 to work as a gas sensor
BLYNK_READ(ADC_VOLTAGE_VIRTUAL)
{
float adcRaw = analogRead(A0); // Read in A0
// Divide by 1024, then multiply by the hard-wired voltage divider max (3.2)
float voltage = ((float)adcRaw / 1024.0) * ADC_VOLTAGE_DIVIDER;
Blynk.virtualWrite(ADC_VOLTAGE_VIRTUAL, voltage); // Output value to Blynk
BB_DEBUG("Blynk DC Voltage: " + String(voltage));
if (adcRaw < 512)
{
led4.off();
lcd.clear(); //Use it to clear the LCD Widget
lcd.print(0, 0, "Everything"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
lcd.print(0, 1, "OK");
}else{
led4.on();
lcd.clear(); //Use it to clear the LCD Widget
lcd.print(0, 0, "Something"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
lcd.print(0, 1, "Wrong");
}
}
#define SERVO_PIN 15 // Servo attached to pin 15
#define SERVO_MINIMUM 5 // Minimum servo angle
unsigned int servoMax = 180; // Default maximum servo angle
int servoX = 0; // Servo angle x component
int servoY = 0; // Servo angle y component
Servo myServo; // Servo object
bool firstServoRun = true;
BLYNK_WRITE(SERVO_XY_VIRTUAL)
{
// If it's the first time the servo is set, turn it on
if (firstServoRun)
{
myServo.attach(SERVO_PIN);
myServo.write(15);
firstServoRun = false;
}
// Read in the servo value:
int servoXIn = param[0].asInt();
int servoYIn = param[1].asInt();
BB_DEBUG("Servo X: " + String(servoXIn));
BB_DEBUG("Servo Y: " + String(servoYIn));
servoX = servoXIn - 128; // Center xIn around 0 (+/-128)
servoY = servoYIn - 128; // Center xIn around 0 (+/-128)
// Calculate the angle, given x and y components:
float pos = atan2(servoY, servoX) * 180.0 / PI; // Convert to degrees
// atan2 will give us an angle +/-180
if (pos < 0) // Convert it to 0-360:
pos = 360.0 + pos;
int servoPos = map(pos, 0, 360, SERVO_MINIMUM, servoMax);
// Write the newly calculated angle to a virtual variable:
Blynk.virtualWrite(SERVO_ANGLE_VIRUTAL, servoPos);
// Constrain the angle between min/max:
myServo.write(servoPos); // And set the servo position
}
BLYNK_WRITE(SERVO_MAX_VIRTUAL)
{
int sMax = param.asInt();
BB_DEBUG("Servo Max: " + String(sMax));
servoMax = constrain(sMax, SERVO_MINIMUM + 1, 360);
Serial.println("ServoMax: " + String(servoMax));
}
void loop()
{
Blynk.run();
timer.run();
}
Comments