2016-12-01 118 views
0

我已经做了一个按钮/开关电路的程序,将显示它输出使用wxPython但我想发送数据到服务器。如何从python树莓派发送数据到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实例连接,我从这个2网站,该网站是遵循https://tryolabs.com/blog/2012/06/25/connecting-sql-server-database-python-under-ubuntu/https://gist.github.com/rduplain/1293636

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

这里我全成代码,它可以将数据发送到服务器,除了它我的孙中山像UPDATE_TIME 2016年1月12日,但是当我使用选择查看在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

或者如果nybody有另一种选择请告诉我... – anubismmt