help me, please! I can't get the signal on thingspeak eventhough HC-SR04 can mesure distance. Could you show me where i was wrong? Thanks alot!!

Hoang Long on 3 Jul 2021
Latest activity Reply by Vinod on 7 Jul 2021

#include WiFi.h #include "secrets.h" #include "ThingSpeak.h" #include hcsr04.h // connect to wifi char ssid[] = "long2000"; // your network SSID (name) char pass[] = "88888888"; // your network passwword int keyIndex = 0; // your network key Index number (needed only for WEP) WiFiClient client; unsigned long myChannelNumber = 1434038; const char * myWriteAPIKey = "1SXFRPC5DG3FMDM0"; const char * server = "api.thingspeak.com"; unsigned long duration; //time variable

// setup HC-SR04 #define HC-SR04

const int trig = 18; const int echo = 19; void setup() {

Serial.begin(9600); // communicate Serial to baudrate 9600   
pinMode(trig,OUTPUT);   
pinMode(echo,INPUT);  
while (!Serial) 
 // đợi cổng nối tiếp kết nối. Chỉ cần cho cổng USB gốc Leonardo 
WiFi.mode(WIFI_STA);   
ThingSpeak.begin(client);  // Initialize ThingSpeak

} void loop() { float Distance; //distance variable // 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

    digitalWrite(trig,0);   
    delayMicroseconds(2);   
    digitalWrite(trig,1);   
    delayMicroseconds(5);   
    digitalWrite(trig,0);   
    duration = pulseIn(echo,HIGH);   
       Distance = (duration/2/29.412); 
     Serial.print(Distance);
    Serial.println("cm");
    delay(2000);    
    // 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, Distance, myWriteAPIKey);
    } 
 delay(2000);
 }
}  
Vinod
Vinod on 7 Jul 2021

Do you have a paid license of ThingSpeak? If not, how are you making sure the channel is only being updated every 15s or less frequently? From your code snippet, it appears you are updating your channel every 2s, which is only possible if you have a paid ThingSpeak license.