Hi,
I have a elapsed time nicely packed in the VOID Time1().
Now on serialPrint it looks good and i want the same format to a Value Display widget. I tried Blynk.virtualWrite(V10, time1()); but i get this compile error:
Licht_2000_ESP32_4:248:34: error: invalid use of void expression
Blynk.virtualWrite(V10, time1());
#include <TimeLib.h>
time_t start;
/*---------------------------------------------------------
WIFI MANAGER
--------------------------------------------------------*/
#include <FS.h>
#include <WiFi.h>
#include "SPIFFS.h"
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
#include <ArduinoJson.h>
//instructions: https://www.youtube.com/watch?v=saAv7YOiAyM
/*---------------------------------------------------------
BLYNK APP
--------------------------------------------------------*/
#define BLYNK_PRINT Serial
//#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
BlynkTimer timer;
bool isFirstConnect = true;
char auth[] = "xxxxxxx";
/*----------------------------------------------------------
MH-Z19 CO2 sensor
----------------------------------------------------------*/
#include <MHZ19_uart.h>
const int rx_pin = 16; //Serial rx pin no
const int tx_pin = 17; //Serial tx pin no
MHZ19_uart mhz19;
/*----------------------------------------------------------
BME280 SENSOR
----------------------------------------------------------*/
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#define SEALEVELPRESSURE_HPA (1003.25)
Adafruit_BME280 bme; // I2C
/*----------------------------------------------------------
Status LED's
----------------------------------------------------------*/
const int GreenLed = 15;
const int RedLed = 2;
int ledState = LOW; // ledState used to set the LED
unsigned long previousMillis = 0; // will store last time LED was updated
unsigned long interval = 100;
/*----------------------------------------------------------
SWITCH/LIGHT
----------------------------------------------------------*/
#define Warmwhite 25
#define Coolwhite 26
const int freq = 200;
const int ledChannel = 0;
const int resolution = 8;
int warmwitdimmer;
#define schakelaar 27
int relaisStatus;
int light;
int schakelaarStatus;
int laststate;
//////////////WIFI MANAGER PART 2//////////////
char LampName[10];
bool shouldSaveConfig = true;
void saveConfigCallback () {
Serial.println("Should save config");
shouldSaveConfig = true;
}
BLYNK_CONNECTED() {
if (isFirstConnect) {
// Request Blynk server to re-send latest values for all pins
Blynk.syncAll();
// You can also update individual virtual pins like this:
//Blynk.syncVirtual(V0, V2);
isFirstConnect = false;
}
}
/*---------------------------------------------------------
BEGIN VOID SETUP
--------------------------------------------------------*/
void setup()
{
Serial.begin(115200);
start = now();
timer.setInterval(5000L, myTimerEvent);
pinMode(GreenLed, OUTPUT);
pinMode(RedLed, OUTPUT);
digitalWrite(RedLed, HIGH);
pinMode(schakelaar, INPUT_PULLUP);
// configure LED PWM functionalitites
ledcSetup(ledChannel, freq, resolution);
// attach the channel to the GPIO to be controlled
ledcAttachPin(Warmwhite, ledChannel);
mhz19.begin(rx_pin, tx_pin);
mhz19.setAutoCalibration(false);
/*---------------------------------------------------------
WIFI MANAGER SETUP
--------------------------------------------------------*/
SPIFFS.begin (true);
if (SPIFFS.begin()) {
Serial.println("mounted file system");
if (SPIFFS.exists("/config.json")) {
//file exists, reading and loading
Serial.println("reading config file");
File configFile = SPIFFS.open("/config.json", "r");
if (configFile) {
Serial.println("opened config file");
size_t size = configFile.size();
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());
json.printTo(Serial);
if (json.success()) {
Serial.println("\nparsed json");
strcpy(LampName, json["LampName"]);
} else {
Serial.println("failed to load json config");
}
}
}
} else {
Serial.println("failed to mount FS");
}
WiFiManagerParameter custom_LampName("LampName", "LampName", LampName, 10);
WiFiManager wifiManager;
wifiManager.setSaveConfigCallback(saveConfigCallback);
wifiManager.addParameter(&custom_LampName);
if (!wifiManager.autoConnect("Lamp2000", "G42")) {
Serial.println("failed to connect and hit timeout");
delay(3000);
ESP.restart();
delay(5000);
}
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
strcpy(LampName, custom_LampName.getValue());
if (shouldSaveConfig) {
Serial.println("saving config");
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
json["LampName"] = LampName;
File configFile = SPIFFS.open("/config.json", "w");
if (!configFile) {
Serial.println("failed to open config file for writing");
}
json.printTo(Serial);
json.printTo(configFile);
configFile.close();
}
Serial.println("local ip");
Serial.println(WiFi.localIP());
Serial.println();
Serial.print("LampName: ");
Serial.print(LampName);
Blynk.config(auth);
bool result = Blynk.connect(180);
if (result != true)
{
Serial.println("BLYNK Connection Fail");
wifiManager.resetSettings();
ESP.restart();
delay (5000);
}
else
{
Serial.println("BLYNK Connected");
}
digitalWrite(RedLed, LOW);
bool status;
status = bme.begin(0x76);
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
delay(100);
}
/*---------------------------------------------------------
END VOID SETUP
--------------------------------------------------------*/
BLYNK_WRITE(V1) { /// uitlezen van App knoppen
light = param.asInt();
//Serial.println(light);
if (light == 1) {
digitalWrite(Warmwhite, HIGH);
//digitalWrite(Coolwhite, HIGH);
}
if (light == 0) {
digitalWrite(Warmwhite, LOW);
//digitalWrite(Coolwhite, LOW);
}
}
BLYNK_WRITE(V6) { /// uitlezen van App knoppen
warmwitdimmer = param.asInt();
// Serial.println(warmwitdimmer);
}
//***************************************************************
void loop()
{
//esp_wifi_restore(); //erases store credentially
//delay(1000);
//SPIFFS.format(); //erases stored values
//ESP.restart();
//Serial.println(e);
//int elapsedtime =
Blynk.run();
interal_led();
timer.run();
time1();
Blynk.virtualWrite(V10, time1());
ledcWrite(ledChannel, warmwitdimmer);
relaisStatus = digitalRead(Warmwhite);
schakelaarStatus = digitalRead(schakelaar);
if (schakelaarStatus != laststate)
{
//Serial.println("IN DE IF");
laststate = schakelaarStatus;
if (relaisStatus == LOW) {
digitalWrite(Warmwhite, HIGH);
Blynk.virtualWrite(V1, 1);
delay(50);
}
else if (relaisStatus == HIGH) {
digitalWrite(Warmwhite, LOW);
Blynk.virtualWrite(V1, 0);
delay(50);
}
}
}
void myTimerEvent() {
int co2ppm = mhz19.getPPM();
Blynk.virtualWrite(V2, co2ppm);
Blynk.virtualWrite(V3, bme.readTemperature());
Blynk.virtualWrite(V4, (bme.readPressure() / 100.0F));
Blynk.virtualWrite(V5, bme.readHumidity());
//Serial.print("co2: "); Serial.println(co2ppm);
//Serial.print("temp: "); Serial.println(temp);
//Serial.print(LampName);
}
void time1() {
time_t long e = now() - start;
int days = elapsedDays(e);
int hours = numberOfHours(e);
int minutes = numberOfMinutes(e);
int seconds = numberOfSeconds(e);
// digital clock display of current time
Serial.print(days, DEC);
printDigits(hours);
printDigits(minutes);
printDigits(seconds);
Serial.println();
}
void printDigits(byte digits) {
// utility function for digital clock display: prints colon and leading 0
Serial.print(":");
if (digits < 10)
Serial.print('0');
Serial.print(digits, DEC);
}
void interal_led () {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
digitalWrite(GreenLed, ledState);
}
}