Valor de campo no disponible

6 views (last 30 days)
Felipe
Felipe on 3 Nov 2023
#include <ThingSpeak.h>
//#include "WiFi.h"
#include <ESP8266WiFi.h>
#include <Wire.h>
#include <Adafruit_ADS1X15.h>
#include <LiquidCrystal_I2C.h>
#define SDA_PIN D2
#define SCL_PIN D1
Adafruit_ADS1115 ads;
LiquidCrystal_I2C lcd(0x27, 16, 2);
const float NIVEL_INFERIOR = 10000.0;
const float NIVEL_SUPERIOR = 49900.0;
int litros;
int centimetros;
unsigned long tiempoadc = 0;
const char* ssid = "RED .4G";
const char* password = "xxxxxxxxxx";
const char* server = "api.thingspeak.com";
unsigned long channelID = 330507;
const char* WriteAPIKey = "xxxxxxxxxxxxxxxx";
WiFiClient cliente;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Conectando a la red WiFi...");
}
Serial.println("Conectado a la red WiFi");
// Inicialización del display LCD
Wire.begin(SDA_PIN, SCL_PIN);
ads.begin(0x48);
lcd.begin(16, 2);
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
Serial.begin(9600);
ads.setGain(GAIN_TWOTHIRDS);
ads.begin();
lcd.setCursor(0, 0);
lcd.print(" INTERPRISE MX ");
lcd.setCursor(0, 1);
lcd.print(" AGUA CRUDA ");
delay(5000);
lcd.clear();
pinMode(D6, OUTPUT);
pinMode(D5, OUTPUT);
digitalWrite(D6, HIGH);
digitalWrite(D5, HIGH);
tiempoadc = millis();
ThingSpeak.begin(cliente);
ThingSpeak.writeFields(channelID, WriteAPIKey);
}
void loop() {
if (millis() - tiempoadc > 500UL) {
tiempoadc = millis();
short adc0 = ads.readADC_SingleEnded(0);
float centimetros = (adc0 / 53.40);
float litros = (centimetros * 212.1);
if (litros < NIVEL_INFERIOR) {
digitalWrite(D5, LOW);
} else {
digitalWrite(D5, HIGH);
}
if (litros > NIVEL_SUPERIOR) {
digitalWrite(D6, LOW);
} else {
digitalWrite(D6, HIGH);
}
Serial.print("litros ");
Serial.println(litros);
Serial.print("ctm: ");
Serial.println(centimetros);
Serial.print("A0: ");
Serial.println(adc0);
lcd.setCursor(0, 0);
lcd.print("lts ");
lcd.print(litros);
lcd.setCursor(0, 1);
lcd.print("cm ");
lcd.print(centimetros);
ThingSpeak.setField (1, litros);
ThingSpeak.setField (2, centimetros);
ThingSpeak.setField (channelID, WriteAPIKey );
Serial.println("datos enviados a ThingSpeak");
delay(60000);
}
}
  4 Comments
Felipe
Felipe on 3 Nov 2023
Edited: Felipe on 3 Nov 2023
Ya intente en configuracón, y no e podido resolver el problema, quiero leer el nivel de agua en un deposito de 50,000 litros, el código funciona bien da la información en monitor Serial, LCD, es una sonda que envia una señal de 4-20 mA ,esa señal un modulo la convierte en 0 a 5 volts, y ADS1115 convierte la señal analogica en digital vía I2C, y esp8266 debe enviar los datos ThingSpeark, esp8266 si se conecta a internet al parecer no envia la informacion al servidor de ThingSpeark, no creo que el problema este el canal o en los campos de ThingSpeark.
Uso el IDE de Arduino para subir los programas a la placa esp8266.
Saludos desde queretaro MX
Christopher Stapels
Christopher Stapels on 3 Nov 2023
I suggest looking a the example code to help you with that error. The example uses char variable[] instead of const char *
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password

Sign in to comment.

Answers (2)

Christopher Stapels
Christopher Stapels on 3 Nov 2023
Edited: Christopher Stapels on 3 Nov 2023
Here are a few things I notice about your code.
You run this line in the setup function, im not sure why.
ThingSpeak.setField (channelID, WriteAPIKey );
You also have this line in your loop, that seems to execute the function every 500 miliseconds. That is way too frequent to write to ThingSpeak, and might result in your accoubnt being blocked.
if (millis() - tiempoadc > 500UL) {
Fortunately, your code doesnt yet send data to ThingSpeak in the loop. Make sure you increase the delay to at least 15000UL if you have a free account ot 1000 if you have a paid account.
This line at the end of the main loop is incorrect for sure (the comment says "data sent to ThingSpeak", but the setField command you have does not send data.)
ThingSpeak.setField (channelID, WriteAPIKey );
Here is where you want to use the writeFields command
have a look at this example code from the library. The example I linked is for esp32, but there is also very similar code for esp8266 in the repo.
If you fix those things, it should start to work soon, please let us know!
  4 Comments
Felipe
Felipe on 3 Nov 2023
//ThingSpeak.writeFields(channelID, WriteAPIKey);
Felipe
Felipe on 3 Nov 2023
Esta linea que de momento, la comente para que no cause conflictos. Saludos

Sign in to comment.


Felipe
Felipe on 5 Nov 2023
/*
ReadField
Description: Demonstates reading from a public channel which requires no API key and reading from a private channel which requires a read API key.
The value read from the public channel is the current outside temperature at MathWorks headquaters in Natick, MA. The value from the
private channel is an example counter that increments every 10 seconds.
Hardware: ESP8266 based boards
!!! IMPORTANT - Modify the secrets.h file for this project with your network connection and ThingSpeak channel details. !!!
Note:
- Requires ESP8266WiFi library and ESP8622 board add-on. See https://github.com/esp8266/Arduino for details.
- Select the target hardware from the Tools->Board menu
- 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 <ESP8266WiFi.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;
// Weather station channel details
unsigned long weatherStationChannelNumber = SECRET_CH_ID_WEATHER_STATION;
unsigned int temperatureFieldNumber = 4;
// Counting channel details
unsigned long counterChannelNumber = SECRET_CH_ID_COUNTER;
const char * myCounterReadAPIKey = SECRET_READ_APIKEY_COUNTER;
unsigned int counterFieldNumber = 1;
void setup() {
Serial.begin(115200); // Initialize serial
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo native USB port only
}
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client); // Initialize ThingSpeak
}
void loop() {
int statusCode = 0;
// 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(5000);
}
Serial.println("\nConnected");
}
// Read in field 4 of the public channel recording the temperature
float temperatureInF = ThingSpeak.readFloatField(weatherStationChannelNumber, temperatureFieldNumber);
// Check the status of the read operation to see if it was successful
statusCode = ThingSpeak.getLastReadStatus();
if(statusCode == 200){
Serial.println("Temperature at MathWorks HQ: " + String(temperatureInF) + " deg F");
}
else{
Serial.println("Problem reading channel. HTTP error code " + String(statusCode));
}
delay(15000); // No need to read the temperature too often.
// Read in field 1 of the private channel which is a counter
long count = ThingSpeak.readLongField(counterChannelNumber, counterFieldNumber, myCounterReadAPIKey);
// Check the status of the read operation to see if it was successful
statusCode = ThingSpeak.getLastReadStatus();
if(statusCode == 200){
Serial.println("Counter: " + String(count));
}
else{
Serial.println("Problem reading channel. HTTP error code " + String(statusCode));
}
delay(15000); // No need to read the counter too often.
}
  2 Comments
Felipe
Felipe on 5 Nov 2023
Hola estoy intentando con códigos de ejmplo de la librería ThingSpeark tiene el código una pestaña anexa donde de pone la informacón de el modem #define SECRET_SSID la contraseña #define SECRET_PASS
Christopher Stapels
Christopher Stapels on 6 Nov 2023
Yes, sometimes people like to keep the passwords in a seperate file. That way when you post your code, you arent giving them away. Do you have a question about this?

Sign in to comment.

Communities

More Answers in the  ThingSpeak Community

Categories

Find more on Write Data to Channel in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!