Hallo zusammen,
ich habe vermutlich ein Problem mit dem Speicher und weiß mir nicht zu helfen.
Kurzer Überblick was das Programm machen soll. Ich habe einen Wifi-Webserver laufen, auf dem eine Website abgerufen wird. Die Website zeigt die Temperatur an und einen Button mit dem eine LED ein- und ausgeschaltet werden soll. Die Temperatur wird angezeigt, das Schalten der LED verhält sich allerdings seltsam. Mal funktioniert es mit geringer Verzögerung, sobald ich die Seite neu lade ist es wieder völlig anders. Die Seite schickt einen sauberen String, wenn ich diesen vom Arduino ausgeben lasse hängt immer „Müll“ hinten dran. Mal läuft das Programm recht zügig ab, und ein andermal unglaublich träge. Ich vermute, dass da irgendetwas mit dem Speicher nicht Stimmt aber ich lasse mich gerne eines Besseren belehren.
vielleicht kann mir jemand Tipps geben wie ich das in den Griff bekomme.
Danke im Voraus
LG laptor
Terminal
22.94
HTTP_req: GET / HTTP/1.1
Host: 192.168.0.100
User-Agent: Mozilla/5.
22.94
HTTP_req: GET /ajax_inputs&nocache=228192.43950718592 HTTP/1.1
H
client: 1
22.87
HTTP_req: GET /favicon.ico HTTP/1.1
Host: 192.168.0.100
User-Agent:
22.87
HTTP_req: GET /ajax_inputs&nocache=979538.2871924393 HTTP/1.1
Ho
client: 1
22.87
HTTP_req: GET /ajax_inputs&nocache=675553.981215456 HTTP/1.1
Hos
Arduino Prog
#include <SPI.h>
#include <WiFi.h>
#include <SD.h>
//#include <dht.h>
#include <math.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <TextFinder.h>
#define ONE_WIRE_BUS 2 /* Digitalport Pin 2 definieren */
OneWire ourWire(ONE_WIRE_BUS); /* Ini oneWire instance */
DallasTemperature sensors(&ourWire);
#define REQ_BUF_SZ 60
float Temp = sensors.getTempCByIndex(0);
char ssid[] = "TP-LINK_2ABA44"; // your network SSID (name)
char pass[] = "76745287"; // your network password
int maxBuf = 60;
int status = WL_IDLE_STATUS;
WiFiServer server(80); // create a server at port 80
File webFile;
char HTTP_req[REQ_BUF_SZ] = {0};
char req_index = 0;
int counter = 0;
boolean LED = 0; // stores the states of the LEDs
void setup()
{
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
SPI.begin();
Serial.begin(9600);
pinMode(9, OUTPUT); //LED
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
//Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid); // print the network name (SSID);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
digitalWrite(10, HIGH);
// wait 10 seconds for connection:
delay(10000);
}
server.begin();
//Serial.println("Now Connected... ");
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
//-------------------------------
SD Initialisierung
//-----------------------------
}
void loop()
{
sensors.requestTemperatures(); // Temp abfragen
Serial.println(sensors.getTempCByIndex(0) );
Temp = sensors.getTempCByIndex(0);
//Serial.print("Counter: ");
//Serial.println(counter);
WiFiClient client = server.available(); // listen for incoming clients
if (client) {
boolean currentLineIsBlank = true;
while (client.connected())
{
if (client.available())
{
char c = client.read();
if (req_index < (REQ_BUF_SZ - 1)) {
HTTP_req[req_index] = c;
req_index++;
}
if (c == '\n' && currentLineIsBlank) {
client.println("HTTP/1.1 200 OK");
Serial.print("HTTP_req: ");
Serial.println(HTTP_req);
if (StrContains(HTTP_req, "ajax_inputs")) {
client.println("Content-Type: text/xml");
client.println("Connection: keep-alive");
client.println();
SetLED();
Serial.print("client: ");
Serial.println(client);
XML_response(client);
}
else {
client.println("Content-Type: text/html");
client.println("Connection: keep-alive");
client.println();
webFile = SD.open("index.htm");
if (webFile) {
//Serial.println(zaehler++);
byte clientBuf[maxBuf];
int clientCount = 0;
while (webFile.available())
{
clientBuf[clientCount] = webFile.read();
clientCount++;
if (clientCount > (maxBuf - 1))
{
client.write(clientBuf, maxBuf);
clientCount = 0;
}
}
if (clientCount > 0) {
client.write(clientBuf, clientCount);
}
webFile.close();
}
}
req_index = 0;
StrClear(HTTP_req, REQ_BUF_SZ);
break;
}
if (c == '\n') {
currentLineIsBlank = true;
}
else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
delay(1);
client.stop();
}
}
void SetLED(void)
{
// LED 4 (pin 9)
if (StrContains(HTTP_req, "LED4=1")) {
LED = 1; // save LED state
digitalWrite(9, HIGH);
}
else if (StrContains(HTTP_req, "LED4=0")) {
LED = 0; // save LED state
digitalWrite(9, LOW);
}
}
void XML_response(WiFiClient cl)
{
delay(800);
Temp = sensors.getTempCByIndex(0);
cl.print("<?xml version = \"1.0\" ?>");
cl.print("<inputs>");
cl.print("<temp>");
cl.print(Temp);
cl.print("</temp>");
cl.print("<LED>");
if (LED) {
cl.print("on");
}
else {
cl.print("off");
}
cl.println("</LED>");
cl.print("</inputs>");
}
// sets every element of str to 0 (clears array)
void StrClear(char *str, char length)
{
for (int i = 0; i < length; i++) {
str[i] = 0;
}
}
// searches for the string sfind in the string str
// returns 1 if string found
// returns 0 if string not found
char StrContains(char *str, char *sfind)
{
//counter++;
//Serial.print("String: ");
//Serial.println(str);
char found = 0;
char index = 0;
char len;
len = strlen(str);
if (strlen(sfind) > len) {
return 0;
}
while (index < len) {
if (str[index] == sfind[found]) {
found++;
if (strlen(sfind) == found) {
return 1;
}
}
else {
found = 0;
}
index++;
}
return 0;
}
Website
<!DOCTYPE html>
<html>
<head>
<title>Fango</title>
<script>
strLED4 = "";
var LED4_state = 0;
var data_val1 = 0;
function GetArduinoInputs()
{
nocache = "&nocache=" + Math.random() * 1000000;
var request = new XMLHttpRequest();
request.onreadystatechange = function()
{
if (this.readyState == 4) {
if (this.status == 200) {
if (this.responseXML != null) {
document.getElementById("Temp").innerHTML =
this.responseXML.getElementsByTagName('temp')[0].childNodes[0].nodeValue;
data_val1 = this.responseXML.getElementsByTagName('temp')[0].childNodes[0].nodeValue;
// LED 4
if (this.responseXML.getElementsByTagName('LED')[1].childNodes[0].nodeValue === "on") {
document.getElementById("LED4").innerHTML = "LED 4 is ON (D9)";
LED4_state = 1;
}
else {
document.getElementById("LED4").innerHTML = "LED 4 is OFF (D9)";
LED4_state = 0;
}
}
}
}
}
request.open("GET", "ajax_inputs" + strLED4 + nocache, true);
request.send(null);
setTimeout('GetArduinoInputs()', 1000);
strLED4 = "";
}
function GetButton2()
{
if (LED4_state === 1) {
LED4_state = 0;
strLED4 = "&LED4=0";
}
else {
LED4_state = 1;
strLED4 = "&LED4=1";
}
}
</script>
</head>
<body onload="GetArduinoInputs()">
<center>
<h1>FangoTest</h1>
<table border="0">
<tr>
<th>Temperatur: <span id="Temp">...</span></th>
</tr>
</table>
<button type="button" id="LED4" onclick="GetButton2()">LED 4 is OFF (D9)</button>
</center>
</body>
</html>