So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By schufti
#84814 here is - as requested - frankensteins monster:

Code: Select all#include <ESP8266WiFi.h>
#include <math.h>
// https://arduino.esp8266.com/stable/package_esp8266com_index.json
unsigned int Rs = 150000;
double Vcc = 3.3;
int cycle_cnt = 0;

// WiFi parameters
#define mySSID "**********"
#define myPWD "********************"

// thingspeak defines
#define tspKEY "api key goes here"
#define tspSVR "api.thingspeak.com"
#define CYCLE 60

WiFiClient tsp_client;
WiFiServer server(80);

void setup() {
  Serial.begin(115200);
  delay(10);
  Serial.println();
  // Init DHT
  // Connect to WiFi network
  WiFi.mode(WIFI_STA);
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(mySSID);
  WiFi.begin(mySSID, myPWD);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  // Start the server
  server.begin();
  Serial.println("Server started");

  // Print the IP address
  Serial.println(WiFi.localIP());
}

void loop() {
  delay(1000);
  float temp = Thermister(AnalogRead());
  float temperatureF = (temp * 9.0 / 5.0) + 32.0;
  Serial.print(temperatureF); Serial.println(" degrees F");
  //Serial.print(Thermister(AnalogRead())); Serial.println(" degrees C");
  WiFiClient client = server.available();
  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 xmlns='http://www.w3.org/1999/xhtml'>");
  client.println("<head>\n<meta charset='UTF-8'>");
  client.println("<title>F-HIVE Wifi Temp Sensor</title>");
  client.println("<link href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css' rel='stylesheet'>");
  client.println("</head>\n<body>");
  client.println("<div class='container'>");
  client.println("<div class='panel panel-default' margin:15px>");
  client.println("<div class='panel-heading'>");
  client.println("<H2>******** Wifi Temp Sensor</H2>");
  client.println("<div class='panel-body' style='background-color: powderblue'>");
  client.println("<pre>");
  client.print("Temperature (°F) : ");
  client.print(temperatureF);
  client.print('\n');
  //client.print("Temperature (°C) : ");
  //client.println(temp, 2);
  client.println("</pre>");
  client.println("</div>");
  client.println("</div>");
  client.println("</div>");
  client.print("</body>\n</html>");
  // send to tsp every CYCLE seconds
  if (++cycle_cnt > CYCLE) tsp(temperatureF);
}

int AnalogRead() {
  int val = 0;
  for (int i = 0; i < 20; i++) {
    val += analogRead(A0);
    delay(1);
  }
  val = val / 20;
  return val;
}

double Thermister(int val) {
  double V_NTC = (double)val / 1024;
  double R_NTC = (Rs * V_NTC) / (Vcc - V_NTC);
  R_NTC = log(R_NTC);
  double Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * R_NTC * R_NTC )) * R_NTC );
  //Temp = Temp - 273.15;
  Temp = Temp - 303.01;
  return Temp;
}

boolean tsp(float tmp) {
  // replace with your channel's thingspeak API key,

  byte rept = 20;
  while (!tsp_client.connect(tspSVR, 80) && --rept > 0) delay(50);
  if (tsp_client.connected()) {
    String payload = "&field1=";
    payload += String(tmp, 2);
    //
    String postStr = "POST /update HTTP/1.1\n";
    postStr += "Host: " + String(tspSVR) + "\n";
    postStr += "Connection: close\n";
    postStr += "X-THINGSPEAKAPIKEY: " + String(tspKEY) + "\n";
    postStr += "Content-Type: application/x-www-form-urlencoded\n";
    postStr += "Content-Length: " + String(payload.length()) + "\n";
    postStr += "\n";
    postStr += payload;
    //
    tsp_client.print(postStr);
    //
    //  read status from tsp  or discard status with client.flush();
    while (tsp_client.connected()) postStr = tsp_client.readStringUntil('\n');
    tsp_client.stop();
    return true;
  }
  else {
    return false;
  }
}


it compiles ok, it's up to you to get it working .....
User avatar
By hackerjoe
#84820 [quote="schufti"]here is - as requested - frankensteins monster:

Thank you that did the trick.
I also had to reverse the pins for ground and VCC the NTC was reading negative numbers in the serial monitor.
I just have one question on the writing of data to thinkspeak which is where do I make the change to upload every hour or half hour only?
I will try and change some things to see if I can figure it out until I hear back.
Thank you again Joe.
The next sensor will either be a DHT11 or DHT22 - I happened to have some NTC's setup and ready for use right now :)
User avatar
By schufti
#84822 there was an error:
Code: Select all  // send to tsp every CYCLE seconds
  if (++cycle_cnt > CYCLE) tsp(temperatureF);
}

needs to be
Code: Select all  // send to tsp every CYCLE seconds
  if (++cycle_cnt > CYCLE) {
    tsp(temperatureF);
    cycle_cnt=0;
   }
}


to upload only every Xsec change the CYCLE define, it is in seconds (but will not be very accurate because of your frankenstein code) you should also consider if to upload just the temp at this moment or an average over the last periode.