wemos(8266)+Mqtt+MySQL实现环境光照强度上传数据库

Wemos+云+数据库MySQL

注:本文系湛江市第十七中学星火创客团队及岭南师范学院物联网俱乐部原创部分参赛项目,转载请保留声明。

一、项目硬件准备

1、一块wemos开发板
在这里插入图片描述

2、一个光照传感器
在这里插入图片描述

材料名称数量
Wemos一块
光照传感器一个
杜邦线若干
二、项目软件准备

1、一个云服务器+创好的MySQL(安装宝塔可快速搭建数据库)
2、mqtt x测试软件
3、arduino IDE

三、项目开始
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
  1. Install
unzip emqx-ee-ubuntu20.04-4.2.5-x86_64.zip
  1. 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.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 A0

void setup_wifi() {

  delay(10);
  // We start by connecting to a WiFi network
  Serial.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 character
  if ((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 reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP8266Client-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    
    if (client.connect(clientId.c_str())) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish(pubTopic, "hello world");
      // ... and resubscribe
      client.subscribe("inTopic");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup() {
  pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
  pinMode(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));//要上传的数据,这里是sun
    Serial.print("Publish message: ");
    Serial.println(msg);
    client.publish(pubTopic, msg);
  }
}

2.3效果展示
(1)硬件运行效果
在这里插入图片描述

(2)mqttx
在这里插入图片描述

(3)规则监测
在这里插入图片描述

(4)数据库情况
在这里插入图片描述

3.总结

通过这个项目可以了解到,运用wemos、云数据库MySQL、mqtt协议可以实现把采集到的光照强度上传到MySQL,其中涉及emqx的资源和规则创建、mqttx的测试和最后的硬件实现数据上传,数据格式JSON。首先得有一个云服务器(如果没有也可以,利用电脑的虚拟机进行操作也可以,但是要注意ip问题,这是网络的常识),利用它安装好宝塔(方便相关的操作,比如开放端口)进行MySQL的安装和MySQL的表格创建,然后安装好EMQ X Dashboaord进行相关的资源(即连接好服务器的MySQL)和规则(即连接好数据库的表和相关的数据,还有定义的topic)创建,下一步就是利用mqttx进行测试,最后就到硬件部分进行相关的数据采集和上传。总体来说,还是比较好玩的,欢迎各位大佬来批评指正!

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

X 、case

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值