[mw_shl_code=cpp,true]/*
### Connect TCP and send GET request.
1. This example is used to test DFRobot_SIM808 GPS/GPRS/GSM Shield's connect TCP and send GET request.
2. Open the SIM808_TCPConnection example or copy these code to your project
3. Download and dial the function switch to Arduino
4. Open serial helper
5. Waiting for a few minutes, until serial has sent "Connect mbed.org success"
6. Serial will send "Hello world!"
create on 2016/09/23, version: 1.0
by jason
*****利用gprs,TCP协议向tlink.io发送温度数据
*date: 2016-12-25
利用TCP协议,上传温度数据至tlink.io
http://www.tlink.io/ 8647端口
设备:ID:7615
序列号:O****************34//(代换您自己的设备序列号)
by 沧海笑1122
*/
//*********dfsim808的设置
#include <DFRobot_sim808.h>
DFRobot_SIM808 sim808(&Serial);
//*************温度传感器部分设置
int potPin = 4; //设置模拟口4为LM35的信号输入端口
float temperature = 0; //设置temperature为浮点变量
long val=0; //设置val为长整数变量
//*********关于连接的管理
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次
//Declare pin 12 as a power control pin for DFSIM808
int pinPowerKey = 12; //可以写一个打开SIM808电源的函数,D12
void setup(){
Serial.begin(9600);
//******** Initialize sim808 module *************
while(!sim808.init()) {
delay(1000);
Serial.print("Sim808 init error\r\n");
}
delay(3000);
//*********** Attempt DHCP *******************
while(!sim808.join(F("cmnet"))) { //登录中移动网络gprs
Serial.println("Sim808 join network error");
delay(2000);
}
//************ Successful DHCP ****************获取IP
Serial.print("IP Address is ");
Serial.println(sim808.getIPAddress());
}
void loop(){
val=analogRead(potPin);//温度传感器LM35接到模拟PIN4上;val变量为从LM35信号口读取到的数值
delay(200);
if((millis() - lastConnectionTime > postingInterval)) { //达到发送间隔时间,则发送
temperature = (val*0.0048828125*100);//温度转换,系数基于5V--1024的比例关系,详见极客工坊温度传感器笔记,未标定
sendData(temperature);
}
}
// this method makes a TCP connection to the server:
void sendData(float thisData) {
// if there's a successful connection:
//*********** Establish a TCP connection ************建立TCP连接
if(!sim808.connect(TCP,"t.tlink.io",8647)) {
Serial.println("Connect error");
sim808.close();// 释放TCP
}else{
Serial.println("Connect
www.tlink.io success");
//*********** Send a GET request *****************
delay(2000);
String cmd = "#RTU,";//报文头,具体在tlink中定义
char s_temp[5];
dtostrf(thisData,2,2,s_temp);//把温度值转换为两位整数,两位小数的字符串
cmd+= s_temp;
cmd+="#";//结束符,具体在tlink中定义
char tcpcmd[12];
cmd.toCharArray(tcpcmd,12); //Copies the string's characters to the supplied buffer.
//测试语句,运行时可以去除
Serial.print("TCP CMD------------");
Serial.print(cmd);
Serial.print(tcpcmd);
Serial.println("waiting to fetch...");
sim808.send(O****************34//(代换您自己的设备序列号), sizeof(O****************34//(代换您自己的设备序列号)-1); //发送设备的ID号,即建
立与该设备的对应,下面的数据即发送至该设备
sim808.send( tcpcmd, sizeof(tcpcmd)-1); //发送第一次温度数据
lastConnectionTime = millis();
delay(2000);
//************* Close TCP or UDP connections **********一个短连接结束
sim808.close();// 释放TCP
delay(1000);
}
}[/mw_shl_code]