esp8266 读取 php信息,我怎么样让esp8266wifi模块接收数据并显示在串口监视器上啊?...

我刚好有段测试程序

#include

#include

//SoftwareSerial mySerial(7, 8); // RX, TX

SoftwareSerial mySerial(9, 10); // RX, TX

dht11 DHT11;

#define DHT11PIN 3  //Connect D3 to data pin of DHT11

#define httpled 13

#define pinOut 5

//请到m.znck007.com申请登陆-》家中环境-》网关,里可以看到!

//#define APIKEY "8c5b8d26a8010092" //设置apikey

#define APIKEY "734f00510f77ca88"

char server[] = "api.znck007.com";//声明API网址

//#define server "220.181.111.85"    //baidu.com

unsigned long last_http;

unsigned long timer,last;

char serialbuff[129]={0};

char buffer[35];

char buff[30];

bool ishttpget=false;

bool last_send=false;

bool wsd=false;

int temperature=25;

int humdity=75;

int Count=0;

int sid=3;

int nid=1;

bool update=false;

void setup(void)

{

Serial.begin(115200);  //设置串口波特率

mySerial.begin(9600);

//Serial.flush();  //等待超出的串行数据完成传输

pinMode(httpled,OUTPUT);

pinMode(pinOut,OUTPUT);

delay(1000);

if(close_AT_show()) mySerial.println("close_AT_show ok");

mySerial.println("wait join AP");

if(conf_JAP("sun","zw889989")) mySerial.println("join sun ok");  //登陆网络需要的ssid名称以及密码

else mySerial.println("join sun fail");

mySerial.println("setup success");

}

void loop(void)

{

if(millis() - last_http >= 5000)

{

//last = millis();//获取现在的时间

if(!ishttpget || millis() - last_http >= 30000)

{

last_http = millis();

ishttpget=true;

Count++;

mySerial.println(Count);

if(Count>=120)

{

Count = 0;

wsd = true;

}

digitalWrite(httpled,HIGH);

mySerial.println("\n>>> REQ");

static char buf[42]={0};

sprintf(buf,"{ck001000000000}"); //%s ,updateData

if(wsd)

{

wsd = false;

int thsid=001;

int thnid=002;

DHT11.read(DHT11PIN);  //读温湿度数据

humdity = DHT11.humidity;

temperature = DHT11.temperature;

get_send_char(thsid,thnid,humdity,temperature,buff);//"{ck001001100000}"

sendData(buff);

}

else sendData(buf);  //POST到网址,APIKEY,字符

}

}

if (Serial.available() > 0)

{

int index=0;

delay(100); // let the buffer fill up

int numChar = Serial.available();  //获取从串口读取有效的字节数(字符)

if (numChar>128) numChar=128;

while (numChar--)

{

serialbuff[index++] = Serial.read();

}

serial_String(serialbuff);

}

/*

if (mySerial.available() > 0)

{

int index=0;

delay(100); // let the buffer fill up

int numChar = mySerial.available();

if (numChar>29) numChar=29;

while (numChar--)

{

buffer[index++] = mySerial.read();

}

splitString(buffer);

}

*/

}

void serial_String(char* data)

{

mySerial.println(data);

char body[30]={0};

get_znck_body(data,body);//获取只是{ckxxxxxx}的字符,因为这是我们的命令格式

mySerial.println(body);

int s=get_sid(body);

int n=get_nid(body);

int d=get_data(body);

if(  s==sid && n==nid )

{

if( d==1 ) digitalWrite(pinOut,HIGH);

else digitalWrite(pinOut,LOW);

}

if(last_send)

{

mySerial.print("<<< reply ");

mySerial.print(millis() - timer);

mySerial.println(" ms");

mySerial.println("");

last_send = false;

}

//if(Serial.find("Content-Type")==true)

//{

last_http = millis();

ishttpget=false;

digitalWrite(httpled,LOW);

//}

// Clear the text and serial buffers

for (int x=0; x<129; x++)

{

serialbuff[x]='\0';

}

Serial.flush();  //等待超出的串行数据完成传输

}

/*

void splitString(char* data)

{

mySerial.println(data);

char* parameter;

parameter = strtok (data, " ,");

while (parameter != NULL)

{

mySerial.println(parameter);

//set_data(parameter);

parameter = strtok (NULL, " ,");

}

// Clear the text and serial buffers

for (int x=0; x<30; x++)

{

buffer[x]='\0';

}

mySerial.flush();  //等待超出的串行数据完成传输

}

void set_data(char* data)

{

if ((data[0] == 'y') || (data[0] == 'Y'))

{

int Ans = strtol(data+1, NULL, 10);

mySerial.println("\n>>> REQ");

static char buf[20]={0};

sprintf(buf,"{ck001000000000}"); //这个字符串代表获取新命令数据

if(update)

{

update = false;

sendData(buff);

}

else sendData(buf);

}

if ((data[0] == 'd') || (data[0] == 'D'))

{

int getdata = strtol(data+1, NULL, 10);

get_getdata_char(sid,nid,getdata,buff);

update = true;

}

if ((data[0] == 't') || (data[0] == 'T'))

{

int Ans = strtol(data+1, NULL, 10);

temperature = Ans;

get_send_char(sid,nid,humdity,temperature,buff);//"{ck001001100000}"

update = true;

}

if ((data[0] == 'h') || (data[0] == 'H'))

{

int Ans = strtol(data+1, NULL, 10);

humdity = Ans;

get_send_char(sid,nid,humdity,temperature,buff);

update = true;

}

if ((data[0] == 's') || (data[0] == 'S'))

{

int Ans = strtol(data+1, NULL, 10);

sid = Ans;

get_send_char(sid,nid,humdity,temperature,buff);

update = true;

}

if ((data[0] == 'n') || (data[0] == 'N'))

{

int Ans = strtol(data+1, NULL, 10);

nid = Ans;

get_send_char(sid,nid,humdity,temperature,buff);

update = true;

}

if ((data[0] == 'j') || (data[0] == 'J'))

{

int Ans = strtol(data+1, NULL, 10);

//setdata = Ans;

if(Ans<10)

{

if(conf_JAP("860","zw889989")) mySerial.println("join 860 ok");

else mySerial.println("join 860 fail");

}

if(Ans>10)

{

if(conf_JAP("sun","zw889989")) mySerial.println("join sun ok");

else mySerial.println("join sun fail");

}

//update = true;

}

}

*/

void sendData(char* thisData)  //this method makes a HTTP connection to the server:

{

//digitalWrite(httpled,HIGH);

if(start_Mux(1,server,80))  //if there's a successful connection:

{

mySerial.println("connecting...");

// send the HTTP PUT request:

String cmd;

cmd = "POST /index.php HTTP/1.0\r\n";

cmd += "Host: api.znck007.com\r\n";

cmd += "apikey:";

cmd += APIKEY;

cmd += "\r\n";

cmd += "Accept: *";

cmd += "/";

cmd += "*\r\n";

cmd += "Content-Length: ";

size_t thisLength = strlen(thisData);

cmd += String(thisLength);

cmd += "\r\n";

cmd += "Content-Type: application/x-www-form-urlencoded\r\n";

cmd += "\r\n";

cmd += String(thisData);

cmd += "\r\n";

//mySerial.println(cmd);

//Send_test(cmd);

if(Send(cmd)) mySerial.println("Send ok");

else  mySerial.println("Send fail");

}

else

{

// if you couldn't make a connection:

mySerial.println("connection failed");

//mySerial.println("disconnecting.");

//Serial.println("AT+CIPCLOSE");

}

}

boolean Send(String str)

{

Serial.print("AT+CIPSEND=");

Serial.println(str.length());

unsigned long start;

start = millis();

bool found=false;

while (millis()-start<3000)

{

if(Serial.find(">")==true )

{

found = true;

break;

}

}

if(found) Serial.print(str);

else return false;

start = millis();

while (millis()-start<8000)

{

if(Serial.available()>0)

{

if(Serial.find("SEND OK")==true)

last_send = true;

timer = millis();

return true;

}

}

return false;

}

//confJAP("Rookie","zw889989");  //配置登陆网络需要的ssid名称以及密码

boolean conf_JAP(String ssid , String pwd)  //配置登陆网络需要的ssid名称以及密码

{

Serial.print("AT+CWJAP=");

Serial.print("\"");     //"ssid"

Serial.print(ssid);

Serial.print("\"");

Serial.print(",");

Serial.print("\"");      //"pwd"

Serial.print(pwd);

Serial.println("\"");

unsigned long start = millis();

while (millis()-start<18000)

{

if(Serial.find("OK")==true)

{

return true;

}

}

return false;

}

//startMux_0(1,"192.168.1.16",5000);  //建立tcp链接或者注册UDP端口号(单路模式)

boolean start_Mux(byte type, String addr, int port)  //建立tcp链接或者注册UDP端口号(单路模式)

{

String data;

Serial.print("AT+CIPSTART=");

if(type>0) Serial.print("\"TCP\"");

else Serial.print("\"UDP\"");

Serial.print(",");

Serial.print("\"");

Serial.print(addr);

Serial.print("\"");

Serial.print(",");

Serial.println(String(port));

unsigned long start = millis();

while (millis()-start<5000)  //当串口有完成数据返回时,结束语句

{

if(Serial.available()>0)

{

char a =Serial.read();

data=data+a;

}

if (data.indexOf("OK")!=-1 || data.indexOf("ALREAY CONNECT")!=-1 || data.indexOf("ERROR")!=-1)

{

//if (data.indexOf("Linked")!=-1) return true;

return true;

}

}

return false;

}

boolean close_AT_show()

{

String data;

Serial.println("ATE0");

unsigned long start = millis();

while (millis()-start<3000)  //当串口有完成数据返回时,结束语句

{

if(Serial.find("OK")==true)

{

return true;

}

}

return false;

}

void get_znck_body(char *p,char *s)  //获取只是{ckxxxxxx}的字符,因为这是我们的命令格式

{

char rechar[129]={0};

int bufi=0;

bool isend=false;

int charnum=0;

for(bufi=0;bufi

{

//Serial.print(p[bufi]);

if(p[bufi]=='{') isend=true;

if(p[bufi]=='}' && isend==true)

{

isend=false;

rechar[charnum]=p[bufi];

break;

}

if(isend)

{

if(charnum<129)

{

rechar[charnum]=p[bufi];//Serial.print(rechar[charnum]);

charnum++;

}

}

}

//Serial.println("");

//memcpy(s,rechar,129);

sprintf(s,"%s",rechar);

}

int get_sid(char *buff)  //获取sid

{

if( strstr(buff,"{ck") && strstr(buff,"}") && strlen(buff)>10)

{

char charsid[4]={0};

memcpy(charsid,buff+3,3);

//Serial.println(charsid);

int intsid=atoi(charsid);

mySerial.print("sid=");

mySerial.println(intsid);

return intsid;

}

else return 0;

}

int get_nid(char *buff)  //获取nid

{

if( strstr(buff,"{ck") && strstr(buff,"}") && strlen(buff)>10)

{

char charNid[4]={0};

memcpy(charNid,buff+6,3);

//Serial.println(charNid);

int intNid=atoi(charNid);

mySerial.print("Nid=");

mySerial.println(intNid);

return intNid;

}

else return 0;

}

int get_data(char *buff)  //获取data

{

if( strstr(buff,"{ck") && strstr(buff,"}") && strlen(buff)>10)

{

char charData[4]={0};

memcpy(charData,buff+9,1);

//Serial.println(charData);

int intData=atoi(charData);

mySerial.print("Data=");

mySerial.println(intData);

return intData;

}

else return 0;

}

void get_send_char(int sid,int nid,int humdity,int temperature,char *p)

{

sprintf(p,"{ck%.3d%.3d%d.%d}",sid,nid,humdity,temperature);

}

void get_getdata_char(int sid,int nid,int getdata,char *p)

{

sprintf(p,"{ck%.3d%.3d%d}",sid,nid,getdata);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值