极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 11226|回复: 3

enc28j60网络应用问题求解

[复制链接]
发表于 2014-1-16 09:48:40 | 显示全部楼层 |阅读模式
本帖最后由 孤独舰 于 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);
}
回复

使用道具 举报

发表于 2014-1-16 10:24:45 | 显示全部楼层
乱码是什么样子的?截图看一下
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-1-16 11:06:45 | 显示全部楼层
lwcount第一次从0-4正常,再往下就不对了:



>>> send
[{"Name":"T5","Value":"1.0"}]
Server: 42.121.254.11
4
<<< reply 170 ms
HTTP/1.1 200 OK
Date: Thu, 16 Jan 2014 03:00:36 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 44
Connection: close
Cache-Control: private
Set-Cookie: SERVERID=a4a5b2bbca16d8c8b2ba6d5b6e55f36e|1389841236|1389841236ath=/

{"Successful":true,"Message":"Successful. "}

>>> send
[{"Name":"ation/json; charset=utf-8
Content-Length: 44
Connection: close
Cache-Control: private
Set-Cookie: SERVERID=a4a5b2bbca16d8c8b2ba6d5b6e55f36e|1389841236|1389841236ath=ˉ

&#146;{"Sccessful":true,"Message":"Successful. "}","Value":"1.2"}]
Server: 0.0.0.0
104

红色是第二次串口输出 不对了。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-1-16 11:10:07 | 显示全部楼层
网络上显示value是0.2  0.4  0.6  0.8  1.0  就不在显示了,求解呀?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-5 11:27 , Processed in 0.035408 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表