void serialEvent1() { // Check for data on 2nd serial port
while (Serial1.available()) {
char inChar = (char)Serial1.read();
inputString += inChar;
if (inChar == '\n') {
Blynk.virtualWrite (V1, inputString);
inputString = "";
}
}
}
BLYNK_LOG("Gunner has Ser>Term too ;) "); // Called every second via timer for RTC display
hi - would you be able to show full sketch of this working. I tried below sketch with no luck.
You mentioned “shorten rx/tx pin” - do you mean physically connecting them together ?
I can validate that the sketch connects to blynk server and outputs to serial monitor on the PC however the “Serial.Avaliable” is always false (which I also validated).
I can’t seem to work out what I may be doing wrong
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <ArduinoOTA.h>
// Pins for WEMOS D1 mini and presume D1 R2
#define D0 16
#define D1 5
#define D2 4
#define D3 0
#define D4 2
#define D5 14
#define D6 12
#define D7 13
#define D8 15
#define RX 3
#define TX 1
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
// char auth_local_server[] = "xxx"; //
char auth_local_server[] = "xxx"; //
char auth_cloud_server[] = "xxx";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxx";
char pass[] = "xxx";
SimpleTimer timer;
// Attach virtual serial terminal to Virtual Pin V8
WidgetTerminal terminal(V8);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("\n Starting");
WiFiManager wifiManager;
Blynk.config(auth_local_server, "192.168.0.251", 8442);
Blynk.connect(3333); // timeout set to 10 seconds and then continue without Blynk
timer.setInterval (1000L, heartbeat);
timer.setInterval (500L, Sent_serial);
}
void Sent_serial() {
// Sent serial data to Blynk terminal - Unlimited string readed
String content = ""; //null string constant ( an empty string )
char character;
while(Serial.available()) {
character = Serial.read();
content.concat(character);
}
if (content != "") {
Blynk.virtualWrite (V8, content);
}
Serial.println ("in serial");
}
int n = 0;
void heartbeat()
{
Serial.print("Still going....");
Serial.println (n);
n++;
Blynk.virtualWrite(V0, n);
}
void loop() {
if (Blynk.connected()) {
Blynk.run(); }
timer.run();
}
Yes, or as in my case a TTL-USB adapter (so I can compare what I see on the widget terminal with a terminal program on the PC. (I did test with just a jumper on the TX/RX pins, and that also works).
I was actualy working on an earlier post about getting the Blynk debug info onto the widget terminal… just never got around to finishing it off before posting my solution here
here is my code. its a work in progress as iits used for prototyping.
there are a few extra items in it; specifically the ability to switch from my local blynk server to the public cloud blynk server and back again.
// This code uses wifi manageer to autoselect a pre-saved Access point or use captive portal
// it also allows you to swtich between local and cloud server's
// todo:
// - try auto switchover based on connection attempts
//
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
//needed for library
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <ArduinoOTA.h>
#define vPIN_localORcloud_server 127
// Pins for WEMOS D1 mini and presume D1 R2
#define D0 16
#define D1 5
#define D2 4
#define D3 0
#define D4 2
#define D5 14
#define D6 12
#define D7 13
#define D8 15
#define RX 3
#define TX 1
// select wich pin will trigger the configuraton portal when set to LOW
#define TRIGGER_PIN D4
char source_filename[] = "Filename: wifimanager_with_local_cloud_server_example_ondemand_mdv1";
char source_version[] = "V1 03Feb2017 16:48";
char auth_local_server[] = "xxx"; // on local server
char auth_cloud_server[] = "xxx"; // blynk public cloud server
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxx";
char pass[] = "xxx";
SimpleTimer timer;
// Attach virtual serial terminal to Virtual Pin V8
WidgetTerminal terminal(V8);
// flags used to switch between cloud and local Blynk server
bool cloud_server_active = false;
bool local_server_active = true;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("\n Starting");
pinMode(TRIGGER_PIN, INPUT);
int waittime = millis() + 10000;
bool x = true;
WiFiManager wifiManager;
if (!wifiManager.autoConnect("AutoConnectAP")) { // might not need if statement
Serial.println("failed to connect, we should reset as see if it connects");
delay(3000); }
/*
WiFi.begin(ssid, pass); // use this code if we want to hardwire an access point
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
if (waittime < millis()) { break; }
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
*/
//
Blynk.config(auth_local_server, "192.168.0.251", 8442);
Blynk.connect(3333); // timeout set to 10 seconds and then continue without Blynk - thank Costas :-)
timer.setInterval (1000L, heartbeat); // just for my own debugging purposes
timer.setInterval (500L, Sent_serial); // check to see if anything to send from hardware serial to Blynk terminal
Serial.println (source_filename);
Serial.println (source_version);
Blynk.virtualWrite (V1, "YES");
Blynk.syncVirtual (V1);
timer.setInterval(12000L, reconnectBlynk); // check to see if we are still connected to Blynk
}
void Sent_serial() {
// Sent serial data to Blynk terminal - Unlimited string readed
String content = ""; //null string constant ( an empty string )
char character;
while(Serial.available()) {
character = Serial.read();
content.concat(character);
// Serial.println ("in serial loop");
}
if (content != "") {
Blynk.virtualWrite (V8, content); // send serial to blynk terminal
}
}
int n = 0;
void heartbeat()
{
if (Blynk.connected()){
Serial.print("BlynkConnected & still going....");
Serial.println (n);
n++;
Blynk.virtualWrite(V0, n);
}
}
BLYNK_WRITE(vPIN_localORcloud_server) // V127 button to select weather we use local blynk server or cloud blynk server
{
int i = param.asInt();
if (cloud_server_active)
{
Blynk.virtualWrite (V1, "NO");
Blynk.syncVirtual (V1);
Serial.println("Switching to local blynk server") ;
Serial.println("Disconnecting from cloud blynk server and then re-connecting local blynk server...") ;
Blynk.disconnect();
Blynk.config(auth_local_server, "192.168.0.251", 8442);
Blynk.connect();
cloud_server_active = false;
local_server_active = true;
Blynk.virtualWrite (V1, "YES");
Blynk.syncVirtual (V1);
} else
//(local_server_active must be active)
{
Blynk.virtualWrite (V1, "NO");
Blynk.syncVirtual (V1);
Serial.println("Switching to cloud blynk server") ;
Serial.println("Disconnecting from local blynk server and then re-connecting to cloud blynk server...") ;
Blynk.disconnect();
Blynk.config(auth_cloud_server);
Blynk.connect();
cloud_server_active = true;
local_server_active = false;
Blynk.virtualWrite (V1, "YES");
Blynk.syncVirtual (V1);
}
}
//****************************************************************
//====== RUN THIS CODE ONCE WHEN BLYNK IS FIRST CONNECTED ========
bool isFirstConnect = true;
BLYNK_CONNECTED() {
if (isFirstConnect) {
Blynk.virtualWrite (V1, "YES");
isFirstConnect = false;
}
}
//===== RE CONNECT IF BLYNK NOT CONNECTED =====
void reconnectBlynk() {
if (!Blynk.connected()) {
Serial.println ("Blyn.connected is FALSE"); // for debugging
if(Blynk.connect(3333)) {
bool isFirstConnect = true; // assume this is first time we connected to Blynk again :-) i.e. ensure BLYNK_CONNECTED()to executed again, now that we re-connected
} else {
// nothing to do
}
}
}
void loop() {
// is configuration portal requested?
if (digitalRead(TRIGGER_PIN) == LOW ) { // if PIN D4 is set to GND it will initiate WifiManager Captive portal - but you need to select "OnDemandAP" from your smartphone wifi settings
//WiFiManager
//Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
//reset settings - for testing
wifiManager.resetSettings(); // needed to clear previous wifi settings help in non volatile ram
//sets timeout until configuration portal gets turned off
//useful to make it all retry or go to sleep
//in seconds
//wifiManager.setTimeout(120);
//it starts an access point with the specified name
//here "AutoConnectAP"
//and goes into a blocking loop awaiting configuration
//WITHOUT THIS THE AP DOES NOT SEEM TO WORK PROPERLY WITH SDK 1.5 , update to at least 1.5.1
//WiFi.mode(WIFI_STA);
if (!wifiManager.startConfigPortal("OnDemandAP")) {
Serial.println("failed to connect and hit timeout");
delay(3000);
//reset and try again, or maybe put it to deep sleep
ESP.reset();
delay(5000);
}
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
}
if (Blynk.connected()) {
Blynk.run(); }
timer.run();
// put your main code here, to run repeatedly:
}