c#ras获取宽带账号密码
This post is a learning experience for myself figuring out a way to get my data from a remote raspberry pi in near real time to a server on the cloud. There’s nothing like learning in public! The material here would be too basic for the full stack developer but for the scientist or engineer with little or no software engineering experience it could be worth a read. Ill cover my script running on the rasp pi that uses the Python requests
package to send the data as form to a site called pythonanywhere.com (free to use for one site) where I am hosting a Flask App.
这篇文章对我自己来说是一次学习经历,他想出了一种方法,可以将我的数据从远程树莓派实时获取到云中的服务器。 没有什么比 在公共场所学习好! 对于全栈开发人员来说,这里的资料太基础了,但是对于几乎没有软件工程经验或没有软件工程经验的科学家或工程师来说,值得一读。 我遇到的麻烦是我在使用Python requests
包的rasp pi上运行的脚本,将数据作为表单发送到名为pythonanywhere.com的站点(可免费用于一个站点),该站点托管了Flask App。
So there is most likely well established methods (like this) to do the same thing (and likely much more developed) but I thought this experience was worth sharing. On my rasp pi the script shown in the Gist below is retrieving data from a building automation system in a remote building. My line of work revolves around building systems, energy, and HVAC so retrieving data from a building management systems is crucial. In the building automation world vendors communicate with a protocol developed by ASHRAE called BACnet. There is a Python package that I use frequently that is a wrapper around another Python based BACnet stack called BAC0. For any readers that are engineering consultants (commonly thought as the Jedi’s in the consulting world) this could be a method to get data from a building where there is an uncooperative building automation contractor (commonly thought as the dark side in the consulting world…) that sets up these systems as methods here go beyond a user interface that requires a username & password. BACnet/IP traffic on a LAN in a building requires no authentication which allows multiple vendor devices/manufactures to communicate with each other. Its only the user interfaces that have usernames & passwords, thus spinning up your own BACnet IP server any device will respond on a read or write if the nomenclature is correct in your code.
因此,最有可能采用成熟的方法( 例如 )来做同样的事情(并且可能会更加完善),但我认为这种经验值得分享。 在我的rasp pi上,下面要点中显示的脚本正在从远程建筑物中的建筑物自动化系统中检索数据。 我的工作范围围绕建筑物系统,能源和HVAC,因此从建筑物管理系统中检索数据至关重要。 在楼宇自动化领域,供应商与ASHRAE开发的称为BACnet的协议进行通信。 我经常使用一个Python程序包,该程序包是另一个基于Python的BACnet堆栈BAC0的包装 。 对于任何是工程顾问的读者(在咨询界通常被认为是绝地的),这可能是一种从建筑物中获取数据的方法,而该建筑物的建筑自动化承包商不合作(在咨询界通常被认为是阴暗的一面……)设置这些系统的方法超出了需要用户名和密码的用户界面的范围。 建筑物中LAN上的BACnet / IP通信不需要身份验证,该身份验证允许多个供应商设备/制造商相互通信。 它只是具有用户名和密码的用户界面,因此如果您的代码中的命名正确,则旋转您自己的BACnet IP服务器的任何设备都将在读取或写入时做出响应。
My script on the rasp pi runs as a scheduled task every 15 minutes where there are BACnet read requests being performed on sensors within the building. The scripts starts by creating a BACnet IP server with BAC0 and an empty Python dictionary that I created called allStuff
. The time stamp in UNIX format is also recorded as time.time()
when the script was executed. Make sure your rasp pi has the correct time as well as time zone configured when deployed. The sensor readings are all bundled into the allStuff
dictionary including the time stamp. If a TCP/IP connection cannot made across the LAN inside the building to the building automation system an exception will occur and numpy.nan
values will be utilized instead.
我在rasp pi上的脚本每15分钟作为计划任务运行一次,其中对建筑物内的传感器执行BACnet读取请求。 这些脚本首先创建一个带有BAC0的BACnet IP服务器和一个我创建的名为allStuff
的空Python字典。 执行脚本时,UNIX格式的时间戳记也记录为time.time()
。 确保您的rasp pi具有正确的时间以及在部署时配置的时区。 传感器读数全部捆绑到allStuff
词典中,包括时间戳。 如果无法通过建筑物内部的LAN与建筑物自动化系统建立TCP / IP连接,则会发生异常,而将使用numpy.nan
值。
Next the script will try
to post to my Flask app on pythonanywhere with the line in the code
接下来,脚本将try
使用代码行将其发布到pythonanywhere上的Flask应用程序中
r = requests.post(‘https://rcxreporter.pythonanywhere.com/datainform', data=allStuff, timeout=5)
.
r = requests.post('https://rcxreporter.pythonanywhere.com/datainform', data=allStuff, timeout=5)
。
My rasp pi will receive back an HTTP response code back from my Flask app if the data was accepted or not. If access to the world wide web is not available to the rasp pi or the pythonanywhere site was down for what ever reason the code would resort to another exception and print(‘Issues with sending data to cloud!’)
.
无论是否接受数据,我的rasp pi都会从我的Flask应用程序中返回HTTP响应代码。 如果rasp pi或pythonanywhere站点由于某种原因无法访问万维网,则代码出于任何原因将诉诸于另一个异常并print('Issues with sending data to cloud!')
。
From here I am also using the Pandas
library to save the data to Sqlite which I think is a neat feature of Pandas
. For what ever reason if my data cannot make it to the cloud or something happens in transit I have another copy saved locally on the rasp pi. Also worth mentioning I do not know much about databases in general but with Pandas
its actually super easy.
从这里,我还使用Pandas
库将数据保存到Sqlite中,我认为这是Pandas
一项简洁功能。 无论出于什么原因,如果我的数据无法传输到云中或发生传输中的某些情况,我都会在rasp pi上本地存储另一个副本。 值得一提的是,我对数据库的了解并不多,但是使用Pandas
其实非常容易。
This link directly below is to my pythonanywhere site where my Flask app is hosting the web page that uses some HTML code and JavaScript to render the chart:
下面的直接链接是我的Flask应用程序所在的pythonanywhere网站所在的网页,该网页使用一些HTML代码和JavaScript来绘制图表:
https://rcxreporter.pythonanywhere.com/
https://rcxreporter.pythonanywhere.com/
Pandas
also has a neat feature where an entire data frame cant be sent to HTML where the data can be viewed in a table format on my Flask app which can be easily manually copied into Microsoft Excel via highlighting the entire table thru my web browser.
Pandas
还具有一个巧妙的功能,即不能将整个数据帧发送到HTML,在Flask应用程序上可以以表格格式查看数据,可以通过突出显示整个表格通过Web浏览器轻松地将其手动复制到Microsoft Excel中。
The Python code to make the Flask app which contains the URL that the rasp pi is posting data to looks like this below in the Gist. If you scroll down to line 37 this is where the rasp pi is posting data to the URL on my site /datainform
. There wouldn’t by anything to look at for a human to browse in but the rasp pi can post data here.
用来制作Flask应用程序的Python代码,其中包含rasp pi正在发布数据的URL,如下所示(要点)。 如果您向下滚动到第37行,这是/datainform
pi将数据发布到我的站点/datainform
上的URL的/datainform
。 几乎没有人可以浏览,但是rasp pi可以在此处发布数据。
What happens below on the Flask app during the event of an incoming message is the form data is transformed to a dictionary format where the script checks for correct keys. What is meant by this is the content within the HTTP form needs to contain the same data format as what was sent on the rasp pi
在收到消息时,Flask应用程序下面发生的事情是将表单数据转换为字典格式,脚本在其中检查了正确的键。 这意味着HTTP表单中的内容需要包含与rasp pi上发送的数据格式相同的数据格式
{‘Date’,’ambient_temp’,’water_temp’, ‘water_stp’,’heat_command’}
{'Date','ambient_temp','water_temp', 'water_stp','heat_command'}
else anyone would be able to post data to my site and the server would accept it. I am far from a data security expert, but one added layer of protection is (also using an HTTPS route) I can verify the IP address of the rasp pi itself.
否则任何人都可以将数据发布到我的站点,服务器将接受它。 我距离数据安全专家还很远,但是我可以验证rasp pi本身的IP地址,这是另外一个保护层(也使用HTTPS路由)。
ip = request.headers[‘X-Real-IP’]
ip = request.headers['X-Real-IP']
The problem on pythonanywhere is apps are hosted behind a proxy server where it is difficult to obtain a client IP address. Pythonanywhere has more information about this here and they definitely proclaim that people can replicate your device so BE CAREFUL! Maybe keep your project a secret and what every you do dont blog about reviling all your code that someone can hack.
pythonanywhere上的问题是应用托管在代理服务器后面,在该代理服务器上很难获得客户端IP地址。 Pythonanywhere 在此处提供了有关此信息的更多信息 ,他们肯定宣称人们可以复制您的设备,因此请务必小心! 也许对您的项目保密,而您所做的每件事都不要写关于修改某人可以破解的所有代码的博客。
So after the data is verified for correct keys and the Flask app will attempt to save this to Sqlite thru Pandas
. Also worth mentioning the server parses the UNIX time stamp with
因此,在验证了正确密钥的数据之后,Flask应用程序将尝试通过Pandas
将其保存到Sqlite中。 还值得一提的是服务器使用以下命令解析UNIX时间戳
df[‘Date’] = datetime.fromtimestamp(df[‘Date’].astype(float))
df['Date'] = datetime.fromtimestamp(df['Date'].astype(float))
to a more readable ISO 8601 date and time format.
转换为更具可读性的ISO 8601日期和时间格式 。
This method is very bare bones and the database isn't registered to the Flask app but it will get the job done from what I have been testing.
该方法非常简单,并且数据库未注册到Flask应用程序中,但是它将根据我所测试的内容完成工作。
One item worth mentioning is there is a cool testing tool called Postman when you are attempting get your site up and running. This can be done even before your rasp pi is deployed that you can test that the your Flask app will work correctly. This screenshot below is a little dated but I can replicate what my rasp pi will send to my Flask App to verify proper HTTP response codes as well as data handling on the server. Only thing required in Postman is replicating the same ‘key’s’ or not to see how the server handles the incoming form data. Postman lets me test this one HTTP message at a time.
值得一提的是,当您尝试启动并运行网站时,有一个很酷的测试工具称为Postman。 甚至可以在部署rasp pi之前完成此操作,以便可以测试Flask应用程序是否可以正常运行。 下面的屏幕快照有些过时,但是我可以复制rasp pi发送到Flask App的内容,以验证正确的HTTP响应代码以及服务器上的数据处理。 邮递员唯一需要做的就是复制相同的“键”,还是不查看服务器如何处理传入的表单数据。 邮递员让我一次测试一条HTTP消息。
![Image for post](https://miro.medium.com/max/9999/1*5SWKT-eUbt7AvY1ooKx4ow.png)
Postman can be downloaded here and there are lots of tutorials on the web as it is a very popular tool to use.
邮递员可以在这里下载,网络上有很多教程,因为它是一种非常流行的工具。
What is also worth mentioning is the data rendering process on the Flask app. The basics of it all is when someone browses the my site site on pythonanywhere all of the data sent from the rasp pi which has been saved by the server is pulled from the Sqlite database on the server and sent to the front end (browser) in HTML code.
还值得一提的是Flask应用程序上的数据渲染过程。 这一切的基础是当有人在pythonany上浏览我的网站时,从rasp pi发送的所有数据(已由服务器保存)都将从服务器上的Sqlite数据库中提取并发送到以下位置的前端(浏览器)中: HTML代码。
return render_template(‘line_chart.html’, values=dfMeter, labels=dfDate, legend=legend, tables=tables, tableTwo=tableTwo)
return render_template('line_chart.html', values=dfMeter, labels=dfDate, legend=legend, tables=tables, tableTwo=tableTwo)
Any site on pythonanywhere there is a dashboard that looks like this where typical Flask app directories are setup:
pythonanywhere上的任何站点上都有一个仪表盘,该仪表盘看起来像这样,其中典型的Flask应用程序目录已设置:
![Image for post](https://miro.medium.com/max/9999/1*QsNMVjVnnTaUfo7QlUn8qA.png)
In the static directory there is javascript D3 library script Chart.min.js
that is used to render the charts. This is a miniaturized version of the entire D3 JavaScript rendering library. In the templates directory the HTML code is the code below. Jinja is utilized to pass the data from the Flask app to the front end with syntax
在静态目录中,存在用于呈现图表的javascript D3库脚本Chart.min.js
。 这是整个D3 JavaScript渲染库的微型版本。 在模板目录中,HTML代码是下面的代码。 Jinja利用语法将数据从Flask应用传递到前端
{ labels : [{% for item in labels %} “{{item}}”, {% endfor %}],
.
{ labels : [{% for item in labels %} “{{item}}”, {% endfor %}],
。
The table view of the data and summary statistics is also similar Jinja syntax with a for loop
数据和汇总统计信息的表视图也类似于带有for循环的Jinja语法
{% for table in tables %} {{ table|safe }} {% endfor %}
.
{% for table in tables %} {{ table|safe }} {% endfor %}
。
For more information regarding the D3 library see this site. Future endeavors for myself if I could do this professionally would be checking out this site called thingsboard. I can see this is a not free service and it has a variety of protocol connections (see screenshot below) that could be used on building systems like BACnet, Modbus, as well as HTTP(s) requests that I demonstrated on this blog to package data as form… It would be interesting to know if this service what the IoT gateway device is, how involved is it to set it up, and the overall reliability. One downfall to note on my experiments has been my rasp pi would lock up occasionally requiring a power cycle when I would attempt to remote into the pi to revise some code and re-run. This process was a bit frustrating and it would be very frustrating if my remote experiment was hundreds perhaps even thousands of miles away. Reader beware and also be sure to post a comment if there is better methods than Teamviewer to remote into your rasp pi when something needs to be revised; this has been the only downfall from a reliability stand point to get good data.
有关D3库的更多信息, 请访问此站点 。 如果我可以专业地完成自己的工作,将来会去看看这个叫做Thingsboard的网站。 我可以看到这不是一项免费服务,它具有多种协议连接(请参见下面的屏幕截图),这些协议连接可用于构建系统,例如BACnet,Modbus以及我在此博客中演示的打包的 HTTP(s)请求数据以表格的形式 ……知道此服务是否是物联网网关设备,设置它涉及的程度以及整体可靠性会很有趣。 在实验中需要注意的一个缺点是,当我尝试远程访问pi修改某些代码并重新运行时,我的rasp pi有时会锁定,需要重新启动。 这个过程有点令人沮丧,如果我的远程实验距离数百甚至数千英里,将会非常令人沮丧。 读者要当心 ,如果有需要修改的地方,有比Teamviewer更好的方法可以远程访问rasp pi,请确保发表评论。 从可靠性的角度来看,这是唯一获得良好数据的失败。
![Image for post](https://miro.medium.com/max/9999/1*0aQdPyTiIYjB523jNvYCXg.png)
Thank you for reading, post a comment, and happy coding :)
感谢您的阅读,发表评论和编写愉快的代码:)
c#ras获取宽带账号密码