如果是直接使用ESP8266芯片的话建议直接跳到后面前面的配置就不用看了.
硬件购买传送门
这款的好处是集成了ESP8266模块这就意味着你不需要在单独的去购买并配置ESP8266 WIFI 模块了, 这款板子基于ESP-12E,而且兼容Arduino,有了这款 物联网开发板,我们可以愉快的使用Arduino 方式开发ESP8266,玩转物联网项目.
下载传送门 解压密码: C6FX
drivers/ dpinst-amd64.exe 点击安装就好
http://arduino.esp8266.com/stable/package_esp8266com_index.json
库文件下载地址 密码: RBXX
解压到或者手动添加到:
C:\Users\你电脑的用户名\AppData\Local\Arduino15\staging\packages
知道为什么刚刚要添加开发管理器网址了吧
在里面查找8266添加2.4.0版本
#include <ESP8266WiFi.h>
#define WIFI_SSID "zxy0016zxy" //这里是你自己的wifi名称
#define WIFI_PASSWD "lyf!@#815168" //你自己的wifi密码
void setup() {
Serial.begin(115200); //波特率设置你要在你的串口中设置同样的波特率不然就是乱码
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWD); //这个就是TCP连接一样的通过wifi名字和密码绑定wifi
WiFi.setAutoConnect (true);
WiFi.setAutoReconnect (true);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.println("connecting......");
}
Serial.println("Connected to AP");
delay(3000);
}
void loop() {
if (WiFi.status() == WL_CONNECTED)
{
Serial.println("Zibo Zhao's FYP:");
Serial.println("---------------------------------------------------------");
Serial.println("Connect to router success");
Serial.println(String("local IP is ")+WiFi.localIP().toString());
Serial.println(String("Subnet mask is ")+WiFi.subnetMask().toString());
Serial.println(String("gatewayIP address is ")+WiFi.gatewayIP().toString());
Serial.println(String("dnsIP address is ")+WiFi.dnsIP().toString());
Serial.println(String("SSID is ") + WiFi.SSID());
Serial.println(String("PSK is ") + WiFi.psk());
Serial.println(String("BSSID is ") + WiFi.BSSIDstr());
Serial.println(String("RSSI is ") + WiFi.RSSI());
Serial.println("---------------------------------------------------------");
delay(10000);
}else
{
Serial.println("Connect to router failed");
}
// Wait a bit before scanning again
delay(1000);
}
里面的打印内容基本是IP地址 子网掩码 WIFI入口号等等 就不详细解释了 来看下运行效果
大家不要来蹭我wifi哈
#include <ESP8266WiFi.h>
#define WIFI_SSID "你的wifi名称"
#define WIFI_PASSWD "你的wifi热点"
IPAddress local_static_ip(192, 168, 43, 10); //设置静态连接的IP
IPAddress local_gateway(192, 168, 43, 1); //设置局域网登录地址
IPAddress local_subnet(255, 255, 255, 0); //设置子网IP
IPAddress local_dns_ip(192, 168, 43, 1); //设置DNS IP
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWD);
WiFi.config(local_static_ip, local_gateway, local_subnet, local_dns_ip);
WiFi.setAutoConnect (true);
WiFi.setAutoReconnect (true);
while (WiFi.status() != WL_CONNECTED) //如果没连上每隔一秒重复尝试一次
{
delay(1000);
Serial.println("connecting......");
}
Serial.println("Connected to AP");
Serial.println(String("mac address is ")+WiFi.macAddress());
delay(3000);
}
void loop() {
if (WiFi.status() == WL_CONNECTED) //如果连上了每隔1秒打印一下以下相关信息
{
Serial.println("Connect to router success");
Serial.println(String("Subnet mask is ")+WiFi.subnetMask().toString());
Serial.println(String("gatewayIP address is ")+WiFi.gatewayIP().toString());
Serial.println(String("dnsIP address is ")+WiFi.dnsIP().toString());
Serial.println(String("SSID is ") + WiFi.SSID());
Serial.println(String("PSK is ") + WiFi.psk());
Serial.println(String("BSSID is ") + WiFi.BSSIDstr());
Serial.println(String("RSSI is ") + WiFi.RSSI());
}else
{
Serial.println("Connect to router failed");
}
// Wait a bit before scanning again
delay(1000);
}
wget http://emqtt.com/static/brokers/emqttd-centos7-v2.3.1.zip
yum install unzip -y
unzip emqttd-centos7-v2.3.1.zip
这里报了错误 不要急很有可能是没下载完全所以导致不能解压, 解决措施很简单多下几遍就行了
cd emqttd 进入文件
./bin/emqttd console 安装主文件
screen -ls 查看所有进程
screen -r -d name 进入name进程
ctrl+a d 从进程中脱离
screen -S name 创建名为name的进程
./bin/emqttd_ctl status 注意要在MQTT的安装目录下才能运行
这个18083是用来进入后台的 我们进入screen MQTT进程查看MQTT监控的端口:
访问自己的公网IP:18083进入MQTT后台 账号 admin 密码 public
连接wifi 相关代码
#define WIFI_SSID "zxy0016zxy"
#define WIFI_PASSWD "lyf!@#815168"
void wifiInit()
{
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWD);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.println("WiFi not Connect");
}
Serial.println("Connected to Zibo's wifi");
Serial.println("----------------------------");
Serial.println("Connected to AP");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("espClient [");
Serial.println("----------------------------");
client.setCallback(callback);
}
连接MQTT服务器(在云服务器上的一个进程中运行)
#define MQTT_SERVER "公网ip"
#define MQTT_PORT 1883 /* 默认连接1883端口 TCP通讯*/
#define MQTT_USRNAME "public" /* 用户名*/
/* MQTT 设置*/
#define CLIENT_ID "Zibo Zhao" /* 你的id*/
#define MQTT_PASSWD "public"
#define ALINK_BODY_FORMAT "{\"id\":\"123\",\"version\":\"1.0\",\"method\":\"thing.event.property.post\",\"params\":%s}"
#define ALINK_TOPIC_PROP_POST "/World" /* 发送数据到哪个主题*/
client.setServer(MQTT_SERVER, MQTT_PORT); /* 连接WiFi 之后,连接MQTT 服务器*/
#include <ESP8266WiFi.h>
/* 依赖PubSubClient 2.4.0 */
#include <PubSubClient.h>
/* 依赖ArduinoJson 5.13.4 */
#include <ArduinoJson.h>
#include <DHT.h> //DHT 温湿度传感器库
#define SENSOR_PIN 13
DHT dht13(13, 11);
int TEMP;
/* 连接您的WIFI SSID 和密码*/
#define WIFI_SSID "zxy0016zxy"
#define WIFI_PASSWD "lyf!@#815168"
/* 设备的三元组信息*/
/* 线上环境域名和端口号,不需要改*/
#define MQTT_SERVER "公网ip"
#define MQTT_PORT 1883
#define MQTT_USRNAME "public"
/* MQTT 设置*/
#define CLIENT_ID "Zibo Zhao"
#define MQTT_PASSWD "public"
#define ALINK_BODY_FORMAT "{\"id\":\"123\",\"version\":\"1.0\",\"method\":\"thing.event.property.post\",\"params\":%s}"
#define ALINK_TOPIC_PROP_POST "/World"
unsigned long lastMs = 0;
WiFiClient espClient;
PubSubClient client(espClient);
void callback(char *topic, byte *payload, unsigned int length)
{
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
payload[length] = '\0';
Serial.println((char *)payload);
}
void wifiInit()
{
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWD);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.println("WiFi not Connect");
}
Serial.println("Connected to Zibo's wifi");
Serial.println("----------------------------");
Serial.println("Connected to AP");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("espClient [");
Serial.println("----------------------------");
client.setServer(MQTT_SERVER, MQTT_PORT); /* 连接WiFi 之后,连接MQTT 服务器*/
client.setCallback(callback);
}
void mqttCheckConnect()
{
while (!client.connected())
{
Serial.println("Connecting to MQTT Server ...");
if (client.connect(CLIENT_ID, MQTT_USRNAME, MQTT_PASSWD))
{
Serial.println("MQTT Connected!");
}
else
{
Serial.print("MQTT Connect err:");
Serial.println(client.state());
delay(5000);
}
}
}
/* 绑定设备,标识符“RoomTemp” */
void mqttIntervalPost()
{
char param[500];
char jsonBuf[1023];
boolean d = client.publish(ALINK_TOPIC_PROP_POST, "hahhahahahha");
Serial.println(d);
}
void setup()
{
pinMode(SENSOR_PIN, INPUT);
/* initialize serial for debugging */
Serial.begin(115200);
Serial.println("Demo Start");
wifiInit();
}
// the loop function runs over and over again forever
void loop()
{
mqttCheckConnect();
/* 上报*/
mqttIntervalPost();
client.loop();
delay(5000);
}
这里Python的作用就是把主题上更新的内容给获取回来
#!/usr/bin/python
# -*- coding: utf-8 -*
import paho.mqtt.client as mqtt
import json
# 服务器地址
host = '你的公网ip'
# 通信端口 默认端口1883
port = 1883
username = 'admin'
password = 'public'
# 订阅主题名
topic = '/World'
# 连接后事件
def on_connect(client, userdata, flags, respons_code):
if respons_code == 0:
# 连接成功
print('Connection Succeed!')
else:
# 连接失败并显示错误代码
print('Connect Error status {0}'.format(respons_code))
# 订阅信息
client.subscribe(topic)
# 接收到数据后事件
def on_message(client, userdata, msg):
# 打印订阅消息主题
print("topic", msg.topic)
# 打印消息数据
print(msg.payload.decode())
def main_demo():
client = mqtt.Client()
print('i am here')
# 注册事件
client.on_connect = on_connect
print('i am here 1')
client.on_message = on_message
print('i am here 2')
# 设置账号密码(如果需要的话)
client.username_pw_set(username, password=password)
print('dawdadwad')
# 连接到服务器
client.connect(host, port=port, keepalive=2)
print('dawdawdawdawdawdad')
# 守护连接状态
client.loop_forever()
print('dawdaw222222')
if __name__ == '__main__':
main_demo()
可以看到全部连到了MQTT服务器上
这里是被Python监控程序订阅的主题
这是Arduino程序发到订阅主题ALINL_TOPIC_PROP_POST的消息 (主题设定为 /World)