Introduction: WizFi250-CSI(C Script Interpreter) for Rapid-prototyping, DIY, IoT-startup or Students.

Picture of WizFi250-CSI(C Script Interpreter) for Rapid-prototyping, DIY, IoT-startup or Students.

Nowadays there are more IoT platform using scripting language as below.

I wanted to make scripting-development-platform for embedded IoT.

I believe that most popular language for embedded engineers is C language so far. But above project are using Javascript style, Lua style and Python style, there is no C style scripting platform. So, I decided to make C language style development-platform for embedded IoT. I like to call it WizFi250-CSI(C Scripting Interpreter).

I used WizFi250 as hardware and picoc as a scripting engine.

WizFi250-CSI already have C scripting engine, so you don't need to install any software to compile(or run) your code. All you need is just a serial terminal program like putty or teraterm.

Then, with this WizFi250-CSI and terminal program, you can write and run "Hello world" application in a 30 seconds.

Anyway, I beleive that WizFi250-CSI would be a good for rapid-prototyping, DIY, IoT-startup or students.

  • Connect WizFi250-CSI to your computer by USB. (If needed, install the serial driver.)
  • You don't need to any specific sowtware. (Compiler, IDE, Linker, Flash-uploader, ....)
  • Just open a serial terminal program and start your application using C language

Step 1: WizFi250-CSI Block Diagram

Picture of WizFi250-CSI Block Diagram

Here is a internal block diagram of WizFi250-CSI.

Step 2: WizFi250-CSI.h

All functions, structure and static values are defined in "WizFi250-CSI.h".

So, your script file needs only to include "WizFi250-CSI.h".

You don't need include another header files like stdio.h, string.h or socket.h.

By referring to this "WizFi250-CSI.h", you can write your own application(C script file).

Most general functions are based on "C standard library".

int atoi(char *);
int atol(char *);
int printf(char *, ...);
int scanf(char *, ...);
void *memcpy(void *,void *,int);
int strlen(char *);
..........................................

Socket functions are based on BSD sockets API.

int socket(int, int, int);
int connect(int, struct sockaddr *, unsigned long);
int recv(int, void *, unsigned long, int);
int recvfrom(int, void *, unsigned long, int, struct sockaddr *, unsigned long *);
int send(int, void *, unsigned long, int);        
..........................................

Most hardware-functions of WizFi250 are based on Arduino style.

void pinMode(int gpio, int type);
void pinOut(int gpio, int value);
int pinIn(int);
int analogRead(int);
void delay_ms(unsigned long milliseconds);
..........................................

Please, refer to the below full "WizFi250-CSI.h".

<p>/*<br> * This file is part of the WizFi250-CSI(C Script Interpreter) project
 * By referring to this header file, you can write a C-Script-file of WizFi250-CSI.
 * 
 * This is published under the "New BSD License".
 * <a href="http://www.opensource.org/licenses/bsd-license.php" rel="nofollow"> http://www.opensource.org/licenses/bsd-license.ph...</a>
 *
 * Copyright (C) 2015 Steve Kim (ssekim@gmail.com)
 *
 * The WizFi250-CSI is based on picoc project.
 * <a href="https://github.com/zsaleeba/picoc" rel="nofollow"> http://www.opensource.org/licenses/bsd-license.ph...</a>
 * Copyright (c) 2009-2011, Zik Saleeba
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are 
 * met:
 * 
 *     * Redistributions of source code must retain the above copyright 
 *       notice, this list of conditions and the following disclaimer.
 *       
 *     * Redistributions in binary form must reproduce the above copyright 
 *       notice, this list of conditions and the following disclaimer in 
 *       the documentation and/or other materials provided with the 
 *       distribution.
 *       
 *     * Neither the name of the Zik Saleeba nor the names of its 
 *       contributors may be used to endorse or promote products derived 
 *       from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
*</p><p>/ WizFi250-CSI(C Script Interpreter) Header File for All</p><p>// Based on "ctype.h" of C standard library
int isalnum(int);
int isalpha(int);
int isblank(int);
int iscntrl(int);
int isdigit(int);
int isgraph(int);
int islower(int);
int isprint(int);
int ispunct(int);
int isspace(int);
int isupper(int);
int isxdigit(int);
int tolower(int);
int toupper(int);</p><p>// Based on "stdbool.h" of C standard library
typedef int bool;
#define true 1
#define false 0</p><p>// Based on "stdio.h"
int puts(char *);
char *gets(char *);
int getchar();
int printf(char *, ...);
int sprintf(char *, char *, ...);
int snprintf(char *, int, char *, ...);
int scanf(char *, ...);
int sscanf(char *, char *, ...);
int vprintf(char *, va_list);
int vsprintf(char *, char *, va_list);
int vsnprintf(char *, int, char *, va_list);
int vscanf(char *, va_list);
int vsscanf(char *, char *, va_list);</p><p>// Based on "stdlib.h" of C standard library
#define NULL 0</p><p>int atoi(char *);
int atol(char *);
int strtol(char *,char **,int);
int strtoul(char *,char **,int);
void *malloc(int);
void *calloc(int,int);
void *realloc(void *,int);
void free(void *);
int rand();
void srand(int);
void abort();
void exit(int);
char *getenv(char *);
int abs(int);
int labs(int);</p><p>// Based on "string.h" of C standard library
void *memcpy(void *,void *,int);
void *memmove(void *,void *,int);
void *memchr(char *,int,int);
int memcmp(void *,void *,int);
void *memset(void *,int,int);
char *strcat(char *,char *);
char *strncat(char *,char *,int);
char *strchr(char *,int);
char *strrchr(char *,int);
int strcmp(char *,char *);
int strncmp(char *,char *,int);
int strcoll(char *,char *);
char *strcpy(char *,char *);
char *strncpy(char *,char *,int);
char *strerror(int);
int strlen(char *);
int strspn(char *,char *);
int strcspn(char *,char *);
char *strpbrk(char *,char *);
char *strstr(char *,char *);
char *strtok(char *,char *);
int strxfrm(char *,char *,int);</p><p>// Based on "time.h" of C standard library
typedef int time_t;
typedef int clock_t;</p><p>struct tm
{
  int	tm_sec;
  int	tm_min;
  int	tm_hour;
  int	tm_mday;
  int	tm_mon;
  int	tm_year;
  int	tm_wday;
  int	tm_yday;
  int	tm_isdst;
};</p><p>char *asctime(struct tm *);
char *ctime(int *);
struct tm *gmtime(int *);
struct tm *localtime(int *);
int mktime(struct tm *ptm);
int strftime(char *, int, char *, struct tm *);</p><p>// Based on "sockets.h" of lwIP
#define AF_UNSPEC_networkLibrary       0
#define AF_INET_networkLibrary         2
#define SOCK_STREAM_networkLibrary     1
#define SOCK_DGRAM_networkLibrary      2
#define SOCK_RAW_networkLibrary        3
#define IPPROTO_IP_networkLibrary      0
#define IPPROTO_TCP_networkLibrary     6
#define IPPROTO_UDP_networkLibrary     17</p><p>#define O_NONBLOCK_networkLibrary      1
#define F_GETFL_networkLibrary         3
#define F_SETFL_networkLibrary         4</p><p>#define EAGAIN_networkLibrary          11</p><p>#define MSG_PEEK_networkLibrary        0x01
#define MSG_DONTWAIT_networkLibrary    0x08</p><p>struct in_addr {
	unsigned long s_addr;
};</p><p>struct sockaddr_in {
	unsigned char sin_len;
	unsigned char sin_family;
	unsigned short sin_port;
	struct in_addr sin_addr;
	char sin_zero[8];
} sockaddr_in;</p><p>typedef struct fd_set {
	unsigned char fd_bits [(8+7)/8];
} fd_set;</p><p>int accept(int, struct sockaddr *, unsigned long *);
int bind(int, struct sockaddr *, unsigned long);
int shutdown(int, int);
int getpeername(int, struct sockaddr *, unsigned long *);
int getsockname(int, struct sockaddr *, unsigned long *);
int getsockopt(int, int, int, void *, unsigned long *);
int setsockopt(int, int, int, void *, unsigned long);
int close(int);
int connect(int, struct sockaddr *, unsigned long);
int listen(int, int);
int recv(int, void *, unsigned long, int);
int recvfrom(int, void *, unsigned long, int, struct sockaddr *, unsigned long *);
int send(int, void *, unsigned long, int);        
int sendto(int, void *, unsigned long, int, struct sockaddr *, unsigned long);
int socket(int, int, int);
int select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
int fcntl(int, int, int);
int inet_addr(char *);
unsigned short htons(unsigned short);</p><p>// Regarding WizFi250-WiFi 
/** 
  * @brief  Joins a Wi-Fi network
  * @param  ssid       : A null terminated string containing the SSID name of the network to join
  * @param  auth_type  : Authentication type:
  *                       open       - Open Security
  *                       wep        - WEP Security
  *                       wpa2_tkip  - WPA2 Security using TKIP cipher
  *                       wpa2_aes   - WPA2 Security using AES cipher
  *                       wpa2       - WPA2 Security using AES and/or TKIP ciphers
  *                       wpa_aes    - WPA Security using AES cipher
  *                       wpa_tkip   - WPA Security using TKIP ciphers
  * @param  key        : Security key
  * @param  ip         : String of IP address string (if 0, DHCP will be applied.)
  * @param  netmask    : String of netamsk string
  * @param  gateway    : String of gateway address string
  * @return 0(Success), the others(Fail)
  */
int wifi_join(char* ssid, char* auth_type, char* key, char* ip, char* netmask, char* gateway);</p><p>/** 
  * @brief  Disassociates from a Wi-Fi network.
  * @return None
  */
int wifi_leave();</p><p>// Regarding WizFi250-Hardware
/**
  * @brief  Initialises a GPIO pin
  * @param  gpio : the gpio pin which should be initialised
  *                GPIO1, GPIO6, GPIO7, GPIO8, GPIO9, GPIO12(LED), GPIO13(LED), GPIO14
  * @param  type : A structure containing the required gpio configuration
                   0 : INPUT_PULL_UP              : Input with an internal pull-up resistor - use with devices that actively drive the signal low - e.g. button connected to ground
                   1 : INPUT_PULL_DOWN            : Input with an internal pull-down resistor - use with devices that actively drive the signal high - e.g. button connected to a power rail
                   2 : INPUT_HIGH_IMPEDANCE       : Input - must always be driven, either actively or by an external pullup resistor
                   3 : OUTPUT_PUSH_PULL           : Output actively driven high and actively driven low - must not be connected to other active outputs - e.g. LED output
                   4 : OUTPUT_OPEN_DRAIN_NO_PULL  : Output actively driven low but is high-impedance when set high - can be connected to other open-drain/open-collector outputs. Needs an external pull-up resistor
                   5 : OUTPUT_OPEN_DRAIN_PULL_UP  : Output actively driven low and is pulled high with an internal resistor when set high - can be connected to other open-drain/open-collector outputs.
  * @retval None
  */
void pinMode(int gpio, int type);</p><p>/**
  * @brief  Sets an output GPIO pin low or hign
  * @param  gpio  : the gpio pin which should be set
  *                 GPIO1, GPIO6, GPIO7, GPIO8, GPIO9, GPIO12, GPIO13, GPIO14
  * @param  value : 0(low) or 1(high)
  * @retval None
  */
void pinOut(int gpio, int value);</p><p>/**
  * @brief  Get the state of an input GPIO pin
  * @param  gpio  : the gpio pin which should be read
  *                 GPIO1, GPIO6, GPIO7, GPIO8, GPIO9, GPIO12, GPIO13, GPIO14
  * @retval 0(low) or 1(high)
  */
int pinIn(int);</p><p>/** 
  * @brief  Transmit data on a UART interface
  * @param  uart : the UART interface. UART1, UART2.
  * @param  data : pointer to the start of data
  * @param  size : number of bytes to transmit
  * @return None
  */
void uart_tx(int uart, unsigned char* data, int size);</p><p>/** 
  * @brief  Takes a single sample from an ADC interface
  * @param  adc : the interface which should be sampled 
                  AD1(Currently, WizFi250-CSI support one ADC)
  * @return a variable which will receive the sample (0 ~ 4095)
  */
int analogRead(int adc);</p><p>/** 
  * @brief  Receive data on a UART interface
  * @param  uart : the UART interface. UART1, UART2.
  * @param  data : pointer to the buffer which will store incoming data
  * @param  size : number of bytes to receive
  * @return number of received bytes
  */
int uart_rx(int uart, unsigned char* data, int size);</p><p>/** 
  * @brief  Sleep for a given period
  * @param  milliseconds : the time to sleep in milliseconds
  * @return None
  */
void delay_ms(unsigned long milliseconds);</p>

Step 3: Hello World Applcaiotn Using WizFi250-CSI

Picture of Hello World Applcaiotn Using WizFi250-CSI

Basically WizFi250-CSI provide a simple shell to write/read/delete "C Script File".

(Or, you can write "C Script File" using any editor.)

Using this shell, you can write(or upload) the "C Script File" via serial with terminal program.

Now, all you have to do is over.

To run the "C Script File", just restart WizFi250-CSI.

WizFi250-CSI executes your "C Script File" in real time because your "C Script File" was already saved in flash.

> c_write
After editing the script, you can stop it with Escape Key.
#include <WizFi250-CSI.h>
void main(void)
{
    printf("Hello world\r\n");
}
Are you sure to save the script file to flash? (Y/N)
>
> Started FreeRTOS v7.1.0
Initialising LwIP
WizFi250-CSI Version 0.1.0.0
If you don't want to launch default script file, please press any key in 3 seconds.
..............................
Hello world
>

Step 4: GPIO/ADC Applcaiotn Using WizFi250-CSI

Picture of GPIO/ADC Applcaiotn Using WizFi250-CSI

In this step, I will write a simple GPIO/ADC applcation.

As above described, most hardware-functions of WizFi250 are based on Arduino style.

For details of the functions, you can refer to the "WizFi250-CSI.h".

#include <WizFi250-CSI.h>

void main(void)

{ pinMode(GPIO12, 3); // 3 : OUTPUT_PUSH_PULL pinMode(GPIO13, 3); int i = 0; for (i=0; i<5; i++) { pinOut(GPIO12, 0); pinOut(GPIO13, 0); delay_ms(100); pinOut(GPIO12, 1); pinOut(GPIO13, 1); delay_ms(100); } int adc_value = analogRead(AD1); printf("Get ADC Value : (%d)\r\n", adc_value); }

Step 5: Simple Socket Applicaiotn Using WizFi250-CSI

In this step, I will write a simple UDP socket applcation in "Berkeley sockets Wiki".

http://en.wikipedia.org/wiki/Berkeley_sockets#Client_2

As you see, there is no big difference between "C script for WizFi250-CSI" and "original C source based on BSD socket API".

#include <WizFi250-CSI.h>

void main(void)
{
  int result = 0;
  int sock;
  struct sockaddr_in sa;
  int bytes_sent;
  char buffer[200];

  if ( result = wifi_join("WizFiDemoAP", "wpa2", "12345678", 0, 0, 0)!=0 )
  {
    printf("wifi_join error : %d", result);
    return;
  }

  strcpy(buffer, "Hello WizFi250!\r\n");

  sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if (-1 == sock)
  {
      printf("Error Creating Socket\r\n");
      return;
  }

  memset(&sa, 0, sizeof sa);

  sa.sin_family = AF_INET;
  sa.sin_addr.s_addr = inet_addr("192.168.3.255");
  sa.sin_port = htons(7000);

  bytes_sent = sendto(sock, buffer, strlen(buffer), 0,(struct sockaddr*)&sa, sizeof(sa));
  if (bytes_sent < 0) {
    printf("Error sending packet: \r\n");
    return;
  }
  close(sock);
  return;
}

Step 6: Epilogue

I wrote WizFi250-CSI with Broadcom WICED SDK.

http://community.broadcom.com/community/wiced-wifi

I want to open all source files, but I can't do that because of Broadcom license.

If you have an interest in WizFi250-CSI, please contact to me.

Comments

bxm (author)2015-08-31

Nice project, but I see no instructions on firmware upload nor firmware itself.

seamster (author)2015-06-02

Nice work! Thanks for this.

About This Instructable

592views

15favorites

License:

More by SteveK2:Serial-To-WiFi Using WizFi250-CSIWizFi250-CSI(C Script Interpreter) for rapid-prototyping, DIY, IoT-startup or students.
Add instructable to: