所以我创建了一个网络服务器,当从Android应用程序收到命令时,它会从几个模拟传感器输出值。然而,只要按下任何按钮它就会滞后,只是偶尔会起作用,通常会使应用程序崩溃。这是arduino代码:
E
这是Android代码:
//ARDUINO 1.0+ ONLY
//ARDUINO 1.0+ ONLY
#include <Ethernet.h>
#include <SPI.h>
// the value of the 'other' resistor
#define SERIESRESISTOR 10000
// What pin to connect the sensor to
#define THERMISTORPIN A0
boolean reading = false;
const int analogInPin = A1;
int sensorValue = 0;
////////////////////////////////////////////////////////////////////////
//CONFIGURE
////////////////////////////////////////////////////////////////////////
byte ip[] = { 192, 168, 0, 101 }; //Manual setup only
byte gateway[] = { 192, 168, 0, 1 }; //Manual setup only
byte subnet[] = { 255, 255, 255, 0 }; //Manual setup only
// if need to change the MAC address (Very Rare)
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EthernetServer server = EthernetServer(80); //port 80
////////////////////////////////////////////////////////////////////////
void setup(){
Serial.begin(9600);
//Pins 10,11,12 & 13 are used by the ethernet shield
pinMode(7, OUTPUT);
Ethernet.begin(mac, ip, gateway, subnet); //for manual setup
server.begin();
Serial.println(Ethernet.localIP());
}
void loop(){
// listen for incoming clients, and process qequest.
checkForClient();
tempReading();
}
void tempReading(){
sensorValue = analogRead(analogInPin);
EthernetClient client2 = server.available();
if (client2) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
boolean sentHeader = false;
while (client2.connected()) {
if (client2.available()) {
if(!sentHeader){
// send a standard http response header
client2.println("HTTP/1.1 200 OK");
client2.println("Content-Type: text/html");
client2.println();
sentHeader = true;
}
float reading;
reading = analogRead(THERMISTORPIN);
Serial.print("Analog reading ");-
Serial.println(reading);
// convert the value to resistance
reading = (1023 / reading) - 1;
reading = SERIESRESISTOR / reading;
Serial.print("Thermistor resistance ");
Serial.println(reading);
delay(1000);
float reading2;
char d = client2.read();
if(reading2 && d == ' ') reading2 = false;
if(d == '?') reading2 = true; //found the ?, begin reading the info
if(reading2){
switch (d) {
case '4':
//add code here to trigger on 2
client2.print("Thermistor resistance: ");
client2.println(reading);
client2.println("\n");
break;
case '5':
client2.print("Light Level: ");
client2.println(sensorValue);
client2.println("\n");
}
}
}
}
delay(1); // give the web browser time to receive the data
client2.stop(); // close the connection:
}
}
void checkForClient(){
EthernetClient client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
boolean sentHeader = false;
while (client.connected()) {
if (client.available()) {
if(!sentHeader){
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
sentHeader = true;
}
char c = client.read();
if(reading && c == ' ') reading = false;
if(c == '?') reading = true; //found the ?, begin reading the info
if(reading){
Serial.print(c);
switch (c) {
case '1':
//add code here to trigger on 2
pinOn(7, client);
break;
case '2':
//add code here to trigger on 3
pinOff(7, client);
break;
}
}
if (c == '\n' && currentLineIsBlank) break;
if (c == '\n') {
currentLineIsBlank = true;
}else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection:
}
void pinOff(int pin, EthernetClient client){
digitalWrite(pin, HIGH);
}
void pinOn(int pin, EthernetClient client){
digitalWrite(pin, LOW);
}
有什么想法吗?
答案 0 :(得分:0)
我尝试重构您的代码
//ARDUINO 1.0+ ONLY
#include <Ethernet.h>
#include <SPI.h>
// the value of the 'other' resistor
#define SERIESRESISTOR 10000
// What pin to connect the sensor to
#define THERMISTORPIN A0
const int analogInPin = A1;
////////////////////////////////////////////////////////////////////////
//CONFIGURE
////////////////////////////////////////////////////////////////////////
byte ip[] = { 192, 168, 0, 101 }; //Manual setup only
byte gateway[] = { 192, 168, 0, 1 }; //Manual setup only
byte subnet[] = { 255, 255, 255, 0 }; //Manual setup only
// if need to change the MAC address (Very Rare)
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EthernetServer server = EthernetServer(80); //port 80
////////////////////////////////////////////////////////////////////////
void setup(){
Serial.begin(9600);
//Pins 10,11,12 & 13 are used by the ethernet shield
pinMode(7, OUTPUT);
Ethernet.begin(mac, ip, gateway, subnet); //for manual setup
server.begin();
Serial.println(Ethernet.localIP());
}
void loop() {
boolean reading = false;
EthernetClient client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
boolean sentHeader = false;
while (client.connected()) {
if (client.available()) {
if (!sentHeader){
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
sentHeader = true;
}
char c = client.read();
if (reading && c == ' ') {
reading = false;
}
if (c == '?') {
//found the ?, begin reading the info
reading = true;
}
if (reading){
Serial.print(c);
switch (c) {
case '1':
pinOn(7, client);
break;
case '2':
pinOff(7, client);
break;
case '4':
print_thermistor(&client);
break;
case '5':
print_light(&client);
break;
}
}
if (c == '\n' && currentLineIsBlank)
break;
if (c == '\n') {
currentLineIsBlank = true;
} else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
delay(500); // give the web browser time to receive the data
client.stop(); // close the connection:
}
}
void print_thermistor(EthernetClient *client) {
float th;
th = analogRead(THERMISTORPIN);
Serial.print("Analog th ");
Serial.println(th);
// convert the value to resistance
th = (1023 / th) - 1;
th = SERIESRESISTOR / th;
Serial.print("Thermistor resistance ");
Serial.println(th);
client->print("Thermistor resistance: ");
client->println(th);
client->println("\n");
}
void print_light(EthernetClient *client) {
int sensorValue;
sensorValue = analogRead(analogInPin);
client->print("Light Level: ");
client->println(sensorValue);
client->println("\n");
}
void pinOff(int pin, EthernetClient client){
digitalWrite(pin, HIGH);
}
void pinOn(int pin, EthernetClient client){
digitalWrite(pin, LOW);
}