本帖最后由 孤独舰 于 2014-1-16 09:50 编辑
最近试验了enc28j60网络模块,上传单个数据是可以的,但隔断时间发5个数据串口显示会乱码?求高手指点。
代码如下:
#include <EtherCard.h>
static byte mymac[] = {0xe8,0x9a,0x8f,0xcf,0x8a,0xcf};
byte Ethernet::buffer[400];
char buf[50];
static long timer;
uint8_t lwCount=0;
float temp=0;
#define REQUEST_RATE 10000 // milliseconds
char website[] PROGMEM = "www.lewei50.com";
char urlBuf[] PROGMEM = "/api/V1/gateway/UpdateSensors/01";
char apiKey[] PROGMEM = "userkey:***********************";
char *lwSensor[5]={"T1", "T2", "T3", "T4", "T5"};
static void my_result_cb (byte status, word off, word len) {
Serial.print("<<< reply ");
Serial.print(millis() - timer);
Serial.println(" ms");
Serial.println((const char*) Ethernet::buffer + off);
}
void setup () {
Serial.begin(9600);
Serial.println("\n[Lewei50.com]");
if (!ether.begin(sizeof Ethernet::buffer, mymac, 10))
Serial.println( "Failed to access Ethernet controller");
else
Serial.println("Ethernet controller initialized");
if (!ether.dhcpSetup())
Serial.println("Failed to get configuration from DHCP");
else
Serial.println("DHCP configuration done");
ether.printIp("IP Address:\t", ether.myip);
ether.printIp("Netmask:\t", ether.mymask); //子掩码
ether.printIp("Gateway:\t", ether.gwip);//网关
if (!ether.dnsLookup(website))
Serial.println("DNS failed");
else
ether.printIp("Server: ", ether.hisip);
timer = - REQUEST_RATE; // start timing out right away
}
void loop () {
ether.packetLoop(ether.packetReceive());
if (millis() > timer + REQUEST_RATE)
{
timer = millis();
e_send();
}
}
void e_send(void)
{
Serial.println("\n>>> send");
temp=temp+0.2;
if(temp>2000)
temp=0;
uint8_t whole, fract;
whole=(int)(temp*10.0)/10;
fract = (int)(temp*10.0) % 10;
sprintf(buf,"[{\"Name\":\"%s\",\"Value\":\"%d.%d\"}]",lwSensor[lwCount], whole,fract);
Serial.println(buf);
ether.dnsLookup(website);
ether.printIp("Server: ", ether.hisip);
ether.httpPost (urlBuf, website, apiKey, buf, my_result_cb);
lwCount++;
if(lwCount>5)
lwCount=0;
Serial.println(lwCount);
}
|