0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
会员中心
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

怎样将Arduino数据直接存储到MySQL

454398 来源:网络整理 作者:网络整理 2019-11-20 17:08 次阅读
加入交流群
微信小助手二维码

扫码添加小助手

加入工程师交流群

步骤1:设置Arduino

怎样将Arduino数据直接存储到MySQL

刻录以下内容

void setup()

{

Serial.begin(9600);

}

void loop()

{

int i=0,j=0;

i=analogRead(A0);

j=analogRead(A1);

Serial.print(i);

Serial.print(“,”);

Serial.println(i);

}

步骤2:设置启动MySQL

为MySQL安装Wamp服务器并将其配置为存储数据

运行wamp服务器

打开MySQL控制台

选择数据库

然后为您的数据创建表

create table data(sno int(4) primary key auto_increment,LDR int(4),TEMP int(4));

使用desc your_table_name显示表详细信息

desc data;

这就是数据库的全部内容,现在我们可以进行处理了……

第3步:设置处理IDE

下载并安装Processing IDE 2.2.1

将上述给定的ZIP压缩到MyDocuments/Processing/Libraries中

现在打开正在处理的IDE和检查库是否已正确安装(如上图所示)

然后将以下代码复制并进行处理,并自行命名

/*

ARDUINO TO MYSQL THROUGH PROCESSING

Read Serial messages from Arduino then write it in MySQL.

Author : J.V.JohnsonSelva September 2016

*/

import de.bezier.data.sql.*; //import the MySQL library

import processing.serial.*; //import the Serial library

MySQL msql; //Create MySQL Object

String[] a;

int end = 10; // the number 10 is ASCII for linefeed (end of serial.println), later we will look for this to break up individual messages

String serial; // declare a new string called ‘serial’ 。 A string is a sequence of characters (data type know as “char”)

Serial port; // The serial port, this is a new instance of the Serial class (an Object)

void setup() {

String user = “root”;

String pass = “”;

String database = “iot_database”;

msql = new MySQL( this, “localhost”, database, user, pass );

port = new Serial(this, Serial.list()[0], 9600); // initializing the object by assigning a port and baud rate (must match that of Arduino)

port.clear(); // function from serial library that throws out the first reading, in case we started reading in the middle of a string from Arduino

serial = port.readStringUntil(end); // function that reads the string from serial port until a println and then assigns string to our string variable (called ‘serial’)

serial = null; // initially, the string will be null (empty)

}

void draw()

{

while (port.available() 》 0)

{

//as long as there is data coming from serial port, read it and store it

serial = port.readStringUntil(end);

}

if (serial != null)

{

//if the string is not empty, print the following

//Note: the split function used below is not necessary if sending only a single variable. However, it is useful for parsing (separating) messages when

//reading from multiple inputs in Arduino. Below is example code for an Arduino sketch

a = split(serial, ‘,’); //a new array (called ‘a’) that stores values into separate cells (separated by commas specified in your Arduino program

println(a[0]); //print LDR value

println(a[1]); //print LM35 value

function();

}

}

void function()

{

if ( msql.connect() )

{

msql.query( “insert into data(LDR,Temp)values(”+a[0]+“,”+a[1]+“)” );

}

else

{

// connection failed !

}

msql.close(); //Must close MySQL connection after Execution

}

第4步:执行程序。

通过单击“运行”按钮运行程序,请关闭弹出窗口。关闭窗口将停止执行,并在下面的查询中查看在MySQL中存储数据。..

select * from data;

查看数据插入器的数量可以使用下面的查询。

select count(*) from data;

责任编辑:wv

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • MySQL
    +关注

    关注

    1

    文章

    897

    浏览量

    29251
  • Arduino
    +关注

    关注

    190

    文章

    6516

    浏览量

    196053
收藏 人收藏
加入交流群
微信小助手二维码

扫码添加小助手

加入工程师交流群

    评论

    相关推荐
    热点推荐

    工业数据中台支持接入MySQL数据库吗

    工业数据中台完全支持接入MySQL数据库 ,且通过数据同步、集成与治理等技术手段,能够充分发挥MySQL
    的头像 发表于 12-04 11:23 228次阅读
    工业<b class='flag-5'>数据</b>中台支持接入<b class='flag-5'>MySQL</b><b class='flag-5'>数据</b>库吗

    mysql数据恢复—mysql数据库表被truncate的数据恢复案例

    某云ECS网站服务器,linux操作系统,部署了mysql数据库。工作人员在执行数据库版本更新测试时,错误地本应在测试库执行的sql脚本在生产库上执行了,导致部分表被truncate
    的头像 发表于 09-11 09:28 610次阅读
    <b class='flag-5'>mysql</b><b class='flag-5'>数据</b>恢复—<b class='flag-5'>mysql</b><b class='flag-5'>数据</b>库表被truncate的<b class='flag-5'>数据</b>恢复案例

    如何触摸校准存储 SPI 闪存?

    触摸校准存储 SPI 闪存
    发表于 09-04 07:06

    MySQL数据备份与恢复策略

    数据是企业的核心资产,MySQL作为主流的关系型数据库管理系统,其数据的安全性和可靠性至关重要。本文深入探讨
    的头像 发表于 07-14 11:11 494次阅读

    企业级MySQL数据库管理指南

    在当今数字化时代,MySQL作为全球最受欢迎的开源关系型数据库,承载着企业核心业务数据存储与处理。作为数据库管理员(DBA),掌握
    的头像 发表于 07-09 09:50 537次阅读

    MySQL数据库是什么

    MySQL数据库是一种 开源的关系型数据库管理系统(RDBMS) ,由瑞典MySQL AB公司开发,后被Oracle公司收购。它通过结构化查询语言(SQL)进行
    的头像 发表于 05-23 09:18 937次阅读

    数据采集MYSQL和SQLSERVER数据库可以实现哪些功能

    ,确保企业各项数据资产得到合理存储与管理,方便随时调取、搜索,有助于提升管理水平与工作效率。 1.实时监控与预警​ 通过生产设备数据采集
    的头像 发表于 05-07 15:32 427次阅读

    #### KEPServerEX 数据存储插件\\-2

    ,同类型的/同设备的参数可以设置成一样的,这样存储数据库之后,能够更方便的在数据库中进行检索。 默认“数据项ID”是整形
    发表于 04-09 16:09

    NXP MCU RT1166如何使用JTAG/SWD工具数据存储内部闪存中?

    我需要使用 JTAG 数据存储内部闪存中。我能够使用 JTAG/SWD 工具 ARM J-Link 将它们存储在 FlexSPI1 连接
    发表于 04-01 06:54

    水电表通过数据采集平台接入MySQL数据

    数字化、智能化的能耗监测管理系统十分有必要。 对此,数之能提供基于数据采集平台实现水电表接入MySQL数据库的解决方案,将用水、用电等能耗数据
    的头像 发表于 02-26 13:51 611次阅读

    从Delphi、C++ Builder和Lazarus连接到MySQL数据

    平台。 基于 MyDAC 的应用程序可以直接连接到 MySQL 服务器或通过 MySQL 客户端库工作。MySQL 数据访问组件旨在帮助程序
    的头像 发表于 01-20 13:47 1356次阅读
    从Delphi、C++ Builder和Lazarus连接到<b class='flag-5'>MySQL</b><b class='flag-5'>数据</b>库

    使用插件Excel连接到MySQL/MariaDB

    ,可以快速地数据MySQL 或 MariaDB 加载到 Excel,立即从数据库刷新 Excel 工作簿中的数据,编辑这些
    的头像 发表于 01-20 12:38 1154次阅读
    使用插件<b class='flag-5'>将</b>Excel连接到<b class='flag-5'>MySQL</b>/MariaDB

    适用于MySQL和MariaDB的Python连接器:可靠的MySQL数据连接器和数据

    和 Linux 的 wheel 包分发。 直接连接 该解决方案使您能够通过 TCP/IP 建立与 MySQL 或者 MariaDB 数据库服务器的直接连接,而无需
    的头像 发表于 01-17 12:18 857次阅读
    适用于<b class='flag-5'>MySQL</b>和MariaDB的Python连接器:可靠的<b class='flag-5'>MySQL</b><b class='flag-5'>数据</b>连接器和<b class='flag-5'>数据</b>库

    MySQL数据库的安装

    MySQL数据库的安装 【一】各种数据库的端口 MySQL :3306 Redis :6379 MongoDB :27017 Django :8000 flask :5000 【二】
    的头像 发表于 01-14 11:25 878次阅读
    <b class='flag-5'>MySQL</b><b class='flag-5'>数据</b>库的安装

    数据数据恢复—Mysql数据库表记录丢失的数据恢复流程

    Mysql数据库故障: Mysql数据库表记录丢失。 Mysql数据库故障表现: 1、
    的头像 发表于 12-16 11:05 1013次阅读
    <b class='flag-5'>数据</b>库<b class='flag-5'>数据</b>恢复—<b class='flag-5'>Mysql</b><b class='flag-5'>数据</b>库表记录丢失的<b class='flag-5'>数据</b>恢复流程