Add new project for IOT testing [19_m1284p_WIZNET_blynk]

This commit is contained in:
maxxir_w
2019-03-12 15:44:05 +04:00
parent 1338fb35dd
commit 271dfb17ce
21 changed files with 8521 additions and 0 deletions

View File

@@ -0,0 +1,757 @@
/**************************************************************
* < Simple Blynk library for WIZnet products >
*
* WIZnet official website: http://www.wiznet.co.kr
* WIZnet Museum http://www.wiznetmuseum.com
* WIZnet Wiki http://wizwiki.net
* WIZnet Forum http://wizwiki.net/forum
*
* Downloads, docs, tutorials: http://www.blynk.cc
* Blynk community: http://community.blynk.cc
* Social groups: http://www.fb.com/blynkapp
* http://twitter.com/blynk_app
*/
#include <stdio.h>
#include <string.h>
#include "socket.h"
#include "blynk.h"
#include "blynkDependency.h"
//#include "common.h" // When the project has no "common.h" file, this line have to commented out.
#ifndef DATA_BUF_SIZE
#define DATA_BUF_SIZE 2048
#endif
uint8_t blynk_connect(void);
void processInput(void);
void processCmd(uint8_t * buff, size_t len);
uint8_t readHeader(BlynkHeader * hdr);
void sendCmd(uint8_t cmd, uint16_t id, uint8_t * data, size_t length, uint8_t * data2, size_t length2);
uint16_t getNextMsgId(void);
void BlynkAverageSample (uint32_t * avg, const uint32_t input, uint8_t n);
//uint32_t millis(void);
uint8_t blynk_custom_delay(uint32_t delayms);
void blynkparam_init(BlynkParam p);
uint8_t * blynkparam_get(void);
// Util functions
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);
uint8_t * authkey;
uint32_t lastActivityIn;
uint32_t lastActivityOut;
uint32_t lastHeartbeat;
#ifdef BLYNK_MSG_LIMIT
uint32_t deltaCmd;
#endif
uint16_t currentMsgId;
uint8_t blynk_connected = 0;
uint8_t blynk_connection_available = 0;
uint32_t ptime = 0;
// Init variables
uint8_t blynk_socket, s;
uint8_t * server_ip;
uint16_t server_port;
uint8_t * msgbuf;
// variables for parameter parsing
uint8_t * param_ptr;
uint8_t * param_end;
volatile uint32_t blynk_time_1ms;
uint16_t blynkclient_port = BLYNK_DEFAULT_CLIENT_PORT;
uint8_t flag_blynkinit_complete = 0;
void blynk_begin(uint8_t * auth, uint8_t * dest_ip, uint16_t dest_port, uint8_t * buf, uint8_t socket)
{
s = socket;
authkey = auth;
server_ip = dest_ip;
server_port = dest_port;
msgbuf = buf;
flag_blynkinit_complete = 1;
}
uint8_t blynk_connection_try = 0;
void blynk_run(void)
{
uint32_t t;
#ifdef BLYNK_DEBUG
uint8_t destip[4];
uint16_t destport;
#endif
if(!flag_blynkinit_complete) return;
switch(getSn_SR(s))
{
case SOCK_ESTABLISHED:
// Interrupt clear
if(getSn_IR(s) & Sn_IR_CON)
{
setSn_IR(s, Sn_IR_CON);
#ifdef BLYNK_DEBUG
getSn_DIPR(s, destip);
destport = getSn_DPORT(s);
printf("Blynk[%d] : Connected - %d.%d.%d.%d:%d\r\n",s, destip[0], destip[1], destip[2], destip[3], destport);
#endif
}
if(!blynk_connected)
{
if(!(blynk_connection_available = blynk_connect())) return;
else
{
blynk_connected = true;
#ifdef BLYNK_DEBUG
printf("Blynk[%d] : Auth connection complete\r\n", s);
#endif
}
}
if(blynk_connection_available > 0) processInput();
t = millis();
if (t - lastActivityIn > (1000UL * BLYNK_HEARTBEAT + BLYNK_TIMEOUT_MS*3)) {
#ifdef BLYNK_DEBUG
printf("Heartbeat timeout (last in: %lu)\r\n", lastActivityIn);
#else
printf("Heartbeat timeout\r\n");
#endif
blynk_connected = false;
blynk_connection_available = false;
disconnect(s);
}
else if (( t - lastActivityIn > 1000UL * BLYNK_HEARTBEAT ||
t - lastActivityOut > 1000UL * BLYNK_HEARTBEAT) &&
t - lastHeartbeat > BLYNK_TIMEOUT_MS)
{
// Send ping if we didn't both send and receive something for BLYNK_HEARTBEAT seconds
#ifdef BLYNK_DEBUG
printf("Heartbeat\r\n");
#endif
sendCmd(BLYNK_CMD_PING, 0, NULL, 0, NULL, 0);
lastHeartbeat = t;
}
break;
case SOCK_CLOSE_WAIT:
#ifdef BLYNK_DEBUG
printf("Blynk[%d] : ClOSE WAIT\r\n", s); // if a peer requests to close the current connection
#endif
disconnect(s);
break;
case SOCK_CLOSED:
#ifdef BLYNK_DEBUG
//printf("> Blynk[%d] : CLOSED\r\n", s);
#endif
blynk_connected = false;
blynk_connection_available = false;
if(socket(s, Sn_MR_TCP, blynkclient_port++, 0x00) == s) /* Reinitialize the socket */
{
#ifdef BLYNK_DEBUG
printf("Blynk[%d] : SOCKET OPEN\r\n", s);
#endif
}
break;
case SOCK_INIT:
#ifdef BLYNK_DEBUG
printf("Blynk[%d] : Connecting to ", s);
printf("%d.%d.%d.%d:%d\r\n", server_ip[0], server_ip[1], server_ip[2], server_ip[3], server_port);
#endif
connect(s, server_ip, server_port);
break;
default :
break;
} // end of switch
}
uint8_t blynk_connect(void)
{
BlynkHeader hdr;
static uint16_t id = 0;
uint8_t i;
uint8_t hsize = 0;
//#ifdef BLYNK_DEBUG
uint32_t t = millis();
//#endif
// changed parts
//////////////////////////////////////////////////////////////////////////////////
static uint8_t cmd_sent = false;
if(blynk_custom_delay(0)) return false;
if(!cmd_sent)
{
id = getNextMsgId();
sendCmd(BLYNK_CMD_LOGIN, id, authkey, strlen((char *)authkey), NULL, 0);
cmd_sent = true;
ptime = millis(); // for check connection timeout
}
if(millis() < (ptime + BLYNK_CONNECTION_TIMEOUT_MS)) // wait data received during 5sec before connection timeout occur
{
if(!readHeader(&hdr))
{
return false;
}
else // Auth response received
{
cmd_sent = false;
}
}
else
{
hdr.length = BLYNK_TIMEOUT;
cmd_sent = false;
}
//////////////////////////////////////////////////////////////////////////////////
if (BLYNK_CMD_RESPONSE != hdr.type ||
id != hdr.msg_id ||
(BLYNK_SUCCESS != hdr.length && BLYNK_ALREADY_LOGGED_IN != hdr.length))
{
if (BLYNK_TIMEOUT == hdr.length)
{
printf("Timeout\r\n");
}
else if (BLYNK_INVALID_TOKEN == hdr.length)
{
printf("Invalid auth token\r\n");
}
else
{
printf("Connect failed (code: %d)\r\n", hdr.length);
// Send some invalid headers to server for disconnection
hdr.type = 255;
hdr.msg_id = 0;
hdr.length = 0;
// problem fixed for header structure size
hsize = 0;
msgbuf[hsize++] = hdr.type;
msgbuf[hsize++] = hdr.msg_id;
msgbuf[hsize++] = hdr.msg_id;
msgbuf[hsize++] = hdr.length;
msgbuf[hsize++] = hdr.length;
for (i = 0; i < 10; i++)
{
send(s, msgbuf, hsize);
}
}
disconnect(s);
// old delay function removed
blynk_custom_delay(5000);
return false;
}
lastHeartbeat = lastActivityIn = lastActivityOut = millis();
#ifdef BLYNK_MSG_LIMIT
deltaCmd = 1000;
#endif
printf("Ready!\r\n");
#ifdef BLYNK_DEBUG
printf("Roundtrip: %ldms\r\n", lastActivityIn-t);
#endif
return true;
}
void processInput(void)
{
BlynkHeader hdr;
#ifdef BLYNK_DEBUG
uint16_t i;
#endif
if (!readHeader(&hdr)) return;
switch (hdr.type)
{
case BLYNK_CMD_RESPONSE:
{
if (BLYNK_NO_LOGIN == hdr.length)
{
disconnect(s);
return;
}
// TODO: return code may indicate App presence
} break;
case BLYNK_CMD_PING:
{
sendCmd(BLYNK_CMD_RESPONSE, hdr.msg_id, NULL, BLYNK_SUCCESS, NULL, 0);
} break;
case BLYNK_CMD_HARDWARE:
case BLYNK_CMD_BRIDGE:
{
if (hdr.length > BLYNK_MAX_READBYTES)
{
printf("Packet size (%u) > max allowed (%u)\r\n", hdr.length, BLYNK_MAX_READBYTES);
disconnect(s);
return;
}
//printf("hdr.length = %d\r\n", hdr.length);
if (hdr.length != recv(s, msgbuf, hdr.length))
{
printf("Can't read body\r\n");
return;
}
msgbuf[hdr.length] = '\0'; // Add 1 to zero-terminate
#ifdef BLYNK_DEBUG
printf(">");
for(i = 0; i < hdr.length; i++)
{
if(msgbuf[i] != '\0') printf("%c", msgbuf[i]);
else printf(" ");
}
printf("\r\n");
#endif
currentMsgId = hdr.msg_id;
processCmd(msgbuf, hdr.length);
currentMsgId = 0;
} break;
default:
printf("Invalid header type: %d\r\n", hdr.type);
disconnect(s);
return;
}
lastActivityIn = millis();
}
void blynkparam_init(BlynkParam p)
{
param_ptr = p.buff;
param_end = param_ptr + p.len;
}
uint8_t * blynkparam_get(void)
{
uint8_t size;
uint8_t * ret_ptr = param_ptr;
if(param_ptr >= param_end) return NULL;
size = strlen((char *)param_ptr);
param_ptr += (size+1);
return ret_ptr;
}
void processCmd(uint8_t * buff, size_t len)
{
uint8_t * nexttok;
const char * cmd;
uint8_t pin;
uint8_t rsp_mem[16];
uint16_t rsp_len;
uint16_t w_param;
BlynkParam param;
param.buff = buff;
param.len = len;
// for virtual read / write functions
//BlynkParam param2;
//uint8_t * start;
memset(rsp_mem, 0, sizeof(rsp_mem));
blynkparam_init(param);
nexttok = blynkparam_get();
if(!nexttok) return;
cmd = (char *)nexttok;
if(!strcmp(cmd, "info"))
{
static uint8_t profile[] =
BLYNK_PARAM_KV("ver" , BLYNK_VERSION)
BLYNK_PARAM_KV("h-beat" , TOSTRING(BLYNK_HEARTBEAT))
BLYNK_PARAM_KV("buff-in", TOSTRING(BLYNK_MAX_READBYTES))
#ifdef BLYNK_INFO_DEVICE
BLYNK_PARAM_KV("dev" , BLYNK_INFO_DEVICE)
#endif
#ifdef BLYNK_INFO_CPU
BLYNK_PARAM_KV("cpu" , BLYNK_INFO_CPU)
#endif
#ifdef BLYNK_INFO_CONNECTION
BLYNK_PARAM_KV("con" , BLYNK_INFO_CONNECTION)
#endif
;
const size_t profile_len = sizeof(profile)-1;
sendCmd(BLYNK_CMD_HARDWARE, 0, profile, profile_len, NULL, 0);
}
nexttok = blynkparam_get();
if(!nexttok) return;
pin = (uint8_t)ATOI((uint8_t *)nexttok, 10);
if(!strcmp(cmd, "dr")) // digital pin read
{
rsp_len = sprintf((char *)rsp_mem, "dw %d %d ", pin, digitalRead(pin));
replacetonull(rsp_mem, ' ');
sendCmd(BLYNK_CMD_HARDWARE, 0, rsp_mem, rsp_len, NULL, 0);
}
else if(!strcmp(cmd, "ar")) // analog pin read
{
rsp_len = sprintf((char *)rsp_mem, "aw %d %d ", pin, analogRead(pin));
replacetonull(rsp_mem, ' ');
sendCmd(BLYNK_CMD_HARDWARE, 0, rsp_mem, rsp_len, NULL, 0);
}
else if(!strcmp(cmd, "vr")) // virtual pin read
{
#ifdef BLYNK_DEBUG
printf("vr command: Not fully supported yet\r\n");
#endif
rsp_len = sprintf((char *)rsp_mem, "vr %d %d ", pin, virtualRead(pin));
replacetonull(rsp_mem, ' ');
sendCmd(BLYNK_CMD_HARDWARE, 0, rsp_mem, rsp_len, NULL, 0);
/*
WidgetReadHandler handler;
handler = GetReadHandler(pin);
if(handler)
{
BlynkReq req = { 0, BLYNK_SUCCESS, (uint8_t)pin };
handler(req);
}
*/
}
else
{
if(!strcmp(cmd, "vw")) // virtual pin write
{
#ifdef BLYNK_DEBUG
printf("vw command: Not fully supported yet\r\n");
#endif
nexttok = blynkparam_get();
w_param = ATOI((uint8_t *)nexttok, 10);
virtualWrite(pin, w_param);
// update widget state
//rsp_len = sprintf((char *)rsp_mem, "vw %d %d ", pin, w_param);
//replacetonull(rsp_mem, ' ');
//sendCmd(BLYNK_CMD_HARDWARE, 0, rsp_mem, rsp_len, NULL, 0);
/*
WidgetWriteHandler handler;
handler = GetWriteHandler(pin);
if(handler)
{
BlynkReq req = { 0, BLYNK_SUCCESS, (uint8_t)pin };
nexttok = blynkparam_get();
start = nexttok;
param2.buff = start;
param2.len = len - (start - buff);
handler(req, param2);
}
*/
}
if(!strcmp(cmd, "pm")) // pin mode setting
{
while(nexttok) // end condition: nexttok == NULL
{
nexttok = blynkparam_get();
if (!strcmp((char *)nexttok, "in")) {
pinMode(pin, INPUT);
} else if (!strcmp((char *)nexttok, "out") || !strcmp((char *)nexttok, "pwm")) {
pinMode(pin, OUTPUT);
} else if (!strcmp((char *)nexttok, "pu")) {
pinMode(pin, INPUT_PULLUP);
} else {
#ifdef BLYNK_DEBUG
printf("Invalid pinMode %u -> %s\r\n", pin, nexttok);
#endif
}
nexttok = blynkparam_get();
if(!nexttok) {break;}
pin = (uint8_t)ATOI((uint8_t *)nexttok, 10); // pin info update
}
}
nexttok = blynkparam_get();
if(!nexttok) return;
// Should be 1 parameter (value)
if(!strcmp(cmd, "dw")) // digital pin write
{
w_param = (uint8_t)ATOI((uint8_t *)nexttok, 10);
pinMode(pin, OUTPUT);
digitalWrite(pin, w_param ? HIGH : LOW);
// update widget state
//rsp_len = sprintf((char *)rsp_mem, "dw %d %d ", pin, digitalRead(pin));
//replacetonull(rsp_mem, ' ');
//sendCmd(BLYNK_CMD_HARDWARE, 0, rsp_mem, rsp_len, NULL, 0);
}
else if(!strcmp(cmd, "aw")) // analog pin write
{
w_param = (uint8_t)ATOI((uint8_t *)nexttok, 10);
analogWrite(pin, w_param);
// update widget state
//rsp_len = sprintf((char *)rsp_mem, "aw %d %d ", pin, digitalRead(pin));
//replacetonull(rsp_mem, ' ');
//sendCmd(BLYNK_CMD_HARDWARE, 0, rsp_mem, rsp_len, NULL, 0);
}
else
{
printf("Invalid HW cmd: %s\r\n", cmd);
}
}
}
uint8_t readHeader(BlynkHeader * hdr)
{
uint16_t len;
uint8_t hsize = 0;
if((len = getSn_RX_RSR(s)) > 0)
{
#ifdef BLYNK_DEBUG
//printf("recv header len = %d\r\n", len);
#endif
if(BLINK_HEADER_SIZE != recv(s, msgbuf, BLINK_HEADER_SIZE))
{
return false;
}
// problem fixed for header structure size
hdr->type = msgbuf[hsize++];
hdr->msg_id = (uint16_t)msgbuf[hsize++];
hdr->msg_id |= ((uint16_t)msgbuf[hsize++]) << 8;
hdr->length = (uint16_t)msgbuf[hsize++];
hdr->length |= ((uint16_t)msgbuf[hsize++]) << 8;
hdr->msg_id = ntohs(hdr->msg_id);
hdr->length = ntohs(hdr->length);
#ifdef BLYNK_DEBUG
printf(">msg %d,%u,%u\r\n", hdr->type, hdr->msg_id, hdr->length);
#endif
return true;
}
return false;
}
void sendCmd(uint8_t cmd, uint16_t id, uint8_t * data, size_t length, uint8_t * data2, size_t length2)
{
BlynkHeader hdr;
size_t wlen = 0;
uint32_t ts;
uint8_t hsize = 0;
#ifdef BLYNK_DEBUG
uint16_t i;
#endif
if(getSn_SR(s) != SOCK_ESTABLISHED)
{
#ifdef BLYNK_DEBUG
printf("Cmd not sent\r\n");
#endif
return;
}
if (0 == id) {
id = getNextMsgId();
}
hdr.type = cmd;
hdr.msg_id = htons(id);
hdr.length = htons(length+length2);
// problem fixed for header structure size
msgbuf[hsize++] = hdr.type;
msgbuf[hsize++] = (uint8_t)(0x00ff & hdr.msg_id);
msgbuf[hsize++] = (uint8_t)((0xff00 & hdr.msg_id) >> 8);
msgbuf[hsize++] = (uint8_t)(0x00ff & hdr.length);
msgbuf[hsize++] = (uint8_t)((0xff00 & hdr.length) >> 8);
#ifdef BLYNK_DEBUG
printf("<msg %d,%u,%u\r\n", cmd, id, length+length2);
#endif
wlen += (size_t)send(s, msgbuf, hsize);
if (cmd != BLYNK_CMD_RESPONSE) {
if (length) {
#ifdef BLYNK_DEBUG
printf("<");
for(i = 0; i < length; i++)
{
if(data[i] != '\0') printf("%c", data[i]);
else printf(" ");
}
printf("\r\n");
#endif
wlen += (size_t)send(s, (uint8_t *)data, length);
}
if (length2) {
#ifdef BLYNK_DEBUG
printf("<");
for(i = 0; i < length2; i++)
{
if(data2[i] != '\0') printf("%c", data2[i]);
else printf(" ");
}
printf("\r\n");
#endif
wlen += (size_t)send(s, (uint8_t *)data2, length2);
}
if (wlen != hsize+length+length2) {
printf("Sent %u/%u\r\n", wlen, hsize+length+length2);
disconnect(s);
return;
}
}
ts = millis();
#ifdef BLYNK_MSG_LIMIT
BlynkAverageSample(&deltaCmd, ts - lastActivityOut, 10);
lastActivityOut = ts;
if (deltaCmd < (1000/BLYNK_MSG_LIMIT)) {
//::delay(5000);
printf("Flood\r\n");
disconnect(s);
}
#else
lastActivityOut = ts;
#endif
}
uint16_t getNextMsgId(void)
{
static uint16_t last = 0;
if (currentMsgId != 0)
return currentMsgId;
if (++last == 0)
last = 1;
return last;
}
void BlynkAverageSample (uint32_t * avg, const uint32_t input, uint8_t n)
{
* avg -= * avg/n;
* avg += input/n;
}
/*
uint32_t millis(void)
{
return blynk_time_1ms;
}
*/
// Time count function; this function have to call by timer (1ms)
void blynk_time_handler(void)
{
blynk_time_1ms++;
}
// Custom delay for checking timeout count
uint8_t blynk_custom_delay(uint32_t delayms)
{
uint8_t ret = false;
static uint32_t basetime;
if(delayms > 0)
{
if(!basetime) basetime = millis() + delayms;
}
else
{
if(millis() < basetime)
{
ret = true; // delaying
}
else
{
basetime = 0;
ret = false; // no delay
}
}
return ret;
}
/**
@brief CONVERT STRING INTO INTEGER
@return a integer number
*/
static uint16_t ATOI(
uint8_t * str, /**< is a pointer to convert */
uint8_t base /**< is a base value (must be in the range 2 - 16) */
)
{
unsigned int num = 0;
while ((*str !=0) && (*str != 0x20)) // not include the space(0x020)
num = num * base + C2D(*str++);
return num;
}
static uint8_t C2D(
uint8_t c /**< is a character('0'-'F') to convert to HEX */
)
{
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'a' && c <= 'f')
return 10 + c -'a';
if (c >= 'A' && c <= 'F')
return 10 + c -'A';
return (char)c;
}
static void replacetonull(uint8_t * str, uint8_t c)
{
int x;
for (x = 0; str[x]; x++)
if (str[x] == c) str[x] = NULL;
}

View File

@@ -0,0 +1,130 @@
#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
// Professional settings
#define BLYNK_VERSION "0.2.1"
#define BLYNK_HEARTBEAT 10
#define BLYNK_TIMEOUT_MS 1500
//#define BLYNK_MSG_LIMIT 20
#define BLYNK_DEBUG
#ifndef BLYNK_INFO_DEVICE
#define BLYNK_INFO_DEVICE "Arduino"
//#define BLYNK_INFO_DEVICE "WIZWiki"
#endif
#ifndef BLYNK_INFO_CPU
#define BLYNK_INFO_CPU "ATmega2560"
//#define BLYNK_INFO_CPU "ST103FRB"
#endif
#ifndef BLYNK_INFO_CONNECTION
#define BLYNK_INFO_CONNECTION "W5000"
#endif
#define BLYNK_PARAM_KV(k, v) k "\0" v "\0"
// General defines
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
// Custom defines
#define BLYNK_DEFAULT_CLIENT_PORT 1025
#define BLYNK_CONNECTION_TIMEOUT_MS 5000
#define BLINK_HEADER_SIZE 5
//#ifndef BlynkProtocolDefs_h
//#define BlynkProtocolDefs_h
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_PING = 6,
BLYNK_CMD_TWEET = 12,
BLYNK_CMD_EMAIL = 13,
BLYNK_CMD_PUSH_NOTIFICATION = 14,
BLYNK_CMD_BRIDGE = 15,
BLYNK_CMD_HARDWARE = 20
};
enum BlynkStatus
{
BLYNK_SUCCESS = 200,
BLYNK_TIMEOUT = 1,
BLYNK_BAD_FORMAT = 2,
BLYNK_NOT_REGISTERED = 3,
BLYNK_ALREADY_REGISTERED = 4,
BLYNK_NO_LOGIN = 5,
BLYNK_NOT_ALLOWED = 6,
BLYNK_NO_CONNECTION = 7,
BLYNK_NOT_SUPPORTED = 8,
BLYNK_INVALID_TOKEN = 9,
BLYNK_SERVER_ERROR = 10,
BLYNK_ALREADY_LOGGED_IN = 11
};
typedef struct _BlynkHeader
{
uint8_t type;
uint16_t msg_id;
uint16_t length;
}
BlynkHeader;
typedef struct _BlynkParam
{
uint8_t * buff;
uint16_t len;
}
BlynkParam;
#if defined(ARDUINO) || defined (ESP8266)
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define htons(x) ( ((x)<<8) | (((x)>>8)&0xFF) )
#define htonl(x) ( ((x)<<24 & 0xFF000000UL) | \
((x)<< 8 & 0x00FF0000UL) | \
((x)>> 8 & 0x0000FF00UL) | \
((x)>>24 & 0x000000FFUL) )
#define ntohs(x) htons(x)
#define ntohl(x) htonl(x)
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define htons(x) (x)
#define htonl(x) (x)
#define ntohs(x) (x)
#define ntohl(x) (x)
#else
#error byte order problem
#endif
#endif
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);
#endif

View File

@@ -0,0 +1,119 @@
#include <stdio.h>
#include "blynkDependency.h"
#include "../globals.h"
#ifdef WIZNET_WIZ550WEB
#include "gpioHandler.h"
#include "userHandler.h"
#elif defined WIZNET_W5500_EVB
#include "board.h"
#include "adcHandler.h"
#endif
uint8_t digitalRead(uint8_t pin)
{
uint8_t val = pin;
#ifdef WIZNET_WIZ550WEB
val = get_IO_Status(pin);
#elif defined WIZNET_W5500_EVB
val = Chip_GPIO_GetPinState(LPC_GPIO, dio_ports[pin], dio_pins[pin]);
#else
printf("digital pin %d read\r\n", pin);
#endif
return val;
}
void digitalWrite(uint8_t pin, uint8_t val)
{
#ifdef WIZNET_WIZ550WEB
IOdata.ios[pin] = val;
if(val == HIGH) IO_On(pin);
else if(val == LOW) IO_Off(pin);
write_IOstorage(&IOdata, sizeof(IOdata));
#elif defined WIZNET_W5500_EVB
if(val == HIGH) Chip_GPIO_SetPinState(LPC_GPIO, dio_ports[pin], dio_pins[pin], true); // High
else if(val == LOW) Chip_GPIO_SetPinState(LPC_GPIO, dio_ports[pin], dio_pins[pin], false); // Low
#else
printf("digital pin %d write val %d\r\n", pin, val);
if(pin == 13)
{
if(val == 0)
{
led1_low();
}
else
{
led1_high();
}
}
#endif
}
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
//printf("analog_pin = %d\r\n", analog_pin);
val = get_ADC_val(analog_pin);
#elif defined WIZNET_W5500_EVB
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
printf("analog pin %d read\r\n", analog_pin);
#endif
return val;
}
void analogWrite(uint8_t pin, uint8_t val)
{
#ifdef WIZNET_WIZ550WEB
printf("Analog Write: Not supported yet. pin %d, val %d", pin, val);
#elif defined WIZNET_W5500_EVB
printf("Analog Write: Not supported yet. pin %d, val %d", pin, val);
#else
printf("analog pin %d write val %d\r\n", pin, val);
#endif
}
// Pin mode (dir) defines
// 0: Input
// 1: Output
// 2: Input Pull-up
void pinMode(uint8_t pin, pinmode_dir dir)
{
#ifdef WIZNET_WIZ550WEB
if(dir == INPUT) IOdata.io[pin] = Input;
else if(dir == INPUT_PULLUP) IOdata.io[pin] = Input;
else if(dir == OUTPUT) IOdata.io[pin] = Output; // Output
IO_Init(pin);
write_IOstorage(&IOdata, sizeof(IOdata));
#elif defined WIZNET_W5500_EVB
if(dir == INPUT) Chip_GPIO_SetPinDIRInput(LPC_GPIO, dio_ports[pin], dio_pins[pin]); // Input
else if(dir == INPUT_PULLUP) Chip_GPIO_SetPinDIRInput(LPC_GPIO, dio_ports[pin], dio_pins[pin]); // Input
else if(dir == OUTPUT) Chip_GPIO_SetPinDIROutput(LPC_GPIO, dio_ports[pin], dio_pins[pin]); // Output
#else
printf("pinmode setting: pin %d dir %d\r\n", pin, dir);
if((pin == 13)&&(dir ==1))
{
//m1284p LED1 pin to out
led1_conf();
}
#endif
}
// Virtual Pin Read / Write functions; Not fully supported yet
uint16_t virtualRead(uint8_t pin)
{
printf("virtual pin %d read\r\n", pin);
return pin;
}
void virtualWrite(uint8_t pin, uint16_t val)
{
printf("virtual pin %d write val %d\r\n", pin, val);
}

View File

@@ -0,0 +1,31 @@
#ifndef _WIZNET_BLYNK_DEPENDENCY_H_
#define _WIZNET_BLYNK_DEPENDENCY_H_
#include <stdint.h>
#include "blynk.h"
////////////////////////////////////////////////////////////////
typedef enum { // Pin mode; directions
INPUT,
OUTPUT,
INPUT_PULLUP
}pinmode_dir;
typedef enum {false = 0, true = !false} Boolian;
#define HIGH 1
#define LOW 0
////////////////////////////////////////////////////////////////
uint8_t digitalRead(uint8_t pin);
void digitalWrite(uint8_t pin, uint8_t val);
uint16_t analogRead(uint8_t pin);
void analogWrite(uint8_t pin, uint8_t val);
uint16_t virtualRead(uint8_t pin);
void virtualWrite(uint8_t pin, uint16_t val);
void pinMode(uint8_t pin, pinmode_dir dir);
#endif

View File

@@ -0,0 +1,225 @@
#include <stdio.h>
#include "loopback.h"
#include "socket.h"
#include "wizchip_conf.h"
#if LOOPBACK_MODE == LOOPBACK_MAIN_NOBLCOK
int32_t loopback_tcps(uint8_t sn, uint8_t* buf, uint16_t port)
{
int32_t ret;
uint16_t size = 0, sentsize=0;
#ifdef _LOOPBACK_DEBUG_
uint8_t destip[4];
uint16_t destport;
#endif
switch(getSn_SR(sn))
{
case SOCK_ESTABLISHED :
if(getSn_IR(sn) & Sn_IR_CON)
{
#ifdef _LOOPBACK_DEBUG_
getSn_DIPR(sn, destip);
destport = getSn_DPORT(sn);
printf("%d:Connected - %d.%d.%d.%d : %d\r\n",sn, destip[0], destip[1], destip[2], destip[3], destport);
#endif
setSn_IR(sn,Sn_IR_CON);
}
if((size = getSn_RX_RSR(sn)) > 0) // Don't need to check SOCKERR_BUSY because it doesn't not occur.
{
if(size > DATA_BUF_SIZE) size = DATA_BUF_SIZE;
ret = recv(sn, buf, size);
if(ret <= 0) return ret; // check SOCKERR_BUSY & SOCKERR_XXX. For showing the occurrence of SOCKERR_BUSY.
size = (uint16_t) ret;
sentsize = 0;
while(size != sentsize)
{
ret = send(sn, buf+sentsize, size-sentsize);
if(ret < 0)
{
close(sn);
return ret;
}
sentsize += ret; // Don't care SOCKERR_BUSY, because it is zero.
}
}
break;
case SOCK_CLOSE_WAIT :
#ifdef _LOOPBACK_DEBUG_
//printf("%d:CloseWait\r\n",sn);
#endif
if((ret = disconnect(sn)) != SOCK_OK) return ret;
#ifdef _LOOPBACK_DEBUG_
printf("%d:Socket Closed\r\n", sn);
#endif
break;
case SOCK_INIT :
#ifdef _LOOPBACK_DEBUG_
printf("%d:Listen, TCP server loopback, port [%d]\r\n", sn, port);
#endif
if( (ret = listen(sn)) != SOCK_OK) return ret;
break;
case SOCK_CLOSED:
#ifdef _LOOPBACK_DEBUG_
//printf("%d:TCP server loopback start\r\n",sn);
#endif
if((ret = socket(sn, Sn_MR_TCP, port, 0x00)) != sn) return ret;
#ifdef _LOOPBACK_DEBUG_
//printf("%d:Socket opened\r\n",sn);
#endif
break;
default:
break;
}
return 1;
}
int32_t loopback_tcpc(uint8_t sn, uint8_t* buf, uint8_t* destip, uint16_t destport)
{
int32_t ret; // return value for SOCK_ERRORs
uint16_t size = 0, sentsize=0;
// Destination (TCP Server) IP info (will be connected)
// >> loopback_tcpc() function parameter
// >> Ex)
// uint8_t destip[4] = {192, 168, 0, 214};
// uint16_t destport = 5000;
// Port number for TCP client (will be increased)
static uint16_t any_port = 50000;
// Socket Status Transitions
// Check the W5500 Socket n status register (Sn_SR, The 'Sn_SR' controlled by Sn_CR command or Packet send/recv status)
switch(getSn_SR(sn))
{
case SOCK_ESTABLISHED :
if(getSn_IR(sn) & Sn_IR_CON) // Socket n interrupt register mask; TCP CON interrupt = connection with peer is successful
{
#ifdef _LOOPBACK_DEBUG_
printf("%d:Connected to - %d.%d.%d.%d : %d\r\n",sn, destip[0], destip[1], destip[2], destip[3], destport);
#endif
setSn_IR(sn, Sn_IR_CON); // this interrupt should be write the bit cleared to '1'
}
//////////////////////////////////////////////////////////////////////////////////////////////
// Data Transaction Parts; Handle the [data receive and send] process
//////////////////////////////////////////////////////////////////////////////////////////////
if((size = getSn_RX_RSR(sn)) > 0) // Sn_RX_RSR: Socket n Received Size Register, Receiving data length
{
if(size > DATA_BUF_SIZE) size = DATA_BUF_SIZE; // DATA_BUF_SIZE means user defined buffer size (array)
ret = recv(sn, buf, size); // Data Receive process (H/W Rx socket buffer -> User's buffer)
if(ret <= 0) return ret; // If the received data length <= 0, receive failed and process end
size = (uint16_t) ret;
sentsize = 0;
// Data sentsize control
while(size != sentsize)
{
ret = send(sn, buf+sentsize, size-sentsize); // Data send process (User's buffer -> Destination through H/W Tx socket buffer)
if(ret < 0) // Send Error occurred (sent data length < 0)
{
close(sn); // socket close
return ret;
}
sentsize += ret; // Don't care SOCKERR_BUSY, because it is zero.
}
}
//////////////////////////////////////////////////////////////////////////////////////////////
break;
case SOCK_CLOSE_WAIT :
#ifdef _LOOPBACK_DEBUG_
//printf("%d:CloseWait\r\n",sn);
#endif
if((ret=disconnect(sn)) != SOCK_OK) return ret;
#ifdef _LOOPBACK_DEBUG_
printf("%d:Socket Closed\r\n", sn);
#endif
break;
case SOCK_INIT :
#ifdef _LOOPBACK_DEBUG_
printf("%d:Try to connect to the %d.%d.%d.%d : %d\r\n", sn, destip[0], destip[1], destip[2], destip[3], destport);
#endif
if( (ret = connect(sn, destip, destport)) != SOCK_OK) return ret; // Try to TCP connect to the TCP server (destination)
break;
case SOCK_CLOSED:
close(sn);
if((ret=socket(sn, Sn_MR_TCP, any_port++, 0x00)) != sn){
if(any_port == 0xffff) any_port = 50000;
return ret; // TCP socket open with 'any_port' port number
}
#ifdef _LOOPBACK_DEBUG_
//printf("%d:TCP client loopback start\r\n",sn);
//printf("%d:Socket opened\r\n",sn);
#endif
break;
default:
break;
}
return 1;
}
int32_t loopback_udps(uint8_t sn, uint8_t* buf, uint16_t port)
{
int32_t ret;
uint16_t size, sentsize;
uint8_t destip[4];
uint16_t destport;
switch(getSn_SR(sn))
{
case SOCK_UDP :
if((size = getSn_RX_RSR(sn)) > 0)
{
if(size > DATA_BUF_SIZE) size = DATA_BUF_SIZE;
ret = recvfrom(sn, buf, size, destip, (uint16_t*)&destport);
if(ret <= 0)
{
#ifdef _LOOPBACK_DEBUG_
printf("%d: recvfrom error. %ld\r\n",sn,ret);
#endif
return ret;
}
size = (uint16_t) ret;
sentsize = 0;
while(sentsize != size)
{
ret = sendto(sn, buf+sentsize, size-sentsize, destip, destport);
if(ret < 0)
{
#ifdef _LOOPBACK_DEBUG_
printf("%d: sendto error. %ld\r\n",sn,ret);
#endif
return ret;
}
sentsize += ret; // Don't care SOCKERR_BUSY, because it is zero.
}
}
break;
case SOCK_CLOSED:
#ifdef _LOOPBACK_DEBUG_
//printf("%d:UDP loopback start\r\n",sn);
#endif
if((ret = socket(sn, Sn_MR_UDP, port, 0x00)) != sn)
return ret;
#ifdef _LOOPBACK_DEBUG_
printf("%d:Opened, UDP loopback, port [%d]\r\n", sn, port);
#endif
break;
default :
break;
}
return 1;
}
#endif

View File

@@ -0,0 +1,38 @@
#ifndef _LOOPBACK_H_
#define _LOOPBACK_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
/* Loopback test debug message printout enable */
#define _LOOPBACK_DEBUG_
/* DATA_BUF_SIZE define for Loopback example */
#ifndef DATA_BUF_SIZE
#define DATA_BUF_SIZE 2048
#endif
/************************/
/* Select LOOPBACK_MODE */
/************************/
#define LOOPBACK_MAIN_NOBLOCK 0
#define LOOPBACK_MODE LOOPBACK_MAIN_NOBLOCK
/* TCP server Loopback test example */
int32_t loopback_tcps(uint8_t sn, uint8_t* buf, uint16_t port);
/* TCP client Loopback test example */
int32_t loopback_tcpc(uint8_t sn, uint8_t* buf, uint8_t* destip, uint16_t destport);
/* UDP Loopback test example */
int32_t loopback_udps(uint8_t sn, uint8_t* buf, uint16_t port);
#ifdef __cplusplus
}
#endif
#endif