Made fix correction blynk.h/blynk.c 16.02.2019 to match BLYNK protocol

0.6.0
master
maxxir 7 years ago
parent b6a3e05684
commit 46e1104869

@ -44,6 +44,8 @@ static uint16_t ATOI(uint8_t * str, uint8_t base);
static uint8_t C2D(uint8_t c);
static void replacetonull(uint8_t * str, uint8_t c);
static void printBanner();
uint8_t * authkey;
uint32_t lastActivityIn;
uint32_t lastActivityOut;
@ -120,7 +122,9 @@ void blynk_run(void)
#ifdef BLYNK_DEBUG
PRINTF("Blynk[%d] : Auth connection complete\r\n", s);
#endif
printBanner();
}
}
if(blynk_connection_available > 0) processInput();
@ -230,7 +234,8 @@ uint8_t blynk_connect(void)
//////////////////////////////////////////////////////////////////////////////////
if (BLYNK_CMD_RESPONSE != hdr.type ||
id != hdr.msg_id ||
(BLYNK_SUCCESS != hdr.length && BLYNK_ALREADY_LOGGED_IN != hdr.length))
//(BLYNK_SUCCESS != hdr.length && BLYNK_ALREADY_LOGGED_IN != hdr.length)) Deprecated on BLYNK_VERSION 0.6.0
(BLYNK_SUCCESS != hdr.length && BLYNK_ILLEGAL_COMMAND_BODY != hdr.length))
{
if (BLYNK_TIMEOUT == hdr.length)
{
@ -294,7 +299,7 @@ void processInput(void)
{
case BLYNK_CMD_RESPONSE:
{
if (BLYNK_NO_LOGIN == hdr.length)
if (BLYNK_NOT_AUTHENTICATED == hdr.length)
{
disconnect(s);
return;
@ -339,7 +344,17 @@ void processInput(void)
processCmd(msgbuf, hdr.length);
currentMsgId = 0;
} break;
//!! On BLYNK_VERSION 0.6.0 also present (look BlynkProtocol.h bool BlynkProtocol<Transp>::processInput(void)):
/*
*
case BLYNK_CMD_LOGIN: {..
case BLYNK_CMD_REDIRECT: {..
case BLYNK_CMD_INTERNAL: {..
case BLYNK_CMD_DEBUG_PRINT: {..
*/
default:
PRINTF("Invalid header type: %d\r\n", hdr.type);
disconnect(s);
@ -411,6 +426,11 @@ void processCmd(uint8_t * buff, size_t len)
#ifdef BLYNK_INFO_CONNECTION
BLYNK_PARAM_KV("con" , BLYNK_INFO_CONNECTION)
#endif
#ifdef BOARD_FIRMWARE_VERSION
BLYNK_PARAM_KV("fw" , BOARD_FIRMWARE_VERSION)
#endif
BLYNK_PARAM_KV("build" , __DATE__ " " __TIME__)
"\0"
;
const size_t profile_len = sizeof(profile)-1;
@ -785,6 +805,19 @@ void blynk_push_pin(uint8_t pin)
}
/*
* Sends integer value to a Virtual Pin
*/
void blynk_push_virtual_pin(uint8_t pin)
{
uint8_t rsp_mem[16];
uint16_t rsp_len;
memset(rsp_mem, 0, sizeof(rsp_mem));
rsp_len = SPRINTF((char *)rsp_mem, "vw %d %d", pin, virtualRead(pin));
replacetonull(rsp_mem, ' ');
sendCmd(BLYNK_CMD_HARDWARE, 0, rsp_mem, rsp_len, NULL, 0);
}
/*
* Sends buffer (string message for example) to a Virtual Pin
*/
void blynk_push_virtual_pin_msg(uint8_t pin, uint8_t * data)
@ -797,4 +830,22 @@ void blynk_push_virtual_pin_msg(uint8_t pin, uint8_t * data)
sendCmd(BLYNK_CMD_HARDWARE, 0, rsp_mem, rsp_len+1, data, strlen(data));
}
void BLYNK_LOG_TIME() {
PRINTF(BLYNK_NEWLINE "[%lu] ", millis());
}
#define BLYNK_LOG1(p1) { BLYNK_LOG_TIME(); PRINTF(p1); }
static void printBanner() {
#if defined(BLYNK_NO_FANCY_LOGO)
BLYNK_LOG1(BLYNK_F("Blynk v" BLYNK_VERSION " on " BLYNK_INFO_DEVICE));
#else
BLYNK_LOG1(BLYNK_NEWLINE
" ___ __ __" BLYNK_NEWLINE
" / _ )/ /_ _____ / /__" BLYNK_NEWLINE
" / _ / / // / _ \\/ '_/" BLYNK_NEWLINE
" /____/_/\\_, /_//_/_/\\_\\" BLYNK_NEWLINE
" /___/ v" BLYNK_VERSION " on " BLYNK_INFO_DEVICE BLYNK_NEWLINE
);
#endif
}

@ -1,51 +1,65 @@
#ifndef _WIZNET_BLYNK_H_
#define _WIZNET_BLYNK_H_
#define ARDUINO
//#define WIZNET_W5500_EVB
//#define WIZNET_WIZ550WEB
#if defined(WIZNET_W5500_EVB)
#define WIZNET_DEVICE WIZNET_W5500_EVB
#elif defined (WIZNET_WIZ550WEB)
#define WIZNET_DEVICE WIZNET_WIZ550WEB
#else
#define WIZNET_DEVICE ARDUINO
#endif
// Change these settings to match your need
#define BLYNK_DEFAULT_DOMAIN "blynk-cloud.com"
#define BLYNK_DEFAULT_PORT 80
#define BLYNK_MAX_READBYTES 255
//SSL shouldn't used here
//#define BLYNK_DEFAULT_PORT_SSL 8441
//***************Professional settings: BEGIN
// Library version.
#define BLYNK_VERSION "0.6.0"
// Professional settings
#define BLYNK_VERSION "0.2.1"
// Heartbeat period in seconds.
#define BLYNK_HEARTBEAT 10
#define BLYNK_TIMEOUT_MS 1500
//#define BLYNK_MSG_LIMIT 20
// Network timeout in milliseconds.
#define BLYNK_TIMEOUT_MS 3000UL
// Limit the amount of outgoing commands per second.
//#define BLYNK_MSG_LIMIT 15
// Limit the incoming command length.
#define BLYNK_MAX_READBYTES 255
// Limit the outgoing command length.
#define BLYNK_MAX_SENDBYTES 128
//***************Professional settings: END
//Print out BLYNK debug messages
#define BLYNK_DEBUG
//Print out cool BLYNK ASCII LOGO
//#define BLYNK_NO_FANCY_LOGO
#undef BLYNK_NO_FANCY_LOGO
#ifndef BLYNK_INFO_DEVICE
#define BLYNK_INFO_DEVICE "Arduino"
//#define BLYNK_INFO_DEVICE "Arduino"
//#define BLYNK_INFO_DEVICE "WIZWiki"
#endif
#ifndef BLYNK_INFO_CPU
/*
#if defined (__AVR_ATmega644P__)
#define BLYNK_INFO_CPU "ATmega644"
#elif defined (__AVR_ATmega1284P__)
#define BLYNK_INFO_CPU "ATmega1284"
#else
#define BLYNK_INFO_CPU "ATmega2560"
//#define BLYNK_INFO_CPU "ST103FRB"
#define BLYNK_INFO_DEVICE "m644p_W5500"
#elif defined (__AVR_ATmega1284P__)
#define BLYNK_INFO_DEVICE "m1284p_W5500"
#else
#define BLYNK_INFO_DEVICE "Custom platform"
#endif
#endif
*/
#define BLYNK_INFO_CPU "ATmega1284"
#ifndef BLYNK_INFO_CPU
//#define BLYNK_INFO_CPU "ATmega1284"
//#define BLYNK_INFO_CPU "ATmega2560"
//#define BLYNK_INFO_CPU "ATmega328P"
#if defined (__AVR_ATmega644P__)
#define BLYNK_INFO_CPU "ATmega644P"
#elif defined (__AVR_ATmega1284P__)
#define BLYNK_INFO_CPU "ATmega1284P"
#else
#define BLYNK_INFO_CPU "ATmega2560"
#endif
#endif
#ifndef BLYNK_INFO_CONNECTION
@ -66,37 +80,70 @@
//#ifndef BlynkProtocolDefs_h
//#define BlynkProtocolDefs_h
//BlynkCmd values compatible && synced with BLYNK_VERSION 0.6.0
enum BlynkCmd
{
BLYNK_CMD_RESPONSE = 0,
BLYNK_CMD_REGISTER = 1,
BLYNK_CMD_LOGIN = 2,
BLYNK_CMD_SAVE_PROF = 3,
BLYNK_CMD_LOAD_PROF = 4,
BLYNK_CMD_GET_TOKEN = 5,
//BLYNK_CMD_LOGIN = 2, Deprecated on BLYNK_VERSION 0.6.0
BLYNK_CMD_LOGIN = 29,
BLYNK_CMD_PING = 6,
BLYNK_CMD_TWEET = 12,
BLYNK_CMD_EMAIL = 13,
BLYNK_CMD_PUSH_NOTIFICATION = 14,
//BLYNK_CMD_PUSH_NOTIFICATION = 14, Deprecated on BLYNK_VERSION 0.6.0
BLYNK_CMD_NOTIFY = 14,
BLYNK_CMD_BRIDGE = 15,
BLYNK_CMD_HARDWARE_SYNC = 16,
BLYNK_CMD_HARDWARE = 20
BLYNK_CMD_INTERNAL = 17,
BLYNK_CMD_SMS = 18,
BLYNK_CMD_PROPERTY = 19,
BLYNK_CMD_HARDWARE = 20,
//BLYNK_CMD_SAVE_PROF = 3, Deprecated on BLYNK_VERSION 0.6.0
//BLYNK_CMD_LOAD_PROF = 4, Deprecated on BLYNK_VERSION 0.6.0
//BLYNK_CMD_GET_TOKEN = 5, Deprecated on BLYNK_VERSION 0.6.0
BLYNK_CMD_REDIRECT = 41,
BLYNK_CMD_DEBUG_PRINT = 55,
BLYNK_CMD_EVENT_LOG = 64
};
//BlynkStatus values compatible && synced with BLYNK_VERSION 0.6.0
enum BlynkStatus
{
BLYNK_SUCCESS = 200,
BLYNK_TIMEOUT = 1,
BLYNK_BAD_FORMAT = 2,
//BLYNK_TIMEOUT = 1, Deprecated on BLYNK_VERSION 0.6.0
BLYNK_QUOTA_LIMIT_EXCEPTION = 1,
//BLYNK_BAD_FORMAT = 2, Deprecated on BLYNK_VERSION 0.6.0
BLYNK_ILLEGAL_COMMAND = 2,
BLYNK_NOT_REGISTERED = 3,
BLYNK_ALREADY_REGISTERED = 4,
BLYNK_NO_LOGIN = 5,
//BLYNK_NO_LOGIN = 5, Deprecated on BLYNK_VERSION 0.6.0
BLYNK_NOT_AUTHENTICATED = 5,
BLYNK_NOT_ALLOWED = 6,
BLYNK_NO_CONNECTION = 7,
BLYNK_NOT_SUPPORTED = 8,
//BLYNK_NO_CONNECTION = 7, Deprecated on BLYNK_VERSION 0.6.0
BLYNK_DEVICE_NOT_IN_NETWORK = 7,
//BLYNK_NOT_SUPPORTED = 8, Deprecated on BLYNK_VERSION 0.6.0
BLYNK_NO_ACTIVE_DASHBOARD = 8,
BLYNK_INVALID_TOKEN = 9,
BLYNK_SERVER_ERROR = 10,
BLYNK_ALREADY_LOGGED_IN = 11
//BLYNK_SERVER_ERROR = 10, Deprecated on BLYNK_VERSION 0.6.0
//BLYNK_ALREADY_LOGGED_IN = 11, Deprecated on BLYNK_VERSION 0.6.0
BLYNK_ILLEGAL_COMMAND_BODY = 11,
BLYNK_GET_GRAPH_DATA_EXCEPTION = 12,
BLYNK_NO_DATA_EXCEPTION = 17,
BLYNK_DEVICE_WENT_OFFLINE = 18,
BLYNK_SERVER_EXCEPTION = 19,
BLYNK_NTF_INVALID_BODY = 13,
BLYNK_NTF_NOT_AUTHORIZED = 14,
BLYNK_NTF_ECXEPTION = 15,
BLYNK_TIMEOUT = 16,
BLYNK_NOT_SUPPORTED_VERSION = 20,
BLYNK_ENERGY_LIMIT = 21
};
typedef struct _BlynkHeader
@ -114,7 +161,7 @@ typedef struct _BlynkParam
}
BlynkParam;
#if defined(ARDUINO) || defined (ESP8266)
#if defined(__AVR_ATmega644P__) || defined (__AVR_ATmega1284P__)
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define htons(x) ( ((x)<<8) | (((x)>>8)&0xFF) )
#define htonl(x) ( ((x)<<24 & 0xFF000000UL) | \
@ -133,15 +180,19 @@ BlynkParam;
#endif
#endif
#define BLYNK_NEWLINE "\r\n"
void blynk_begin(uint8_t * auth, uint8_t * dest_ip, uint16_t dest_port, uint8_t * buf, uint8_t socket);
void blynk_run(void);
//void blynk_time_handler(void);
uint8_t is_blynk_connection_available(void);
/*
void sendCmd(uint8_t cmd, uint16_t id, uint8_t * data, size_t length, uint8_t * data2, size_t length2);
*/
void blynk_syncAll(void);
void blynk_push_pin(uint8_t pin);
void blynk_push_virtual_pin(uint8_t pin);
void blynk_push_virtual_pin_msg(uint8_t pin, uint8_t * data);
#endif

@ -66,23 +66,25 @@ void digitalWrite(uint8_t pin, uint8_t val)
uint16_t analogRead(uint8_t pin)
{
uint8_t analog_pin = 0;
uint16_t val = 0;
if(pin > 14) analog_pin = pin - 14;
#ifdef WIZNET_WIZ550WEB
uint8_t analog_pin = 0;
if(pin > 14) analog_pin = pin - 14;
//PRINTF("analog_pin = %d\r\n", analog_pin);
val = get_ADC_val(analog_pin);
#elif defined WIZNET_W5500_EVB
uint8_t analog_pin = 0;
if(pin > 14) analog_pin = pin - 14;
PRINTF("analog_pin = %d\r\n", analog_pin);
if(analog_pin == A0) analog_pin = AIN;
PRINTF("changed analog_pin = %d\r\n", analog_pin);
val = get_ADC_val(analog_pin);
#else
if(pin = 6)
if(pin == 6)
{
val = adc_read(6);
}
PRINTF("analog pin %d = %d\r\n", analog_pin, val);
PRINTF("analog pin %d = %d\r\n", pin, val);
#endif
return val;
}
@ -112,6 +114,7 @@ void analogWrite(uint8_t pin, uint8_t val)
// 2: Input Pull-up
void pinMode(uint8_t pin, pinmode_dir dir)
{
// TODO: Add here example for AVR PULL-UP
#ifdef WIZNET_WIZ550WEB
if(dir == INPUT) IOdata.io[pin] = Input;
else if(dir == INPUT_PULLUP) IOdata.io[pin] = Input;
@ -137,14 +140,48 @@ void pinMode(uint8_t pin, pinmode_dir dir)
#endif
}
// Virtual Pin Read / Write functions; Not fully supported yet
// Virtual Pin Read
uint16_t virtualRead(uint8_t pin)
{
uint16_t val = 0;
PRINTF("virtual pin %d read\r\n", pin);
return pin;
//Example virtual pin reading
//Override your own handlers here like:
if(pin == 13)
{
//Digital read example from Virtual Pin 13
val = led2_read()?1:0;
PRINTF("LED2 is: %d %s\r\n", val, val?"HIGH":"LOW");
}
else if(pin == 6)
{
//Analog read example from Virtual Pin 6
val = adc_read(6);
PRINTF("analog pin %d = %d\r\n", pin, val);
}
return val;
}
void virtualWrite(uint8_t pin, uint16_t val)
{
PRINTF("virtual pin %d write val %d\r\n", pin, val);
//Example virtual pin writing
//Override your own handlers here like:
if(pin == 15)
{
//Analog write example to Virtual Pin 15
OCR2A = (uint8_t)val;
}
else if(pin == 20)
{
//Digital write example to Virtual Pin 20
if(val == 0)
{
led1_low();
}
else
{
led1_high();
}
}
}

@ -26,7 +26,7 @@
//***********BLYNK related: BEGIN
#define SOCK_BLYNK_CLIENT 6
//My auth token for my android test application GENERIC+W5500:
//My auth token for my android test application "m1284 + W5500":
uint8_t auth[] = "c113f724351444fc872ae586d70b18cd"; // You should get your own Auth Token in the BLYNK App
// Shouldn't used here, because used DNS resolving BLYNK server IP
@ -69,9 +69,14 @@ uint8_t Domain_IP[4] = {0, }; // Translated IP address by DNS S
* OK (v1.5)Restore pins state on board reboot
* OK (v1.6) Add push event (P13/PD.5 toggle every 10 sec && send state P13 to BLYNK server)
* OK (v1.7) Add push event to Virtual PIN1. Every 10sec push message: "Uptime: xxx sec", to BLYNK server (widget Terminal)
* Need compare local blynk.c code with modern <blynk> library - (Too old version here - 0.2.1 (On git blynk March 2019 - 0.6.x) )
* OK (v1.8) Need compare local blynk.c code with modern <blynk> library - (Too old version here - 0.2.1 (On git blynk March 2019 - 0.6.x) )
* OK (v1.8) Made fix correction blynk.h/blynk.c 16.02.2019 to match BLYNK protocol 0.6.0
*
* Author of porting to AVR Mega:
* PS.
* Further correction of the code from MBED authors (Vladimir Shimansky, Dmitriy Dumanskiy ..) is highly desirable.
* Because I'm not the author of mbed libs. And I do not quite well understand how this should work in their opinion.
*
* Author of unofficial porting to AVR Mega1284p/644p + W5500 Ethernet NIC (Wiznet sockets library using without Arduino):
* Ibragimov Maxim aka maxxir, Russia Togliatty ~xx.03.2019
*/
@ -97,7 +102,7 @@ volatile unsigned long _millis; // for millis tick !! Overflow every ~49.7 days
//*********Program metrics
const char compile_date[] PROGMEM = __DATE__; // Mmm dd yyyy - Дата компиляции
const char compile_time[] PROGMEM = __TIME__; // hh:mm:ss - Время компиляции
const char str_prog_name[] PROGMEM = "\r\nAtMega1284p v1.7a Static IP BLYNK WIZNET_5500 ETHERNET 15/03/2019\r\n"; // Program name
const char str_prog_name[] PROGMEM = "\r\nAtMega1284p v1.8 Static IP BLYNK WIZNET_5500 ETHERNET 16/03/2019\r\n"; // Program name
#if defined(__AVR_ATmega128__)
const char PROGMEM str_mcu[] = "ATmega128"; //CPU is m128
@ -172,7 +177,7 @@ unsigned long millis(void)
#define UART_BAUD_RATE 115200
static int uart0_putchar(char ch,FILE *stream);
static void uart0_rx_flash(void);
//static void uart0_rx_flash(void);
static FILE uart0_stdout = FDEV_SETUP_STREAM(uart0_putchar, NULL, _FDEV_SETUP_WRITE);
//PS. stdin не переназначаю, т.к. удобнее с ним работать через uart.h - api:
@ -197,6 +202,7 @@ static int uart0_putchar(char ch,FILE *stream)
}
// Очищаем буфер приема UART1 RX (иногда нужно)
/*
static void uart0_rx_flash(void)
{
// Считываем все из ring-buffer UART1 RX
@ -207,6 +213,7 @@ static void uart0_rx_flash(void)
} while (( c & UART_NO_DATA ) == 0); // Check RX1 none-empty
}
*/
//***************** UART0: END
//***************** ADC: BEGIN