-->
Page 1 of 3

NodeMCU Amica V.3 and LM35 (temperature sensor)

PostPosted: Tue Nov 10, 2015 11:23 am
by smartgatto
Hello, my new toy is an NodeMCU Amica V3 (I came from Arduino with Ethernet Shield).
I'd like to connect an LM35 temperature sensor and I write a sketch, but I can't read anything...can you help me?
The result is always 499.51 (with or without sensor pin connected to PIN A0, other on 3.3v and GND).


Code: Select all#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiClient.h>

const char* ssid = "xxxxxxxxxxxxx";
const char* password = "xxxxxxxxxxxxx";


const int pin = 0; // analog pin
float celsius = 0, farhenheit =0;
float millivolts;
int sensor;

// multicast DNS responder
MDNSResponder mdns;

// TCP server at port 80 will respond to HTTP requests
WiFiServer server(80);

void setup(void)

  Serial.begin(115200);
 
  // Connect to WiFi network
  WiFi.begin(ssid, password);
  Serial.println(""); 
 
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  if (!mdns.begin("esp8266", WiFi.localIP())) {
    Serial.println("Error setting up MDNS responder!");
    while(1) {
      delay(1000);
    }
  }
  Serial.println("mDNS responder started");
 
  // Start TCP (HTTP) server
  server.begin();
  Serial.println("TCP server started");
}

void loop(void)
{
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  Serial.println("");
  Serial.println("New client");

  // Wait for data from client to become available
  while(client.connected() && !client.available()){
    delay(1);
  }
 
  // Read the first line of HTTP request
  String req = client.readStringUntil('\r');
 
  // First line of HTTP request looks like "GET /path HTTP/1.1"
  // Retrieve the "/path" part by finding the spaces
  int addr_start = req.indexOf(' ');
  int addr_end = req.indexOf(' ', addr_start + 1);
  if (addr_start == -1 || addr_end == -1) {
    Serial.print("Invalid request: ");
    Serial.println(req);
    return;
  }
  req = req.substring(addr_start + 1, addr_end);
  Serial.print("Request: ");
  Serial.println(req);
  client.flush();
 
  String s;
  if (req == "/")
  {
    float letturamedia = lettura();   
    IPAddress ip = WiFi.localIP();
    String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
   s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>Hello from ESP8266 at ";
   
    s += ipStr;
   
    Serial.println("Sending 200");
    Serial.println("La temperatura e' ");
    Serial.println(letturamedia);
  }
  else
  {
    s = "HTTP/1.1 404 Not Found\r\n\r\n";
    Serial.println("Sending 404");
  }
  client.print(s);
 
  Serial.println("Done with client");
}


float lettura(){
    float somma=0.0;
    float media=0.0;
    int i=0;
   
    for(i=0;i<8;i++)
      {
      sensor = analogRead(pin);
      millivolts = ( sensor/1024.0)*5000;
      celsius =millivolts/10;
      somma=somma + celsius;
      delay(500); //ritardo tra due campionature successive
      }
    media= (somma/8.0); //calcolo del valore medio di 8 rilievi
    media = media;
    somma=0; // riazzeramento della variabile somma
    return media;
   
}

Re: NodeMCU Amica V.3 and LM35 (temperature sensor)

PostPosted: Tue Nov 10, 2015 2:25 pm
by Villa
Hello smartgatto ,

removes this line of code:
millivolts = ( sensor/1024.0)*5000;

try this:
celsius= ( sensor*100/1024.0);

By

Re: NodeMCU Amica V.3 and LM35 (temperature sensor)

PostPosted: Wed Nov 11, 2015 5:55 am
by smartgatto
Hi, thanks for your reply..
I try your modified code, but I have only 99.90. Everytime.
I tried to connect LM35 Vcc and GND with an external (FTDI232) and connect OUT to NodeMCU but nothing...
This LM35 on Arduino do its work...

The output pin of LM35 I connect to A0 nodemcu pin, is it correct?
Image

Re: NodeMCU Amica V.3 and LM35 (temperature sensor)

PostPosted: Wed Nov 11, 2015 7:13 am
by krzychb
Hi smartgatto,

If “This LM35 on Arduino do its work” you are already there :D.

I would check first if I am able to correctly read the voltage applied to A0.
The A0 input range is from 0 to 1V. The “analogRead(A0)” will show that as integer value from 0 to 1023.
You can do it by uploading a minimalistic sketch as below and checking what is displayed on Serial Terminal

Code: Select allvoid setup(){
  Serial.begin(115200);
}

void loop(){
  Serial.println(analogRead(A0));
  delay(1000);
}


Here is what I see if nothing no voltage is connected (readings from 0 to 6), or if I touch A0 with my finger (readings about 15 – 30).
SerialTerminal-A0.png

Such readings may be different for you - just check if they change at all.

Then check the voltage on LM35. If it is within 0 to 1V range, connect it to A0 and observer if you see corresponding reading on Serial Monitor.

For instance If the voltage is about 0.5V you should see about 512 on Serial Monitor, etc.

Only after that go to next steps like: converting A0 output to millivolts or Celsius, hooking to WiFi and so on.

Good luck :D

Krzysztof