Linkit One display temperature on a webpage

In our previous LM35 example   we simply displayed the temperature via the serial port but lets take it a bit further and display some temperature readings on a web page.

First of all the Linkit One comes with various aerials that you can connect to the board, for this example you will need to connect the Wifi one, you can see these in the image underneath

600px-linkit_one_antenna

The schematics and parts are the same as the example mentioned at the start of the article

Schematics and Parts

You will need the following parts for this example

Linkit One
LM35 sensor or module
Hook up wire (dupont cables)

Very simple to connect. Connect the Vcc to 5v on your board, Gnd goes to the Gnd and out goes to Linkit One A0, you can see this below

%e2%80%8clinkitone-and-lm35_bb

 

Code

There are 3 lines that will/may need changed for your Wifi details

#define WIFI_AP "wifi ssid here"
#define WIFI_PASSWORD "password here"
#define WIFI_AUTH LWIFI_WPA

The last one you can select from from LWIFI_OPEN, LWIFI_WPA, or LWIFI_WEP according to your WiFi AP configuration

[codesyntax lang=”cpp”]

#include <LTask.h>
#include <LWiFi.h>
#include <LWiFiServer.h>
#include <LWiFiClient.h>

#define WIFI_AP "wifi ssid here"
#define WIFI_PASSWORD "password here"
#define WIFI_AUTH LWIFI_WPA


LWiFiServer server(80);

float tp[10];
int outputpin= A0;


float getTemperature()
{

    int rawvoltage= analogRead(outputpin);
    float celsius = rawvoltage*0.48828125;
    return (celsius);
}

void getNewTemperature()
{
    float tp_new = getTemperature();
    
    for(int i=9; i>=1; i--)
    {
        tp[i] = tp[i-1];
    }
    
    tp[0] = tp_new;
}

void setup()
{
    LTask.begin();
    LWiFi.begin();
    Serial.begin(9600);
    //while(!Serial.available());     // input anything to start the program
    for(int i=0; i<20; i++)
    {
        tp[i] = getTemperature();
    }

    // keep retrying until connected to AP
    Serial.println("Connecting to AP");
    while (0 == LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD)))
    {
        delay(1000);
    }

    printWifiStatus();

    Serial.println("Start Server");
    server.begin();
    Serial.println("Server Started");
}

int loopCount = 0;

void loop()
{
    // put your main code here, to run repeatedly:
    delay(500);
    loopCount++;
    LWiFiClient client = server.available();
        
    getNewTemperature();
    
    if (client)
    {
        Serial.println("new client");
        // an http request ends with a blank line
        boolean currentLineIsBlank = true;
        
        unsigned long timer_out = millis();
        
        while (client.connected())
        {
        
            if(millis()-timer_out > 5000)break;
            
            if (client.available())
            {
                // we basically ignores client request, but wait for HTTP request end
                int c = client.read();
                Serial.print((char)c);

                if (c == '\n' && currentLineIsBlank)
                {
                    Serial.println("send response");
                    // send a standard http response header
                    client.println("HTTP/1.1 200 OK");
                    client.println("Content-Type: text/html");
                    client.println("Connection: close");  // the connection will be closed after completion of the response
                    client.println("Refresh: 5");  // refresh the page automatically every 5 sec
                    client.println();
                    client.println("<!DOCTYPE HTML>");
                    client.println("<html>");
                    // output the value of each analog input pin
                    
                    
                    for(int i=0; i<10; i++)
                    {
                        client.println(tp[i]);
                        client.println("<br />");
                    }
                    client.println("<br />");
                    client.println("</html>");
                    client.println();
                    break;
                }
                if (c == '\n')
                {
                    // you're starting a new line
                    currentLineIsBlank = true;
                }
                else if (c != '\r')
                {
                    // you've gotten a character on the current line
                    currentLineIsBlank = false;
                }
            }
        }
        // give the web browser time to receive the data
        delay(500);

        // close the connection:
        Serial.println("close connection");
        client.stop();
        Serial.println("client disconnected");
    }
}

void printWifiStatus()
{
    // print the SSID of the network you're attached to:
    Serial.print("SSID: ");
    Serial.println(LWiFi.SSID());

    // print your WiFi shield's IP address:
    IPAddress ip = LWiFi.localIP();
    Serial.println("Please open your browser, and input the following address:");
    Serial.println(ip);

    Serial.print("\r\nsubnet mask: ");
    Serial.println(LWiFi.subnetMask());

    Serial.print("gateway IP: ");
    Serial.println(LWiFi.gatewayIP());

    // print the received signal strength:
    long rssi = LWiFi.RSSI();
    Serial.print("signal strength (RSSI):");
    Serial.print(rssi);
    Serial.println(" dBm");
}

[/codesyntax]

 

Testing

This example has test code to help you out – the printWifiStatus function, open up the USB Modem Port – not the programming port. You shoudl see a message like this

Connecting to AP
SSID: your ssid here
Please open your browser, and input the following address:
192.168.1.15

Now open the IP address above using your favourite web browser and you should see something like this

20.51
20.51
20.51
20.51
20.51
20.51
20.51
20.51
20.51
20.51

It displays a series of temperature readings, obviously since the value from the sensor is being displayed on a web page you could add otehr bells and whistles such as an alert, you could have multiple sensors in a table – say readings from different rooms, a tropical fish tank, outside temperature but this is a nice little starter example

 

Links