arduino 与新浪云的连接代码问题?

代码如下:#include "Arduino.h"
#include
#include

char state = '0';
char c;
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192,168,0,102);

IPAddress myDns(192,168,0,1);

EthernetClient client;

char server[] = "1.zhinengzhushou.applinzi.com";
int sensrdata = 50;

unsigned long lastConnectionTime = 0;
boolean lastConnected = false;
const unsigned long postingInterval = 30*1000;

//The setup function is called once at startup of the sketch
void setup()
{
// Add your initialization code here
Serial.begin(9600);
delay(1000);
Ethernet.begin(mac, ip, myDns);
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
pinMode(13, OUTPUT);
pinMode(7, OUTPUT);
}

// The loop function is called in an endless loop
void loop()
{
//Add your repeated code here
client.flush();
while(client.available()!=0) {
state = client.read();
if(state == '{'){ //关灯 0
Serial.println(state);
digitalWrite(13, LOW);
digitalWrite(7, HIGH);
delay(9000);
}else if(state == '}'){ //开灯 1
Serial.println(state);
digitalWrite(13, HIGH);
digitalWrite(7, LOW);
delay(9000);
}
}
//client.flush();
if (!client.connected() && lastConnected) {
Serial.println("disconnecting 2.");
client.stop();
}

if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
  if (client.connect(server, 80)) {

    Serial.println("connecting");
    // send the HTTP PUT request:
    client.print("GET /downup.php?token=weixin&data=");
    client.print(sensrdata);
    client.println(" HTTP/1.1");
    client.println("Host:1.zhinengzhushou.applinzi.com");
    client.println("User-Agent: arduino-ethernet");
    client.println("Connection: close");
    client.println();

    lastConnectionTime = millis();
  }else {
    Serial.println("connection failed");
    Serial.println("disconnecting 1.");
    client.stop();
  }
}
lastConnected = client.connected();

}
出口的输出如下:
My IP address: 192.168.0.102
connecting
}
{
{
{
}
{
}
{
{
}
{
}
}
}
{
{
}
{
}
}
}
disconnecting 2.
connecting
}
{
{
问题是 没有接收到微信端发送的数据,串口为什么还有输出,而且每个一循环,出现一个disconnecting 然后开始connecting ......
是新浪云的问题 还是arduino 程序的问题

写了另一段代码
#include "Arduino.h"
#include
#include
byte mac[]={0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
EthernetClient client;
char server[]="1.zhinengzhushou.applinzi.com";
unsigned long lastConnectionTime=0;
boolean lastConneted=false;
const unsigned long postingInterval=3*1000;
String returnValue="";
boolean ResponseBegin=false;

void setup()
{
pinMode(7,OUTPUT);
Serial.begin(9600);
if (Ethernet.begin(mac)==0)
{
Serial.println("Failed to configure Ethernet using DHCP");
for(;;);
}
else
{
Serial.println("Ethernet configuration OK");
}
}
void loop()
{
if(client.available())
{
char c = client.read();
if(c=='{')
{
ResponseBegin=true;
}
else if(c=='}')
{
ResponseBegin=false;
}
if(ResponseBegin)
{
returnValue+=c;

}
}

if (returnValue.length()!=0&&(ResponseBegin==false))
{
Serial.println(returnValue);
if(returnValue.charAt(returnValue.length())=='}')
{
Serial.println("turn on the LED");
digitalWrite(7,HIGH);
}
else if(returnValue.charAt(returnValue.length())=='{')
{
Serial.println("turn off the LED");
digitalWrite(7,LOW);
}
returnValue="";
}
if(!client.connected()&&lastConneted)
{
Serial.println();
Serial.println("disconnecting");
client.stop();
}
if(!client.connected()&&(millis()-lastConnectionTime>postingInterval))
{
Serial.println("good");
getdata();
}
lastConneted=client.connected();
}
void getdata(void)
{
if(client.connect(server,80))
{
Serial.println("connnecting...");
client.print("GET /downup.php?token=weixin&data=");
client.println(" HTTP/1.1");
client.println("Host:1.zhinengzhushou.applinzi.com");
client.println("User-Agent:arduino-ethernet");
client.println("Connection:close");
client.println();
}
else
{
Serial.println("connection failed");
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
lastConnectionTime=millis();
}
串口输出如下:Ethernet configuration OK
good
connnecting...
{var closed=false,bdiv;var charset=document.charset||document.characterSet;var utf8=['该页面的提供者尚未完成','实名认证','您的访问可能存在风险'];var gbk=['⸮⸮ҳ⸮⸮⸮⸮ṩ⸮⸮⸮⸮δ⸮⸮⸮','ʵ⸮⸮⸮⸮֤','⸮⸮⸮ķ⸮⸮ʿ⸮⸮ܴ⸮⸮ڷ⸮⸮⸮'];var en=['The provider of this page is not','verified','there are risks when you visiting'];function banner(){var char=charset.toLowerCase(),lang='';switch(true){case char.indexOf('utf')==0:lang=utf8;break;case char.indexOf('gb')==0:lang=gbk;break;default:lang=en;
{var e=event?event:window.event;x=e.clientX;y=e.clientY;
{if(event&&!event.initMouseEvent){return
{div.style.display='none';closed=true;
{if(closed){clearInterval(tt);return
{bdiv.parentNode.removeChild(bdiv)

disconnecting
good
connnecting...
{var closed=false,bdiv;var charset=document.charset||document.characterSet;var utf8=['该页面的提供者尚未完成','实名认证','您的访问可能存在风险'];var gbk=['⸮⸮ҳ⸮⸮⸮⸮ṩ⸮⸮⸮⸮δ⸮⸮⸮','ʵ⸮⸮⸮⸮֤','⸮⸮⸮ķ⸮⸮ʿ⸮⸮ܴ⸮⸮ڷ⸮⸮⸮'];var en=['The provider of this page is not','verified','there are risks when you visiting'];function banner(){var char=charset.toLowerCase(),lang='';switch(true){case char.indexOf('utf')==0:lang=utf8;break;case char.indexOf('gb')==0:lang=gbk;break;default:lang=en;
{var e=event?event:window.event;x=e.clientX;y=e.clientY;
{if(event&&!event.initMouseEvent){return
{div.style.display='none';closed=true;
{if(closed){clearInterval(tt);return
{bdiv.parentNode.removeChild(bdiv)

问题是,代码错了 还是其他问题 还望指教。感激不尽。

1个回答

IPAddress ip(192,168,0,102);
IPAddress myDns(192,168,0,1);
你这些都是内网ip啊

weixin_39744726
icemoons 但是他这个要求的就是内网的ip(>﹏<)
大约 3 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问