all_heart 发表于 2012-10-29 02:49:27

asp.net读取arduino上温度传感器数据

我手上有arduino 2560 和 一个w5100的网络模块,我想从一个asp.net的网站读取arduino上温度传感器数据,各位能否指点一二?我只熟悉asp.net,对PHP没有太多了解。

bg1lsy 发表于 2012-10-29 08:36:04

如果只是在asp.net页面上显示温度的话,可以把Arduino2560和W5100写成一个WebServer,读取温度值并输出一个网页。用asp.net写的页面里嵌入这个2560的页面,但一定要把页面组织好,要不不好看。只是一个建议,效果不一定好。如果要是还要把数据读取并写入数据库,建议用Arduino2560和W5100写成一个WebClient,通过url调用把温度定时写入数据库,然后asp.net再从数据库里取温度值。

all_heart 发表于 2012-11-4 01:12:45

#include <SPI.h>
#include <Ethernet.h>

#define APIKEY         "YOUR API KEY GOES HERE" // replace your pachube api key here
#define FEEDID         00000 // replace your feed ID
#define USERAGENT      "My Project" // user agent is the project name

// assign a MAC address for the ethernet controller.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
// fill in your address here:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};

// fill in an available IP address on your network here,
// for manual configuration:
IPAddress ip(192,168,1,254);
// initialize the library instance:
EthernetClient client;

// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
IPAddress server(192,168,1,5);


unsigned long lastConnectionTime = 0;          // last time you connected to the server, in milliseconds
boolean lastConnected = false;               // state of the connection last time through the main loop
const unsigned long postingInterval = 10*1000; //delay between updates to Pachube.com

void setup() {
// Open serial communications and wait for port to open:
   Serial.begin(9600);
    while (!Serial) {
   ; // wait for serial port to connect. Needed for Leonardo only
   }


// start the Ethernet connection:
   if (Ethernet.begin(mac) == 0) {
   Serial.println("Failed to configure Ethernet using DHCP");
   // DHCP failed, so use a fixed IP address:
   Ethernet.begin(mac, ip);
   }
}

void loop() {
   // read the analog sensor:
   int sensorReading = analogRead(A0);   

// if there's incoming data from the net connection.
   // send it out the serial port.This is for debugging
   // purposes only:
   if (client.available()) {
   char c = client.read();
   Serial.print(c);
   }

// if there's no net connection, but there was one last time
   // through the loop, then stop the client:
   if (!client.connected() && lastConnected) {
   Serial.println();
   Serial.println("disconnecting.");
   client.stop();
   }

// if you're not connected, and ten seconds have passed since
   // your last connection, then connect again and send data:
   if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
   sendData(sensorReading);
   }
   // store the state of the connection for next time through
   // the loop:
   lastConnected = client.connected();
}

// this method makes a HTTP connection to the server:
void sendData(int thisData) {
   // if there's a successful connection:
   if (client.connect(server, 80)) {
   // if there's a successful connection:
    Serial.println("connecting...");
    // send the HTTP PUT request:
    client.print("POST /iems/default.aspx?");
    client.print("temp_sensor=");
    client.print(thisData);
    client.println(" HTTP/1.1");
    client.println("Host: 192.168.1.5");
    client.print("Content-Length: ");
    client.println(thisData.length());
    // last pieces of the HTTP PUT request:
    client.println("Content-Type: application/x-www-form-urlencoded");
    client.println("Connection: close");
    client.println();
    // here's the actual content of the PUT request:
    client.println(thisData);   
}
else {
   // if you couldn't make a connection:
   Serial.println("connection failed");
   Serial.println();
   Serial.println("disconnecting.");
   client.stop();
   }
    // note the time that the connection was made or attempted:
   lastConnectionTime = millis();
}


// This method calculates the number of digits in the
// sensor reading.Since each digit of the ASCII decimal
// representation is a byte, the number of digits equals
// the number of bytes:

int getLength(int someValue) {
   // there's at least one byte:
   int digits = 1;
   // continually divide the value by ten,
// adding one to the digit count for each
   // time you divide, until you're at 0:
   int dividend = someValue /10;
   while (dividend > 0) {
   dividend = dividend /10;
   digits++;
   }
   // return the number of digits:
   return digits;
}



串口监视一直提示:connection failed       disconnecting

能帮忙分析一下原因吗?192.168.1.5是一个asp.net网站,获取的数据上传asp.net网站的页面显示

飞翔的红猪 发表于 2012-11-4 10:39:16

会asp.net,想必也会C#或者VB.net了,你可以这样的:

1.写一个窗口程序,通过串口读取arduino的温度值,然后写入一个xml文件

2.做asp.net网站,读取前面的xml文件,解析后写入页面。。。

all_heart 发表于 2012-11-6 22:58:31

可以通过arduino的Ethernet模块先将数据传送到Mysql数据库吗?我不想数据走串口,直接走网络传送,有解决的办法吗?

龙宫宝玉 发表于 2015-8-11 16:15:18

有同样的问题求解,php一点不懂

苦苦砸坏五块石 发表于 2015-12-28 15:23:09

有同样的问题求解,php一点不懂可以通过arduino的Ethernet模块先将数据传送到Mysql数据库吗?我不想数据走串口,直接走网络传送,有解决的办法吗?
页: [1]
查看完整版本: asp.net读取arduino上温度传感器数据