The problem is that when in my sketch I include Blynk.virtualWrite, the program hangs at Blynk.begin(). If I comment out all the writes or the whole function that sends data to blynk the program works.
For Software, I’m using Arduino IDE 1.8.9. with AVR boards 1.6.21 and Blynk 0.6.1.
Hardware is Nano with ESP8266 as shield via software serial.
Serial output without any writes:
13:46:01.859 -> Starting in debug mode
13:46:01.859 -> ----------------------------
13:46:01.892 ->
13:46:02.268 -> 1
13:46:02.268 -> 2
13:46:02.334 -> 3
13:46:02.334 -> [462]
13:46:02.334 -> ___ __ __
13:46:02.368 -> / _ )/ /_ _____ / /__
13:46:02.368 -> / _ / / // / _ \/ '_/
13:46:02.403 -> /____/_/\_, /_//_/_/\_\
13:46:02.437 -> /___/ v0.6.1 on Arduino Nano
13:46:02.471 ->
13:46:02.946 -> [1057] Connecting to SYRION13417
13:46:06.113 -> [4231] AT version:0.51.0.0(Nov 27 2015 13:37:21)
13:46:06.147 -> SDK version:1.5.0
13:46:06.181 -> compile time:Nov 27 2015 13:57:56
13:46:06.181 -> OK
13:46:11.376 -> [9511] +CIFSR:STAIP,"192.168.254.107"
13:46:11.409 -> +CIFSR:STAMAC,"cc:50:e3:55:65:e2"
13:46:11.443 -> [9522] Connected to WiFi
13:46:21.739 -> [19865] Ready (ping: 25ms).
13:46:22.214 -> 4
13:46:22.283 -> bmeInside data:
13:46:22.283 -> t °C: h %: p hPa:
13:46:22.317 -> 28.86 48.32 981.33
13:46:22.317 -> state of charge: 0%
13:46:22.351 -> adc1 data:
13:46:22.351 -> BatV:20.65V
And with writes:
14:39:16.022 -> Starting in debug mode
14:39:16.022 -> ----------------------------
14:39:16.056 ->
14:39:16.397 -> 1
14:39:16.397 -> 2
14:39:16.499 -> 3
14:39:16.499 -> [462]
14:39:16.499 -> ___ __ __
14:39:16.533 -> / _ )/ /_ _____ / /__
14:39:16.533 -> / _ / / // / _ \/ '_/
14:39:16.567 -> /____/_/\_, /_//_/_/\_\
14:39:16.601 -> /___/ v0.6.1 on Arduino Nano
14:39:16.635 ->
14:39:17.078 -> [1057] Connecting to SYRION13417
14:39:20.243 -> [4230] AT version:0.51.0.0(Nov 27 2015 13:37:21)
14:39:20.277 -> SDK version:1.5.0
14:39:20.311 -> compile time:Nov 27 2015 13:57:56
14:39:20.345 -> OK
14:39:25.550 -> [9510] +CIFSR:STAIP,"192.168.254.107"
14:39:25.550 -> +CIFSR:STAMAC,"cc:50:e3:55:65:e2"
14:39:25.585 -> [9521] Connected to WiFi
14:39:35.898 -> [19868] Ready (ping: 25ms).
The sketch is all over the place with syntax and comments.
#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <SoftwareSerial.h>
#include <Adafruit_ADS1015.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#define DEBUGMODE 1
#define SERIALDATRATE 9600
#define ESP8266_BAUD 9600
Adafruit_ADS1115 adc1;
Adafruit_BME280 bmeInside;
SoftwareSerial EspSerial(3, 2); // RX, TX
ESP8266 wifi(&EspSerial);
char auth[] = "";
char ssid[] = "";
char pass[] = "";
typedef const uint8_t pin;
struct adc0 {
pin NTC = 0;
pin MotorI = 1;
pin AcIn = 2;
pin AcOut = 3;
} pinAdc0;
struct adc1 {
pin PvV = 0;
pin PvI = 1;
pin BatV = 2;
pin BatI = 3;
} pinAdc1;
struct blynkPins {
pin graphWattIn = V20;
pin graphWattOut = V21;
pin graphKWhSolar = V22;
pin graphKWhAC = V23;
pin graphBatCharge = V24;
pin graphInTemperature = V25;
pin graphBatVolt = V26;
pin graphInPressure = V27;
pin LCD0 = V5;
pin LCD1 = V6;
pin menuSwitch = V10;
pin terminal = V0;
} pinBlynk;
// how often to update the data in ms
struct updateRateStruct {
const uint16_t sensor = 1 * 1000;
const uint16_t blynk = 1 * 60000;
const uint32_t thing = 10 * 60000;
} updateRate;
struct sensorData {
float inTemperature;
float inHumidity;
float inPressure;
float pvVolt;
float batVolt;
int8_t batCharge;
} sensor;
// 1 000 000, 110 000
const float PvVoltageDividerRatio = 10.0919;
// 1 000 000, 147 000
const float BatVoltageDividerRatio = 8.1966;
#define ADC1REFVOLT 5.0
const float adc1BitPerVolt = 0.125;
adsGain_t adc1Gain = GAIN_ONE;
// voltage when battery is considered empty/full
const float batteryChargeVoltage[2] = {22.0, 27.15};
// -------------------------------------------------------------------
void sendBlynk(void);
void updateSensors(void);
void pollBmeInside(void);
void pollAdc1(void);
float calculateVoltage(int16_t adcOutput, float bitPerVolt, float voltageDividerRatio);
int8_t calculateBatteryCharge (float voltage);
uint8_t menuSwitch = 0;
uint32_t currentMillis = 0;
void setup() {
#if DEBUGMODE
Serial.begin(SERIALDATRATE);
Serial.println("Starting in debug mode");
Serial.println("----------------------------\n");
#endif
adc1.setGain(adc1Gain);
adc1.begin();
bmeInside.begin(0x76);
Serial.println("1");
EspSerial.begin(ESP8266_BAUD);
Serial.println("2");
delay(50);
Serial.println("3");
Blynk.begin(auth, wifi, ssid, pass);
Serial.println("4");
}
void loop() {
currentMillis = millis();
Blynk.run();
updateSensors();
sendBlynk();
}
void updateSensors(){
static uint32_t previousMillis = 0;
if ((currentMillis - previousMillis) >= updateRate.sensor){
previousMillis = currentMillis;
pollBmeInside();
pollAdc1();
}
}
void pollBmeInside(){
sensor.inTemperature = bmeInside.readTemperature();
sensor.inHumidity = bmeInside.readHumidity();
sensor.inPressure = (bmeInside.readPressure() / 100.0F);
#if DEBUGMODE
Serial.println("bmeInside data:");
Serial.println("t °C:\th %:\tp hPa:");
Serial.print(sensor.inTemperature);
Serial.print("\t");
Serial.print(sensor.inHumidity);
Serial.print("\t");
Serial.println(sensor.inPressure);
#endif
}
void pollAdc1 (){
int16_t adc1BatV = 0;
adc1BatV = adc1.readADC_SingleEnded(pinAdc1.BatV);
sensor.batVolt = calculateVoltage(adc1BatV, adc1BitPerVolt, BatVoltageDividerRatio);
sensor.batCharge = calculateBatteryCharge(sensor.batVolt);
#if DEBUGMODE
Serial.println("adc1 data:");
Serial.print("BatV:");
Serial.print(sensor.batVolt);
Serial.println("V");
#endif
}
float calculateVoltage( int16_t adcOutput,
float bitPerVolt,
float voltageDividerRatio){
return (( (float) adcOutput * bitPerVolt) / 1000.0) * voltageDividerRatio;
}
int8_t calculateBatteryCharge (float voltage){
int8_t val = (voltage - batteryChargeVoltage[0]) * 100 /
(batteryChargeVoltage[1] - batteryChargeVoltage[0]);
val = constrain(val, 0, 100);
#if DEBUGMODE
Serial.print("state of charge: ");
Serial.print(val);
Serial.println("%");
#endif
return val;
}
void sendBlynk(){
static uint32_t previousMillis = 0;
if ((currentMillis - previousMillis) >= updateRate.blynk){
previousMillis = currentMillis;
//Blynk.virtualWrite(pinBlynk.graphInTemperature, sensor.inTemperature);
//Blynk.virtualWrite(pinBlynk.graphInHumidity, sensor.inHumidity);
//Blynk.virtualWrite(pinBlynk.graphInPressure, sensor.inPressure);
//Blynk.virtualWrite(V24, sensor.batCharge);
Blynk.virtualWrite(V26, 10);
#if DEBUGMODE
Serial.println("blynk trig\n");
#endif
}
}