- Tue May 05, 2015 12:53 pm
#16607
This is working for me with both thingspeak and sparkfun.. hacked together from a few samples I saw around and I created the sub down the bottom to do the hard yards.
should be easy enough to see where you need to go..I knocked it up yesterday so its not completely finished, need to decide what to do on a failed connection for eg..
main thing is to open the url in the client request and then send the get..
just need to remove all my keys...
Code: Select all/*
* This sketch sends data via HTTP GET requests to data.sparkfun.com service.
*
* You need to get streamId and privateKey at data.sparkfun.com and paste them
* below. Or just customize this script to talk to other HTTP servers.
*
*/
#define DHTCONNECTED
#define DSCONNECTED
#define SPARKFUN
#define THINGSPEAK
#define USEDEEPSLEEP
#define SLEEPTIME 60
#define SECONDS 1000000
#include <ESP8266WiFi.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <string.h>
#ifdef DHTCONNECTED
#include <DHT.h>
#define DHTTYPE DHT11 // DHT 11
#define DHTPIN 0
DHT dht(DHTPIN, DHTTYPE , 15);
#endif
#ifdef DSCONNECTED
#define ONE_WIRE_BUS 2
#define DS_ADDRESS Thermometer7
OneWire oneWire(ONE_WIRE_BUS);
DeviceAddress Thermometer4 = { 0x28, 0xFF, 0x0C, 0x47, 0x31, 0x04, 0x00, 0x41 };
DeviceAddress Thermometer7 = { 0x28, 0x6A, 0xCD, 0xAA, 0x05, 0x00, 0x00, 0x5B };
DallasTemperature sensors(&oneWire);
#endif
const char* ssid = "TP-LINK_2.4GHz_xxxxx";
const char* password = "abcdefgh";
#ifdef SPARKFUN
const char* sparkhost = "data.sparkfun.com";
const char* sparkprivateKey = "aaaaaaa";
const char* sparkpublicKey = "xxxxxxxxx";
#endif
#ifdef THINGSPEAK
const char* thinghost = "api.thingspeak.com";
const char* thingprivateKey = "ccccccccccc";
#endif
int spark , thing;
void setup() {
Serial.begin(115200);
#ifdef DSCONNECTED
sensors.begin();
// set the resolution to 12 bit
sensors.setResolution(DS_ADDRESS, 12);
#endif
#ifdef DHTCONNECTED
dht.begin();
#endif
delay(10);
// We start by connecting to a WiFi network
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
#ifdef DHTCONNECTED
int dht11_temp = dht.readTemperature();
int dht11_humid = dht.readHumidity();
if ((dht11_humid < 1) || (dht11_humid > 100.0))
{
Serial.println("Error reading humidity");
Serial.print("Humidity ");
Serial.println(dht11_humid);
return;
}
if ((dht11_temp < 0) || (dht11_temp > 70))
{
Serial.println("Error reading dht11 temp");
Serial.print("Temp ");
Serial.println(dht11_temp);
return;
}
#endif
#ifdef DSCONNECTED
sensors.requestTemperatures();
float value = sensors.getTempC(DS_ADDRESS);
if ((value <= 0.0) || (value > 70.0))
{
Serial.println("Error reading temp");
Serial.print("Temperature = ");
Serial.println(value);
return;
}
#endif
#ifdef THINGSPEAK
String url = "/update?key=";
url += thingprivateKey;
#ifdef DSCONNECTED
url += "&field1=";
url += String(value);
#endif
#ifdef DHTCONNECTED
url += "&field2=";
url += String(dht11_temp);
url += "&field3=";
url += String(dht11_humid);
#endif
#endif
if (sendTCP(thinghost, 80, url)) thing++;
#ifdef SPARKFUN
url = "/input/";
url += sparkpublicKey;
url += "?private_key=";
url += sparkprivateKey;
#ifdef DSCONNECTED
url += "&dstemp=";
url += String(value);
#endif
#ifdef DHTCONNECTED
url += "&temp=";
url += String(dht11_temp);
url += "&humidity=";
url += String(dht11_humid);
#endif
#endif
if (sendTCP(sparkhost, 80, url)) spark++;
delay(100);
Serial.print("Thingspeak: ");
Serial.print(thing);
Serial.print(" Sparkfun: ");
Serial.println(spark);
#ifdef DHTCONNECTED
Serial.print("Temp: ");
Serial.print(dht11_temp);
Serial.print(" Humidity: ");
Serial.println(dht11_humid);
#endif
#ifdef DSCONNECTED
Serial.print("DS Temperature = ");
Serial.println(value);
#endif
#ifdef DEEPSLEEP
ESP.deepSleep(SLEEPTIME * SECONDS);
#else
delay(SLEEPTIME * 1000);
#endif
}
boolean sendTCP (const char* host, int httpPort, String request)
{
Serial.print("connect:");
Serial.println(host);
WiFiClient client;
if (!client.connect(host, httpPort)) {
Serial.println("Connection failed");
return false;
}
Serial.print("Requesting URL: ");
Serial.println(request);
client.print(String("GET ") + request + " HTTP/1.1\r\n" + "Host: " + host + "\r\nConnection: close\r\n\r\n");
delay(200);
// Read all the lines of the reply from server and print them to Serial
while (client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println("Success - closed");
return true;
}