本文主要是介绍wemos+mptt+mysql实现环境光照强度上传数据库,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Wemos+云+数据库MySQL
一、项目硬件准备
1、一块wemos开发板
2、一个光照传感器
3、若干杜邦线
二、项目软件准备
1、一个云服务器+创好的MySQL(安装宝塔可快速搭建数据库)
2、mqtt x测试软件
三、项目开始
1、软件部分
(1)安装宝塔:https://www.bt.cn/download/linux.html
注:安装适合自己的版本
(2)在宝塔开放相关端口
这里开放了18083、1883、8080等端口,其中18083用来打开EMQ X Dashboaord,1883用来mqtt协议通信。
(3)创建MySQL数据库
(4)建立相关数据库的表(我这里主要是上传关照强度的值)
相关的SQL代码:
CREATE TABLE `sun_test` (`id` int(11) unsigned NOT NULL AUTO_INCREMENT,`up_timestamp` timestamp NULL DEFAULT NULL,`client_id` varchar(32) DEFAULT NULL,`sun` float unsigned DEFAULT NULL,PRIMARY KEY (`id`),KEY `up_timestamp_client_id` (`up_timestamp`,`client_id`)
) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8mb4;
1.2、进入云平台的安全组开放相关端口(注:主要是因为端口开放不了,所以只能在这里开放)
这一步可有可无,开具体的运行情况,如果出现发送数据后接收不到,或者链接不上mqtt x等等,可以考虑是端口没有开放问题。
1.3、进入EMQ X Dashboaord(你的ip:18083,即可进入emqx的mqtt代理网站),同时创建好相关的资源和规则。
(1)下载安装EMQ X Dashboaord:https://www.emqx.io/downloads
(2)相关的Linux指令:
1.Download emqx-ee-ubuntu20.04-4.2.5-x86_64.zip SHA256
wget https://www.emqx.io/downloads/enterprise/v4.2.5/emqx-ee-ubuntu20.04-4.2.5-x86_64.zip
2
Install
unzip emqx-ee-ubuntu20.04-4.2.5-x86_64.zip
3
Run
./bin/emqx start
1.3.1、安装好EMQ X Dashboaord之后,进行资源和规则设置
(3)资源
(4)规则
相关的SQL代码
SELECT timestamp as up_timestamp, clientid as client_id, payload.sun as sun FROM "testtopic/sun"
相关代码:
insert into sun_test(up_timestamp, client_id, sun) values (FROM_UNIXTIME(${up_timestamp}/1000), ${client_id}, ${sun})
1
1.4、利用mqtt x进行测试
(1)下载软件安装:https://mqttx.app/cn/
(2)进行相关的配置(分为连接服务器和创建topic)
1.5、测试的数据上传效果
(1)数据库
2、硬件部分
2.1硬件接线
wemos 光照强度传感器
3.3v ------------------ vcc
gnd ------------------ gnd
A0 ------------------ data
2.2相关代码
(1)arduino代码
#include <ESP8266WiFi.h>
#include <PubSubClient.h>// Update these with values suitable for your network.//连接wifi
const char* ssid = "602iot";
const char* password = "18wulian";
const char* mqtt_server = "xxx.xxx.xxx.xxx";//云服务器ip//自行添加的订阅
const char* pubTopic = "testtopic/sun";WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];#define Lux_Pin A0void setup_wifi() {delay(10);// We start by connecting to a WiFi networkSerial.println();Serial.print("Connecting to ");Serial.println(ssid);WiFi.begin(ssid, password);while (WiFi.status() != WL_CONNECTED) {delay(500);Serial.print(".");}randomSeed(micros());Serial.println("");Serial.println("WiFi connected");Serial.println("IP address: ");Serial.println(WiFi.localIP());
}void callback(char* topic, byte* payload, unsigned int length) {Serial.print("Message arrived [");Serial.print(topic);Serial.print("] ");for (int i = 0; i < length; i++) {Serial.print((char)payload[i]);}Serial.println();// Switch on the LED if an 1 was received as first characterif ((char)payload[0] == '1') {digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level// but actually the LED is on; this is because// it is active low on the ESP-01)} else {digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH}}void reconnect() {// Loop until we're reconnectedwhile (!client.connected()) {Serial.print("Attempting MQTT connection...");// Create a random client IDString clientId = "ESP8266Client-";clientId += String(random(0xffff), HEX);// Attempt to connectif (client.connect(clientId.c_str())) {Serial.println("connected");// Once connected, publish an announcement...client.publish(pubTopic, "hello world");// ... and resubscribeclient.subscribe("inTopic");} else {Serial.print("failed, rc=");Serial.print(client.state());Serial.println(" try again in 5 seconds");// Wait 5 seconds before retryingdelay(5000);}}
}void setup() {pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an outputpinMode(Lux_Pin, INPUT);Serial.begin(115200);setup_wifi();client.setServer(mqtt_server, 1883);client.setCallback(callback);
}void loop() {if (!client.connected()) {reconnect();}client.loop();long now = millis();if (now - lastMsg > 2000) {lastMsg = now;snprintf (msg, 50, "{\"sun\": \"%d\"}", analogRead(Lux_Pin));//要上传的数据,这里是sunSerial.print("Publish message: ");Serial.println(msg);client.publish(pubTopic, msg);}
}
2.3效果展示
(1)硬件运行效果
(2)mqqtx
(3)规则监测
(4)数据库情况
3.总结
通过这个项目可以了解到,运用wemos、云数据库MySQL、mqtt协议可以实现把采集到的光照强度上传到MySQL,其中涉及emqx的资源和规则创建、mqttx的测试和最后的硬件实现数据上传,数据格式JSON。
这篇关于wemos+mptt+mysql实现环境光照强度上传数据库的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!