Hello GTT
Thanks for the response. I have successfully connected my system consisting of an Arduino Mega 2560 (and using a very similar program on the Arduino Uno) to Blynk via an ESP8266 WiFI module using the code i adapted from the Blynk example sketches:
/*************************************************************
Download latest Blynk library here:
https://github.com/blynkkk/blynk-library/releases/latest
Blynk is a platform with iOS and Android apps to control
Arduino, Raspberry Pi and the likes over the Internet.
You can easily build graphic interfaces for all your
projects by simply dragging and dropping widgets.
Downloads, docs, tutorials: http://www.blynk.cc
Sketch generator: http://examples.blynk.cc
Blynk community: http://community.blynk.cc
Follow us: http://www.fb.com/blynkapp
http://twitter.com/blynk_app
Blynk library is licensed under MIT license
This example code is in public domain.
*************************************************************
WARNING!
It's very tricky to get it working. Please read this article:
http://help.blynk.cc/hardware-and-libraries/arduino/esp8266-with-at-firmware
Youâll need:
- Blynk App (download from AppStore or Google Play)
- Arduino Mega ADK board
- Decide how to connect to Blynk
(USB, Ethernet, Wi-Fi, Bluetooth, ...)
There is a bunch of great example sketches included to show you how to get
started. Think of them as LEGO bricks and combine them as you wish.
For example, take the Ethernet Shield sketch and combine it with the
Servo example, or choose a USB sketch and add a code from SendData
example.
*************************************************************/
/* Comment this out to disable prints and save space */
//#define BLYNK_PRINT Serial
#include <Blynk.h>
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <FastLED.h>
#define LED_PIN 10
#define NUM_LEDS 30
CRGB leds[NUM_LEDS];
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
// Your WiFi credentials.
// Set password to "" for open networks.
//WIFI 1 Credentials
char ssid0[] = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
char pass0[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";
/WIFI 2 Credentials
char ssid1[] = "xxxxxxxxxxxxxxxxxxxxxxxx";
char pass1[] = "xxxxxxxxxxxxxxxxxxxxxxxx";
// 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 9600
ESP8266 wifi(&EspSerial);
void setup()
{
// Debug console
Serial.begin(9600);
// Set ESP8266 baud rate
EspSerial.begin(ESP8266_BAUD);
delay(10);
//String APlist = wifi.getAPList ();
//Serial.print ("AP List:");
//Serial.println (APlist);
Serial.println("Establishing WIFI connection with Blynk...");
//Blynk.begin(auth, wifi, ssid0, pass0); //Wifi credentials 1
Blynk.begin(auth, wifi, ssid1, pass1); //Wifi credentials 2
// You can also specify server:
//Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 80);
//Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8080);
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
Serial.println("LED strip control with preset light settings using Blynk ");
timer1.setInterval(100L, LightSettings);
}
With a manual selection of credentials 1 or 2 everything works great. However i thought to improve the capability of my project by allowing the firmware to connect to whichever of the networks was available (i.e. if Wifi credentials 1 is not available on startup then check if Wifi credentials 2 is available and connect to it). Since i learned that Blynk.begin()
is a blocking function (which ever credentials i used with the first Blynk.begin("credentials1")
it will hung there if that network was not available did not try the next Blynk.begin(credentials2)
).
The attempted to resolve this by first trying to connect to the network using the Wifi.begin()
and once i have a connection then use a Blynk.config()
to establish a connection with blynk as shown in the sketch below:
/* Comment this out to disable prints and save space */
//#define BLYNK_PRINT Serial
#include <Blynk.h>
#include <WiFi.h>
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <FastLED.h>
#define LED_PIN 10
#define NUM_LEDS 30
CRGB leds[NUM_LEDS];
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxxxxxxxxxxxxxxxx";
// Your WiFi credentials.
// Set password to "" for open networks.
//Wifi credentials 1
char ssid0[] = "xxxxxxxxxxxxxxxxxxx";
char pass0[] = "xxxxxxxxxxxxxxxxxx";
//Wifi credentials 2
char ssid1[] = "xxxxxxxxxxxxxx";
char pass1[] = "xxxxxxxxxxxxxx";
char server[] = "blynk-cloud.com"; // URL for Blynk Cloud Server
int port = 8080;
// 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 9600
ESP8266 wifi(&EspSerial);
void setup()
{
// Debug console
Serial.begin(9600);
// Set ESP8266 baud rate
EspSerial.begin(ESP8266_BAUD);
delay(10);
//String APlist = wifi.getAPList ();
//Serial.print ("AP List:");
//Serial.println (APlist);
Serial.println("Establishing WIFI connection with Blynk...");
WiFi.begin(ssid1, pass1); // Non-blocking if no WiFi available
delay(7000);
Blynk.config(auth, server, port);
Serial.println(WiFi.status());
while(WiFi.status()==WL_IDLE_STATUS){
Serial.print(".");
delay(1000);
}
if(WiFi.status()==WL_CONNECTED){
Serial.println("Esp8266 is connected to wifi");
Blynk.begin(auth, wifi, ssid1, pass1);
if (Blynk.connected()){
Serial.println("ESP8266 is connected to Blynk cellphone wifi");
}
}
else if (WiFi.status()==WL_CONNECT_FAILED){
Serial.println("Try the next available wifi");
}
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
Serial.println("LED strip control with preset light settings using Blynk ");
timer1.setInterval(100L, LightSettings);
}
The first challenge however is when i try to compile the sketch in the Arduino IDE, i get an error message:
âno matching function for call to âBlynkWifi::config(char [33], char [16], int&)ââ
I canât seem to figure out why i am getting this message as I have included the blynk library and it stands to reason that this function would be a part of it.
The second challenge that I am trying to resolve is that when i use the Wifi.status()
function it actually does not work as expected which i assume is because i am using the esp8266 wifi module instead of an arduino shield (the result of this function is 255 which is a âWL_NO_SHIELDâ). I came across a few suggestions but after a few attempts I realized these would not work because it seems they work only for an ESP8266 controller (not an arduino paired to an Esp8266 wifi module), which was evident by the fact that the ESP8266WiFi.h library would only be recognized in the arduino IDE when the ESP8266 board was selected.
If someone can suggest a better way in which to achieve the goal-Enable the Arduino to check which my two networks are available and connect to the one detected to be available-i will be extremely grateful.