I'm having issue with connecting to thingspeak.

wael elsisi on 27 Jun 2021 (Edited on 27 Jun 2021)
Latest activity Edit by Christopher Stapels on 28 Jun 2021

I'm a newbie in Thingspeak. I have tried manytime to upload my code using the example from library to thingspeak but nothing be happened. I'm using Esp6266. Would you mind helping me?

if true
/*
WriteMultipleFields
Description: Writes values to fields 1,2,3,4 and status in a single ThingSpeak update every 20 seconds.
Hardware: ESP8266 based boards
!!! IMPORTANT - Modify the secrets.h file for this project with your network connection and ThingSpeak channel details. !!!
Note:
- Requires ESP8266WiFi library and ESP8622 board add-on. See https://github.com/esp8266/Arduino for details.
- Select the target hardware from the Tools->Board menu
- This example is written for a network using WPA encryption. For WEP or WPA, change the WiFi.begin() call accordingly.
ThingSpeak ( https://www.thingspeak.com ) is an analytic IoT platform service that allows you to aggregate, visualize, and 
analyze live data streams in the cloud. Visit https://www.thingspeak.com to sign up for a free account and create a channel.  
Documentation for the ThingSpeak Communication Library for Arduino is in the README.md folder where the library was installed.
See https://www.mathworks.com/help/thingspeak/index.html for the full ThingSpeak documentation.
For licensing information, see the accompanying license file.
Copyright 2020, The MathWorks, Inc.
*/

#include ESP8266WiFi.h #include "secrets.h" #include "ThingSpeak.h" // always include thingspeak header file after other header files and custom macros #include Arduino.h #include "Adafruit_SHT31.h" Adafruit_SHT31 sht31 = Adafruit_SHT31();

char ssid[] = SECRET_SSID; // your network SSID (name) char pass[] = SECRET_PASS; // your network password int keyIndex = 0; // your network key Index number (needed only for WEP) WiFiClient client;

unsigned long myChannelNumber = SECRET_CH_ID; const char * myWriteAPIKey = SECRET_WRITE_APIKEY;

String myStatus = "";

void setup() { Serial.begin(115200); // Initialize serial while (!Serial) { ; // wait for serial port to connect. Needed for Leonardo native USB port only }

WiFi.mode(WIFI_STA); 
ThingSpeak.begin(client);  // Initialize ThingSpeak

sht31.begin(0x44);

}

void loop() {

// Connect or reconnect to WiFi
if(WiFi.status() != WL_CONNECTED){
  Serial.print("Attempting to connect to SSID: ");
  Serial.println(SECRET_SSID);
  while(WiFi.status() != WL_CONNECTED){
 //   WiFi.begin(ssid, pass);  // Connect to WPA/WPA2 network. Change this line if using open or WEP network
   WiFi.begin(); Serial.print(".");
    delay(5000);     
  } 
  Serial.println("\nConnected.");
}

//+++++++++++++++++++++++++++SHT31 Sensor float t = sht31.readTemperature(); float h= sht31.readHumidity();

if (! isnan(t)) {  // check if 'is not a number'
  Serial.print("Temp *C = ");
  Serial.print(t);
  Serial.print("\t\t");
} 
if (! isnan(h)) {  // check if 'is not a number'
  Serial.print("Hum. % = ");
  Serial.println(h);
}
// set the fields with the values
ThingSpeak.setField(1,t);
ThingSpeak.setField(2,h);
// write to the ThingSpeak channel
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if(x == 200){
  Serial.println("Channel update successful.");
}
else{
  Serial.println("Problem updating channel. HTTP error code " + String(x));
}
delay(20000); // Wait 20 seconds to update the channel again
}
end
Christopher Stapels
Christopher Stapels on 28 Jun 2021 (Edited on 28 Jun 2021)

There is a good step by step example in the documentation to set up an esp8266 . Make sure you've completed all the steps there that involve the software (your hardware may be different). Then when you load the code, watch the serial monitor and tell us what you see. Was the program uploaded successfully? Are there any messages on the serial monitor?

Tags

No tags entered yet.