I took on the challenge of electronic work during the summer vacation. This is Qiita's first post.
Detects vibration (acceleration) using a small IOT sensor with acceleration (TWE-LITE-2525A) and POSTs to Twitter's API.
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
- Windows10(64bit) --Python3 series
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.
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.
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.
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.
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
After completing the settings, turn on the lithium power while keeping the sensor close to the USB, Rewrite the farm on the sensor 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
The setting is completed on the USB side as it is.
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)
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()
It's simple, but it's perfect for the door.
He tweeted wonderfully by opening and closing the door.
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