after uploading thingsspeak code to arduino the data is not showing dynamically after providing power source and i need to connect arduino to run code again in serail monitor

5 visualizzazioni (ultimi 30 giorni)
I have written a thingspeak arduino code for mkr1000 and uploaded it but i had problem in sending data to thingspeak once i run the code in serial monitor the values appeared in thingsspeak website , and now i unpluged the arduino board from my pc and connected it to a battery and the values are not displaying in thingspeak website and i waited for morethan 2 minutes .Then i again connected the board to my pc and opened the serial monitor in arduino ide now i can see the data appearing in thingspeak website . Do i need to run serial monitor everytime when i want to send my sensor data to arduino. or can i upload the code and use a battery directly as power souce to send data to thingspeak.
/*
WriteSingleField
Description: Writes a value to a channel on ThingSpeak every 20 seconds.
Hardware: Arduino MKR1000
!!! IMPORTANT - Modify the secrets.h file for this project with your network connection and ThingSpeak channel details. !!!
Note:
- Requires WiFi101 library version 0.15.3 or newer.
- 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 ) 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 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 for the full ThingSpeak documentation.
For licensing information, see the accompanying license file.
Copyright 2020, The MathWorks, Inc.
*/
#include <WiFi101.h>
#include "secrets.h"
#include "ThingSpeak.h" // always include thingspeak header file after other header files and custom macros
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;
int number = 0;
#define sensorPin A1
void setup() {
Serial.begin(9600); // Initialize serial
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo native USB port only
}
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(1000);
}
Serial.println("\nConnected.");
}
int reading = analogRead(sensorPin);
// Convert the reading into voltage:
float voltage = reading * (5000 / 1024.0);
// Convert the voltage into the temperature in degree Celsius:
float temp = voltage / 100;
// Print the temperature in the Serial Monitor:
//Serial.print(temp);
//Serial.print(" \xC2\xB0"); // shows degree symbol
//Serial.println("C");
// Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
// pieces of information in a channel. Here, we write to field 1.
int x = ThingSpeak.writeField(myChannelNumber, 1, temp, myWriteAPIKey);
if(x == 200){
Serial.println("Channel update successful.");
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
// change the value
number++;
if(number > 99){
number = 0;
}
delay(20000); // Wait 20 seconds to update the channel again
}

Risposte (1)

Chetan
Chetan il 27 Dic 2023
Modificato: Chetan il 27 Dic 2023
I understand you're trying to send data to ThingSpeak when the device is powered by a battery, but you're encountering some issues.
The MKR1000 should indeed be capable of transmitting data to ThingSpeak independently of a PC connection. The challenge you're facing seems to stem from the fact that the code is programmed to wait for a serial connection in the `setup()` function, which naturally only occurs when the device is connected to a PC.
To resolve this, please consider the following steps:
1. Adjust the Serial Initialization:
- You should either comment out or entirely remove the line containing `while (!Serial)` in your `setup()` function. This change will allow your MKR1000 to continue with the Wi-Fi connection process and ultimately send data to ThingSpeak without waiting for a serial connection.
2. Verify the Power Supply:
- Make sure that the battery is providing sufficient power for the MKR1000, with particular attention to the Wi-Fi module's requirements.
3. Enhance Wi-Fi Connection Logic:
- It's crucial to ensure that the Wi-Fi module initializes correctly upon startup. Consider placing a `WiFi.begin()` call before the `while(WiFi.status() != WL_CONNECTED)` loop in your `setup()` function to establish the Wi-Fi connection right away.
Here's a revised version of the `setup()` function:
void setup() {
%Initialize serial communication at 9600 baud (optional for debugging)
Serial.begin(9600);
%Comment out or remove the following line to prevent waiting for serial connection
%while (!Serial) {
% Wait indefinitely for Serial to connect (only necessary for debugging)
% }
% Start the Wi-Fi connection process
WiFi.begin(ssid, pass);
% Wait for Wi-Fi connection to establish
while (WiFi.status() != WL_CONNECTED) {
delay(1000); % Wait 1 second before retrying
% Optionally, add an LED blink or serial print here for status indication
}
% Initialize ThingSpeak after Wi-Fi connection is established
ThingSpeak.begin(client);
}
4. Use Conditional Debugging:
- If you still need serial debugging, you can wrap the debugging statements in an `if (Serial)` check, which will ensure they're only executed when a serial connection is available.
By implementing these modifications, the MKR1000 should be ready to send data to ThingSpeak when powered by a battery.
I hope this helps.

Categorie

Scopri di più su Write Data to Channel in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by