Up to the last time, automatic measurement has become possible in consideration of errors. This time, we will use slack's API to notify slack of the end of measurement. Even with other chat tools (Discord, Line, etc.), the flow of acquiring tokens and sending requests to the API is the same, so I think that it can be used for various purposes.
conda install
pySerial in (2)The python slackclient package didn't work in my environment, so
--Send a message to Slack from python --Qiita -How to send to Slack using Python --Qiita
With reference to
--using the requests package --Using the token obtained from the slack web page
You can send any message to any channel using the slack API. What should be changed in the code below
You should be able to send it only
How to add mention
-How to skip mentions with Incoming Webhooks in Slack --Qiita
Please refer to this site
import serial
import time
import pandas as pd
##Constant specification
MAX = 40000
COMampere = "COM10"
COMpulse = "COM9"
bitRate = 9600
##Variable initialization
pulse = 0
ampere_list = []
pulse_list = []
ampere_average_list =[]
##Move to the back origin(Initialization)
ser = serial.Serial(COMsigma, bitRate, timeout=0.1)
ser.write(b"H:2-\r\n")
# time.sleep(0.1)
# print(ser.read_all())
ser.close()
import requests
class SlackDriver:
def __init__(self, _token):
self._token = _token # api_token
self._headers = {'Content-Type': 'application/json'}
def send_message(self, message, channel):
params = {"token": self._token, "channel": channel, "text": message}
r = requests.get('https://slack.com/api/chat.postMessage',
headers=self._headers,
params=params)
print("return ", r.json())
token = 'xxxx-oooooooooooo-000000000000-hogehoge' #Please use the token you got this part
slack = SlackDriver(token)
#Start measurement
try:
while 1:
if pulse >= MAX:
##End the while statement when the position reaches MAX
break
if pulse ==2000:
slack.send_message("<@IDhogehoge>1000 is over", "#bot-test") #The acquired ID is<>Please enclose
if pulse ==30000:
slack.send_message("<@IDhogehoge>15000 is over", "#bot-test")
##Record current location information
pulse_list.append(pulse/2)
##Measure the current(Take 5 times and average it as the value at that position)
for i in range(5):
ser = serial.Serial(COMampere,bitRate,timeout=0.1)
ser.write(b"F5, R0,PR2\r\n")
time.sleep(1)
ser.write(b"MD?\r\n")
time.sleep(1)
tmp = ser.read_all()
#Skip if no current is available
if len(tmp)== 0:
ser.close()
continue
ampere = float(tmp.split()[2])
ampere_average_list.append(ampere)
time.sleep(1)
ser.close()
##Current and pulse(position)To list
ampere_list.append(sum(ampere_average_list)/len(ampere_average_list))
ampere_average_list = []
##Move the optical table
pulse += 1000
position = "A:2+P"+str(pulse)+"\r\n"
ser = serial.Serial(COMpulse,bitRate,timeout=0.1)
ser.write(bytes(position, 'UTF-8'))
time.sleep(1)
ser.write(b"G:\r\n")
ser.close()
##Turn the list into a dataframe
print(ampere_list)
print(pulse_list)
df = pd.DataFrame({'ampere(A)':ampere_list,'pulse':pulse_list})
def pulseToMilliMeter(pulse):
return pulse*0.006
df["position(mm)"] = df["pulse"].map(pulseToMilliMeter)
df.to_csv('./csv/result.csv',index=False)
plt.figure()
df.plot(x='position(mm)',y='ampere(A)',marker='o')
plt.savefig('./img/sample.png')
plt.close('all')
except IndexError:
ser.close()
slack.send_message("<@IDhogehoge>The measurement has failed", "#bot-test")
#I want to notify slack
slack.send_message("<@IDhogehoge>The measurement is over", "#bot-test")
##Variable initialization
pulse = 0
ampere_list = []
pulse_list = []
ampere_average_list =[]
##Move to the back origin(Initialization)
ser = serial.Serial(COMpulse, bitRate, timeout=0.1)
ser.write(b"H:2-\r\n")
time.sleep(0.1)
print(ser.read_all())
ser.close()
Recommended Posts