<?php
namespace account;
require_once "verify_user_credentials.php";
function add_user_cookie($name)
{
   $hash = get_stored_hash($name);
   $expiration = time() + 30 * 24 * 60 * 60;
   $path = "/";
   setcookie("name", $name, $expiration, $path);
   setcookie("hash", $hash, $expiration, $path);
   setcookie("id", null, 0, $path);
   setcookie("code", null, 0, $path);
}
<?php
namespace account;
class Mail
{
   public function __construct($recipient, $sender, $subject)
   {
      $this->recipient = $recipient;
      $this->sender = $sender;
      $this->subject = $subject;
   }
   public function send()
   {
      $header = $this->build_header();
      $message = $this->build_message();
      return mail($this->recipient, $this->subject, $message, $header);
   }
   private function build_header()
   {
      $header = "From: " . $this->sender . "\r\n";
      $header .= "Content-Type: text/plain\r\n";
      return $header;
   }
   protected function build_message()
   {
      return "default message\n";
   }
}
<?php
namespace account;
require_once "Errors.php";
require_once "get_user_path.php";
require_once "user_exists.php";
$GLOBALS["HASH_FILE_NAME"] = "hash";
function verify_user_credentials($name, $password=null, $hash=null)
{
   $errors = new Errors();
   if (!user_exists($name))
   {
      $errors->add("username not found (note: usernames are case-sensitive)");
   }
   else if (!is_null($password))
   {
      if (!match_password_to_hash($name, $password))
      {
         $errors->add("password doesn't match username");
      }
   }
   else
   {
      if ($hash != get_stored_hash($name))
      {
         $errors->add("invalid or missing hash in cookie");
      }
   }
   return $errors;
}
function match_password_to_hash($username, $password)
{
   return match_to_hash($password, get_stored_hash($username));
}
function get_stored_hash($username)
{
   $path = build_user_path($username) . $GLOBALS["HASH_FILE_NAME"];
   return trim(file_get_contents($path));
}
function match_to_hash($key, $hash)
{
   return $hash == crypt($key, $hash);
}
<?php
namespace account;
class User
{
   function __construct(
      $name, $password=null, $repeated_password=null, $submitted_hash=null,
      $email_address=null)
   {
      $this->name = $name;
      $this->password = $password;
      $this->repeated_password = $repeated_password;
      $this->submitted_hash = $submitted_hash;
      $this->email_address = $email_address;
   }
}
<?php
namespace account;
require_once "Errors.php";
require_once "user_exists.php";
require_once "get_user_path.php";
require_once "Password_Mail.php";
require_once "add_user_account.php";
$GLOBALS["EMAIL_ADDRESS_FILE_NAME"] = "email";
$GLOBALS["GENERATED_PASSWORD_LENGTH"] = 10;
submit_reset_password_request();
function submit_reset_password_request()
{
   $name = $_GET["name"];
   $email_address = $_GET["email"];
   $errors = new Errors();
   if (!user_exists($name))
   {
      $errors->add("username not found");
   }
   else if (!match_to_existing($name, $email_address))
   {
      $errors->add("submitted address doesn't match account address");
   }
   else
   {
      store_password(build_user_path($name), email_password($email_address));
   }
   echo $errors;
}
function match_to_existing($name, $email_address)
{
   $path = build_user_path($name) . $GLOBALS["EMAIL_ADDRESS_FILE_NAME"];
   if (file_exists($path))
   {
      return $email_address == trim(file_get_contents($path));
   }
   return false;
}
function email_password($email_address)
{
   $password = generate_password();
   $mail = new Password_Mail($email_address, $password);
   $mail->send();
   return $password;
}
function generate_password()
{
   $length = $GLOBALS["GENERATED_PASSWORD_LENGTH"];
   $set = generate_character_set();
   $password = "";
   for ($ii = 0; $ii < $length; $ii++)
   {
      $password .= $set[rand(0, strlen($set) - 1)];
   }
   return $password;
}
function generate_character_set()
{
   $set = "";
   for ($ii = 0; $ii <= 90 - 65; $ii++)
   {
      $set .= chr($ii + 65);
   }
   for ($ii = 0; $ii <= 122 - 97; $ii++)
   {
      $set .= chr($ii + 97);
   }
   for ($ii = 0; $ii <= 9; $ii++)
   {
      $set .= $ii;
   }
   return $set;
}
function send_password($password)
{
   $email_address = $this->get_email_address();
}
<?php
namespace account;
require_once "Errors.php";
define("USERNAME_MIN_LENGTH", 5);
define("USERNAME_MAX_LENGTH", 15);
define("PASSWORD_MIN_LENGTH", 7);
define("PASSWORD_MAX_LENGTH", 15);
function validate_submission($username, $password, $email_address)
{
   $errors = new Errors();
   validate_username($username, $errors);
   validate_password($password, $errors);
   validate_email_address($email_address, $errors);
   return $errors;
}
function validate_username($username, $errors)
{
   if (user_exists($username))
   {
      $errors->add("username taken");
   }
   $length = strlen($username);
   if ($length < USERNAME_MIN_LENGTH)
   {
      $errors->add("username too short");
   }
   if ($length > USERNAME_MAX_LENGTH)
   {
      $errors->add("username too long");
   }
   if (!validate_username_characters($username))
   {
      $errors->add("username contains invalid characters");
   }
}
function validate_username_characters($username)
{
   return !preg_match("/[^0-9a-zA-Z]/", $username);
}
function validate_password($password, $errors)
{
   if ($password[0] != $password[1])
   {
      $errors->add("submitted passwords don't match");
   }
   $length = strlen($password[0]);
   if ($length < PASSWORD_MIN_LENGTH)
   {
      $errors->add("password too short");
   }
   if ($length > PASSWORD_MAX_LENGTH)
   {
      $errors->add("password too long");
   }
}
function validate_email_address($email_address, $errors)
{
   if (!preg_match("/.+@.+\..+/", $email_address))
   {
      $errors->add("invalid email address format");
   }
}
18.97.9.170
18.97.9.170
18.97.9.170
 
January 23, 2021

I wanted to document this chat-controlled robot I made for Babycastles' LOLCAM📸 that accepts a predefined set of commands like a character in an RPG party 〰 commands like walk, spin, bash, drill. It can also understand donut, worm, ring, wheels, and more. The signal for each command is transmitted as a 24-bit value over infrared using two Arduinos, one with an infrared LED, and the other with an infrared receiver. I built the transmitter circuit, and the receiver was built into the board that came with the mBot robot kit. The infrared library IRLib2 was used to transmit and receive the data as a 24-bit value.


fig. 1.1: the LEDs don't have much to do with this post!

I wanted to control the robot the way the infrared remote that came with the mBot controlled it, but the difference would be that since we would be getting input from the computer, it would be like having a remote with an unlimited amount of buttons. The way the remote works is each button press sends a 24-bit value to the robot over infrared. Inspired by Game Boy Advance registers and tracker commands, I started thinking that if we packed multiple parameters into the 24 bits, it would allow a custom move to be sent each time, so I wrote transmitter and receiver code to process commands that looked like this:

bit
name
description
00
time
multiply by 64 to get duration of command in ms
01
02
03
04
left
multiply by 16 to get left motor power
05
06
07
08
right
multiply by 16 to get right motor power
09
10
11
12
left sign
0 = left wheel backward, 1 = left wheel forward
13
right sign
0 = right wheel forward, 1 = right wheel backward
14
robot id
0 = send to player one, 1 = send to player two
15
flip
negate motor signs when repeating command
16
repeats
number of times to repeat command
17
18
19
delay
multiply by 128 to get time between repeats in ms
20
21
22
23
swap
swap the motor power values on repeat
fig 1.2: tightly stuffed bits

The first command I was able to send with this method that seemed interesting was one that made the mBot do a wheelie.

$ ./send_command.py 15 12 15 1 0 0 0 7 0 1
sending 0xff871fcf...


fig 1.3: sick wheels

A side effect of sending the signal this way is any button on any infrared remote will cause the robot to do something. The star command was actually reverse engineered from looking at the code a random remote button sent. For the robot's debut, it ended up with 15 preset commands (that number is in stonks 📈). I posted a highlights video on social media of how the chat controls turned out.

This idea was inspired by a remote frog tank LED project I made for Ribbit's Frog World which had a similar concept: press a button, and in a remote location where 🐸 and 🐠 live, an LED would turn on.


fig 2.1: saying hi to froggo remotely using an LED

😇 The transmitter and receiver Arduino programs are available to be copied and modified 😇