Using ESP8266 to post temp/humidity data to Phant

Everything pertaining to the data.sparkfun.com service, the phant project which powers it, and user projects which talk to the service.

Moderators: phalanx, toddtreece, Brennen@SparkFun

Post Reply
b3bowen
Posts: 1
Joined: Thu Oct 29, 2015 12:51 pm

Using ESP8266 to post temp/humidity data to Phant

Post by b3bowen » Thu Oct 29, 2015 12:55 pm

I am trying to use a Sparkfun redboard and an ESP8266 wifi Shield to post humidity and temperature data to Data.sparkfun.com (phant). My sketch is below. Currently, I can use my wifi shield to post data to phant (either read one of the inputs or assign a random value to post), and I can use my serial monitor to read temp/humidity from my DHT22. As soon as I try to read the sensor and post to phant, however, it quits posting. The program won't even post the value I assigned to val, for debugging reasons, let alone if I tell it to post "h" or "t". The goal of the program below would be to post humidity/temp each time I strike a key in serial monitor. I would appreciate any guidance.

Thanks.

#include <SoftwareSerial.h>
#include "DHT.h"
#include <SparkFunESP8266WiFi.h>
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
float h;
float t;
float f;
float val;




//////////////////////////////
// WiFi Network Definitions //
//////////////////////////////
// Replace these two character strings with the name and
// password of your WiFi network.
const char mySSID[] = "*****";
const char myPSK[] = "*****";

/////////////////////
// Phant Constants //
/////////////////////
// Phant detsination server:
const String phantServer = "data.sparkfun.com";
// Phant public key:
const String publicKey = "***********";
// Phant private key:
const String privateKey = "*************";
String httpHeader = "POST /input/" + publicKey + ".txt HTTP/1.1\n" +
"Host: " + phantServer + "\n" +
"Phant-Private-Key: " + privateKey + "\n" +
"Connection: close\n" +
"Content-Type: application/x-www-form-urlencoded\n";

void setup()
{
int status;
Serial.begin(9600);

status = esp8266.begin();
if (status <= 0)
{
Serial.println(F("Unable to communicate with shield. Looping"));
while (1) ;
}

esp8266.setMode(ESP8266_MODE_STA); // Set WiFi mode to station
if (esp8266.status() <= 0) // If we're not already connected
{
if (esp8266.connect(mySSID, myPSK) < 0)
{
Serial.println(F("Error connecting"));
while (1) ;
}
}

// Get our assigned IP address and print it:
Serial.print(F("My IP address is: "));
Serial.println(esp8266.localIP());

Serial.println(F("Press any key to post to Phant!"));
dht.begin();
}

void loop()
{
val = 2.1;
delay(2000);
readSensor();
// If a character has been received over serial:
if (Serial.available())
{
// !!! Make sure we haven't posted recently
// Post to Phant!
postToPhant();

// Then clear the serial buffer:
while (Serial.available())
Serial.read();
}

}


void postToPhant()
{
// Create a client, and initiate a connection
ESP8266Client client;

if (client.connect(phantServer, 80) <= 0)
{
Serial.println(F("Failed to connect to server."));
return;
}
Serial.println(F("Connected."));

// Set up our Phant post parameters:
String params;
params += "moisture=" + String(digitalRead(4)) + "&";
params += "temperature=" + String(val);

Serial.println(F("Posting to Phant!"));

client.print(httpHeader);
client.print("Content-Length: "); client.println(params.length());
client.println();
client.print(params);

// available() will return the number of characters
// currently in the receive buffer.
while (client.available())
Serial.write(client.read()); // read() gets the FIFO char

// connected() is a boolean return value - 1 if the
// connection is active, 0 if it's closed.
if (client.connected())
client.stop(); // stop() closes a TCP connection.
}

void readSensor()
{
h = dht.readHumidity();
// Read temperature as Celsius (the default)
t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
f = dht.readTemperature(true);

// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}



}

Post Reply