Hello, I’m still a beginner using blynk 2.0. The program I made uses sensors on digital (8) and analog (A0) pins. The microcontrollers used, namely Arduino Mega and ESP01, can connect properly to the Blynk server and display serial monitor reading results, but for virtual writes on the Blynk server, the pin 8 and A0 sensor values are not displayed.
//Blynk
#define BLYNK_TEMPLATE_ID "TMPL6Zb0gxcon"
#define BLYNK_TEMPLATE_NAME "Tutor"
#define BLYNK_AUTH_TOKEN "mbHeYIcumpvCw_cBempiw92P2s5Aze-y"
#define BLYNK_PRINT Serial
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "";
char pass[] = "";
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#define EspSerial Serial1
#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);
BlynkTimer timer;
//
#define TdsSensorPin A0
#define VREF 5.0 // analog reference voltage(Volt) of the ADC
#define SCOUNT 30 // sum of sample point
int analogBuffer[SCOUNT]; // store the analog value in the array, read from ADC
int analogBufferTemp[SCOUNT];
int analogBufferIndex = 0,copyIndex = 0;
float averageVoltage = 0,tdsValue = 0,temperature = 25;
//
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 8
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
int tempC;
int tempF;
void setup() {
Serial.begin(9600);
sensors.begin();
pinMode(TdsSensorPin,INPUT);
EspSerial.begin(ESP8266_BAUD);
delay(10);
Blynk.begin(auth, wifi, ssid, pass);
}
void loop() {
// put your main code here, to run repeatedly:
Blynk.run();
sensorTDS();
sensor_suhu();
timer.run(); // Initiates BlynkTimer
}
void sendSensor(){
Blynk.virtualWrite(V0,tempC);
Blynk.virtualWrite(V1,tempF);
Blynk.virtualWrite(V2,tdsValue);
delay(500);
}
void sensor_suhu(){
sensors.requestTemperatures();
Serial.print("Celsius temperature: ");
//We can connect more than one IC on the same data wire. 0 refers to the first IC on the wire
Serial.print(sensors.getTempCByIndex(0));
Serial.print(" - Fahrenheit temperature: ");
Serial.println(sensors.getTempFByIndex(0));
tempC=sensors.getTempCByIndex(0);
tempF=sensors.getTempFByIndex(0);
//lcd.setCursor(0,0);
//lcd.print(tempC);
delay(1000);
}
void sensorTDS(){
static unsigned long analogSampleTimepoint = millis();
if(millis()-analogSampleTimepoint > 40U) //every 40 milliseconds,read the analog value from the ADC
{
analogSampleTimepoint = millis();
analogBuffer[analogBufferIndex] = analogRead(TdsSensorPin); //read the analog value and store into the buffer
analogBufferIndex++;
if(analogBufferIndex == SCOUNT)
analogBufferIndex = 0;
}
static unsigned long printTimepoint = millis();
if(millis()-printTimepoint > 800U)
{
printTimepoint = millis();
for(copyIndex=0;copyIndex<SCOUNT;copyIndex++)
analogBufferTemp[copyIndex]= analogBuffer[copyIndex];
averageVoltage = getMedianNum(analogBufferTemp,SCOUNT) * (float)VREF / 1024.0; // read the analog value more stable by the median filtering algorithm, and convert to voltage value
float compensationCoefficient=1.0+0.02*(temperature-25.0); //temperature compensation formula: fFinalResult(25^C) = fFinalResult(current)/(1.0+0.02*(fTP-25.0));
float compensationVolatge=averageVoltage/compensationCoefficient; //temperature compensation
tdsValue=(133.42*compensationVolatge*compensationVolatge*compensationVolatge - 255.86*compensationVolatge*compensationVolatge + 857.39*compensationVolatge)*0.5; //convert voltage value to tds value
//Serial.print("voltage:");
//Serial.print(averageVoltage,2);
//Serial.print("V ");
Serial.print("TDS Value:");
Serial.print(tdsValue,0);
Serial.println("ppm");
}
}
int getMedianNum(int bArray[], int iFilterLen)
{
int bTab[iFilterLen];
for (byte i = 0; i<iFilterLen; i++)
bTab[i] = bArray[i];
int i, j, bTemp;
for (j = 0; j < iFilterLen - 1; j++)
{
for (i = 0; i < iFilterLen - j - 1; i++)
{
if (bTab[i] > bTab[i + 1])
{
bTemp = bTab[i];
bTab[i] = bTab[i + 1];
bTab[i + 1] = bTemp;
}
}
}
if ((iFilterLen & 1) > 0)
bTemp = bTab[(iFilterLen - 1) / 2];
else
bTemp = (bTab[iFilterLen / 2] + bTab[iFilterLen / 2 - 1]) / 2;
return bTemp;
}