A look at various ESP specific APIs

 

Description

Some ESP-specific APIs related to deep sleep, RTC and flash memories are available in the ESP object. These may be useful, we have already looked at the restart one

Code

 

/*
ESP.restart() restarts the CPU.
ESP.getResetReason() returns a String containing the last reset reason in human readable format.
ESP.getFreeHeap() returns the free heap size.
ESP.getHeapFragmentation() returns the fragmentation metric (0% is clean, more than ~50% is not harmless)
ESP.getMaxFreeBlockSize() returns the maximum allocatable ram block regarding heap fragmentation
ESP.getChipId() returns the ESP8266 chip ID as a 32-bit integer.
ESP.getCoreVersion() returns a String containing the core version.
ESP.getSdkVersion() returns the SDK version as a char.
ESP.getCpuFreqMHz() returns the CPU frequency in MHz as an unsigned 8-bit integer.
ESP.getSketchSize() returns the size of the current sketch as an unsigned 32-bit integer.
ESP.getFreeSketchSpace() returns the free sketch space as an unsigned 32-bit integer.
ESP.getSketchMD5() returns a lowercase String containing the MD5 of the current sketch.
ESP.getFlashChipId() returns the flash chip ID as a 32-bit integer.
ESP.getFlashChipSize() returns the flash chip size, in bytes, as seen by the SDK (may be less than actual size).
ESP.getFlashChipRealSize() returns the real chip size, in bytes, based on the flash chip ID.
ESP.getFlashChipSpeed(void) returns the flash chip frequency, in Hz.
ESP.getCycleCount() returns the cpu instruction cycle count since start as an unsigned 32-bit. This is useful for accurate timing of very short actions like bit banging.
ESP.getVcc() may be used to measure supply voltage. ESP needs to reconfigure the ADC at startup in order for this feature to be available. Add the following line to the top of your sketch to use
*/
 
void setup() {
 
Serial.begin(115200);
 
Serial.print("SDK version:");
Serial.println(ESP.getSdkVersion());
 
Serial.print("Core version:");
Serial.println(ESP.getCoreVersion());
 
Serial.print("CPU ferquency:");
Serial.println(ESP.getCpuFreqMHz());
 
Serial.print("Chip ID:");
Serial.println(ESP.getChipId());
 
Serial.print("Reset Reason:");
Serial.println(ESP.getResetReason());
 
Serial.print("Free heap:");
Serial.println(ESP.getFreeHeap());
 
}
 
void loop() {}

 

Output

Open the serial monitor

SDK version:2.2.1(cfd48f3)
Core version:2_4_2
CPU ferquency:80
Chip ID:1947093
Reset Reason:External System
Free heap:51880

A look at SHA-1 and ESP8266

Description

In cryptography, SHA-1 (Secure Hash Algorithm 1) is a cryptographic hash function which takes an input and produces a 160-bit (20-byte) hash value known as a message digest – typically rendered as a hexadecimal number, 40 digits long

Cipher detail
Digest sizes 160 bits
Block sizes 512 bits
Structure Merkle–Damgård construction
Rounds 80

These are examples of SHA-1 message digests in hexadecimal and in Base64 binary to ASCII text encoding.

SHA1("The quick brown fox jumps over the lazy dog")
gives hexadecimal: 2fd4e1c67a2d28fced849ee1bb76e7391b93eb12
gives Base64 binary to ASCII text encoding: L9ThxnotKPzthJ7hu3bnORuT6xI=

Even a small change in the message will, with overwhelming probability, result in many bits changing due to the avalanche effect. For example, changing dog to cog produces a hash with different values for 81 of the 160 bits:

SHA1("The quick brown fox jumps over the lazy cog")
gives hexadecimal: de9f2c7fd25e1b3afad3e85a0bd17d9b100db4b3
gives Base64 binary to ASCII text encoding: 3p8sf9JeGzr60+haC9F9mxANtLM=

The hash of the zero-length string is:

SHA1("")
gives hexadecimal: da39a3ee5e6b4b0d3255bfef95601890afd80709
gives Base64 binary to ASCII text encoding: 2jmj7l5rSw0yVb/vlWAYkK/YBwk=

more – https://en.wikipedia.org/wiki/SHA-1

Code

 

#include "Hash.h"
 
void setup()
{
Serial.begin(115200);
 
String result = sha1("testing sha1");
 
Serial.println();
Serial.print(result);
 
}
 
void loop() {}

 

Output

I saw the following in the serial monitor

47d4387d5b2dcf196e295d48219b2c535c023eea

Wemos Mini and ISL29125 RGB color light sensor

On this page we take a look at the ISL29125 RED, GREEN and BLUE color light sensor

The ISL29125 is a low power, high sensitivity, RED, GREEN and BLUE color light sensor (RGB) with an I2 C (SMBus compatible) interface. Its state-of-the-art photodiode array provides an accurate RGB spectral response and excellent light source to light source variation (LS2LS). The ISL29125 is designed to reject IR in light sources allowing the device to operate in environments from sunlight to dark rooms. The integrating ADC rejects 50Hz and 60Hz flicker caused by artificial light sources. A selectable range allows the user to optimize sensitivity suitable for the specific application.

In normal operation mode the device consumes 56µA, which reduces to 0.5µA in power-down mode. The ISL29125 supports hardware and software user programmable interrupt thresholds. The Interrupt persistency feature reduces false trigger notification. The device operates on supplies (VDD) from 2.25V to 3.63V, I2 C supply from 1.7V to 3.63V, and operating temperature across the -40°C to +85°C range.

FEATURES

  • 56µA operating current, 0.5µA shutdown current
  • Selectable range (Via I2C)
  • I2C (SMBus compatible) output
  • ADC resolution 16 bits
  • Programmable interrupt windows
  • Two optical sensitivity ranges
  • Range 0 = 5.7m lux to 375 lux
  • Range 1 = 0.152 lux to 10,000 lux
  • Operating power supply 2.25 to 3.63V
  • I2C power supply 1.7V to 3.63V
  • 6 Ld ODFN (1.65×1.65×0.7mm) package

 

Connection

 

Wemos Mini CJMCU-0401 module
3v3 Vcc
Gnd Gnd
D2 – SDA SDA
D1 – SCL SCL

Parts List

Here are the parts I used

Part name Link
Wemos Mini
ISL29125 module ISL29125 Red/Green/Blue RGB Color Light Sensor with IR Blocking Filter Module
Dupont cable Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire Dupont cablefor Arduino

 

Code

This example requires the https://github.com/sparkfun/SparkFun_ISL29125_Breakout_Arduino_Library

#include <Wire.h>
#include "SparkFunISL29125.h"
 
// Declare sensor object
SFE_ISL29125 RGB_sensor;
 
void setup()
{
  // Initialize serial communication
  Serial.begin(115200);
 
  // Initialize the ISL29125 with simple configuration so it starts sampling
  if (RGB_sensor.init())
  {
    Serial.println("Sensor Initialization Successful\n\r");
  }
}
 
// Read sensor values for each color and print them to serial monitor
void loop()
{
  // Read sensor values (16 bit integers)
  unsigned int red = RGB_sensor.readRed();
  unsigned int green = RGB_sensor.readGreen();
  unsigned int blue = RGB_sensor.readBlue();
 
  // Print out readings, change HEX to DEC if you prefer decimal output
  Serial.print("Red: "); Serial.println(red,HEX);
  Serial.print("Green: "); Serial.println(green,HEX);
  Serial.print("Blue: "); Serial.println(blue,HEX);
  Serial.println();
  delay(2000);
}

 

Output

 

Links

 

ESP8266 and vibration sensor module

In this example we connect a vibration motor module to an ESP8266. This is the type of motor that you could find in a mobile phone which vibrates when you receive a text message for example

This is the module I bought


When the Logic level is HIGH, the motor is ON. When its LOW, the motor is OFF.

Connection

 

Wemos Mini Vibration motor
3v3 Vcc
Gnd Gnd
D4 In

Parts List

Here are the parts I used

Part name Link
Wemos Mini D1 mini – Mini NodeMcu 4M bytes Lua WIFI Internet of Things development board based ESP8266 by WeMos
Vibration Motor Module PWM Vibration Motor Module DC Motor Phone Vibrator for Arduino UNO R3 MEGA2560 Electronic DIY Kit
Dupont cable Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire Dupont cablefor Arduino

 

Code

This is a simple example which simply switches the motor on for 1 second and off for 1 second
int motorPin = D4;    // vibration motor digital pin D4
 
void setup()  
{
    pinMode(motorPin, OUTPUT );
}
 
void loop()  
{
    digitalWrite(motorPin, HIGH);
    delay(1000);
    digitalWrite(motorPin, LOW);
    delay(1000);
}

Links