[mw_shl_code=cpp,true]/*
* from:
https://github.com/Gabriellavoura/Mqtt-and-Sim800-900
Basic MQTT example
- connects to an MQTT server
- publishes "hello world" to the topic "test/out"
- subscribes to the topic "test/in"
*******************************
* 2017-01-31
* 采用sim808+leo板,lm35接入,mqtt协议测试,为gps数据上送做准备
* 序列号:4G***********OBH(请用您申请到的设备序列号,个人用户是免费的)
* json格式:{"sensorDatas":[{"value":23.04}]}
* 上送间隔:20S
* LED13显示上送过程,闪动一次上送一次,如果灯灭则本次十组数据上送完毕
*/
//定义数据接入点,本例采用中国移动
#define GSMAPN "cmnet"
#define GSMUSER ""
#define GSMPASSWORD ""
//定义库,重点是 <sim800Client.h>
#include <Time.h>
#include <sim800Client.h>
#include <PubSubClientHotlog.h> //此处做了修改,原例题为#include <PubSubClien.h>编译报错
#include <TimeAlarms.h> //原例题使用,本文未用,但为了编译顺利,没有去除
sim800Client s800;//定义一个client实例
char imeicode[16];
char server[] = "t.tlink.io";
//*********关于连接的管理
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 10*2000; // delay between 2 datapoints, 20s
int sendnum=1; //发送次数
int sendsum=10;//发送总次数,10次
//关于LM35变量*************温度传感器部分设置
int potPin = 4; //设置模拟口4为LM35的信号输入端口
float temperature = 0; //设置temperature为浮点变量
long val=0; //设置val为长整数变量
void callback(char* topic, byte* payload, unsigned int length) {
// handle message arrived
char mypl[48];
Serial.println(length);
memcpy(mypl,payload,length);
mypl[length]=char(0);
Serial.print("receive: ");
Serial.print(topic);
Serial.print("->");
Serial.println(mypl);
}
PubSubClient client(server, 1883, callback, s800); //设置一个client实例,命名为s800,连接.tlink.io:1883
void setup()
{
Serial.begin(9600);
Serial.println("SIM800 Shield testing.");
pinMode(13, OUTPUT); //用于显示数据上送
for (int i=0; i<10; i++){
delay(5000);
Serial.println("try to init sim800");
//判断是否是硬串口,leo采用serial1
#ifdef HARDWARESERIAL
if (s800.init( 7, 6)) break;
#else
if (s800.init(&Serial1 , 7, 6)) break;
#endif
}
Serial.println("try to setup sim800");
s800.setup();
s800.stop();
s800.TCPstop();
s800.getIMEI(imeicode);
Serial.print("IMEI: ");
Serial.println(imeicode);
//建立TCP连接,接入点cmnet
while (!s800.TCPstart("cmnet","","")) {
Serial.println("TCPstart failed");
s800.TCPstop();
delay(1000);
}
Serial.println("TCPstart started");
while (!client.connect(imeicode)) {
Serial.println("connect failed");
delay(1000);
}
Serial.println("connected");
}
void loop()
{
val=analogRead(potPin);//温度传感器LM35接到模拟PIN4上;val变量为从LM35信号口读取到的数值
delay(200);
if((millis() - lastConnectionTime > postingInterval)&(sendnum<sendsum)) { //达到发送间隔时间且不到sendsum次,则发送
temperature = (val*0.0048828125*100);//温度转换,系数基于5V--1024的比例关系,详见极客工坊温度传感器笔记,本文未做标定
sendData(temperature);
sendnum=sendnum+1;
}
client.loop();
}
void sendData(float thisData) {
String msg = "{\"sensorDatas\":[{\"value\":"; //mqtt head
char s_temp[5];
dtostrf(thisData,2,2,s_temp);//把温度值转换为两位整数,两位小数的字符串
msg+= s_temp;
msg+="\" }] }";//结束符,具体在tlink中API定义
char mqttmsg[40];
msg.toCharArray(mqttmsg,40); //Copies the string's characters to the supplied buffer.
delay(1000);
//发布一组数据,标题是设备序列号,内容是json格式的传感器数据
client.publish("4G***********OBH",mqttmsg);
lastConnectionTime = millis();
Serial.println(mqttmsg);
digitalWrite(13, HIGH); // LED13闪动,显示数据上送
delay(1000);
digitalWrite(13, LOW);
}[/mw_shl_code]