Category: application

Controlling two BI2 with Blynk

Controlling more than one body interaction 2 boards is very easy.

At first go to project settings in the Blynk app, go to devices and add a new device.

Then create a new device and press Email. You will get an Email with the auth token.

Add slider widget for the motor and another ZEGBRA widget for controlling the LED. For LED select a new virtual pin, e.g. V10.

You can start with this ready made app:

It should look like this:

 

Then reuse  the code from this blog post.

Change the following:

  • fill-in the auth token which you got per Email
  • change the name of the virtual pin V0 (for the LED) to e.g. V10 (the same name as in the Blynk app)

 

That’s it!

 

Sending motion data to the Blynk app (part 2 of the Blynk tutorial)

In the first blog post we explained the basics of controlling the body interaction 2 (BI2) vibrator development board using the concept of  (virtual) pins. This time we want to send data from the BI2 board to the Blynk app. The BI2 has the MPU-9250 9DoF (9 Degrees of Freedom) IMU (Inertial Measurement Unit) sensor on board. This sensor is a combination of an accelerometer, gyroscope and magnetometer. Especially the accelerometer is important for motion detection. This could be used for controlling the vibrator as show with the body interaction 1 (BI1).

For measuring the motion data we use the asukiaa library. Please search and install the library in the Arduino library manager.

In the program code the library must be included and a MPU9250 sensor object must be defined. Finally we need several variables of the type float.

#include <MPU9250_asukiaaa.h>
MPU9250 mySensor;
float aX, aY, aZ, mDirection, pitch, roll, yaw;

In the setup part of the program we need to tell the MPU9250 how it is connected to the ESP8266 microcontroller. [The MPU9250 IMU is connected by the I2C bus to the ESP8266 microcontroller: the sda pin of the IMU is connected to pin 4, the scl pin to pin 5. The connection between MPU9250 and ESP8266 is managed with the standard Wire library.]

For using the accelerometer and magnetometer we have to initialize the sensor with a begiAccel() call to the IMU library.

Wire.begin(4, 5); //sda, scl
mySensor.setWire(&Wire);
mySensor.beginAccel();
mySensor.beginMag();

We have to tell the program how often data is sent to the app. Therefore we need an important concept in microcontroller programming:

Timer

With the help of the timer we can tell the microcontroller to do a given tasks again and again e.g. after 1000 microsecond. You cannot use the delay function to pass time as this would interrupt the important call to the Blynk.run(); function which is located in the loop part of the program.

First we have to define an object of type Timer.

BlynkTimer timer;

In the setup part we have to say how often what the timer has to do. in this example the timer will call the function myTimerEvent every 1000 microsecond.

timer.setInterval(1000L, myTimerEvent);

In the loop part of the program we have to call the timer to keep things going:

timer.run(); // Initiates BlynkTimer

Now we need the function myTimerEvent what has to be done every 1000 seconds.

void myTimerEvent()
{
  // here add was has to be done
}

First we have to update the sensors (accelUpdate, magUpdate). Then we read out the acceleration data in the X, Y and direction. You can already use this data but they are hard to catch. Therefore we can calculate the pitch, roll and yaw. These are angles from -180° to +180°. The calculation is complicated and I don’t understand it. But with the given formulas you get a very rough approximation which makes the data quite accessible.

void myTimerEvent() {
  mySensor.accelUpdate();
  aX = mySensor.accelX();
  aY = mySensor.accelY();
  aZ = mySensor.accelZ();

  // calculate pitch, roll, yaw (raw approximation)
  float pitch = 180 * atan (aX/sqrt(aY*aY + aZ*aZ))/M_PI;
  float roll = 180 * atan (aY/sqrt(aX*aX + aZ*aZ))/M_PI;
  float yaw = 180 * atan (aZ/sqrt(aX*aX + aZ*aZ))/M_PI;

  // read gyroscope update
  mySensor.magUpdate();
  mDirection = mySensor.magHorizDirection();
}

Finally we send the data back to the Blynk app. Now we use the virtual pins. For the variables pitch we  use virtual pin 2 (V2), for roll V3, for yaw V4 and for mDirection V5. We have to add the following line to the myTimerEvent function.

void myTimerEvent() {
  // send data to app via virtual ports, e.g. virtual pin V2 is set to pitch
  Blynk.virtualWrite(V2, pitch);
  Blynk.virtualWrite(V3, roll);
  Blynk.virtualWrite(V4, yaw);
  Blynk.virtualWrite(V5, mDirection);
}

Now the data are continously sent to the Blynk app. To visualize the data we add the widget SuperChart.

For each variable we have to define the input (virtual) pin. For pitch we use the virtual pin V2. In addition we define the color and style of the graph and more.

 

Finally the super graph shows us the date from the accelerometer which are updated every second.

First part of the tutorial (setup Arduino, setup Blynk, LED and motor control) is here

Here is the complete code:

 

/*************************************************************
bodyinteraction.org
sample program for reading MPU data, setting LED color and motor speed
*/
#define BLYNK_PRINT Serial
// include this library in the Arduino library manager
#include "FastLED.h"

// How many leds in your strip?
#define NUM_LEDS 1
// LED data pin is connected to pin?
#define DATA_PIN 14

// Define the array of leds
CRGB leds[NUM_LEDS];
int wave;

// include this library in the Arduino library manager
#include <MPU9250_asukiaaa.h>
MPU9250 mySensor;
float aX, aY, aZ, mDirection, pitch, roll, yaw;

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "Your Auth Token XXXXXXXXXX";

// Your WiFi credentials.
char ssid[] = "YOUR SSID   XXXXXXXXXXXXXX";
char pass[] = "YOUR Password XXXXXXXXXXXX";
BlynkTimer timer;

void myTimerEvent()
{
  // read acceleration data
  mySensor.accelUpdate();
  aX = mySensor.accelX();
  aY = mySensor.accelY();
  aZ = mySensor.accelZ();
  // read gyroscope update
  mySensor.magUpdate();
  mDirection = mySensor.magHorizDirection();
  // calculate pitch, roll, yaw (raw approximation)
  float pitch = 180 * atan (aX/sqrt(aY*aY + aZ*aZ))/M_PI;
  float roll = 180 * atan (aY/sqrt(aX*aX + aZ*aZ))/M_PI;
  float yaw = 180 * atan (aZ/sqrt(aX*aX + aZ*aZ))/M_PI;
  // send data to app via virtual ports, e.g. virtual pin V2 is set to pitch
  Blynk.virtualWrite(V2, pitch);
  Blynk.virtualWrite(V3, roll);
  Blynk.virtualWrite(V4, yaw);
  Blynk.virtualWrite(V5, mDirection);
}

BLYNK_WRITE(V0) // set RGB color values which are transmitted from the app as V0 (virtual pin 0)
{ 
  int i = param[0].asInt();
  int j = param[1].asInt();
  int k = param[2].asInt();
  leds[0].setRGB(j,i,k);
  FastLED.show();
}

void setup()
{
  Serial.begin(115200);
  FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);

  Wire.begin(4, 5); //sda, scl
  mySensor.setWire(&Wire);
  mySensor.beginAccel();
  mySensor.beginMag();

  Blynk.begin(auth, ssid, pass);
  timer.setInterval(1000L, myTimerEvent);
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
}

 

Please feel free to comment or write to jacardano@gmail.com

Programming the body interaction 2 (BI2) with Blynk part 1

This in an intro to using and programming the BI2 with the Blnyk app. Read here how to set up Arduino. For a more general basic intro (based on the body interaction 1 board) read here.

Pins

The communication between app and BI2 microcontroller is realized by pins. The idea is very easy: Each widget in the Blynk app is connected to a physical pin of the microcontroller. Every microcontroller has several pins where you can connect other electronic parts like a LED or a vibration motor. For each pin you have to configure if it is a output or input pin. Output pins are for controlling actuators, like LED, motor or display. Input pins are connected to sensors, like buttons, temperature sensors, acceleration sensor.  In addition each pin can be digital, analog or virtual.

Digital output pins can only set the actuator to on or off e.g. turning the LED on or off. Analog pins can set the actuator to a specific value in a given range. Usual this in done in the range [0..255] or [0..1023]. For a motor 0 will set the motor off, 50 may be make the motor move very slowly and 255 will be full speed. An analog output pin is sometimes called PWM. (PWM is a method to simulate an analog signal with a sequence of digital on/off signals.)

Digital input pins can read the position of a button (on/off). Analog input pins can read a value in a given range, e.g. the acceleration in the X-axis or the temperature.

So what you have to do to connect a widget to a pin? Just set the widget (e.g. on/off switch widget) to the pin you want to set on/off (e.g. a pin which is connected to a LED). That’s all. No programming required. All you need is this small program which must be uploaded to the microcontroller with the Arduino IDE.

The body interaction 2 use the ESP8266 microcontroller. There are 16 pins, all could be used as digital or analog, input or output. But only pin 12 and 13 are free to use (the rest if for internal communication). Pin 14 is connected to the LED WS2821B.

The Arduino sketch

The first 3 lines are for configuring Blynk and using two libraries. The 3 variables auth, ssid and pass are defined. (The variables are from thy type char (=character) and in this case it is not only one character but an array which you can see by the “[” and “]”. Here you have to add your AUTH token from the Blynk app, and SSID and password from your local WLAN/WIFI.

#define BLYNK_PRINT Serial 
#include <ESP8266WiFi.h> 
#include <BlynkSimpleEsp8266.h> 

char auth[] = "XXXXXXXXXXXXXXXXXXXXXXXXXXX"; 
char ssid[] = "XXXXXXXX"; 
char pass[] = "XXXXXXXX";

Each Arduino program consists of a setup and a loop procedure. The setup is called only one time when the microcontroller is started (or connected to a battery). It is used to initialize the microcontroller, in this case Blynk is started. The loop will be called indefinitely and all statements are executed in the given order. To get Blynk running you have to call Blynk again and again (“Blynk.run();”). According to the Blynk manual, you should not add a delay() function here, because this could disturb the communication between the app and the microcontroller.

void setup() { 
  Blynk.begin(auth, ssid, pass); 
} 

void loop() { 
  Blynk.run(); 
}

Virtual pins

So far communication is only possible with physical pins. But how can you exchange other information? Maybe you want to tell the microcontroller to “shut up immediately”,  or you want to play a given vibration pattern like a sinus curve. For this you can use “virtual pins”. (IMHO there is no reason to call this mean of data exchange “virtual” and it is has nothing to do with a pin. You can call it a variable or channel for data exchange.) The zeRGBa widget is a good example. The color of the LED is controlled by 3 values, the amount of red, green and blue color. This 3 values can be connected to one virtual pin (“V0”) and then they will be transmitted to the microcontroller. To change the color of the LED you have to program the microcontroller  to read out the amount of each color and set the LED to the appropriate value.

We will demonstrate virtual pins with the LED. The WS2821B LED is connected to pin 14, but you cannot control the LED directly by setting the pin to a given value. This is done by a library which controls the LED.

First we have to include the library, we use FastLED.

#include "FastLED.h"

Then  we have to tell how many LEDs we have (you can put several of them in a chain). The BI2 has only one on board (but you can add more).

#define NUM_LEDS 1 // number of LEDs

The you have to tell to which physical pin the LED is connected (14). Finally you have to set up a (instance of an) object “CRGB” for the LED where all relevant data is hidden.

#define DATA_PIN 14 // pin for LED 
CRGB leds[NUM_LEDS]; // define the array of leds

Now comes the more difficult part. The zeRGBa widget has 3 values (one for red, one for green, one for blue) and all are put in the virtual variable V0.

We have add a new function called “BLYNK_WRITE(V0)”. To get the first value we have to read out “param[0]”, for the second “param[1]” etc. We want to store this first value in a variable “i” of the type integer. To assure that param[0] is also from the type integer we add “.asInt()”. The value for red is put in variable i, green in j and blue in k.

BLYNK_WRITE(V0) {
  int i = param[0].asInt();
  int j = param[1].asInt(); 
  int k = param[2].asInt();
}

Now we have to tell the function BLYNK_WRITE what to do with the values i, j an k. This is done by using the method setRGB which is attached to the LED (which is number 0)

leds[0].setRGB(j,i,k);

Now we can make changes to other LEDs (if we have more than one). If you are ready you have to tell the LED to show the new color.

FastLED.show();

In addition a new statement has to be added to setup the LED within the setup part.

void setup() { 
  FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);
  [...]

Now we can put everything together the script will look like this:

/*************************************************************
Controling the body interaction 2 board with the Blynk app
*/

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// Auth Token infor the Blynk App.
char auth[] = "XXXXXXXXXXXXXXXXXXXXXXXXXXX";

// Your WiFi credentials.
char ssid[] = "XXXXXXXX";
char pass[] = "XXXXXXXX";

// Library for controlling the WS2821B (Neopixel) LED or LED strip
#include "FastLED.h"
#define NUM_LEDS 1 // number of LEDs
#define DATA_PIN 14 // pin for LED
CRGB leds[NUM_LEDS]; // define the array of leds

// This function set the LED color according to the selected RGB values in the app.
// RGB values are controlled in the app with zeRGBa widget
// values are stored in the virtual pin V0
// V0 consists of 3 values for Red, Green, Blue
BLYNK_WRITE(V0) // set LED RGB color values
{
  int i = param[0].asInt();
  int j = param[1].asInt();
  int k = param[2].asInt();
  leds[0].setRGB(j,i,k);
  FastLED.show();
}

void setup()
{
  // init LEDs
  FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);

  // connect to Blynk
  Blynk.begin(auth, ssid, pass);
}

void loop()
{
  Blynk.run();
}

Do you like this, do you need this, do you understand this? Tell me jacardano@gmail.com

Basic Node for the Internet of Sex Toys – part 3: software

This the third part of the tutorial which has the following parts:

part 1: Basic Node for the Internet of Sex Toys

part 2: Molding the Basic Node

part 3: Software for the Basic Node

For the basic node a simple software realizes all features like Mqtt communication, Web server, basic web user interface, reading data from the accelerometer. Please use the code at github and send request over github. Now the imported parts of the code are explained.

To communicate with the IOT Mqtt is used (read more here). This is a fast protocol for data transmission. Therefore we need a Mqtt server. You can install one on your local computer or use a cloud-based Mqtt server. We use the free CloudMqtt. The following variables must be initiated with the data  of your server. Please get your own account at CloudMqtt or use my server (but don’t spam it, please). Please remember: Transmission is not encrypted, everybody can read it.

const char* mqtt_server = "m12.cloudmqtt.com";
uint16_t mqtt_port = 15376;
const char* mqtt_user = "nvcuumkf";
const char* mqtt_password = "C-X6glwisHOP";

We have now  7 different modes. In each mode the basic node behaves different.

const int offMode = 0;
const int maxMode = 1;
const int sinusMode = 2;
const int motionMode = 3;
const int constantMode = 4;
const int listenMode = 5;
const int listenAndMotionMode = 6;

In off mode the basic node is off, in max mode the vibration is maximum. In sinus mode the vibration speed is altered according to a sinus curve.

Web user interface of the basic node

In motion mode the vibration changes according to the movement of the basic node. When moved fast the speed goes up, when moved slowly or movement stops, the speed goes down. In constant mode any vibration speed can be set to any strength. This feature is only available by Mqtt messages eg. from the IOT node-RED user interface. The listen mode is still experimental. In this mode the speed will be changed by OTHER basic nodes. Finally in the listenAndMotionMode the speed is changed by movements of the basic node and by other nodes. This feature was already available with the body interaction 1 development board as standard mode!

The basic node starts a web server (see image). A web page is generated which build up the user interface. There are buttons for every mode. In addition the speed and the battery power is displayed. This is done in this function:

void generateWebpage() {

The next lengthy procedure is this:

void mqttCallback(char* topic, byte* payload, unsigned int length) {

This is a call back function which is executed whenever a Mqtt message comes in. It parses the Mqtt message which is in the popular JSON format. The commands which are communicated within JSON are explained here. In principle there is a command for every mode, when the command “set mode to off” is send the mode is set to offMode.

In the setup() part of the code you will find a lot of lines like that:

httpServer.on("/MOTOR=MAX", []() {

They corresponds to the generateWebpage() function. When say the max button on the web page is pressed than the affiliated httpServer function is executed. So for every button on the webpage you need a corresponding httpServer function to implement the functionality. In this case (MOTOR=MAX) the mode is set to the constant speed maxMode.

Finally in the loop section of the code the following functions are implemented:

  • reading the accelerometer data
  • change the vibration motor speed according to the mode
  • generate a new JSON message which is send out via Mqtt
  • do the timing

Not mentioned is the OTA (over the air update) function, which is integrated in the code.

Node-RED

For controlling the toy via the internet you can use node-Red. You can find the code at github via this link.

The flow is explained here and here.

 

Smooth 3d printed vibrator form for silicone molding

shinybluemoreorlesscompleteWe started with printing sex toys (see here, here and here), then moved to printing mold forms for sex toys and finally we made sex toys which are partially silicone molded and partially printed. The best results were achieved with printing mold forms and fill them with silicone. But even there we have the problem that the surface isn’t really smooth and that it is hard to clean.

 

xtcTo overcome this problem we use a 3d print smoothing (XTC 3D) to smooth the surface. Application is very easy. It is like applying a transparent varnish. The results are impressive: The silicone gets a smooth shiny surface.

Shiny blue vibrator with vibration motor and air bubble in silicone formBut you can still see printer artefacts. To overcome this issue you could apply a thicker layer of XTC-3D. If you use a better printer than my daVinci 1.0 the “staircase effect” shouldn’t be a  problem at all. Another problem are tiny – sometimes quite large – air bubbles. To remove this air bubbles you need a vacuum chamber. So it is still not perfect, but it works and looks quite good…shinybluecomplete

Internet of (sex) things – part 4: Building a sex toy dashboard with Node-RED

In the fourth part of the tutorial we explain the development of a dashboard for our sex toy.

The series has 4 parts:

part 1: Exploring the internet of (sex) things

part 2: MQTT messages

part 3: Node-RED

part 4: Building a sex toy dashboard with Node-RED

The dashboard is used to visualize certain data eg. the speed of the vibration motor and the movements of the vibrator. In addition it can have some control elements eg. for changing the vibration pattern.

ui-all

The window above is called a tab. You can have multiple tabs. The Motor, Data, Status and Controls – windows are called groups. The Controls – group has buttons to set the motor mode. In addition there is a slider which will set the motor to a constant speed. And there is an on/off button for the LED.

You have to install the Node-RED dashboard. Therefore you need at least version 0.14 of Node-RED. At the time this text was written the standard Node-RED installation is version 0.13 which is not sufficient for the dashboard.

Therefore check your Node-RED version. If it is equal or better than 0.14, skip this step:

  • Download & unzip the latest version from github (eg. https://github.com/node-red/node-red/releases/tag/0.15.2).
  • Now change to newly created directory eg “node-red-0.15.2”
  • On Windows: Start a command shell in adminstration mode
  • Execute “npm install” to install Node-RED.

If your Node-RED version is 0.14 or better install the dashboard:

Some  remarks:

Let’s start and have a look at the new Node-RED interface: On the left side you will find the dashboard or user interface nodes. And on the right side there is new dashboard – tab. It contains all dashboard nodes which are used in the flow ordered hierarchical.

node-red-ui

But where is the dashboard? Just open your browser and go to one of the URLs:

http://localhost:1880/ui/#/0 or
http://127.0.0.1:1880/ui/#/0

How can I get all the flows? You can download all flows here: bi-ui-node-red. Unzip the file and you will get a text file. Open the text file in an editor. Select the text and copy it to the clipboard.

Now we will explain two flows in detail.

Now go the Node-RED window, open the menu (it is on the left top side), select Import -> Clipboard.import1

A new window will open where you can insert the text (CTRL+V). Then press the Import-button.

import2

nodemcu prototype breadboardThe Arduino sketch was updated for the dashboard. Please download the Arduino sketch from here: iost-part4-v12. Unzip the file. Compile and upload to your hardware (see part 1 of the tutorial).

 

In the first flow we will receive some vibration motor sex toy data which are sent by the MQTT protocol. We will display the data using a gauge and a graph element.

Let’ start with the MQTT input node. There is nothing new. Just connect to the MQTT server and subscribe the topic “BIoutTopic2” – which the sex toy uses to send out data.mqtt-in

Now add the function node “JSON” which will parse the incoming message and places the result in “payload”.

But we want to know the motor speed only. Therefore we need a function node which passes the vibration motor speed as payload and deletes all other data. Please use the following JavaScript code:

get-motor-speed-function-node

To display the speed we need to more nodes. The gauge node gauge-node displays the actual speed. Connect the gauge node with the function node. You can add the range – the minimum and maximum value (0 and 1023).motor-gauge

 

Now add a chart node chart-nodeand connect it with the function node, too.

 

Next we want the chart node and the gauge node to be together as shown in the next image.

motor

We have to make a group and put both nodes into the group. Have a look on the dashboard at the right side. Make a new group and move the gauge and chart node below the group called “Motor”.

dashboard-top

slider-nodeNow we explain the second flow. It is used to send commands to the sex toy. We will use a slider to control the speed. But there is one problem: the slider should display the actual speed of the vibration motor. Therefore we manipulate the slider. To display the actual motor speed we have to move the slider appropriate to the actual speed. The slider will be part of the control group:

controls

Now get the slider node and edit the node as follows:

slider-motor-speed

Then connect the node with the “get motor speed” function which was introduced in the first flow.

construct-json-motor-speed-function-nodeFinally comes the trick part. Get a new function node and connect it with the slider. This node will construct the JSON message which will be sent to the sex toy using MQTT. The JavaScript code of the function node is as follows:

function-node-for-constructing-json-speed-command

msg.payload={messageType:"execute", actuator:"motor1",
             actuatorMode:"constant", actuatorValue:msg.payload}
msg.topic = "BIinTopic";
return msg;

Now connect the function node with a new MQTT output node. Leave the topic empty as it will be passed from the input nodes:

mqtt-out-message-to-bi

Using Node-RED with the Node-RED dashboard we are able to make a user interface for our sex toy(s). We could easily display the motion of the sex toy as well as the vibration motor speed. You could argue that we had a (very simple) user interface already in part 1 of the tutorial, without having to use MQTT, Node-RED and the Node-RED dashboard. That’s true. But imagine you have several sex toys and want to control them. You could easily add a tab in Node-RED for each sex toy. Or you could build more sophisticated flows incorporating several sex toy. Why not interconnecting the vibrating necklace with a penis ring and one of the plugs or dildos?

There are a lot of flows and nodes already available at http://flows.nodered.org/. (Of course not in the sex toy domain)

Why not play music for a given sex toy vibrator mode? Or control the sex toy using another IOT device…

Internet of (sex) things – part 3: Node-RED

in the second part of this tutorial we have seen how to use the MQTT protocol to send data across the internet. In the third part we show how to add additional functionalities to our sex toys.

The series has 4 parts:

part 1: Exploring the internet of (sex) things

part 2: MQTT messages

part 3: Node-RED

part 4: Building a sex toy dashboard with Node-RED

We want to enable sex toys to communicate and  to connect to social media. Although there are a lot of solutions from the Internet of Things (IoT) community Node-RED is outstanding as you could connect devices without or with little knowledge of programming languages: “NodeRED is a visual tool for wiring together hardware devices, APIs and online services – for wiring the Internet of Things.”  (Wikipedia). There are standard building blocks (called nodes) which are categorized as input, output, function and social nodes. You can select nodes and wire them to create a flow. A typical flow would look like that: input node- function node- output node. If you want to know more you can should have a look at this very good tutorial: http://noderedguide.com/

In this part of the tutorial we will receive and display data from the sex toy. And we want to send control commands to the sex toy. This can be achieved with a few flows in Node-RED:

node-red-overview

Installation of Node-RED:

  1. First you have to install “node.js”. Follow the instructions here.
  2. Now follow the node-red installation instruction.
    For Windows: Press the Windows button. Type in “CMD”. Windows should suggest the command shell app. Now RIGHT-click on the command shell app and select “open as administrator”. In the new command shell window type in

    npm install -g --unsafe-perm node-red
  3. Change to the Node-RED directory and start Node-RED by the command “node red” or “node red.js”
  4. Go to your browser and open http://127.0.0.1:1880/

If you don’t want to make a local installation of Node-RED the online service FRED (https://fred.sensetecnic.com/) offers a good alternative. FRED comes with additional nodes with extra functionalities.

Let’s start with a first flow. We want to control the LED and the motor of our IoT sex toys. Therefore we need two nodes: The input node “INJECT” and the output node “MQTT”. Select both nodes and drag them into the flow window in the middle. Wire both nodes. Then open the Inject-node (just double-click the node). Change the  payload to type “string” and type in the command for switching the LED. This command is taken from the second part of the tutorial:

{messageType : "execute",  actuator : "LED",  actuatorValue : 1 }

In addition you have to enter the topic that will be used for the MQTT message. It is “BIinTopic” – the “in” refers to the sex toy. Select a name like “set LED on”:inject-mode-led-on

Then edit the MQTT node. The name of the MQTT server must be entered. Leave the Topic field empty and the MQTT node will use the topic from the predecessor node INJECT.

mqtt-out-message-to-biNow press the deploy button. You should see the message “successful deployed”.

Now you can press one of the INJECT node button (the button is on the left side of the node) and the LED of the sex toy should go on.controlbi2byrednode

You can use the JSON commands from part 2 of the tutorial and make an INJECT node for each command. With just 5 flows you can control all functions of the vibrator from everywhere. Instead of the INJECT node you could use an Email or Twitter node (instead of the INJECT node) to control the sex toy eg by Email.

There are no file open or save menus in Node-RED. Instead the visual flow can be imported and exported using a simple text file. If you want to import the flow above download and unzip this text file. Select Import -> from clipboard and copy the text from the file to the clipboard. If you want to save the flow, select Export -> from clipboard and copy the text of the clipboard in a new text file.

Now let’s receive messages from the vibrator and parse them. You need a MQTT input node, a JSON function node, a SWITCH node and two DEBUG output node.

We will show how to retrieve the status of the LED. When the LED is on the JSON file includes: “LEDstatus: 1”. Otherwise the JSON file includes: “LEDstatus: 0”.

Edit the MQTT input node and enter the URL of the Mosquitto server and the topic “BIoutTopic”.

mqtt-in

Then add a DEBUG node and select “complete msg”. Wire MQTT and DEBUG node. This node will display the MQTT message in the debug slider (on the rights side). This is only for debugging – you will see if the message from the vibrator comes in (or not).

Now add a JSON function node and connect the MQTT node with the JSON node. The JSON function node will parse the text string which was sent via MQTT and transforms it to a JSON object.

Now select a SWITCH node. As property enter “payload.LEDstatus”. The switch node branches the flow according to LED status which was reported in the JSON file. Now we can test, if the LEDstatus is 0 or 1. For each comparison a line will be added.json-switch-node

Finally make two DEBUG output nodes and wire them with the SWITCH node. Complete the “msg” by adding “payload.LEDstatus”. If the flow reaches the DEBUG nodes you will see a message in the debug slider on the right.

ouputnodeled

 

Now you are ready and can press the deploy button. It should look like that:

analyse-json

Again, the visual flow can be imported. If you want to import the flow above download and unzip this text file. Select Import -> from clipboard and copy the text from the file to the clipboard.

Let us test the flow. You have to set the LED on. Press the “turn on” button in your browser as explained in part 2.vibr iot controlnodemcu prototype breadboard

 

Now have a look at the “debug” slider where you should see the result of the action. The first entry is the JSON file which was received. It is displayed by the DEBUG node “message”. In the second entry the DEBUG node (wired with the SWITCH node) displays 1 which means the LED is on.debug-window-led-on

 

With Node-RED you can add a SQL database to store all data, you can connect to social media and especially you can connect MULTIPLE sex toys and let them interact. Read the excellent guides for parsing JSON files and MQTT:

http://noderedguide.com/index.php/2015/10/28/node-red-lecture-3-basic-nodes-and-flows/#h.5zaw60nvfsyj

What we have achieved: In part 1 we have developed a wifi-enabled sex toy prototype based on the ESP8266. The toy was controlled through a web browser on a local smart phone / laptop. Local refers to the access point. Both the local smart phone / laptop and the ESP8266 must have access to the same access point (eg your router at home.)

In part 2 we opened our connections to the internet. With the fast MQTT protocol we are able to send and receive messages from anywhere. For data transmission  we use the JSON file format. We have sketched a protocol for sending data, commands and messages between sex toys and users.

In this part we introduced Node-RED a visual programming tool for the Internet of Things. With this tool we are able to connect sex toys at different locations, to store sensor data and to get social.

In the fourth part of the tutorial we will introduce User Interfaces to create a sex toy dashboard.

New fusion 3d printed and silicone molded vibrator

fusion tinkercadThis is the initial design. The round curved form will be in silicone with vibration motor within (vibration motor not shown on sketch). The red part is 3d printed. It is the enclosure for the body interaction vibrator development board and LiPo battery. You can plug-in the Micro USB connector for battery charging. In addition there is an on/off switch e.g. for travelling.