0

enter image description here

I have a circuit with preferably long wires that get data from thermistors. The Arduino sends this data (formatted in JSON) to a web server where it can be collected by another program. Some of these values appear correct, while others do not.

[
{
    "dht11": {
        "temperature": 73.4,
        "humidity": 41,
        "heat-index": 77.15
    },
    "thermistors": {
        "therm1": {
            "temp": 71.21,
            "resistance": 12727.27
        },
        "therm2": {
            "temp": 76.22,
            "resistance": 12045.51
        },
        "therm3": {
            "temp": nan,
            "resistance": -19393.41
        }
    }
}
]

code:

#define THERMISTORPIN A0
#define THERMISTORNOMINAL 10000
#define TEMPERATURENOMINAL 25
#define NUMSAMPLES 25
#define BCOEFFICIENT 4050
#define SERIESRESISTOR 10000
#define THERMISTORPIN2 A5
#define THERMISTORPIN3 A3
#define DHTPIN 2
#define PHOTORESISTOR 3
#define DHTTYPE DHT11
int samples[NUMSAMPLES];
int samples_two[NUMSAMPLES];
int samples_three[NUMSAMPLES];
int stuckintime;
#include <SPI.h>
#include <Ethernet.h>
#include "DHT.h"
byte mac[] = {
  0x00, 0x00, 0x00, 0x00, 0x00, 0xEE
};
IPAddress ip(192, 168, 1, 3);
EthernetServer server(80);
DHT dht(DHTPIN, DHTTYPE);
void setup() {
  Serial.begin(9600);
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
  dht.begin();
  analogReference(EXTERNAL);
}

void loop() {
  uint8_t i;
          float average;
          float average_two;
          float average_three;
          float stuckintime;
          for (i = 0; i < NUMSAMPLES; i++) {
            samples[i] = analogRead(THERMISTORPIN);
            samples_two[i] = analogRead(THERMISTORPIN2);
            samples_three[i] = analogRead(THERMISTORPIN3);
            delay(10);
          }
          for (i = 0; i < NUMSAMPLES; i++) {
            average += samples[i];
            average_two += samples_two[i];
            average_three += samples_three[i];
          }
          average /= NUMSAMPLES;
          average = 1023 / average - 1;
          average = SERIESRESISTOR / average;
          float tempreading;
          tempreading = average / 10975;
          tempreading = log(tempreading);
          tempreading /= BCOEFFICIENT;
          tempreading += 1.0 / (TEMPERATURENOMINAL + 273.15);
          tempreading = 1.0 / tempreading;
          tempreading -= 273.15;
          tempreading = tempreading * 9 / 5 + 32;
          average_two /= NUMSAMPLES;
          average_two = 1023 / average_two - 1;
          average_two = SERIESRESISTOR / average_two;
          average_two = average_two;
          float tempreadtwo;
          tempreadtwo = average_two / 11811;
          tempreadtwo = log(tempreadtwo);
          tempreadtwo /= BCOEFFICIENT;
          tempreadtwo += 1.0 / (TEMPERATURENOMINAL + 273.15);
          tempreadtwo = 1.0 / tempreadtwo;
          tempreadtwo -= 273.15;
          tempreadtwo = tempreadtwo * 9 / 5 + 32;  
          average_three /= NUMSAMPLES;
          average_three = 1023 / average_three - 1;
          average_three = SERIESRESISTOR / average_three;
          float tempreadthree;
          tempreadthree = average_three / THERMISTORNOMINAL;
          tempreadthree = log(tempreadthree);
          tempreadthree /= BCOEFFICIENT;
          tempreadthree += 1.0 / (TEMPERATURENOMINAL + 273.15);
          tempreadthree = 1.0 / tempreadthree;
          tempreadthree -= 273.15;
          tempreadthree = tempreadthree * 9 / 5 + 32;
          float h = dht.readHumidity();
          float t = dht.readTemperature();
          float f = dht.readTemperature(true);
          float hif = dht.computeHeatIndex(f, h);
  EthernetClient client = server.available();
  if (client) {
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        if (c == '\n' && currentLineIsBlank) {
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: application/json");
          client.println("Connection: close");
          client.println("Refresh: 0");
          client.println("");
          client.println("[");
          client.println("   {");
          client.println("    \"dht11\": {");
          client.print("          \"temperature\": ");  client.print(f);        client.println(",");
          client.print("          \"humidity\": ");         client.print(h);        client.println(",");
          client.print("          \"heat-index\": ");   client.print(hif);  client.println("");
          client.println("     },");
          client.println("    \"thermistors\": {");
          client.println("        \"under-bed\": {");
          client.print("             \"temp\": ");          client.print(tempreading);      client.println(",");
          client.print("             \"resistance\": ");    client.print(average);  client.println("");
          client.println("       },");
          client.println("        \"side-bed\": {");
          client.print("             \"temp\": ");          client.print(tempreadtwo);      client.println(",");
          client.print("             \"resistance\": ");    client.print(average_two);  client.println("");
          client.println("       },");        
          client.println("        \"top-bed\": {");
          client.print("             \"temp\": ");          client.print(tempreadthree);        client.println(",");
          client.print("             \"resistance\": ");    client.print(average_three);    client.println("");
          client.println("       }");
          client.println("     }");
          client.println("   }");
          client.println("]");
          break;
        }
        if (c == '\n') {
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          currentLineIsBlank = false;
        }
      }
    }
    delay(1);
    client.stop();
  }
}

I have no idea what is causing this to happen - pushing RESET doesn't fix it at all, reconnecting wires does not fix it, any help?
(Please tell me what else to include if you need it.)

  • Your code would help somewhat... – Majenko Aug 16 '15 at 19:42
  • @Majenko - added (ignore any redundancy in the code, i should really clean it up but i'm trying to fix it before i do that) – 智障的人 Aug 16 '15 at 20:06
  • 1
    I suggest you write a function to do the maths. Having multiple lines of redundant code hinders investigations. – Nick Gammon Aug 16 '15 at 20:57
  • @NickGammon I found another piece of code that used different maths, but put it in a function - gist.github.com/viktorahlstrom/af1ebe275d54df104bdc – 智障的人 Aug 16 '15 at 21:03
  • Looks a lot simpler. :) – Nick Gammon Aug 16 '15 at 21:19
3

Here's a major problem:

void loop() {

  uint8_t i;
          float average;
          float average_two;
          float average_three;
          float stuckintime;
          for (i = 0; i < NUMSAMPLES; i++) {
            samples[i] = analogRead(THERMISTORPIN);
            samples_two[i] = analogRead(THERMISTORPIN2);
            samples_three[i] = analogRead(THERMISTORPIN3);
            delay(10);
          }
          for (i = 0; i < NUMSAMPLES; i++) {
            average += samples[i];
            average_two += samples_two[i];
            average_three += samples_three[i];
          }

The variables average, average_two, and average_three have undefined values. Thus it isn't surprising that when you add something to them, they are still undefined.

Local variables must be initialized (eg. to zero in your case).

Also, you have two variables named stuckintime - I would do something about that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.