[PYTHON] I made an IoT device to naturally acquire positive thinking

Suddenly, do you know positive thinking?

Positive thinking is the idea that if you think positively about anything, it will come true and life will be successful [1], trying to see the good side of things, positive It is a thinking method that aims to change the reality by maintaining a positive attitude and changing the "thinking itself". Positive thinking brings about a positive reality, and negative thinking brings about a negative reality. (From Wikipedia)

No matter how hard your work is or how unpleasant it may be, positive thinking may help you. From today, I will try to overcome the rough seas of society with positive thinking.


But if you want positive thinking, you want ** friends **, right?

Every time you do one positive thinking, you want a companion who will be happy with you, right? ** **


I made.


IoT devices that make you happy when you make positive statements

When a positive phrase is received, the motor with diaphragm rotates and trembles. As the word "tremble with joy".

Simply attach this to your surrounding figures and stuffed animals to complete an IoT device equipped with natural language processing functions.

I'm entering the phrase as text this time, but I think that if you do your best, you will be able to recognize the voice. I think the final form is that when you talk to the stuffed animal, it responds.

Danbo

First of all, I will try it with this child.

I managed to get up, so I will input positively.

Today is the best with nice weather.


hurueru1.gif


hurueru2.gif

** Success! You can see at a glance that you are trembling with joy! ** **

Stuffed horse

However, I felt that the motor was a little too big for Danbo, so I will try it with a cute stuffed horse.

It fits in the ring of your hand and is cute.

I'm happy to be able to work tomorrow Monday.


hurueru3.gif

** I ran out with joy. ** **

Summary

Even if I was alone, I made friends who would be happy with me just by making a positive statement. ** **

If this is the case, it seems that adults and children will be able to acquire ** naturally ** positive thinking.

However, when I showed this to a friend, I received feedback only as "simple and scary", so development will stop once.

The circuits and programs are described below for reference. COTOHA API is RESTful, so it seems that it can be applied not only to Raspberry Pi but also to various other things.

Circuits and programs

** What I used **

  • RaspberryPi3 --Motor with 24V DC diaphragm (details unknown for the one sold at Marutsu) --TB6643KQ Motor Driver --Other electronic components

** Circuit configuration **

There was no IC equivalent to TB6643KQ, so I used a similar one in the sketch. Replace the battery with a 24V DC power supply.

kairo_hurueru.png

Python3 program


import wiringpi2  #For pin operation of Raspberry Pi
from time import sleep
import requests
import json
import sys

MOTORPIN = 18

#Motor operation class
class OperateMoter:
    def __init__(self, motorpin):
        self.motorpin = motorpin
        wiringpi2.wiringPiSetupGpio()
        wiringpi2.pinMode(self.motorpin, 1)

    def operate_motor(self, action):
        if action == True:
            wiringpi2.digitalWrite(self.motorpin, 1)

        elif action == False:
            wiringpi2.digitalWrite(self.motorpin, 0)

#COTOHA API operation class
class CotohaApi:
    #Initialization
    def __init__(self, client_id, client_secret, developer_api_base_url, access_token_publish_url):
        self.client_id = client_id
        self.client_secret = client_secret
        self.developer_api_base_url = developer_api_base_url
        self.access_token_publish_url = access_token_publish_url
        self.getAccessToken()

    #Get access token
    def getAccessToken(self):
        #Access token acquisition URL specification
        url = self.access_token_publish_url

        #Header specification
        headers={
            "Content-Type": "application/json"
        }

        #Request body specification
        data = {
            "grantType": "client_credentials",
            "clientId": self.client_id,
            "clientSecret": self.client_secret
        }
        #Encode request body specification to JSON
        data = json.dumps(data)

        #Send request
        result = requests.post(url, data=data, headers=headers)

        #Get an access token from the response body
        self.access_token = result.json()["access_token"]


    #Parsing API
    def sentiment(self, sentence):
        #Parsing API URL specification
        url = self.developer_api_base_url + "nlp/v1/sentiment"
        #Header specification
        headers={
            "Authorization": "Bearer " + self.access_token,
            "Content-Type": "application/json;charset=UTF-8",
        }
        #Request body specification
        data = {
            "sentence": sentence
        }

        #Encode request body specification to JSON
        data = json.dumps(data)

        #Send request
        result = requests.post(url, data=data, headers=headers)

        #Get analysis result from response body
        return result.json()

if __name__ == '__main__':
    operate_motor = OperateMoter(MOTORPIN)

    #COTOHA API instantiation
    cotoha_api = CotohaApi(CLIENT_ID, CLIENT_SECRET, DEVELOPER_API_BASE_URL, ACCESS_TOKEN_PUBLISH_URL)

    message = sys.argv[1]
    result = cotoha_api.sentiment(message) 
    if result['status'] != 0:
        print("Failed to execute COTOHA API.")
        exit()
    print(result)

    sentiment = result['result']['sentiment']
    score = result['result']['score']

    if sentiment == 'Positive' and 0.5 < score:
        print("Positive remarks are good!!")
        operate_motor.operate_motor(True)
        sleep(5)
        operate_motor.operate_motor(False)

The site that I used as a reference

COTOHA API Portal

I tried using the COTOHA API rumored to be easy to handle natural language processing in Python

Wikipedia: Positive Thinking

Recommended Posts

I made an IoT device to naturally acquire positive thinking
I made an Ansible-installer
I made an action to automatically format python code
I made an Xubuntu server.
I made an animation to return Othello stones with POV-Ray
I made an Anpanman painter discriminator
I made an Angular starter kit
I made Othello to teach Python3 to children (2)
I made an online frequency analysis app
I made an alternative module for japandas.DataReader
I made Othello to teach Python3 to children (5)
I made a script to display emoji
I want to make an automation program!
I made Othello to teach Python3 to children (3)
I made Othello to teach Python3 to children (1)
I made an appdo command to execute a command in the context of the app
I made an image classification model and tried to move it on mobile
I made a package to create an executable file from Hy source code