Roxy in MATLAB Answers
上次活动时间: 2023-5-7

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!

关于 ThingSpeak

The community for students, researchers, and engineers looking to use MATLAB, Simulink, and ThingSpeak for Internet of Things applications. You can find the latest ThingSpeak news, tutorials to jump-start your next IoT project, and a forum to engage in a discussion on your latest cloud-based project. You can see answers to problems other users have solved and share how you solved a problem.