Monday, August 24, 2020

Wio Terminal: Read Light Sensor and display on LCD graphically


The light sensor on Wio Terminal uses Analog interface and you can simply read the surrounding Light sensor values via reading its pin.

This exercise run on Wio Terminal (using Arduino framework) to read the built-in light sensor in 10ms interval, and display on the LCD screen graphically.

Also, the time to perform the job is displayed as info for reference.

Wio_LightSensor.ino

#include <SPI.h>
#include <TFT_eSPI.h>

TFT_eSPI tft = TFT_eSPI();

#define FrameOx 5
#define FrameOy 5
#define FrameWidth  300
#define FrameHeight 200

#define Title   "Wio Terminal Light Sensor"
#define TitleX  FrameOx
#define TitleY  210
#define InfoX   FrameOx
#define InfoY   230

#define BUFFER_SIZE 300
#define MAX_VAL 200
int BufferSensorVal[BUFFER_SIZE];

int sampling;

// interval at which to sample light sensor (milliseconds)
const long interval = 10;
unsigned long previousMillis = 0;

void drawFrame(){
  tft.fillRect(FrameOx, FrameOy, FrameWidth, FrameHeight, TFT_DARKGREY);
  tft.drawRect(FrameOx-1, FrameOy-1, FrameWidth+2, FrameHeight+2, TFT_LIGHTGREY);
}

void setup() {
  pinMode(WIO_LIGHT, INPUT);
  
  tft.init();
  tft.setRotation(3);
  tft.fillScreen(TFT_BLACK);

  tft.drawString(Title,TitleX,TitleY);

  for(int i=0; i<BUFFER_SIZE;i++)
  {
    int v = map(i, 0, BUFFER_SIZE, 0, MAX_VAL);
    BufferSensorVal[i] = 0;
  }

  sampling = 0;

}

void loop() {
  unsigned long currentMillis = millis();
  unsigned long startMicros = micros();

  if (currentMillis - previousMillis >= interval) {

    if(sampling == 0){
      drawFrame();
    }
    
    previousMillis = currentMillis;

    //Read Light Sensor and map to value suitable for display
    int light = analogRead(WIO_LIGHT);
    int lightVal = map(light, 0, 1024, 0, MAX_VAL);
    BufferSensorVal[sampling] = lightVal;

    tft.drawLine(sampling+FrameOx, FrameOy+MAX_VAL, 
          sampling+FrameOx,FrameOy+MAX_VAL-lightVal, TFT_WHITE);
    
    sampling++;
    if(sampling == BUFFER_SIZE){
      sampling = 0;
    }

    //How long to perform the drawing, 
    //for information only,in microsecond
    String strInfo = String(micros()-startMicros);
    tft.drawString("     ", InfoX, InfoY);
    tft.drawString(strInfo, InfoX, InfoY);

  }
}



Tuesday, August 11, 2020

Python example to read OS info using os.uame()

The function os.uname() returns information identifying the current operating system. The return value is an object with five attributes:

  • sysname - operating system name
  • nodename - name of machine on network (implementation-defined)
  • release - operating system release
  • version - operating system version
  • machine - hardware identifier

example code: sysinfo.py

import os

uname = os.uname()
print(uname)
print("sysname: " + uname.sysname)
print("nodename: " + uname.nodename)
print("release: " + uname.release)
print("version: " + uname.version)
print("machine: " + uname.machine)

Run on Raspberry Pi

Monday, August 10, 2020

Thonny/esptool on Raspberry Pi, to develop ESP32 using MicroPython.

This video show how to install esptool on Raspberry Pi, and setup Thonny to run MicroPython on ESP32.

Install esptool:
$ pip3 install esptool

Download microPython firmware for SP32:
Visit https://micropython.org/download/esp32/.

Start Thonny and switch to Regular mode, close and restart Thonny to run in Regular mode.

Run > Select Interpreter
> Select MicroPython (ESP32) from th pull-download menu.
> Open the dialog for installing or upgrading MicroPython on your device
> Select Port and firmware

Press and Hold the on-board BOOT button untill download start.


Related: