python - 通过 Flask API 将 Arduino 传感器值发布到本地 Sqlite 数据库

标签 python c++ flask arduino

我有一个 Arduino 脚本,它从连接到我的 MKR1000 的光传感器获取 bool 值。

#include <SPI.h>
#include <WiFi101.h>

char ssid[] = "networkname";        // network SSID (name)
char pass[] = "networkpassword";    // network password (use for WPA, or use as key for WEP)
int keyIndex = 0;            // network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;
char server[] = "192.111.1.11";    // name address for Google (using DNS)

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
WiFiClient client;

void setup()
{
  pinMode(6, INPUT);

   //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only

   while (status != WL_CONNECTED) {
   Serial.print("Attempting to connect to SSID: ");
   Serial.println(ssid);
   // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
   status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
   delay(10000);
  }

  Serial.println("\nStarting connection to server...");
  // if you get a connection, report back via serial:
  if (client.connect(server, 5000)) {
    Serial.println("connected to server");
    // Make a HTTP request:
    client.println("GET /search?q=arduino HTTP/1.1");
    client.println("Host: 192.111.1.11");
    client.println("Connection: close");
    client.println();
  }
}
}

void loop()
{
  bool gotLight = (digitalRead(6) == LOW);

  if (gotLight == 1) {
  String PostDataTrue = "TRUE";
  client.connect("https://192.111.1.11/5000/lights/1",5000);
  client.println(PostDataTrue);
   }

  if (gotLight == 0) {
  String PostDataFalse = "FALSE";
  client.connect("https://192.111.1.11/5000/lights/0",5000);
  client.println(PostDataFalse);
  }

//  Serial.println(gotLight);
  delay(1000); 
}

我还有一个用于运行基本 API 和 Sqlite 数据库的 Flask 脚本。

from flask import Flask
import sqlite3

app = Flask(__name__)

@app.route('/lights/0')
def hello_world():
    return 'lights off!'

@app.route('/lights/1')
def hello():
    return 'lights on!'

if __name__ == '__main__':
    app.run()

DATABASE = '/Users/reallymemorable/Desktop/reTest.db'

def get_db():
    db = getattr(g, '_database', None)
    if db is None:
        db = g._database = sqlite3.connect(DATABASE)
    return db

@app.teardown_appcontext
def close_connection(exception):
    db = getattr(g, '_database', None)
    if db is not None:
        db.close()

我认为大部分 .ino 文件都是正确的,但我不知道如何让 void loop() 中的部分向我的发出 POST 请求API 而不是仅在我的串行监视器中生成输出——这就是当前发生的情况。

基本上,我希望我的 Arduino 不断将新数据发布到 Sqlite 数据库。

我对此很陌生,所以我不确定接下来要尝试什么步骤,如果有任何建议,我将不胜感激。

最佳答案

flask 好用吗?

而且,https://192.111.1.11/5000/lights/1 是正确的吗?

如果端口是 5000,https://192.111.1.11:5000/lights/1 将是正确的。

关于python - 通过 Flask API 将 Arduino 传感器值发布到本地 Sqlite 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59080080/

相关文章:

python - 如何替换 Python 中第一次出现的正则表达式?

Python setup.py 仅用于安装所需的包

c++ - 具有无符号短整数的结构的大小

c++ - 为什么在指向对象的指针上调用方法与在对象本身上调用方法具有不同的行为?

nginx + uwsgi + debian + 守护进程

python - 在 python 中从 JSON 数据创建字典列表

Python:使用sendmail以html格式发送邮件

python - 将 int 打包到字符串中

python - 如何监控传出的 HTTPS

python - Flask 领事和领事 DNS