[PYTHON] I tried to make an open / close sensor (Twitter cooperation) with TWE-Lite-2525A

I tried to make an open / close sensor-like with TWE-Lite-2525A

I took on the challenge of electronic work during the summer vacation. This is Qiita's first post.

This goal

Detects vibration (acceleration) using a small IOT sensor with acceleration (TWE-LITE-2525A) and POSTs to Twitter's API.

DSC_0448.png

What I bought this time

Purchased online from Akizuki Denshi. I arrived in about 2 days. --Mono Wireless Co., Ltd .: 1.TWE-LITE-2525A (Twilight Nico Nico) 2.MONOSTICK --Made by Golden Power 3. Lithium battery CR2032

Other development environment:

- Windows10(64bit) --Python3 series

1. Preparation of TWE-LITE-2525A

If you leave it as it was at the time of purchase, it will be difficult to handle the data sent from Twilight 2525, so Rewrite the settings of MONOSTICK and TWE-LITE-2525A.

To rewrite it, I use a function called "OTA". It is a mechanism to "write the firmware to the USB side (receiver: monostick side) and wirelessly overwrite it from the USB side to the sensor side". However, since the USB side will be used as a receiver after that, it is necessary to overwrite the firmware for the receiver again. It seems to be an abbreviation for # Over The Air.

Since there is an official video, it is easy to get an overview by looking at the following.  https://www.youtube.com/watch?v=uq_pkrvypBA

I understand the flow, but how do you actually rewrite it? The information on the official website is cluttered, and I was a little addicted to it before moving it.

1-1. Preparation of data for overwriting

DL the "Samp_Monitor application" from the following.  https://mono-wireless.com/jp/products/TWE-Lite-2525A/firmware_update.html

1.Samp_Monitor_EndDevice_Input_JN5164_CNFMST_1_6_1.bin  2.Samp_Monitor_EndDevice_Input_JN5164_LITE2525A_1_6_1.bin  3.Samp_Monitor_Parent_JN5164_1_6_1.bin

The above three are included in the ZIP file that has been downloaded, 1 is used for the bin installed on the sensor side, and 3 is used for the bin installed on the USB side.

1-2. Farm writing to USB

DL / Install "TWE-LITE Programmer for Windows" from the following.  http://mono-wireless.com/jp/tech/misc/LiteProg/

Use this application to write various firmware to the USB monostick. 3 monopg.png

Please note that if MONOSTICK is recognized in another window, it will not be recognized by this application. In addition, if it is not recognized, it may be better to connect and disconnect the USB.

1-3. Writing firmware to the sensor side

Firmware to write to the sensor side using the above TWE-LITE programmer Write (1.Samp_Monitor_EndDevice_Input_JN5164_CNFMST_1_6_1.bin) to USB.

Change to any setting as in the video. (Don't forget to save the settings with "S") Please refer to the official separately for details such as serial connection and baud rate setting in TeraTerm.

Refer to the image below for this setting sensor.png

After completing the settings, turn on the lithium power while keeping the sensor close to the USB, Rewrite the farm on the sensor side.

1-4. Writing the farm to the USB side

Firmware to write to the USB side using the TWE-LITE programmer Write (3.Samp_Monitor_Parent_JN5164_1_6_1.bin) to USB.

Change to any setting as in the video. (Don't forget to save the settings with "S")

Refer to the image below for this setting usb.png

The setting is completed on the USB side as it is.

1-5. Operation check

If you check from TeraTerm, you can get various values with key = value as shown below.

If there is no response from the sensor, only the "ts" tag will be output. You can get the x-axis, y-axis, and z-axis. (I don't see that much this time) kyacchi.png

2. Creating a program on the Python side

I wrote Python for the first time. Please refer to the installation of Python and the creation of Twitter API separately.

Twitter (OAuth) integration in Python uses requests-oauthlib, an OAuth authentication library for Python. Install the library like this.

pip install requests requests_oauthlib

Create the code by referring to the sample on the website of Monowire. The whole source code looks like this.

DetectionTweet.py


import serial
import requests
from requests_oauthlib import OAuth1Session
from datetime import datetime

CK = 'Write your Consumer Key'
CS = 'Write your Consumer Secret'
AT = 'Write your Access Token'
AS = 'Write your Accesss Token Secert'

#Open COM3
s = serial.Serial('COM3', 115200)

#URL for posting tweets
url = "https://api.twitter.com/1.1/statuses/update.json"

#infinite loop
while 1:
    data = s.readline()
    
    #The first ":Get rid of
    data = data[2:]
    spilitdatum = data.decode('utf-8').split(":")
    
    dict = {}
    
    # key,Save as value type in dictionary
    for spilitdata in spilitdatum:
        
        s_key = spilitdata.split("=")[0]
        s_val = spilitdata.split("=")[1]
        
        dict[s_key] = s_val
    
    #When there is no detection from the sensor, only the "ts" key can be obtained, so
    #Here as an appropriate key[id]Judgment by
    if ("id" in dict):
        
        #Tweet body
        #If Twitter has the same text, Status is a duplicate and it will be 403 Status, so set the time.
        params = {"status": "Vibrate Detection! " + "\n" + datetime.now().strftime('%X')}
        
        #Post by POST method with OAuth authentication
        twitter = OAuth1Session(CK, CS, AT, AS)
        req = twitter.post(url, params = params)

        #Check the response
        if req.status_code == 200:
            print ("OK: %s" % req)
        else:
            print ("Error: %s" % req.content)

s.close()

3. Confirmation of cooperation between TWE-Lite-25252A and Twitter.

It's simple, but it's perfect for the door. DSC_0450.png

He tweeted wonderfully by opening and closing the door. cmd.png

Detect.png

If this is left as it is, there will be too many detections (a few notifications will be sent by opening and closing the door once). It seems that it is necessary to reduce the notification timing on the wireless side (default 0.5 seconds) and make adjustments on the Python side and server side. I use too much Twitter resources ;;

After that, if you combine it with AWS IoT or MQTT, you will have an IOT service. How about electronic work for your child's summer vacation homework?

So I wrote Qiita for the first time, but I was tired.

Recommended Posts

I tried to make an open / close sensor (Twitter cooperation) with TWE-Lite-2525A
I tried to make an OCR application with PySimpleGUI
I tried to make an image similarity function with Python + OpenCV
I tried to detect an object with M2Det!
I tried to make Kana's handwriting recognition Part 3/3 Cooperation with GUI using Tkinter
I tried to implement an artificial perceptron with python
I tried to find an alternating series with tensorflow
I tried to make various "dummy data" with Python faker
I tried to make GUI tic-tac-toe with Python and Tkinter
I tried to create an article in Wiki.js with SQLAlchemy
[Python] I tried to make an application that calculates salary according to working hours with tkinter
[5th] I tried to make a certain authenticator-like tool with python
I tried to make an activity that collectively sets location information
[2nd] I tried to make a certain authenticator-like tool with python
I tried to make deep learning scalable with Spark × Keras × Docker
[3rd] I tried to make a certain authenticator-like tool with python
I tried to make a periodical process with Selenium and Python
I tried to make a 2channel post notification application with Python
I tried to make an analysis base of 5 patterns in 3 years
I tried to make a todo application using bottle with python
[4th] I tried to make a certain authenticator-like tool with python
[Python] Simple Japanese ⇒ I tried to make an English translation tool
[1st] I tried to make a certain authenticator-like tool with python
I tried to make a strange quote for Jojo with LSTM
I tried to log in to twitter automatically with selenium (RPA, scraping)
I tried to make a mechanism of exclusive control with Go
I tried to make an original language "PPAP Script" that imaged PPAP (Pen Pineapple Appo Pen) with Python
I tried sending an SMS with Twilio
I tried to implement Autoencoder with TensorFlow
I tried to visualize AutoEncoder with TensorFlow
I tried sending an email with python.
I tried to implement CVAE with PyTorch
I want to make an automation program!
I tried to make a Web API
I tried to solve TSP with QAOA
Python: I tried to make a flat / flat_map just right with a generator
I tried to make a calculator with Tkinter so I will write it
I tried to make "Sakurai-san" a LINE BOT with API Gateway + Lambda
[AWS] [GCP] I tried to make cloud services easy to use with Python
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
[Zaif] I tried to make it easy to trade virtual currencies with Python
I tried to make a url shortening service serverless with AWS CDK
I tried to predict next year with AI
I tried to make a periodical process with CentOS7, Selenium, Python and Chrome
I tried to detect Mario with pytorch + yolov3
I tried to implement reading Dataset with PyTorch
I tried to use lightGBM, xgboost with Boruta
I tried to learn logical operations with TF Learn
I tried to make my own source code compatible with Chainer v2 alpha
I tried to move GAN (mnist) with keras
I tried to make a simple mail sending application with tkinter of Python
Make a monitoring device with an infrared sensor
I tried to get an image by scraping
I tried to detect motion quickly with OpenCV
When I tried to make a VPC with AWS CDK but couldn't make it
I want to make a game with Python
I tried to automate internal operations with Docker, Python and Twitter API + bonus
[Patent analysis] I tried to make a patent map with Python without spending money
I tried to build an environment of Ubuntu 20.04 LTS + ROS2 with Raspberry Pi 4
I tried to make a castle search API with Elasticsearch + Sudachi + Go + echo
I want to be an OREMO with setParam!