Hello!
I am trying to get a stable connection between my android phone and the Blynk app. But when I add more void loops, the app reconnects and connects over and over. Is there a code that will keep the wifi connection when more void loops are added? Or is there something I need to do within my loops?
I am using an arduino mega and an ESP8266 wifi module.
I would also like to display my values on an lcd screen, that may be the problem but I am not sure how to fix that.
Thank you!
#define BLYNK_DEBUG
#define BLYNK_PRINT Serial
//#if defined(ESP8266)
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <max6675.h>
int lastConnectionAttempt = millis();
int connectionDelay = 5000; // try to reconnect every 5 seconds
//Motor
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
//thermo
// creating a thermocouple instance with software SPI on any three
// digital IO pins.
#define MAXDO1 48
#define MAXCS1 46
#define MAXCLK1 44
// Initialize the Thermocouple
MAX6675 thermocouple1(MAXCLK1, MAXCS1, MAXDO1);
#if defined(ARDUINO_ARCH_SAMD)
// for Zero, output on USB Serial console, remove line below if using programming port to program the Zero!
#define Serial SerialUSB
#endif
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";
// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial1
// or Software Serial on Uno, Nano...
//#include <SoftwareSerial.h>
//SoftwareSerial EspSerial(2, 3); // RX, TX
// Your ESP8266 baud rate:
#define ESP8266_BAUD 115200
ESP8266 wifi(&EspSerial);
BlynkTimer timer;
// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void myTimerEvent()
{
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V0, millis() / 1000);
}
BLYNK_APP_CONNECTED() {
Serial.println("App Connected.");
}
// This is called when Smartphone App is closed
BLYNK_APP_DISCONNECTED() {
Serial.println("App Disconnected.");
}
void getTemp()
{
double t1 = thermocouple1.readCelsius();
//delay(500);
t1= (((t1*9))/5)+32;
Serial.print("Thermocouple Temp1 = *");
Serial.println(t1);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Temp1 = ");
lcd.print(t1);
lcd.print(" F ");
Blynk.virtualWrite(V10, t1);
}
void setup()
{
Serial.begin(9600);//initialize the serial
lcd.init();
lcd.backlight(); //op
// Set ESP8266 baud rate
EspSerial.begin(ESP8266_BAUD);
Blynk.begin(auth, wifi, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 8442);
//Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8442);
// Setup a function to be called every second
timer.setInterval(1000L, myTimerEvent);
timer.setInterval(6000L, getTemp);
while (!Serial); // will pause Zero, Leonardo, etc until serial console opens
#endif
}
void loop()
{
Blynk.run();
timer.run(); // Initiates BlynkTimer
}