How to send data from ultrasonic sensor to thingspeak using ARDUINO UNO WIFI REV 2 OR NANO IOT 33 ,

HI TO ALL AND THE COMMUNITY OF ARDUINO AND THE WORLD OF CODING!!

Can I request for help or any guidance that can be useful to my MP Coding

i been trying to code for the arduino uno wifi rev 2 and ultrasonic to thingspeak but i could not get it work and same for if i use arduino nano 33 IoT board !

If anyone is willing to help and give me a guidance or a code to run that will be helpful

P.S. :
I AM NOT USING ESP8266 , IM USING A BULIT-IN WIFI ARDUINO BOARD Which is WiFiNINA and thingspeak

so do help as it is urgent and i praay to god some one will help

thank you

/////////// testing wifi to cloud and dat to cloud /////////////////////////
#include <SPI.h>
#include <WiFiNINA.h>
#include <ThingSpeak.h>

//setup wifi system
char ssid[] = "xxxxxxxxx";             //  your network SSID (name) between the " "
char pass[] = "xxxxxxxxxx";      // your network password between the " "
int keyIndex = 0;                 // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;      //connection status
WiFiServer server(80);            //server socket
WiFiClient client = server.available();


//define pin and data and component part info
#define echoPin 2 // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 3 //attach pin D3 Arduino to pin Trig of HC-SR04

//read the val and data
long Duration; // variable for the duration of sound wave travel
int Distance; // variable for the distance measurement

// ThingSpeak Settings
char thingSpeakAddress[] = "api.thingspeak.com";
char mychannelID[] = " xxxxxxxx";
String APIKey = "xxxxxxxxxxxxxxxx";             // enter your channel's Write API Key
unsigned long lastConnectionTime = 0; // Track the last connection time
unsigned long lastUpdateTime = 0; // Track the last update time
const unsigned long postingInterval = 20L * 1000L; // Post data every 2 minutes
const unsigned long updateInterval = 15L * 1000L; // Update once every 15 seconds

void setup() 
{
  // put your setup code here, to run once:
  Serial.begin(9600); // // Serial Communication is starting with 9600 of baudrate speed
  Serial.println("MP PROJECT 2022 TESTING"); // print some text in Serial Monitor
  Serial.println("with Arduino UNO WiFi Rev2");
  
  // Attempt to connect to WiFi network
  while (WiFi.status() != WL_CONNECTED) 
  {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    WiFi.begin(ssid, pass);  // Connect to WPA/WPA2 network. Change this line if using open or WEP network
    delay(10000);  // Wait 10 seconds to connect
  }
  // you're connected now, so print out the status:
  Serial.println("WiFi Connected Successfully");
  printWifiStatus(); //Print WiFi connection information

  //////// Configuration of pinMode for ultrasonic sensor ////////
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
  pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT

}
//end "setup()"


void loop()
{
  // put your main code here, to run repeatedly:
   //Start of Program - DISTANCE FALL PROGRAM
  // Clears the trigPin condition
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin HIGH (ACTIVE) for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  Duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  Distance = Duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
  // Displays the distance on the Serial Monitor
  Serial.print("Distance: ");
  Serial.print(Distance);
  Serial.println(" cm");
  delay(500);

  //thingspeak and cloud upload settings and coding 
  if (millis() - lastUpdateTime >= updateInterval)
  {
    String distance = String(Distance);
    updateThingSpeak("field1= 12" + distance);
    delay(500);
  }
  lastUpdateTime = millis(); // Update last update time 
}
//end loop()

void updateThingSpeak(String tsData)
{
  if (client.connect(thingSpeakAddress, 80))
  {
    client.println("POST /update HTTP/1.1\n");
    client.println("Host: api.thingspeak.com\n");
    client.println("Connection: close\n");
    client.println("X-THINGSPEAKAPIKEY: " + APIKey + "\n");
    client.println("Content-Type: application/x-www-form-urlencoded\n");
    client.print("Content-Length: ");
    client.println(tsData.length());
    client.println("\n\n");
    client.println(tsData);
    lastUpdateTime = millis();
    
    if (client.connected())
    {
      Serial.println("Connecting to ThingSpeak...");
      Serial.println();
    }
  }
}

void printWifiStatus()
{
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

above is the code im using Preformatted text

pls pls pls god and pro and god of coding buddies , pray to u and seek ur help in emergence

Before someone comes along to help ( I can’t )
You will need to say what is not working .

  1. Does it compile ?
  2. have you checked the functionality of the pins between the two boards ?
  3. Check (google ) that the libraries are ok with the new board .
  4. Comment out sections of code and use additional Print statements to debug the code by seeing that variable etc are what you expect .
  5. Try the ide examples relevant to each part ( thingspeak examples ) to check for hardware issues .
  1. yes it does
  2. yes it is working
  3. yes there are
  4. & 5.. i have tried too

the wserial monitor is all working
my ultrasonic is displaying data on serial monitor but not on thingspeak unless i change the field code to a fix number then it will update to the nummber according per hour

@hammy

That suggests there is something wrong with the number …
And that’s where to look hard .
If you can upload a number - I presume that number is an integer or maybe a float . You need to do some work in that area , eg …
Is “ long ” different on a nano IOT compared to a uno ? ( no idea ) , load fixed numbers into the code , see which work .
What is the number format in the examples with the library ?
Have a look on the thingspeak page / forum .

Nano IOT has reserved pins for some of the on board stuff - check again for conflicts ( although you say you can upload fixed numbers )

Look carefully ( again ) at the library examples and work from those

I gave no idea if thingspeak , but that’s how I would start to tackle it.

Ok no worries , and thanks for your help

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.