Hello there, I need to do a school project. And it need to upload the variable to IoT cloud and show it on dashboard. But the variable was not changed. Can someone help?
As you see it, My device is already connected sucessfully.
And then, the serial has some value but the variable on IoT cloud is not changed.
Here is my code of project if there are some wrong of the IoT setup, Please tell me that.
#include <WiFi.h>
#include <WiFiClient.h>
#include <ThingSpeak.h>
#include "thingProperties.h"
#define potPin 36
#define PIN_LM35 34
#define ACS712_PIN 35
#define VREF 3300
#define ACS712_SENSITIVITY 185
//for privacy not show it
const char* ssid = "";
const char* password = "";
const char* server = "";
const char* api_key = "";
WiFiClient client;
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
pinMode(potPin, INPUT);
pinMode(ACS712_PIN, INPUT);
pinMode(PIN_LM35, INPUT);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
ThingSpeak.begin(client);
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(4);
ArduinoCloud.printDebugInfo();
}
void loop() {
ArduinoCloud.update();
// Your code here
// voltage
int pininput = analogRead(potPin);
float voltage_value = (float)pininput * 3.3 / 4095 * 2;
Serial.println("======================================================================");
Serial.println("Monitoring Result:");
Serial.println("======================================================================");
Serial.print("ESP32 pinIN voltage value = ");
Serial.print(pininput);
Serial.print("mV");
Serial.print(", Battery Voltage value = ");
Serial.print(voltage_value);
Serial.println("V");
//current
int sensorValue = analogRead(ACS712_PIN);
float current = ( sensorValue * 3300 / 4095 - 2500 ) / ACS712_SENSITIVITY; //2500is zero offset
Serial.println();
Serial.print("ACS712 Vout Voltage: ");
Serial.print(sensorValue);
Serial.print("mV, Current: ");
Serial.print(current);
Serial.println("A");
//Temperature
int adcVal = analogRead(PIN_LM35);
float tempC = (float)adcVal * 3.3 / 4095.0 * 1000 / 10; // ADC * VREF / ADC_RESOLUTION , *1000 is change to mV / SENSITIVITY
Serial.println();
Serial.print("Temperature: ");
Serial.print(tempC);
Serial.println("C ");
Serial.println("======================================================================");
ThingSpeak.setField(1, voltage_value);
ThingSpeak.setField(2, current);
ThingSpeak.setField(3, tempC);
int response = ThingSpeak.writeFields(1, api_key);
Serial.print("Sending data to Thingspeak, response code: ");
Serial.println(response);
Serial.println();
Serial.println();
delay(3000);
}
And it is my thingProperties.h document, I saw some tutorial said that when the type of variable and variable name is same, it will be upload the value automatically.
// Code generated by Arduino IoT Cloud, DO NOT EDIT.
#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>
const char DEVICE_LOGIN_NAME[] = "0e73996a-78aa-4590-bf31-fe67caea3c6f";
const char SSID[] = SECRET_SSID; // Network SSID (name)
const char PASS[] = SECRET_OPTIONAL_PASS; // Network password (use for WPA, or use as key for WEP)
const char DEVICE_KEY[] = SECRET_DEVICE_KEY; // Secret device password
float current;
int pininput;
float tempC;
float voltage_value;
int sensorValue;
void initProperties(){
ArduinoCloud.setBoardId(DEVICE_LOGIN_NAME);
ArduinoCloud.setSecretDeviceKey(DEVICE_KEY);
ArduinoCloud.addProperty(current, READ, ON_CHANGE, NULL);
ArduinoCloud.addProperty(pininput, READ, ON_CHANGE, NULL);
ArduinoCloud.addProperty(tempC, READ, ON_CHANGE, NULL);
ArduinoCloud.addProperty(voltage_value, READ, ON_CHANGE, NULL);
ArduinoCloud.addProperty(sensorValue, READ, ON_CHANGE, NULL);
}
WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);
Maybe the tutorial I saw have pass through something, can someone provide a detail tutorial about that. Thank you.