2016-12-01 5 views
0

私は、wxPythonを使用してoutpuを表示するプログラムを備えたボタン/スイッチ回路を作成しましたが、データをサーバーに送信します。python raspberry piからmssqlサーバーにデータを送信する方法は?

これは、ボタンのプログラムである:

import RPi.GPIO as GPIO 
import datetime 
import os 

GPIO.setwarnings(False) 
GPIO.setmode(GPIO.BOARD) 
GPIO.setup(40, GPIO.IN) 

global count, flag 
count = 0 
flag = 0 
button = "None" 
input = GPIO.input(40) 

global update_time 
update_time = datetime.datetime.strftime(datetime.datetime.now(), '%d-%m-%y  %H:%M:%S') 

try: 
    import wx 
except ImportError: 
    raise ImportError, "The wxPython module is required to run this program." 

class OnOffApp_wx(wx.Frame): 
    def __init__(self, parent, id, title): 
     wx.Frame.__init__(self, parent, id, size = (500, 200), title = 'ON/OFF Status') 
     self.SetBackgroundColour(wx.WHITE) 
     self.SetWindowStyle(wx.STAY_ON_TOP) 
     self.parent = parent 
     self.initialize() 

    def initialize(self): 
     sizer = wx.GridBagSizer() 
     font = wx.Font(20, wx.DECORATIVE, wx.ITALIC, wx.NORMAL) 
     self.SetFont(font) 

     self.label1 = wx.StaticText(self, -1, label=u'Button Status : {}'.format(button)) 
     self.label1.SetBackgroundColour(wx.WHITE) 
     self.label1.SetForegroundColour(wx.BLACK) 
     sizer.Add(self.label1, (1,0), (1,2), wx.EXPAND) 

     self.label2 = wx.StaticText(self, -1, label=u'Flag Status : {}'.format(flag)) 
     self.label2.SetBackgroundColour(wx.WHITE) 
     self.label2.SetForegroundColour(wx.BLACK) 
     sizer.Add(self.label2, (2,0), (1,3), wx.EXPAND) 

     self.label3 = wx.StaticText(self, -1, label=u'Count Status : {}'.format(count)) 
     self.label3.SetBackgroundColour(wx.WHITE) 
     self.label3.SetForegroundColour(wx.BLACK) 
     sizer.Add(self.label3, (3,0), (1,4), wx.EXPAND) 

     self.label4 = wx.StaticText(self, -1, label=u'Time Updated : {}'.format(update_time)) 
     self.label4.SetBackgroundColour(wx.WHITE) 
     self.label4.SetForegroundColour(wx.BLACK) 
     sizer.Add(self.label4, (4,0), (1,5), wx.EXPAND) 

     self.timer = wx.Timer(self) 
     self.Bind(wx.EVT_TIMER, self.on_timer, self.timer) 
     self.timer.Start(50) 

     self.SetSizer(sizer) 
     self.Show(True) 

    def on_timer(self,event): 
     global update_time 
     update_time = datetime.datetime.strftime(datetime.datetime.now(), '%d-%m-%y %H:%M:%S') 

     global count, flag         
     input = GPIO.input(40) 
     if ((not flag) and input): 
      flag = input 
      count += 1 
      button = 'Pressed' 
      #self.label1.SetLabel("Button Status : Pressed") 
      self.label1.SetLabel("Button Status : {}".format(button)) 
      self.label2.SetLabel("Flag Status : {}".format(flag)) 
      self.label3.SetLabel("Count Status : {}".format(count)) 
      self.label4.SetLabel("Time Updated : {}".format(update_time)) 

     elif ((not input) and flag): 
      flag = input 
      count += 1 
      button = 'Debounce' 
      #self.label1.SetLabel("Button Status : Debounce") 
      self.label1.SetLabel("Button Status : {}".format(button)) 
      self.label2.SetLabel("Flag Status : {}".format(flag)) 
      self.label3.SetLabel("Count Status : {}".format(count)) 
      self.label4.SetLabel("Time Updated : {}".format(update_time)) 

if __name__ == "__main__": 
    Rs = wx.App() 
    OnOffApp_wx(None, -1, 'ON/OFF Status') 
    Rs.MainLoop() 

GPIO.cleanup() 

Pythonでこの私のSQLの接続例Iはhttps://tryolabs.com/blog/2012/06/25/connecting-sql-server-database-python-under-ubuntu/https://gist.github.com/rduplain/1293636である。この2サイトから従うよう:

import pyodbc 

dsn = 'datasource' 
user = 'username' 
password = 'password' 
database = 'databasename' 

con_string = 'DSN=%s; UID=%s; PWD=%s; DATABASE=%s;' % (dsn, user, password, database) 
cnxn = pyodbc.connect(con_string) 

私はあなたに私のデータベースを与えることはできません私のボタンスイッチプログラムからmssqlデータベースにデータを送信する方法を誰でも見ることができます。私は自分のサーバーとデータベースに接続しています。

+0

この問題は何ですか?あなたはすでに接続していますか、データの送信に何か問題がありますか、データの送信方法がわからないのですか?後者の場合は[こちら](https://mkleehammer.github.io/pyodbc/)を参照してください –

+0

私はまだそれを行っていないので、それを送信する方法についてはわかりません。 – anubismmt

+0

私は、フラグとカウントデータをデータベース内のテーブルに送信して、スターターを開始するだけです。 – anubismmt

答えて

0

ここでは銭私の2016年1月12日のようなUPDATE_TIMEが、私はMSSQLサーバ内のデータを表示するために選択し使用する場合、それは1月12日2016年

を表示する日時を除き、サーバーにデータを送信することができ、私の成功しコード
import RPi.GPIO as GPIO 
import datetime 
import os 
import pyodbc 

dsn = 'sqlserverdatasource' 
driver = 'FreeTDS' 
user = 'username' 
password = 'password' 
database = 'databasename' 

con_string = 'DSN=%s; DRIVER=%s; UID=%s; PWD=%s; DATABASE=%s;' % (dsn, driver, user, password, database) 
cnxn = pyodbc.connect(con_string) 
global cursor 
cursor = cnxn.cursor() 

GPIO.setwarnings(False) 
GPIO.setmode(GPIO.BOARD) 
GPIO.setup(40, GPIO.IN) 

global count, flag 
count = 0 
flag = 0 
button = "None" 
input = GPIO.input(40) 

global update_time 
update_time = datetime.datetime.strftime(datetime.datetime.now(), '%d-%m-%y  %H:%M:%S') 

try: 
    import wx 
except ImportError: 
    raise ImportError, "The wxPython module is required to run this program." 

class OnOffApp_wx(wx.Frame): 
    def __init__(self, parent, id, title): 
     wx.Frame.__init__(self, parent, id, size = (500, 200), title = 'ON/OFF Status') 
     self.SetBackgroundColour(wx.WHITE) 
     self.SetWindowStyle(wx.STAY_ON_TOP) 
     self.parent = parent 
     self.initialize() 

    def initialize(self): 
     sizer = wx.GridBagSizer() 
     font = wx.Font(20, wx.DECORATIVE, wx.ITALIC, wx.NORMAL) 
     self.SetFont(font) 

     self.label1 = wx.StaticText(self, -1, label=u'Button Status : {}'.format(button)) 
     self.label1.SetBackgroundColour(wx.WHITE) 
     self.label1.SetForegroundColour(wx.BLACK) 
     sizer.Add(self.label1, (1,0), (1,2), wx.EXPAND) 

     self.label2 = wx.StaticText(self, -1, label=u'Flag Status : {}'.format(flag)) 
     self.label2.SetBackgroundColour(wx.WHITE) 
     self.label2.SetForegroundColour(wx.BLACK) 
     sizer.Add(self.label2, (2,0), (1,3), wx.EXPAND) 

     self.label3 = wx.StaticText(self, -1, label=u'Count Status : {}'.format(count)) 
     self.label3.SetBackgroundColour(wx.WHITE) 
     self.label3.SetForegroundColour(wx.BLACK) 
     sizer.Add(self.label3, (3,0), (1,4), wx.EXPAND) 

     self.label4 = wx.StaticText(self, -1, label=u'Time Updated : {}'.format(update_time)) 
     self.label4.SetBackgroundColour(wx.WHITE) 
     self.label4.SetForegroundColour(wx.BLACK) 
     sizer.Add(self.label4, (4,0), (1,5), wx.EXPAND) 

     self.timer = wx.Timer(self) 
     self.Bind(wx.EVT_TIMER, self.on_timer, self.timer) 
     self.timer.Start(50) 

     self.SetSizer(sizer) 
     self.Show(True) 

    def on_timer(self,event): 
     global update_time 
     update_time = datetime.datetime.strftime(datetime.datetime.now(), '%d-%m-%y %H:%M:%S') 

     global count, flag         
     input = GPIO.input(40) 
     cursor = cnxn.cursor() 

     if ((not flag) and input): 
      flag = input 
      count += 1 
      button = 'Pressed' 
      #self.label1.SetLabel("Button Status : Pressed") 
      self.label1.SetLabel("Button Status : {}".format(button)) 
      self.label2.SetLabel("Flag Status : {}".format(flag)) 
      self.label3.SetLabel("Count Status : {}".format(count)) 
      self.label4.SetLabel("Time Updated : {}".format(update_time)) 
      #cursor.execute("INSERT INTO TableTest (Col1, Col2, Col3) VALUES(%s,%s)"%(int(flag),int(count))) 
      cursor.execute("INSERT INTO TableTest (Col1, Col2, Col3) VALUES(%s,%s,'%s')"%(int(flag),int(count),update_time)) 
      cnxn.commit() 

     elif ((not input) and flag): 
      flag = input 
      count += 1 
      button = 'Debounce' 
      #self.label1.SetLabel("Button Status : Debounce") 
      self.label1.SetLabel("Button Status : {}".format(button)) 
      self.label2.SetLabel("Flag Status : {}".format(flag)) 
      self.label3.SetLabel("Count Status : {}".format(count)) 
      self.label4.SetLabel("Time Updated : {}".format(update_time)) 
      #cursor.execute("INSERT INTO TableTest (Col1, Col2, Col3) VALUES(%s,%s)"%(int(flag),int(count))) 
      cursor.execute("INSERT INTO TableTest (Col1, Col2, Col3) VALUES(%s,%s,'%s')"%(int(flag),int(count),update_time)) 
      cnxn.commit() 

if __name__ == "__main__": 
    Rs = wx.App() 
    OnOffApp_wx(None, -1, 'ON/OFF Status') 
    Rs.MainLoop() 

GPIO.cleanup() 
cursor.close() 
cnxn.close() 
+0

誰もが、ボタンを追加して出力をメッセージボックスに表示してselect文を使用してサーバからデータを作成して表示するのに役立つことができます – anubismmt

+0

または、別の方法がありますか? – anubismmt

関連する問題