arduinoesp8266定时器_【分享1】ARDUINO MEGA2560 经过ESP8266 WIFI模块上传温湿度数据......

#include

#include

#include

#define ENCODER_A_PIN 2

#define ENCODER_B_PIN 3

#define SWITCH_PIN    4

long position;

HttpPacketHead packet;

#define DebugSerial Serial

#define ESP8266Serail Serial3

#define Success 1U

#define Failure 0U

int L = 13; //LED指示灯引脚

unsigned long  Time_Cont = 0;       //定时器计数器

const unsigned int esp8266RxBufferLength = 600;

char esp8266RxBuffer[esp8266RxBufferLength];

unsigned int ii = 0;

char OneNetServer[] = "api.heclouds.com";       //不需要修改

const char ssid[] = "HXDwifi_SYS";     //修改为自己的路由器用户名

const char password[] = "12345678"; //修改为自己的路由器密码

char device_id[] = "3254430";    //修改为自己的设备ID

char API_KEY[] = "=n4Qxfef3=CCQCoXvDnFH1JYGAw=";    //修改为自己的API_KEY

char sensor_id1[] = "water1";

char sensor_id2[] = "water2";

void setup() {

pinMode(ENCODER_A_PIN, INPUT);

pinMode(ENCODER_B_PIN, INPUT);

pinMode(SWITCH_PIN, INPUT);

attachInterrupt(0, read_quadrature, CHANGE);

pinMode(L, OUTPUT);

digitalWrite(L, LOW);

DebugSerial.begin(9600);

ESP8266Serail.begin(115200);

Timer1.initialize(1000);

Timer1.attachInterrupt(Timer1_handler);

initEsp8266();

DebugSerial.println("setup end!");

}

void loop() {

int A=position+20;

//发送数据到Onenet

postDataToOneNet(API_KEY,device_id,sensor_id1,A);

delay(100);

postDataToOneNet(API_KEY,device_id,sensor_id2,position);

delay(5000);

}

void read_quadrature(){

// found a low-to-high on channel A ENA脚下降沿中断触发

if (digitalRead(ENCODER_A_PIN) == LOW){

// check channel B to see which way 查询ENB的电平以确认是顺时针还是逆时针旋转

if (digitalRead(ENCODER_B_PIN) == LOW)

position++;

}

// found a high-to-low on channel A ENA脚上升沿中断触发

else{

// check channel B to see which way 查询ENB的电平以确认是顺时针还是逆时针旋转

if (digitalRead(ENCODER_B_PIN) == LOW)

position--;

}

}

void postDataToOneNet(char* API_VALUE_temp,char* device_id_temp,char* sensor_id_temp,double thisData)

{

//合成POST请求

StaticJsonBuffer<200> jsonBuffer;

JsonObject& value = jsonBuffer.createObject();

value["value"] = thisData;

JsonObject& id_datapoints = jsonBuffer.createObject();

id_datapoints["id"] = sensor_id_temp;

JsonArray& datapoints = id_datapoints.createNestedArray("datapoints");

datapoints.add(value);

JsonObject& myJson = jsonBuffer.createObject();

JsonArray& datastreams = myJson.createNestedArray("datastreams");

datastreams.add(id_datapoints);

char p[200];

int num = myJson.printTo(p, sizeof(p));

packet.setHostAddress(OneNetServer);

packet.setDevId(device_id_temp);   //device_id

packet.setAccessKey(API_VALUE_temp);  //API_KEY

// packet.setDataStreamId("");    //datastream_id

// packet.setTriggerId("");

// packet.setBinIdx("");

/*create the http message about add datapoint */

packet.createCmdPacket(POST, TYPE_DATAPOINT, p);

// if (strlen(packet.content))

//  Serial.print(packet.content);

// Serial.print(p);

int httpLength = strlen(packet.content) + num;

//连接服务器

char cmd[400];

memset(cmd, 0, 400);    //清空cmd

strcpy(cmd, "AT+CIPSTART=\"TCP\",\"");

strcat(cmd, OneNetServer);

strcat(cmd, "\",80\r\n");

if (sendCommand(cmd, "CONNECT", 7, 10000, 5) == Success);

else ESP8266_ERROR(1);

//发送数据

memset(cmd, 0, 400);    //清空cmd

sprintf(cmd, "AT+CIPSEND=%d\r\n", httpLength);

if (sendCommand(cmd, ">", 1, 3000, 1) == Success);

else ESP8266_ERROR(2);

memset(cmd, 0, 400);    //清空cmd

strcpy(cmd, packet.content);

strcat(cmd, p);

if (sendCommand(cmd, "\"succ\"}", 7, 3000, 3) == Success);

else ESP8266_ERROR(3);

if (sendCommand("AT+CIPCLOSE\r\n", "CLOSED", 6, 3000, 1) == Success);

else ESP8266_ERROR(4);

}

void initEsp8266()

{

if (sendCommand("AT\r\n", "OK", 2, 3000, 10) == Success);

else ESP8266_ERROR(5);

if (sendCommand("AT+RST\r\n", "ready", 5, 10000, 10) == Success);

else ESP8266_ERROR(6);

if (sendCommand("AT+CWMODE=1\r\n", "OK", 2, 3000, 10) == Success);

else ESP8266_ERROR(7);

char cmd[50];

strcpy(cmd, "AT+CWJAP=\"");

strcat(cmd, ssid);

strcat(cmd, "\",\"");

strcat(cmd, password);

strcat(cmd, "\"\r\n");

if (sendCommand(cmd, "OK", 2, 20000, 10) == Success);

else ESP8266_ERROR(8);

if (sendCommand("AT+CIPMUX=0\r\n", "OK", 2, 3000, 10) == Success);

else ESP8266_ERROR(9);

if (sendCommand("AT+CIFSR\r\n", "OK", 2, 20000, 10) == Success);

else ESP8266_ERROR(10);

}

void(* resetFunc) (void) = 0; //制造重启命令

void ESP8266_ERROR(int num)

{

DebugSerial.print("ERROR");

DebugSerial.println(num);

while (1)

{

digitalWrite(L, HIGH);

delay(300);

digitalWrite(L, LOW);

delay(300);

if (sendCommand("AT\r\n", "OK", 2, 100, 10) == Success)

{

DebugSerial.print("\r\nRESET!!!!!!\r\n");

resetFunc();

}

}

}

unsigned int sendCommand(char *Command, char *Response, unsigned int Res_Length, unsigned long Timeout, unsigned char Retry)

{

clrEsp8266RxBuffer();

for (unsigned char n = 0; n < Retry; n++)

{

DebugSerial.print("\r\nsend AT Command:\r\n----------\r\n");

DebugSerial.write(Command);

ESP8266Serail.write(Command);

Time_Cont = 0;

while (Time_Cont < Timeout)

{

esp8266ReadBuffer();

if ((mystrstr(esp8266RxBuffer, Response, ii, Res_Length)) != NULL)

{

DebugSerial.print("\r\nreceive AT Command:\r\n==========\r\n");

DebugSerial.print(esp8266RxBuffer); //输出接收到的信息

clrEsp8266RxBuffer();

return Success;

}

}

Time_Cont = 0;

}

DebugSerial.print("\r\nreceive AT Command:\r\n==========\r\n");

DebugSerial.print(esp8266RxBuffer);//输出接收到的信息

clrEsp8266RxBuffer();

return Failure;

}

unsigned char mystrstr(char *s, char *t, unsigned int Length_s, unsigned int Length_t)

{   char x = 0; char *p; p = t;

int i = 0, j = 0;

for (; i < Length_s; s++, i++)

{

while (*t == *s)

{   s++; t++; i++; j++;

if (j >= Length_t) return 1;

}

s -= j;

t = p; j = 0;

}

return NULL;

}

void Timer1_handler(void)

{

Time_Cont++;

}

void esp8266ReadBuffer() {

while (ESP8266Serail.available())

{

esp8266RxBuffer[ii++] = ESP8266Serail.read();

if (ii == esp8266RxBufferLength)clrEsp8266RxBuffer();

}

}

void clrEsp8266RxBuffer(void)

{

memset(esp8266RxBuffer, 0, esp8266RxBufferLength);      //清空

ii = 0;

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值