(SKU: TEL0118)Gravity: OBLOQ - IoT Module 物联网模块

来自DFRobot Product Wiki
跳转至: 导航搜索
文件:TEL0118.jpg
Gravity: OBLOQ 物联网模块

目录

简介

物联网是将嵌入式系统技术、移动技术、网页技术全都融合在一起的,其发展的灵魂是以用户体验为核心,特点是与硬件、网络、平台、服务等完全不同的产业领域的相关者直接合作或者融合。
随着物联网的发展,其热度不断增加,目前国内外已有多个成熟物联网平台,但绝大部分物联网平台都是面向专业开发人员,操作复杂,上手困难。为此我们推出了OBLOQ物联网模块,搭配DFRobot自有的物联网平台,大大降低了物联网的使用门槛。
OBLOQ是一款基于ESP8266设计的串口转WIFI物联网模块,用以接收和发送物联网信息。尺寸紧凑,价格低,接口简单,即插即用,适用于3.3V~5V的控制系统。软件编程简单,无需复杂的基础知识,就能迅速搭建出一套物联网应用。
板载固件升级开关,可方便的进行固件的升级。

技术规格

  • 供电电压:3.3~5.0V
  • 工作电流:<240mA
  • 接口类型:Gravity UART 4PIN
  • 接口速率:自适应(4800,9600,38400,115200)
  • 无线模式:IEEE802.11b/g/n
  • 加密类型:WPA WPA2/WPA2–PSK
  • 无线频率:2.4GHz
  • 产品尺寸:35mm * 32mm / 1.38inch * 1.26inch
  • 内置协议: TCP/IP 协议栈

引脚说明

OBLOQ引脚说明
表名
标号 名称 功能描述
1 TX 串口发送端
2 RX 串口接收端
3 GND
4 VCC 电源


基础教程

OBLOQ模块具备的两个基础功能:发送数据到物联网和接收物联网数据。
下面通过实验演示这两个功能:Arduino读取按键状态并通过OBLOQ模块发送状态信息到物联网,Arduino再通过OBLOQ接收物联网上的按键状态信息后控制小灯的亮灭,实现按键按一次小灯亮,再按一次小灯灭的效果。
通过完善程序,可实现更强大的功能,如远程控制设备:控制家里的台灯,空调,微波炉等。


准备工作



  • 创建物联网设备
    • 点击链接:物联网,注册账号并登陆。
    • 登录后,点击菜单栏的工作间,进入工作间后可点击“+”号创建物联网设备,程序中通过绑定设备的Topic来完成对特定设备的消息发送和接收。
    • 下面演示创建一个设备Button:
      • 1.进入菜单栏工作间
      • 2.点击“+”号创建物联网设备,创建成功会出现一个新设备,第一次创建设备名为新设备1
      • 3.将鼠标移动至设备名,会出现铅笔状小图标,点击铅笔小图标可修改设备名,修改为Button。
      • 4.记录账号相关信息:Iot_id(user),Iot_pwd(password),Client ID和设备Topic,其中Iot_id和Iot_pwd可点击左侧状态栏的小眼睛图标查看。


  • 硬件连接

OBLOQ模块的TX,RX,GND和VIN引脚分别连接到Arduino UNO的D10,D11,GND和VCC引脚,数字按键连接到D2引脚。可通过修改程序来自定义相关引脚,此次测试硬件连接参考下图:


样例代码

修改代码中以下信息,然后烧录代码到Arduino UNO.


// parameters of iot-web, you need change all these according to your own situations
String ssid = "test";                    //wifi账号
String password = "test";                //wifi密码
String client_id = "4bea727a13ef3f1b";   //client_id,创建设备的时候记录的相关信息
String iot_id = "r1qHJFJ4y";             //iot_id,创建设备的时候记录的相关信息
String iot_pwd = "SylqH1Y1Vt";           //iot_pwd,创建设备的时候记录的相关信息
String topic = "S1pMPGNR5";              //topic,创建设备的时候记录的相关信息
String host = "iot.dfrobot.com.cn";      //Iot主机地址,此处不用更改
String host_port = "1883";               //Iot连接端口,此处不同更改

  • 样例代码

/*
   +----------------------------------------------------------------------+
   | This program builds MQTT connection between OBLOQ and IOT host       |
   | "iot.dfrobot.com.cn".                                                |
   | You can use a button sending "1"/"0" message to IOT-web.             |
   | Your controller, Arduino UNO, can receive messages from the IOT-web. |
   | Both button and IOT-web can control LED to turn on/off.              |
   | When the controller receives "1", the LED on board turns on;         |
   | when it receives "0", the LED turns off.                             |
   +----------------------------------------------------------------------+
   | contact me : amy.liao@dfrobot.com                                    |
   +----------------------------------------------------------------------+
   | Copyright (c) 2017                                                   |
   +----------------------------------------------------------------------+
   | mainController: Arduino Uno                                          |
   | wireless module: OBLOQ                                               |  
   | IOTwebsite: iot.dfrobot.com.cn                                       |
   +----------------------------------------------------------------------+
   | Author: Amy Liao && Jason Ling                                       |
   +----------------------------------------------------------------------+
 */

#include <Arduino.h>
#include  <SoftwareSerial.h>

/**********************************************************************************/
/*PARAMETERS*/
/**********************************************************************************/

// parameters of iot-web, you need change all these according to your own situations
String ssid = "test";                    //wifi账号
String password = "test";                //wifi密码
String client_id = "4bea727a13ef3f1b";   //client_id,创建设备的时候记录的相关信息
String iot_id = "r1qHJFJ4y";             //iot_id,创建设备的时候记录的相关信息
String iot_pwd = "SylqH1Y1Vt";           //iot_pwd,创建设备的时候记录的相关信息
String topic = "S1pMPGNR5";              //topic,创建设备的时候记录的相关信息
String host = "iot.dfrobot.com.cn";      //Iot主机地址,此处不用更改
String host_port = "1883";               //Iot连接端口,此处不同更改

// parameters of Arduino Pins
char ledPin = 13;
char buttonPin = 2;
SoftwareSerial softSerial(10, 11);
//parameters of self-build functions
typedef void (* Fun_ptr)(char *);
char message[256] = { 0 };
bool is_pinged = false;//is_pinged checks whether Arduino is connected with OBLOQ, once connected, it turns true
bool is_subscribed = false;

enum ObloqState
{
	WAIT = 0,//default state
	PING_OK = 1,//then connect wifi
	WIFI_OK = 2,//then connect mqtt
	MQTT_OK = 3,//then subscribe the topic
	SUBSCRIBE_OK = 4,//try publish message to the topic, then WAIT for new uart message
	GOT_MSG = 5//receive message from IOT web
} obloq_state;


/**********************************************************************************/
/*FUNCTIONS*/
/**********************************************************************************/

//functions that Arduino used to communicate with obloq
void ping()
{
	static unsigned long currentTime = 0;
	if (!is_pinged && millis() - currentTime > 2000)
	{
		currentTime = millis();
		softSerial.print("|1|1|\n");
		Serial.print("ping ");
	}
}

void connectWifi(const String &ssid, const String &password)
{
	softSerial.print("|2|1|" + ssid + "," + password + "|\n");
}

void connectMqtt(const String & client_id, const String & iot_id, const String & iot_pwd, const String & host, const String & host_port)
{
	softSerial.print("|4|1|" + client_id + "|" + iot_id + "|" + iot_pwd + "|" + host + "|" + host_port + "|\n");
}

void subscribeMqtt(const String & topic)
{
	softSerial.print("|4|3|" + topic + "|\n");
}

void publishMqtt(const String & topic, const String & message)
{
	softSerial.print("|4|4|" + topic + "|" + message + "|\n");
	Serial.print("Publish to topic--");
	Serial.print(topic);
	Serial.print("--message: ");
	Serial.println(message);	
}


//below are basic Arduino functions

//Once got message from iot-web/OBLOQ, save it in the message array for further purpose
void saveMsg(const char* iot_msg)
{
	//extract message received   
	char *location = strchr(iot_msg, '|')+1;
	for (int i = 3; location != nullptr && i > 0; --i)
	{
		location = strchr(location, '|')+1;
	}
	
	for (int i = 0; location != nullptr && *location != '|'; location++, i++)
	{
		message[i] = *location;
	}
	Serial.print("Message received:");
	Serial.println(message);
}

//clear saved message
void clearMsg()
{
	char *p = message;
	for (int i = 0; i < 256; ++i)
	{
		*(p++) = 0;
	}
}

//Arduino process message received from iot-web/OBLOQ 
bool processMsg(char *message, Fun_ptr action)
{
	if (message != nullptr)
	{
		action(message);
		clearMsg();
		return true;
	}
	else
	{
		clearMsg();	
		return false;
	}
}

//listening what is returned from iot-web/OBLOQ and set different obloq_state
void handleUart()
{
	while (softSerial.available() > 0)
	{
		String receivedata = softSerial.readStringUntil('\r');
		const char* iot_msg = receivedata.c_str();

		if (strcmp(iot_msg, "|1|1|") == 0)
		{
			Serial.println("Pong");
			is_pinged = true;
			obloq_state = PING_OK;
		}
		else if (strstr(iot_msg, "|2|3|") != nullptr)
		{
			Serial.println("Wifi ready");
			obloq_state = WIFI_OK;
		}
		else if (strcmp(iot_msg, "|4|1|") == 0)
		{
			Serial.println("Mqtt ready");
			obloq_state = MQTT_OK;
		}
		else if (strcmp(iot_msg, "|4|3|") == 0)
		{
			Serial.print("Topic:");
			Serial.print(topic);
			Serial.println(" subscribed");
			is_subscribed = true;
		}
		else if (strcmp(iot_msg, "|4|4|") == 0)
		{
			Serial.println("Message sent");
		}
		else if (strstr(iot_msg, "|4|5|") != nullptr)
		{
			saveMsg(iot_msg);
			obloq_state= GOT_MSG;		
		}
	}
}

//Arduino turn on/off LED by reading what is returned from iot-web/OBLOQ
//message == strings, characters or 0 (off); message == other numbers (on);
static void changeLed(char * message)
{
	if (message != nullptr)
	{		
		int state = atoi(message);
		(0 == state) ? digitalWrite(ledPin, LOW) : digitalWrite(ledPin, HIGH);
	}
	else
	{
		Serial.println("invalid message");
	}
}

//Press button, Arduino will switch sending "1"/"0" to iot-web one by one
void sendMsgButtonTrigger()
{
	static bool is_truely_pressed = false;
	static bool looks_like_pressed = false;
	static bool light_state = false;
	static char normal_vol = 0;
	static unsigned long currentTime = millis();

	looks_like_pressed = (digitalRead(buttonPin) != normal_vol) ? true : false;
	
	if(looks_like_pressed)
	{		
		if (millis() - currentTime > 10)
		{
			currentTime = millis();
			if(digitalRead(buttonPin) != normal_vol) 
			{
				while(digitalRead(buttonPin) != normal_vol);
				is_truely_pressed = true;
			}
			looks_like_pressed = false;
		}					
	}

	if(is_truely_pressed)
	{	
		is_truely_pressed = false;
		light_state = !light_state;
		light_state ? publishMqtt(topic, "1") : publishMqtt(topic, "0");
	}	
}

//update once in a Arduino loop
void update()
{
	if (!is_pinged)
	{
		ping();
	}
	handleUart();
	switch(obloq_state)
	{
		case PING_OK: connectWifi(ssid, password); obloq_state = WAIT; break;
		case WIFI_OK: connectMqtt(client_id, iot_id, iot_pwd, host, host_port); obloq_state = WAIT; break;
		case MQTT_OK: subscribeMqtt(topic); obloq_state = WAIT; break;
		//When new message received, below GOT_MSG branch will turn on, the Led function in processMsg() can be changed to other function 
		case GOT_MSG: processMsg(message,changeLed); obloq_state = WAIT; break;
		default: obloq_state = WAIT; break;
	}
	
	//try publish one test message once subscribed
	if (is_subscribed)
	{
		static bool flag = true;
		if (flag)
		{
			publishMqtt(topic, "test_publish_message @ 12:04");
			flag = false;
		}
	}
	
	//if you want use button to control LED on or off, enable this
	sendMsgButtonTrigger();
}

/**********************************************************************************/
/*ARDUINO ENTRANCE*/
/**********************************************************************************/
void setup()
{
	// 115200 baud rate of software serial, which communicate with OBLOQ, is not stable. 9600 is more recommended.
	softSerial.begin(9600);
	Serial.begin(9600);
	// setup led
	pinMode(ledPin, OUTPUT);
	pinMode(buttonPin, INPUT);
}

void loop()
{
	update();
}

结果演示

程序烧录成功后,复位OBLOQ模块,再复位Arduino。当没有连接wifi的时候,OBLOQ指示灯显示红色,连接到wifi后,OBLOQ指示灯显示绿色。当指示灯变成绿色的时候,按下按键,可以看到板子连接在D13引脚的小灯亮起来,再按一下,小灯灭掉。
同时,登陆物联网,进入工作间,点击Button设备下面的查看详情可查看按键发送到物联网的消息,下面是串口打印的数据!

OBLOQ串口消息.png


常见问题

还没有客户对此产品有任何问题,欢迎通过qq或者论坛联系我们!


更多问题及有趣的应用,可以 访问论坛 进行查阅或发帖。


更多


DFshopping car1.png [Link DFRobot商城购买链接]

个人工具
名字空间

变换
操作
导航
工具箱