Published

LinkIt Smart WiFi Web Server

A WiFi Server is like an IoT but it is only connected to your local WiFi network. The task is to turn on or off GPIO1 on the LinkIt Smart.

IntermediateProtip1 hour88
LinkIt Smart WiFi Web Server

Things used in this project

Hardware components

LinkIt Smart 7688 Duo
MediaTek Labs LinkIt Smart 7688 Duo
×1

Software apps and online services

OpenWRT PHP5
See text for installation guidelines.
OpenWRT uhttpd
See text for installation guidelines.

Story

Read more

Code

LinkIt GPIO Control.

PHP
PHP code to control the LinkIt Smart 7688 GPIOs over a local WiFi network.
Used with mrra_gpio.run.
See text for installation guidelines.
Run by typing "http://mylinkit.local/php/ctrlGPIO.php" in your web-browser.
<?php
  // Save default GPIO (=LED)
  $gpioFile='gpio.dat';
  if (!file_exists($gpioFile)) file_put_contents($gpioFile,"LED");

  // Set GPIO if provided else used saved version
  if (isset($_GET['gpio'])) {
    $GPIO=$_GET['gpio'];
    file_put_contents($gpioFile,$GPIO);
  } else {
    $GPIO=file_get_contents($gpioFile);
  }
?>

<!-- Paint HTML -->
<html>
  <body>
    <font size='32' face='Arial'> GPIO Controller<br><br>
    <form action="ctrlGPIO.php" method="get">
      <font face="monospace" size="10">
      <input type="radio" name="gpio" value= "0" <?php echo ($GPIO== "0")?'checked':'' ?>>GPIO 0
      <input type="radio" name="gpio" value= "1" <?php echo ($GPIO== "1")?'checked':'' ?>>GPIO 1
      <input type="radio" name="gpio" value= "2" <?php echo ($GPIO== "2")?'checked':'' ?>>GPIO 2
      <input type="radio" name="gpio" value= "3" <?php echo ($GPIO== "3")?'checked':'' ?>>GPIO 3
      <input type="radio" name="gpio" value= "4" <?php echo ($GPIO== "4")?'checked':'' ?>>GPIO 4
      <input type="radio" name="gpio" value= "5" <?php echo ($GPIO== "5")?'checked':'' ?>>GPIO 5<br>
      <input type="radio" name="gpio" value= "6" <?php echo ($GPIO== "6")?'checked':'' ?>>GPIO 6
      <input type="radio" name="gpio" value="12" <?php echo ($GPIO=="12")?'checked':'' ?>>GPIO12
      <input type="radio" name="gpio" value="13" <?php echo ($GPIO=="13")?'checked':'' ?>>GPIO13
      <input type="radio" name="gpio" value="14" <?php echo ($GPIO=="14")?'checked':'' ?>>GPIO14
      <input type="radio" name="gpio" value="15" <?php echo ($GPIO=="15")?'checked':'' ?>>GPIO15
      <input type="radio" name="gpio" value="16" <?php echo ($GPIO=="16")?'checked':'' ?>>GPIO16<br>
      <input type="radio" name="gpio" value="17" <?php echo ($GPIO=="17")?'checked':'' ?>>GPIO17
      <input type="radio" name="gpio" value="18" <?php echo ($GPIO=="18")?'checked':'' ?>>GPIO18
      <input type="radio" name="gpio" value="19" <?php echo ($GPIO=="19")?'checked':'' ?>>GPIO19
      <input type="radio" name="gpio" value="20" <?php echo ($GPIO=="20")?'checked':'' ?>>GPIO20
      <input type="radio" name="gpio" value="21" <?php echo ($GPIO=="21")?'checked':'' ?>>GPIO21
      <input type="radio" name="gpio" value="37" <?php echo ($GPIO=="37")?'checked':'' ?>>GPIO37<br>
      <input type="radio" name="gpio" value="43" <?php echo ($GPIO=="43")?'checked':'' ?>>GPIO43
      <input type="radio" name="gpio" value="44" <?php echo ($GPIO=="44")?'checked':'' ?>>GPIO44
      <input type="radio" name="gpio" value="45" <?php echo ($GPIO=="45")?'checked':'' ?>>GPIO45
      <input type="radio" name="gpio" value="46" <?php echo ($GPIO=="46")?'checked':'' ?>>GPIO46
      <input type="radio" name="gpio" value="LED" <?php echo ($GPIO=="LED")?'checked':'' ?>>LED<br>
      <br>
      <?php
        if (isset($_GET['on'])) {
          echo '<input type="submit" name="on"  value="on"  style="color:red; height:150px; width:250px; font-size:72px">';
        } else {
          echo '<input type="submit" name="on"  value="on"  style="color:blue; height:150px; width:250px; font-size:72px">';
        }
        if (isset($_GET['off'])) {
          echo '<input type="submit" name="off" value="off" style="color:red; height:150px; width:250px; font-size:72px">';
        } else {
          echo '<input type="submit" name="off" value="off" style="color:blue; height:150px; width:250px; font-size:72px">';
        }
        if (isset($_GET['read'])) {
          echo '<input type="submit" name="read" value="read" style="color:red; height:150px; width:250px; font-size:72px">';
        } else {
          echo '<input type="submit" name="read" value="read" style="color:blue; height:150px; width:250px; font-size:72px">';
        }
        if (isset($_GET['end'])) {
          echo '<input type="submit" name="end" value="end" style="color:red; height:150px; width:250px; font-size:72px">';
        } else {
          echo '<input type="submit" name="end" value="end" style="color:blue; height:150px; width:250px; font-size:72px">';
        }
      ?>
      <br>
      <br>           
    </form>
  </body>
</html>

<?php
  // Set GPIO if provided
  if (isset($_GET['gpio'])) {
    $GPIO=$_GET['gpio'];
    file_put_contents($gpioFile,$GPIO);
  } else {
    $GPIO=file_get_contents($gpioFile);
  }

  // Action GET
  if (isset($_GET['on'])) {
    onFunc();
  } elseif (isset($_GET['off'])) {
    offFunc();
  } elseif (isset($_GET['read'])) {
    readFunc();
  } elseif (isset($_GET['end'])) {
    endFunc();
  } else {
    startFunc();
  }

  /* Functions */
  function onFunc() {
    global $GPIO;

    // Turn on GPIO
    if ($GPIO=="LED") {
      $OUTPUT=shell_exec("./mraa_gpio.run -gpio 44 -dir out -val 0");
    } else {
      $OUTPUT=shell_exec("./mraa_gpio.run -gpio ".$GPIO." -dir out -val 1");
    }
    echo $OUTPUT;
  }
  function offFunc() {
    global $GPIO;

    // Turn off GPIO
    if ($GPIO=="LED") {
      $OUTPUT=shell_exec("./mraa_gpio.run -gpio 44 -dir out -val 1");
    } else {
      $OUTPUT=shell_exec("./mraa_gpio.run -gpio ".$GPIO." -dir out -val 0");
    }
    echo $OUTPUT;
  }
  function readFunc() {
    global $GPIO;

    // Read GPIO
    if ($GPIO=="LED") {
      $OUTPUT=shell_exec("./mraa_gpio.run -gpio 44 -dir in");
    } else {
      $OUTPUT=shell_exec("./mraa_gpio.run -gpio ".$GPIO." -dir in");
    }
    echo $OUTPUT;
  }
  function endFunc() {
    global $GPIO;

    // Remove GPIO
    if ($GPIO=="LED") {
      $OUTPUT=shell_exec("./mraa_gpio.run -gpio 44 -dir close");
    } else {
      $OUTPUT=shell_exec("./mraa_gpio.run -gpio ".$GPIO." -dir close");
    }
    echo $OUTPUT;
  }
  function startFunc() {
    global $GPIO;

    echo "Ready to use GPIO $GPIO";
  }
?>

mraa_gpio.c

C/C++
Source code for mraa_gpio.run.
Used with ctrlGPIO.php to control the LinkIt Smart 7688.
Refer to text for installation guidelines.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <mraa.h>

int main(int argc, char** argv)
{
  // Pin No GPIO No  Function
  //     P1      43  Eth LED
  //     P8      20  UART TX2, PWM2
  //     P9      21  UART RX2, PWM3
  //    P10       2  I2S WS
  //    P11       3  I2S CLK
  //    P12       0  I2S SDI
  //    P13       1  I2S SDO
  //    P14      37  REF_CLK
  //    P15      44  WiFi LED
  //    P16      46  UART RX1
  //    P17      45  UART TX1
  //    P18      13  UART RX0
  //    P19      12  UART TX0
  //    P20       5  I2C SDA
  //    P21       4  I2C SCL
  //    P25       6  SPI CS
  //    P26      18  PWM0
  //    P27      19  PWM1
  //    P28      17  SPIS MOSI
  //    P29      16  SPIS MISO
  //    P30      15  SPIS SCK
  //    P31      14  SPIS CS

  bool gpioFlag=false;
  bool dirFlag=false;
  bool valFlag=false;
  bool helpFlag=false;
  int gpio=-1;
  int pin=-1;
  char dir[16]="";
  int val=-1;

  // Get parameters
  {
    long k;
    double f1;
    for (k=1;k<argc;k++) {
      if (strcmp(argv[k],"-pin")==0) {
        if (k+1<argc) sscanf(argv[k+1],"%lf",&f1);
        gpio=-1;
        pin=(int)f1;
        if (pin==1) gpio=43;
        if (pin==8) gpio=20;
        if (pin==9) gpio=21;
        if (pin==10) gpio=2;
        if (pin==11) gpio=3;
        if (pin==12) gpio=0;
        if (pin==13) gpio=1;
        if (pin==14) gpio=37;
        if (pin==15) gpio=44;
        if (pin==16) gpio=16;
        if (pin==17) gpio=45;
        if (pin==18) gpio=13;
        if (pin==19) gpio=12;
        if (pin==20) gpio=5;
        if (pin==21) gpio=4;
        if (pin==25) gpio=6;
        if (pin==26) gpio=18;
        if (pin==27) gpio=19;
        if (pin==28) gpio=17;
        if (pin==29) gpio=16;
        if (pin==30) gpio=15;
        if (pin==31) gpio=14;
        if (gpio!=-1) gpioFlag=true; 
      }
      if (strcmp(argv[k],"-gpio")==0) {
        gpioFlag=false;
        if (k+1<argc) sscanf(argv[k+1],"%lf",&f1);
        gpio=(int)f1;
        if ((gpio>=0)&&(gpio<=6)) {
          gpioFlag=true;
        } else if ((gpio>=12)&&(gpio<=21)) {
          gpioFlag=true;
        } else if (gpio==37) {
          gpioFlag=true;
        } else if ((gpio>=43)&&(gpio<=46)) {
          gpioFlag=true;
        }
        if (gpioFlag) gpio=(int)f1;
      }
      if (strcmp(argv[k],"-dir")==0) {
        dirFlag=true;
        if (k+1<argc) strcpy(dir,argv[k+1]);
      }
      if (strcmp(argv[k],"-val")==0) {
        valFlag=true;
        if (k+1<argc) sscanf(argv[k+1],"%lf",&f1);
        val=(int)f1;
        if (val<0) val=0;
        if (val>1) val=1;
      }
      if (strcmp(argv[k],"-help")==0) {
        helpFlag=true;
      }
    }
  }

  // Do it
  if ((gpioFlag)&&(dirFlag)&&(!helpFlag)) {

    mraa_gpio_context mraa_gpio=NULL;
    mraa_result_t mraa_res;

    mraa_gpio=mraa_gpio_init(gpio);
    if (mraa_gpio==NULL) {
      printf("gpio%d error\n",gpio);
      return(-1);
    }
    if (strcmp(dir,"in")==0) {
      mraa_gpio_dir(mraa_gpio,MRAA_GPIO_IN);
      printf("gpio%d read is %d\n",gpio,mraa_gpio_read(mraa_gpio));
      return (0);
    } else if (strcmp(dir,"close")==0) {
      mraa_res=mraa_gpio_close(mraa_gpio);
      if (mraa_res==MRAA_SUCCESS) {
        printf("gpio%d closed\n",gpio);
        return(0);
      } else {
        printf("gpio%d closed with error\n",gpio);
        return(-1);
      }

    } else if ((valFlag)&&(strcmp(dir,"out")==0)) {
      mraa_gpio_dir(mraa_gpio,MRAA_GPIO_OUT);
      mraa_gpio_write(mraa_gpio,val);
      printf("gpio%d write is %d\n",gpio,val);
      return(0);
    } else {
      return(-1);
    }

  } else {

    printf("mraa gpio - Written by Alan Cooper (agp.cooper@gmail.com)\n");
    printf("usage:    ./mraa_gpio.run -gpio n -pin n -dir [in|out|close] -val [0|1]\n");
    printf("options:  -gpio n              Set gpio number\n");
    printf("          -pin n               Set gpio number by using pin number\n");
    printf("          -dir [in|out|close]  Set gpio as input, output or close the gpio\n");
    printf("          -val [0|1]           Set gpio output value\n");

  }

}

mraa_gpio.run

C/C++
The compiled version of mraa_gpio.c.
Refer to text for installation guidelines.
The executable is provided as not everyone knows how to compile C code for LinkIt Smart 7688.
No preview (download only).

My LinkIt Smart 7688 C compiler make file.

Makefile
My be useful if you want to compile mraa_gpio.c yourself.
It assumes you have "toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2" and the mraa library installed.
# Build target executable when user executes "make"
TARGET=mraa_gpio

export STAGING_DIR=$(HOME)/LinkIt-cc
CC=$(HOME)/LinkIt-cc/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/bin/mipsel-openwrt-linux-uclibc-gcc
CFLAGS =-I.
CFLAGS+=-I$(HOME)/LinkIt-cc/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/usr/include
CFLAGS+=-I$(HOME)/LinkIt-cc/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/include
LDFLAGS =-L.
LDFLAGS+=-L$(HOME)/LinkIt-cc/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/usr/lib
LDFLAGS+=-L$(HOME)/LinkIt-cc/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/lib
LDFLAGS+=-lmraa -pthread -ldl

$(TARGET): $(TARGET).c
	$(CC) $(CFLAGS) -o $(TARGET).run $(TARGET).c $(LDFLAGS)

# remove object files and executable when user executes "make clean"
clean:
	rm $(TARGET).run

Credits

Comments

Add projectSign up / Login