-->
Page 1 of 1

Howto add Webserver to DHT Thingspeak sketch

PostPosted: Sat Oct 01, 2016 9:11 pm
by AusUser
I'm successfully sending my bedroom temp and humidity using this DHT and Thingspeak sketch located here https://eryk.io/2016/04/temperature-logging-to-thingspeak-using-esp8266-and-dht22/ however I'd like to my sketch a little further now an enable the webserver function aswell so the the esp8266 can 1) initially display temp and humidity, then move onto hosting the google gauges that I created from thinkspeak plugin page.

I've new to C# programming so I found sketch simple sketch that does what I wanted http://www.elec-cafe.com/esp8266-temperature-humidity-webserver-with-a-dht11-sensor/

However when trying to compile I get

ESP8266WebServer server(80);
conflicting declaration 'ESP8266WebServer server(80);'

Is this because the server(80) is already being used to post the information to thinkspeak ?

Re: Howto add Webserver to DHT Thingspeak sketch

PostPosted: Sun Oct 02, 2016 6:31 pm
by Barnabybear
Hi, have a look at this code, it works for me.
Code: Select all#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>                // for OTA updates uncomment if required
#include <WiFiUdp.h>                    // for OTA updates uncomment if required
#include <ArduinoOTA.h>                 // for OTA updates uncomment if required
extern "C" {
#include <user_interface.h>
}
int Voltage_average = 0;
int Voltage_read = 0;
int Voltage_conv = 0;
int Voltage_peek = 0;
int Voltage_low = 1000;
int Voltage_range =0;
int Current_instant = 0;
int thingspeak_counter =0;
String s;

const char ssid[] = "<SSID>";             // Replace with your SSID
const char password[] = "<passphrase>";   // Replace with your WPA2 passphrase
String apiKey = "<APIkey>";             // Replace with your thigspeak API key
WiFiServer server(80);

void setup() {
  wifi_set_sleep_type(NONE_SLEEP_T);      //needed to stablise internal reference
  Serial.begin(115200);                   // Start serial coms @ 115200
  delay(10);
  WiFi.begin(ssid, password);
  server.begin();
  ArduinoOTA.begin();                   // for OTA updates uncomment if required

}

void loop() {
  ArduinoOTA.handle();                   // for OTA updates uncomment if required

  WiFiClient client = server.available();  // Check for client.
  if (client) {
    browser_update();
    Serial.println("Posting to browser");
    client.print(s);
    client.flush();
    client.stop();
  }
  for( int n =0; n <10; n++){
    Voltage_average += analogRead(A0);
      Serial.print(analogRead(A0));
      Serial.print(" ");
      Serial.print(Voltage_average);
      Serial.print(" ");
    delay(10);
  }
  Serial.println("");
 
  Voltage_read = (Voltage_average / 10);
  Voltage_average = 0;
  Serial.print("Voltage average = ");
  Serial.print(Voltage_read);
  if(Voltage_peek < Voltage_read){
   Voltage_peek = Voltage_read;
  }
   if(Voltage_low > Voltage_read){
   Voltage_low = Voltage_read;
  }
  Serial.print(" : Voltage peek = ");
  Serial.print(Voltage_peek);
  Serial.print(" : Voltage low = ");
  Serial.print(Voltage_low);
  Voltage_range = Voltage_peek - Voltage_low;
  Serial.print(" : Voltage range = ");
  Serial.print(Voltage_range);
  Current_instant = ((792- Voltage_read)  * 47.74);
  Serial.print(" : Current instant = ");
  Serial.print(Current_instant);
  Serial.print(" : thingspeak counter = ");
  Serial.println(thingspeak_counter);
  thingspeak_counter ++;
  while (thingspeak_counter > 20){
    thingspeak();
    thingspeak_counter = 0;
    Voltage_peek = Voltage_read;
    Voltage_low = Voltage_read;
    Voltage_range = 0;
    Current_instant = 0;
  }
    delay(1000);
}

//****************** Prepare browser updated text and data ******************
String browser_update() {
  s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n<font size=\"7\">";
  s += "Voltage is ";
  s += Voltage_conv;
  s += "</font></html>\n";
  return s;
}

//****************** Prepare thingspeak data and post ******************
void thingspeak() {

  WiFiClient client = server.available();
  if (client.connect("api.thingspeak.com", 80)) { //   "184.106.153.149" or api.thingspeak.com
    String postStr1 = apiKey;
    postStr1 += "&field1=";
    postStr1 += String(Voltage_peek);
    postStr1 += "&field2=";
    postStr1 += String(Voltage_low);
    postStr1 += "&field3=";
    postStr1 += String(Voltage_range);
    postStr1 += "&field4=";
    postStr1 += String(Current_instant);
    postStr1 += "\r\n\r\n";
   
    client.print("POST /update HTTP/1.1\n");
    client.print("Host: api.thingspeak.com\n");
    client.print("Connection: close\n");
    client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
    client.print("Content-Length: ");
    client.print(postStr1.length());
    client.print("\n\n");
    client.print(postStr1);
    delay(100);
    Serial.println("Post sent to thingSpeak"); // prints to serial monitor "sent to thingSpeak"
    client.flush();
    client.stop();
  }
}