分享一种不基于类库的连接物联网的Arduino联网代码
虽然各个物联网平台都提供了封装好的联网代码,但是有时以加载第三方类库,Arduino Nano程序内存就不够了。所以就自己写了相关的程序。
本文实例基于Arduino Nano+EPS8266.
程序使用了虚拟串口。
#include <SoftwareSerial.h>
SoftwareSerial WIFISerial(9, 8);//RX, TX
char gBuffer = {0,};
// the setup routine runs once when you press reset:
void setup() {
Serial.begin(9600);
Serial.println("Little Thinking");//
WIFISerial.begin(9600);
// WIFISerial.println("AT+RST");//
readAndWriteSerial();
}
void readAndWriteSerial() {
String buf = "";
unsigned chariB = 0;
unsigned charlen = -1;
char cC = {0,};
while (WIFISerial.available() || Serial.available())
{
if (WIFISerial.available())
{
for (int i = 0; i < 3; i++)
{
cC = WIFISerial.read();
delay(1);
}
Serial.write(cC);
Serial.write(cC);
Serial.write(cC);
if ((cC == 'v') && (cC == 'a') && (cC == 'l')
||
(cC == 'a') && (cC == 'l') && (cC == 'u') ||
(cC == 'l') && (cC == 'u') && (cC == 'e')
) {
iB++;
len = 1;
}
if ((iB == 1) && (len >= 1) && (len < 12)) {
buf += cC;
buf += cC;
buf += cC;
len += 3;
}
}
if (Serial.available())
{
char c = Serial.read();
delay(2);
if ('A' == c)isSetupMode = true;
WIFISerial.write(c);
}
}
#if defined XQX
if (!buf.equals(""))
{
Serial.println(buf);
intiT1 = buf.indexOf(":");
int iT2 = buf.indexOf("}");
Serial.print("Read from IOT");
Serial.println(iReader);
String sT = buf.substring(iT1 + 1, iT2);
int tmpInt = 0;
if (1 == iReader) {
gAirValPlantower = sT.toInt();
if (1 == sT.toInt())
{
isXQXSwitch = false;
} else
{
isXQXSwitch = true;
}
}
if (2 == iReader) {
gAirValPlantower = sT.toInt();
if (isXQXSwitch)
{
if (sT.toInt() < 5)tmpInt = 0;
else if (sT.toInt() < 10)tmpInt = 30;
else if (sT.toInt() < 30)tmpInt = 50 + sT.toInt();
else if (sT.toInt() < 70)tmpInt = 100 + sT.toInt();
else if (sT.toInt() < 100)tmpInt = 130 + sT.toInt();
elsetmpInt = 100 + sT.toInt() * 2;
if (tmpInt > 255)tmpInt = 255;
analogWrite(pPwmFan, tmpInt );
} else
{
tmpInt = 1;
analogWrite(pPwmFan, tmpInt );
}
}
//
buf = "";
}
#endif
}
void sendToNet()
{
Serial.println(gBuffer);
WIFISerial.begin(9600);
WIFISerial.print("AT+CIPSEND=");
WIFISerial.println(strlen(gBuffer) + 2);
WIFISerial.println(gBuffer);
}
void connentToNet()
{
WIFISerial.begin(9600);
WIFISerial.println("AT+CIPSTART=\"TCP\",\"api.XXXXXXXXX.com\",80");
}
void postOneNet(float val, const char APIKEY [], unsigned long DEVICEID , const char SENSORID[])
{
if ( nowTime - lastTime == 17000) {
connentToNet();
}
if (nowTime - lastTime == 19000) {
char str;
char str2;
dtostrf(val, 6, 2, str);
// char gBuffer = {0,};
int thisLength = sprintf(str2, "{\"%s\":%s}\r\n\r\n", SENSORID, str);
sprintf(gBuffer, "POST /devices/%ld/datapoints?type=3 HTTP/1.1" "\r\n"
"api-key: %s" "\r\n"
"Host: api.XXXXXXXXXX.com" "\r\n"
"Content-Length: %d" "\r\n"
"Content-Type: application/x-www-form-urlencoded" "\r\n"
"\r\n"
"%s"
, DEVICEID, APIKEY, thisLength, str2);
sendToNet();
iSensor++;
if (iSensor > MaxSensorNum)iSensor = 1;
}
} 软串口更消耗内存 void getOneNet(const char APIKEY [], unsigned long DEVICEID , const char SENSORID[])
{
if ( nowTime - lastTime == 1001) {
connentToOneNet();
}
if (isReadNetInstant())
{
iReader++;
if (iReader > MaxReaderNum)iReader = 1;
//char gBuffer = {0,};
sprintf(gBuffer, "GET /devices/%ld/datapoints?datastream_id=%s HTTP/1.1\r\napi-key: %s\r\nHost: api.XXXXXXX.com\r\n\r\n"
, DEVICEID, SENSORID, APIKEY);
sendToNet();
}
} 注意:EPS8266 -01模块需重新烧录为如下版本ESP8266固件V0.9.2.3-----------------9600波特率 奈何col 发表于 2016-11-13 10:42
软串口更消耗内存
软串口是没办法。毕竟arduino的物理串口只有1~2个,二传感器有3~4个呢。
页:
[1]