ESP 8266 Wifi not collecting data from TS | ESP8266 Wi-Fi Module | Forum

Avatar

Please consider registering
Guest

Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

Register Lost password?
sp_Feed sp_TopicIcon
ESP 8266 Wifi not collecting data from TS
Avatar
ashebray

Silver
Forum Posts: 8
sp_UserOfflineSmall Offline
1
December 29, 2017 - 12:10 am
sp_Permalink sp_Print

My Thing Speak is attached to my ESP 8266 but no data is being recorded. I did a test on it with my public channel and it worked. However it will not update data on my current private channel. Here is the code. Any help will be appreciated! 

 

 

#include <ESP8266WiFi.h>
#include "DHT.h"

 

/***************************
* Begin Settings
**************************/

 

const char* ssid = "WuTangLAN";
const char* password = "spranktank7664A";

const char* host = "api.thingspeak.com";

const char* THINGSPEAK_API_KEY = "9DAGDL2109WC8DIJ";

// DHT Settings
#define DHTPIN D6 // what digital pin we're connected to. If you are not using NodeMCU change D6 to real pin

 

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)

const boolean IS_METRIC = true;

// Update every 420 seconds = 7 minutes. Min with Thingspeak is ~20 seconds
const int UPDATE_INTERVAL_SECONDS = 420;

/***************************
* End Settings
**************************/

// Initialize the temperature/ humidity sensor
DHT dht(DHTPIN, DHTTYPE);

void setup() {
Serial.begin(115200);
delay(10);

// We start by connecting to a WiFi network

Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}

void loop() {
Serial.print("connecting to ");
Serial.println(host);

// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}

// read values from the sensor
float humidity = dht.readHumidity();
float temperature = dht.readTemperature(!IS_METRIC);

// We now create a URI for the request
String url = "/update?api_key=";
url += THINGSPEAK_API_KEY;
url += "&field1=";
url += String(temperature);
url += "&field2=";
url += String(humidity);

Serial.print("Requesting URL: ");
Serial.println(url);

// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1
" +
"Host: " + host + "
" +
"Connection: close

");
delay(10);
while(!client.available()){
delay(100);
Serial.print(".");
}
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('
');
Serial.print(line);
}

Serial.println();
Serial.println("closing connection");

 

// Go back to sleep. If your sensor is battery powered you might
// want to use deep sleep here
delay(1000 * UPDATE_INTERVAL_SECONDS);
}

Avatar
cstapels

Gold
Forum Posts: 123
sp_UserOfflineSmall Offline
2
December 29, 2017 - 10:56 am
sp_Permalink sp_Print

I think the order of your commands is wrong.  You are printing the GET verb after you provide the address.  Have a look at the soil moisture example in the doc.  It uses POST instead of GET, but the process is similar.  Let us know if you get it going.

Avatar
ashebray

Silver
Forum Posts: 8
sp_UserOfflineSmall Offline
3
December 30, 2017 - 9:40 pm
sp_Permalink sp_Print

cstapels said

I think the order of your commands is wrong.  You are printing the GET verb after you provide the address.  Have a look at the soil moisture example in the doc.  It uses POST instead of GET, but the process is similar.  Let us know if you get it going.  

How do I change the order? Or change from GET to POST? Sorry for the questions, I'm just having a rough time with this!

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online: jacktheripper125, pudderz
23 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 252

Vinod: 164

cstapels: 96

piajola: 75

vespapierre: 63

Adarsh_Murthy: 59

Newest Members:

bbruno22

pudderz

Utkya

Poornak26

LindasHed

reddysiva

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

 

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Administrators: Hans, lee