Help for sending data from wemos to thingspeak

Hello everyone, I'm not able to conclude my project due to a small loophole. I'm trying to connect or send data from my metal proximity sensor to thingspeak and the code which I've been using is displayed below. Using a wemos D1 R2. So if someone could chip in some help, it would be great.

 #include "ThingSpeak.h"
#include "secrets.h"
#include <ESP8266WiFi.h>

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;

const int ProxSensor = D2;

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



void setup() {
 Serial.begin(115200);  // Initialize serial

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

  pinMode(D14, OUTPUT);     
 //Pin 14 is connected to the output of proximity sensor
 pinMode(ProxSensor,INPUT);
}

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
     Serial.print(".");
     delay(5000);     
   } 
   Serial.println("\nConnected.");
 }
 if(digitalRead(ProxSensor)==HIGH)      //Check the sensor output
 {
   digitalWrite(D14, HIGH);   // set the LED on
 }
 // Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
 // pieces of information in a channel.  Here, we write to field 1.
 int x = ThingSpeak.writeField(myChannelNumber, 1, 1, myWriteAPIKey);
 if(x == 200){
   Serial.println("Channel update successful.");
 }
 else{
   Serial.println("Problem updating channel. HTTP error code " + String(x));
 }
 if(digitalRead(ProxSensor)==LOW)
  {
   digitalWrite(13, LOW);    // set the LED off
 }
 delay(100); 
  int y = ThingSpeak.writeField(myChannelNumber, 1, 0, myWriteAPIKey);
 if(y == 200){
   Serial.println("Channel update successful.");
 }
 else{
   Serial.println("Problem updating channel. HTTP error code " + String(x));
 }
 

 // change the value

 
 delay(20000); // Wait 20 seconds to update the channel again
}





p.s: I used a thingspeak library to code this. (Hence why secrets.h)
/code]

Have you looked at their WriteSingleField example?

What would it take to switch their number with your sensor data ?

When you use

ThingSpeak.writeField(myChannelNumber, 1, 0, myWriteAPIKey);

the value you write is supposed to be of type long. Maybe try

long value = 0L; 
ThingSpeak.writeField(myChannelNumber, 1, value, myWriteAPIKey);

If you want to write an int, use setField, but it's used for multi-field update.

lesept:
When you use

ThingSpeak.writeField(myChannelNumber, 1, 0, myWriteAPIKey);

the value you write is supposed to be of type long. Maybe try

long value = 0L; 

ThingSpeak.writeField(myChannelNumber, 1, value, myWriteAPIKey);



If you want to write an int, use [setField](https://github.com/mathworks/thingspeak-arduino/blob/master/src/ThingSpeak.h#L287), but it's used for multi-field update.

No need. the compiler will put the integer in a long variable for you as it's in the function signature. it's a standard promotion.