#include <Adafruit_NeoPixel.h>
#include <NTPClient_Generic.h>
#include <WiFiUdp.h>
#include <SPI.h>
#include <SD.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define BLYNK_PRINT Serial // You should get Auth Token in the Blynk App.Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxxxx_xxxxxxxxx-xxxxxxxxxxx";
char ssid[] = "xxxxxxxxxxxxx"; // Your WiFi credentials.
char password[] = "xxxxxxxxxxx"; // Set password to "" for open networks.
const int chipSelect = D4;
const long utcOffsetInSeconds = 3600;
String weekDays[7]={"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
String months[12]={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
WiFiUDP ntpUDP;// Define NTP Client to get time
NTPClient timeClient(ntpUDP, "pool.ntp.org");
BlynkTimer timer;
int StrokesBlynk = 0;
int InitialDelayBlynk = 0;
int PauseBlynk = 0;
int Aplus = D2;
int Amenus = D3;
int VButton = D0;
int relay = D1;
int Strokes = 0;
int trueStrokes = 0;
int truePause = 0;
#define PIN D8 //RGB
#define LED_NUM 7 //RGB
Adafruit_NeoPixel leds = Adafruit_NeoPixel(LED_NUM, PIN, NEO_GRB + NEO_KHZ800); //RGB
//boolean state = LOW;
void setup()
{
leds.begin(); //RGB
//Debug console
Serial.begin(9600);
Blynk.begin(auth, ssid, password);
pinMode(VButton, INPUT);
pinMode(relay, OUTPUT);
pinMode(Aplus, INPUT);
pinMode(Amenus, INPUT);
timer.setInterval(300L, finecorsa);
//while (!Serial) { ; } wait for serial port to connect. Needed for Leonardo only
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
return;
}
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 );
Serial.print ( "." );
}
timeClient.begin();// Set offset time in seconds to adjust for your timezone
timeClient.setTimeOffset(3600);// In Italy, There is a GMT offset of +1 hour
}
void led_set(uint8 R, uint8 G, uint8 B) {
for (int i = 0; i < LED_NUM; i++) {
leds.setPixelColor(i, leds.Color(R, G, B));
leds.show();
delay(50);
}
}
BLYNK_WRITE(V0) //done strokes from Blynk to Wemos
{
StrokesBlynk = param.asInt();
Serial.print("Strokes: ");
Serial.println(StrokesBlynk);
}
BLYNK_WRITE(V1) //initial delay from Blynk to Wemos
{
InitialDelayBlynk = param.asInt();
Serial.print("InitialDelay : ");
Serial.println(InitialDelayBlynk);
}
BLYNK_WRITE(V2) //Pause between 2 strokes
{
PauseBlynk = param.asInt();
Serial.print("Pause : ");
Serial.println(PauseBlynk);
}
void loop() {
File dataFile = SD.open("datalog.odf", FILE_WRITE);
Blynk.run();
timer.run();
if (digitalRead(VButton) && (digitalRead(Amenus))) {
Strokes = 0;
truePause = PauseBlynk;
trueStrokes = StrokesBlynk;
delay(InitialDelayBlynk * 1000);
digitalWrite(relay, HIGH);
dataFile.print(" requested stroke: ");
dataFile.println(trueStrokes);
dataFile.close();
}
}
void finecorsa () {
timeClient.update(); //String dataString = "";
File dataFile = SD.open("datalog.odf", FILE_WRITE);
if (digitalRead(Aplus)) {
digitalWrite(relay, LOW);
led_set(100, 0, 0); //red
Strokes = Strokes + 1;
Serial.print("done strokes: ");//Serial.print(daysOfTheWeek[timeClient.getDay()]);
Serial.println(Strokes);
Blynk.virtualWrite(V3, Strokes);
unsigned long epochTime = timeClient.getEpochTime();
Serial.print("Epoch Time: ");
Serial.println(epochTime);
dataFile.print("Epoch Time; ");
dataFile.print(epochTime);
dataFile.print("; ");
dataFile.print(timeClient.getFormattedTime());
dataFile.print("; ");
dataFile.print("done strokes: ");
dataFile.print(Strokes);
dataFile.println(";");
dataFile.close();
}
else if (digitalRead(Amenus) && (Strokes < trueStrokes)) {
led_set(100, 100, 0); //yellow
delay(truePause * 1000);
digitalWrite(relay, HIGH);
led_set(100, 0, 0); //red
}
else if (digitalRead(Amenus) && (Strokes >= trueStrokes)) {
Strokes = 0;
trueStrokes = 0;
Blynk.virtualWrite(V3, Strokes);
led_set(0, 100, 0); //green
}
}
Comments
Please log in or sign up to comment.