Cannot get arduino voltage data to upload to Thingspeak
3 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
回答(2 个)
Christopher Stapels
2023-5-4
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.
0 个评论
Ser Gio
2023-5-7
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.
0 个评论
社区
更多回答在 ThingSpeak Community
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Act on Data 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!