Cannot get arduino voltage data to upload to Thingspeak

3 views (last 30 days)
I am trying to collect battery voltage data from arduino parts and upload it from arduino into my Thingspeak channel. So far, I have troubleshot the hardware/wiring aspect, and that doesn't seem to be the issue.
At this point, I am assuming the issue is with the code or the Thingspeak channel. Since my arduino board (elegoo arduino Uno R3) is hooked up to my computer using a USB cord, I tried using an HTTP GET request in order to send the information to the Thingspeak channel. I am under a time crunch and am unable to access any kind of Wifi or Ethernet shield, so that is why I am using the USB cord. I could be completely wrong in using the HTTP GET request --I am quite the novice here! Here is the code so far:
#include <SoftwareSerial.h>
SoftwareSerial Serial1(2, 3); // RX, TX
// Define variables
float voltage;
unsigned long previousMillis = 0;
const long interval = 60000; // Upload interval (in milliseconds)
String apiKey = "P5LPXSRJT0G4K7VA";
String field = "field1";
String server = "api.thingspeak.com";
String channelID = "2131761";
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
}
void loop() {
// Read voltage from sensor
voltage = analogRead(A0) * 0.0049; // convert analog reading to voltage (0-5V)
voltage = voltage * 5.17; // adjust the voltage to reflect the actual voltage
// Print voltage to serial monitor
Serial.print("Voltage: ");
Serial.print(voltage);
Serial.println(" V");
// Get current timestamp
unsigned long currentMillis = millis();
// Check it's time to upload to ThingSpeak
if (currentMillis - previousMillis >= interval) {
// Update timestamp
previousMillis = currentMillis;
// Construct HTTP GET request
String getRequest = "GET /update?api_key=";
getRequest += apiKey;
getRequest += "&";
getRequest += field;
getRequest += "=";
getRequest += String(voltage);
getRequest += "&";
getRequest += "field2=Times";
getRequest += " HTTP/1.1\r\n";
getRequest += "Host: ";
getRequest += server;
getRequest += "\r\n";
getRequest += "Connection: close\r\n\r\n";
getRequest += "X-THINGSPEAK-CLIENT: arduino\r\n";
getRequest += "Content-Type: application/x-www-form-urlencoded\r\n";
getRequest += "Content-Length: ";
getRequest += String(voltage);
getRequest += "\r\n";
getRequest += "Host: api.thingspeak.com\r\n";
getRequest += "User-Agent: Arduino/1.0.0\r\n";
getRequest += "\r\n";
getRequest += "\r\n";
getRequest += channelID;
// Send HTTP GET request to ThingSpeak via serial communication
Serial1.println(getRequest);
// Print response from ThingSpeak to serial monitor
while (Serial1.available()) {
Serial.write(Serial1.read());
}
Serial.println();
}
delay(1000);
}
Again, I really have no clue what I am doing here, so I am not surprised if what I have is entirely wrong. Any feedback is greatly appreciated!

Answers (2)

Christopher Stapels
Christopher Stapels on 4 May 2023
The Uno R3 cannot make HTTP requests without an ethernet or wifi connection. It is not able to send those requests through a USB connection without a corresponding app to send the requests running on the computer that it is conencted to.

Ser Gio
Ser Gio on 7 May 2023
As explained by Mr Stapels, Arduino UNO R3 by itsef cannot connect to internet.
I suggest you to use something like a ESP8266 or ESP32 and use ESP.getVcc()
BR.

Communities

More Answers in the  ThingSpeak Community

Categories

Find more on Act on Data in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!