Setup:
Adafruit Feather Huzzah ESP8266
ADS1015
Blynk Lib: 0.6.1
App Ver: IOS 2.26.1(9)
Here is my issue. I’m not new to using blynk or building projects but I’m stuck at this problem. My two BLYNK_WRITE functions will not trigger. When you move the slider or press the button in the app nothing happens. The serial debug info shows nothing. It doesn’t get the command/trigger/whatever you wanna call it. Interesting thing is when I hard reset my board the slider in the app always jumps back to 3.25. I’ve been staring at this code for over a day and at a loss. I’m using the Arduino IDE to upload (tried both OTA and serial) and I have tried reinstalling the Blynk library and refreshing my auth token. No luck. I realize the two while loops add delay to Blynk.run() but have done longer delays in while loops and not had issues.
I even changed the while loops to only 500ms with no change. What am I missing? Thanks
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <BlynkSimpleEsp8266.h>
#include <Wire.h>
const byte alertPin = 13;
const byte redPin = 0;
const byte bluePin = 2;
const int i2caddress = 0x48;
volatile byte readDone = 0;
char auth[] = "tokentokentoken";
char ssid[] = "IoT";
char pass[] = "passwrd";
int transducer1Reading = 0;
int transducer2Reading = 0;
int transducer1Max = 0;
int transducer2Max = 0;
float dataRate = 500;
float transducer1Amps = 0.0;
float transducer2Amps = 0.0;
unsigned long timer1 = 0;
unsigned long timer2 = 0;
unsigned long timer3 = 0;
uint16_t config1 = 0x0002 | // Disable comparator and set ALERT/RDY pin to high-impedance (default)
0x0000 | // non-latching Comparator
0x0000 | // Alert/Rdy active low (default)
0x0000 | // Traditional comparator (default)
0x00A0 | // 2400 samples per second
0x0000 | // cont mode
0x0000 | // +/-6.144V range
0x4000 | // Single-ended AIN0
0x8000; // Set 'start single-conversion' bit
uint16_t config2 = 0x0002 | // Disable comparator and set ALERT/RDY pin to high-impedance (default)
0x0000 | // non-latching Comparator
0x0000 | // Alert/Rdy active low (default)
0x0000 | // Traditional comparator (default)
0x00A0 | // 2400 samples per second
0x0000 | // cont mode
0x0000 | // +/-6.144V range
0x5000 | // Single-ended AIN1
0x8000; // Set 'start single-conversion' bit
void setup()
{
pinMode(alertPin, INPUT);
pinMode(redPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Wire.begin();
Wire.setClock(400000);
Blynk.begin(auth, ssid, pass);
wifi_station_set_hostname("ESP-Whole House Monitor");
ArduinoOTA.setHostname("ESP-Whole House Monitor");
ArduinoOTA.begin();
Wire.beginTransmission(i2caddress);
byte error = Wire.endTransmission();
if (error != 0)
{
digitalWrite(redPin, HIGH);
}
attachInterrupt(digitalPinToInterrupt(alertPin), conversionDone, FALLING);
}
void loop()
{
Blynk.run();
ArduinoOTA.handle();
if (millis() - timer2 > 10000)
{
sendRSSI();
timer2 = millis();
}
if (millis() - timer3 > dataRate)
{
getCurrent();
timer3 = millis();
}
}
/////////////////////////////////////////
void getCurrent()
{
digitalWrite(bluePin, LOW);
transducer1Max = 0;
write_register_ADC(i2caddress, config1);
delay(5);
timer1 = millis();
while (millis() - timer1 < 1000)
{
while (readDone)
{
delayMicroseconds(1);
}
readDone = 0;
transducer1Reading = (read_ADC(i2caddress, 0x00) >> 4);
if (transducer1Reading > transducer1Max)
{
transducer1Max = transducer1Reading;
if (transducer1Reading == 0)
{
transducer1Max = 0;
}
}
}
transducer2Max = 0;
write_register_ADC(i2caddress, config2);
delay(5);
timer1 = millis();
while (millis() - timer1 < 1000)
{
while (readDone)
{
delayMicroseconds(1);
}
readDone = 0;
transducer2Reading = (read_ADC(i2caddress, 0x00) >> 4);
if (transducer2Reading > transducer2Max)
{
transducer2Max = transducer2Reading;
if (transducer1Reading == 0)
{
transducer1Max = 0;
}
}
}
transducer1Amps = (((float)transducer1Max / 2048) * 6.144) / .05;
transducer2Amps = (((float)transducer2Max / 2048) * 6.144) / .05;
float totalAmps = transducer1Amps + transducer2Amps;
Blynk.virtualWrite(V0, transducer1Amps);
Blynk.virtualWrite(V1, transducer1Reading);
Blynk.virtualWrite(V2, transducer1Max);
Blynk.virtualWrite(V4, transducer2Amps);
Blynk.virtualWrite(V5, totalAmps);
digitalWrite(bluePin, HIGH);
}
/////////////////////////////////////////
ICACHE_RAM_ATTR void conversionDone()
{
readDone = 1;
}
/////////////////////////////////////////
void write_register_ADC(uint8_t address, uint16_t regis)
{
Wire.beginTransmission(address);
Wire.write(0x01);
Wire.write((uint8_t)(regis >> 8));
Wire.write((uint8_t)(regis & 0xFF));
Wire.endTransmission();
}
/////////////////////////////////////////
uint16_t read_ADC(uint8_t address, uint8_t reg1)
{
Wire.beginTransmission(address);
Wire.write(reg1);
Wire.endTransmission();
Wire.requestFrom(address, (uint8_t)2);
return ((Wire.read() << 8) | Wire.read());
}
/////////////////////////////////////////
void sendRSSI()
{
int cumRssi = 0;
int rssi = 0;
for (int i = 0; i < 3; i++)
{
rssi = wifi_station_get_rssi();
cumRssi += rssi;
delay(5);
}
rssi = cumRssi / 3;
Blynk.virtualWrite(V3, rssi);
}
/////////////////////////////////////////
BLYNK_CONNECTED()
{
Blynk.syncAll();
for (int i = 0; i < 5; i++)
{
digitalWrite(bluePin, HIGH);
delay(50);
digitalWrite(bluePin, LOW);
delay(50);
}
}
/////////////////////////////////////////
BLYNK_WRITE(V10)
{
int restartChip = param.asInt();
if (restartChip == 1)
{
ESP.restart();
}
}
/////////////////////////////////////////
BLYNK_WRITE(V11)
{
dataRate = param.asFloat();
dataRate *= 1000;
}