i cannot get my esp8266 to send temp to thingspeak

#include <WiFiClient.h>
#include <ThingSpeak.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <ESP8266WiFi.h>
// Network parameters
const char* ssid = "ATT5vxgjUI";
const char* password = "xxxxxxxxxx";
const char* myWriteAPIKey = "xxxxxxxxxxxxxxxx";
//WiFiClient client;
WiFiClient client;
float temperatureF;
// ThingSpeak information
char thingSpeakAddress[] = "api.thingspeak.com"; //eg: 192.168.0.218
unsigned long channelID = 1498546;
char* readAPIKey = "WO07HWZ161A5RK2K";
char* writeAPIKey = "xxxxxxxxxxxxxxxx";
// GPIO where the DS18B20 is connected to
const int oneWireBus = 4;
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(oneWireBus);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(115200);
Serial.println();
Serial.print("Wifi connecting to ");
Serial.println( ssid );
WiFi.begin(ssid, password);
Serial.println();
Serial.print("Connecting");
while ( WiFi.status() != WL_CONNECTED ) {
delay(500);
Serial.print(".");
}
Serial.println("Wifi Connected Success!");
Serial.print("NodeMCU IP Address : ");
Serial.println(WiFi.localIP() );
}
void loop() {
sensors.requestTemperatures();
float temperatureF = sensors.getTempFByIndex(0);
Serial.print(temperatureF);
Serial.println("ºF");
delay(60000);\
}
void Sending_To_thingspeak() //CONNECTING WITH MYSQL
{
Serial.println("connected");
// Make a HTTP request:
Serial.print("GET /update?api_key=<xxxxxxxxxxxxxxxx>&field1=1");
client.print("GET /update?api_key=<xxxxxxxxxxxxxxxx>&field1=1"); //https://api.thingspeak.com/update?api_key=6H5AD4KPI5WLIYVO&field1=0
client.print(temperatureF);
Serial.println(temperatureF);
client.print("&field1=");
Serial.println("&field1=");
client.print(" "); //SPACE BEFORE HTTP/1.1
client.print("HTTP/1.1");
client.println();
client.println("Host: api.thingspeak.com");
client.println("Connection: close");
client.println();
}

3 Comments

You have included the ThingSpeak library but you arent using it. We really reccomend using the examples in the ThingSpeak library. They take care of many issues you will have. YOu can see them in the github repo or if you are using the Arduino IDE, in the File > examples > ThingSpeak menu.
In the above code, you have some extra '<' and '>' characters, but I really reccomend getting rid of this code and starting with theThingSpeak library examples.
i restarted with the thingspeak example but i am still having a problem writing to my channel
#include <ThingSpeak.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
const int oneWireBus = 4;
OneWire oneWire(oneWireBus);
DallasTemperature sensors(&oneWire);
// Network parameters
const char* ssid = "ATT5vxgjUI";
const char* password = "5615236882";
int number = 0;
// ThingSpeak information
char thingSpeakAddress[] = "api.thingspeak.com";
unsigned long channelID = 1498546;
char* readAPIKey = "WO07HWZ161A5RK2K";
char* writeAPIKey = "6H5AD4KPI5WLIYVO";
const unsigned long postingInterval = 120L * 1000L;
unsigned int dataFieldOne = 1; // Field to write temperature data
unsigned int dataFieldTwo = 2; // Field to write temperature data
unsigned int dataFieldThree = 3; // Field to write elapsed time data
unsigned int aField = 6; //Field to hold first constant of the thermistor calibration
unsigned int bField = 7; //Field to hold second constant of the thermistor calibration
unsigned int cField = 8; //Field to hold third constant of the thermistor calibration
// Global variables
// These constants are device specific. You need to get them from the manufacturer or determine them yourself.
float aConst = 2.25E-02;
float bConst = -0.003422894649;
float cConst = 0.00001518485044;
unsigned long lastConnectionTime = 0;
long lastUpdateTime = 0;
WiFiClient client;
void setup() {
Serial.begin(9600);
Serial.println();
Serial.print("Wifi connecting to ");
Serial.println( ssid );
WiFi.begin(ssid, password);
Serial.println();
Serial.print("Connecting");
while ( WiFi.status() != WL_CONNECTED ) {
delay(500);
Serial.print(".");
}
Serial.println("Wifi Connected Success!");
Serial.print("NodeMCU IP Address : ");
Serial.println(WiFi.localIP() );
}
void loop() {
sensors.requestTemperatures();
float temperatureF = sensors.getTempFByIndex(0);
Serial.print(temperatureF);
Serial.println("ºF");
delay(60000);\
}
int connectWiFi(){
while (WiFi.status() != WL_CONNECTED) {
WiFi.begin( ssid, password );
delay(500);
Serial.println("Connecting to WiFi");
}
Serial.println( "Connected" );
ThingSpeak.begin( client );
}
float readTSData( long TSChannel,unsigned int TSField ){
float data = ThingSpeak.readFloatField( TSChannel, TSField, writeAPIKey );
Serial.println( " Data read from ThingSpeak: " + String( data, 9 ) );
return data;
}
int writeTSData( long TSChannel, unsigned int TSField, float data ){
int writeSuccess = ThingSpeak.writeField( TSChannel, TSField, data, writeAPIKey ); // Write the data to the channel
if ( writeSuccess ){
Serial.println( String(data) + " written to Thingspeak." );
}
return writeSuccess;
}
Great, thanks for switching the example. That one is an ok example, but I actually meant something like one of these examples.
It looks like in your code, you aren't calling the writeTSData() function. I wouldn't expect there to be any write to thingSpeak using the code you provided. I suggest you start with the example and then work in the details of your specific case.
What are you seeing on the serial monitor?

Sign in to comment.

Answers (0)

Communities

More Answers in the  ThingSpeak Community

Categories

Tags

Asked:

on 7 Sep 2021

Edited:

on 10 Sep 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!