# A01NYUB ultrasonic sensor to Thingspeak UNO WiFi Rev 2

**URL:** <https://forum.core-electronics.com.au/t/a01nyub-ultrasonic-sensor-to-thingspeak-uno-wifi-rev-2/8924>\
**Category:** Arduino\
**Created:** [6 December 2020 22:43 UTC](https://forum.core-electronics.com.au/t/a01nyub-ultrasonic-sensor-to-thingspeak-uno-wifi-rev-2/8924 "2020-12-06T22:43:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicholas129394](https://avatars.discourse-cdn.com/v4/letter/n/da6949/32.png) [@Nicholas129394](https://forum.core-electronics.com.au/u/Nicholas129394)\
**Post date:** [6 December 2020 22:43 UTC](https://forum.core-electronics.com.au/t/a01nyub-ultrasonic-sensor-to-thingspeak-uno-wifi-rev-2/8924/1 "2020-12-06T22:43:55Z")

</div>

Hello,

I have an A01NYUB ultrasonic sensor up and running using the DF Robot sample code. the sensor works great.

I have then copied and pasted the identical sensor code into a Thingspeak sample code (write multiple fields), and the sensor now will only return 0 as a value.

Both the original sample codes work fine and now that I have combined them I have a problem - but can’t seem to work it out.

Any ideas to point me in the right direction would be great. Thank you

---

<div class="post-metadata">

**Author:** ![Owen](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/owen/32/2948_2.png) [@Owen](https://forum.core-electronics.com.au/u/Owen)\
**Post date:** [7 December 2020 02:57 UTC](https://forum.core-electronics.com.au/t/a01nyub-ultrasonic-sensor-to-thingspeak-uno-wifi-rev-2/8924/2 "2020-12-07T02:57:45Z")

</div>

Hey Nicholas,

We’re sorry to hear you’re running into some issues with that one. I’m not super familiar with Thingspeak, so someone who has a bit more knowledge in that field may be able to help out a bit, but I’ll do my best to help!

Just to confirm, what microcontroller board were you using?

---

<div class="post-metadata">

**Author:** ![Nicholas129394](https://avatars.discourse-cdn.com/v4/letter/n/da6949/32.png) [@Nicholas129394](https://forum.core-electronics.com.au/u/Nicholas129394)\
**Post date:** [7 December 2020 02:59 UTC](https://forum.core-electronics.com.au/t/a01nyub-ultrasonic-sensor-to-thingspeak-uno-wifi-rev-2/8924/3 "2020-12-07T02:59:30Z")

</div>

Hello!

I am using the Arduino Wifi Rev 2

---

<div class="post-metadata">

**Author:** ![Owen](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/owen/32/2948_2.png) [@Owen](https://forum.core-electronics.com.au/u/Owen)\
**Post date:** [7 December 2020 03:05 UTC](https://forum.core-electronics.com.au/t/a01nyub-ultrasonic-sensor-to-thingspeak-uno-wifi-rev-2/8924/4 "2020-12-07T03:05:11Z")

</div>

Hey Nicholas,

Thanks for confirming!

Have you been able to run other programs/code on the WiFi Rev 2 while using Thingspeak?

If you have been able to get any sort of sensor input reading working (digital or analog), let us know and if possible, shoot through some code of that as well as the code you’re attempting to run for the A01NYUB.

---

<div class="post-metadata">

**Author:** ![Nicholas129394](https://avatars.discourse-cdn.com/v4/letter/n/da6949/32.png) [@Nicholas129394](https://forum.core-electronics.com.au/u/Nicholas129394)\
**Post date:** [7 December 2020 03:09 UTC](https://forum.core-electronics.com.au/t/a01nyub-ultrasonic-sensor-to-thingspeak-uno-wifi-rev-2/8924/5 "2020-12-07T03:09:12Z")

</div>

Yes, this code uploads to Thingspeak no problem:  
/\*  
WriteMultipleFields

Description: Writes values to fields 1,2,3,4 and status in a single ThingSpeak update every 20 seconds.

Hardware: Arduino Uno WiFi Rev2

!!! IMPORTANT - Modify the secrets.h file for this project with your network connection and ThingSpeak channel details. !!!

Note:

- Requires WiFiNINA library.
- 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](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](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](https://www.mathworks.com/help/thingspeak/index.html) for the full ThingSpeak documentation.

For licensing information, see the accompanying license file.

Copyright 2018, The MathWorks, Inc.  
\*/

#include “ThingSpeak.h”  
#include \<WiFiNINA.h\>  
#include “secrets.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;

unsigned long myChannelNumber = SECRET\_CH\_ID;  
const char \* myWriteAPIKey = SECRET\_WRITE\_APIKEY;

// Initialize our values  
int number1 = 0;  
int number2 = random(0,100);  
int number3 = random(0,100);  
int number4 = random(0,100);  
String myStatus = “”;

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

// check for the WiFi module:  
if (WiFi.status() == WL\_NO\_MODULE) {  
Serial.println(“Communication with WiFi module failed!”);  
// don’t continue  
while (true);  
}

String fv = WiFi.firmwareVersion();  
if (fv != “1.3.0”) {  
Serial.println(“Please upgrade the firmware”);  
}

ThingSpeak.begin(client); //Initialize ThingSpeak  
}

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.");  
}

// set the fields with the values  
ThingSpeak.setField(4, number1);  
ThingSpeak.setField(5, number2);  
ThingSpeak.setField(6, number3);  
ThingSpeak.setField(7, number4);

// figure out the status message  
//if(number1 \> number2){  
myStatus = String(“field1 is greater than field2”);  
// }  
// else if(number1 \< number2){  
//myStatus = String(“field1 is less than field2”);  
// }  
// else{  
// myStatus = String(“field1 equals field2”);  
// }

// set the status  
[//ThingSpeak.setStatus](https://ThingSpeak.setStatus)(myStatus);

// 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));  
}

// change the values  
number1++;  
if(number1 \> 99){  
number1 = 0;  
}  
number2 = random(0,100);  
number3 = random(0,100);  
number4 = random(0,100);

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

---

<div class="post-metadata">

**Author:** ![Nicholas129394](https://avatars.discourse-cdn.com/v4/letter/n/da6949/32.png) [@Nicholas129394](https://forum.core-electronics.com.au/u/Nicholas129394)\
**Post date:** [7 December 2020 03:10 UTC](https://forum.core-electronics.com.au/t/a01nyub-ultrasonic-sensor-to-thingspeak-uno-wifi-rev-2/8924/6 "2020-12-07T03:10:23Z")

</div>

An this is the code where the sensor is returning 0:  
#include “ThingSpeak.h”  
#include \<WiFiNINA.h\>  
#include “secrets.h”  
#include \<SoftwareSerial.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;

unsigned long myChannelNumber = SECRET\_CH\_ID;  
const char \* myWriteAPIKey = SECRET\_WRITE\_APIKEY;

// NEW defines variables  
SoftwareSerial mySerial(11,10); // RX, TX  
unsigned char data[4]={};  
int distance;  
int volume;

void setup() {  
Serial.begin(115200); // Initialize serial  
mySerial.begin(9600); // RX TX

// check for the WiFi module:  
if (WiFi.status() == WL\_NO\_MODULE) {  
Serial.println(“Communication with WiFi module failed!”);  
// don’t continue  
while (true);  
}

String fv = WiFi.firmwareVersion();  
if (fv != “1.3.0”) {  
Serial.println(“Please upgrade the firmware”);  
}

ThingSpeak.begin(client); //Initialize ThingSpeak  
}

void loop() {

// RX TX A01NYUB ultrasonic sensor  
do{  
for(int i=0;i\<4;i++)  
{  
data[i]=mySerial.read();  
}  
}while(mySerial.read()==0xff);

mySerial.flush();

if(data[0]==0xff)  
{  
int sum;  
sum=(data[0]+data[1]+data[2])&0x00FF;  
if(sum==data[3])  
{  
distance=(data[1]\<\<8)+data[2];  
}  
volume = -0.067\*distance +125; //calculate the % full

// 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.");  
}

// set the fields with the values  
ThingSpeak.setField(4, distance);  
ThingSpeak.setField(5, volume);

// write to the ThingSpeak channel  
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);  
if(x == 200){  
Serial.println(“Channel update successful.”);  
Serial.println(distance);  
Serial.println(volume);

}  
else{  
Serial.println("Problem updating channel. HTTP error code " + String(x));  
}

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