Main Content

Results for

Hey, so I have a simple air quality measurement IoT system but the values are not arriving in my ThingSpeak channel. I use basic tutorials for it and tried some kind of variation but the code is so simple, I don't see any problem code-wise. Maybe someone here can help me?
I am working with a Raspberry Pi 3 and the following code for a SDS011 sensor:
import time
from datetime import datetime
import paho.mqtt.publish as publish
import paho.mqtt.client as mqtt
import psutil
from sds011 import *
import aqi
sensor = SDS011("/dev/ttyUSB0", use_query_mode=True)
def get_data(n=3):
sensor.sleep(sleep=False)
pmt_2_5 = 0
pmt_10 = 0
time.sleep(10)
for i in range (n):
x = sensor.query()
pmt_2_5 = pmt_2_5 + x[0]
pmt_10 = pmt_10 + x[1]
time.sleep(2)
pmt_2_5 = round(pmt_2_5/n, 1)
pmt_10 = round(pmt_10/n, 1)
sensor.sleep(sleep=True)
time.sleep(2)
return pmt_2_5, pmt_10
def conv_aqi(pmt_2_5, pmt_10):
aqi_2_5 = aqi.to_iaqi(aqi.POLLUTANT_PM25, str(pmt_2_5))
aqi_10 = aqi.to_iaqi(aqi.POLLUTANT_PM10, str(pmt_10))
return aqi_2_5, aqi_10
def save_log():
with open("/YOUR PATH/air_quality.csv", "a") as log:
dt = datetime.now()
log.write("{},{},{},{},{}\n".format(dt, pmt_2_5, aqi_2_5, pmt_10, aqi_10))
log.close()
channelID = "YOUR CHANNEL ID"
apiKey = "YOUR WRITE KEY"
clientID = "YOUR CLIENT ID"
tUsername = "YOUR USERNAME"
tPassword = "YOUR PASSWORD"
topic = "channels/" + channelID + "/publish/" + apiKey
mqttHost = "mqtt3.thingspeak.com"
tTransport = "tcp"
tPort = 1883
tTLS = None
tProtocol = mqtt.MQTTv311
while True:
pmt_2_5, pmt_10 = get_data()
aqi_2_5, aqi_10 = conv_aqi(pmt_2_5, pmt_10)
print ("AQI2.5 =", aqi_2_5," AQI10 =", aqi_10)
tPayload = "field1=" + str(pmt_2_5)+ "&field2=" + str(aqi_2_5)+ "&field3=" + str(pmt_10)+ "&field4=" + str(aqi_10)
try:
publish.single(topic, payload=tPayload, hostname=mqttHost, port=tPort, client_id=clientID, auth={'username':tUsername, 'password':tPassword}, tls=tTLS, transport=tTransport, protocol=tProtocol)
print ("[INFO] Published data")
save_log()
time.sleep(60)
except Exception as e:
print ("[INFO] Failure in sending data")
print (e)
time.sleep(60)
We operate roughly 600 data loggers on Think Speak where there are roughly 150 gateways and each gateway communicates with a node. Our channels are labeled node# - gateway number and each number is 16 diigits. We have not found a wildcard search function so must have each number exactly right or we cannot find our channel. In some cases we wish to query a gateway and see which nodes are successfully communicating with it.
Hello All,
Ive started dabbling with thinkspeak and Matlab which allows me to put a few pens on th esame graph. I would love to be able to choose how far back from now to show on the graph via a date picker or something similar. I now I can code that in to matlab but then its fixed in code.
Thanks in advance
Hi, there ! Need some serious help.... I have trouble trying to upload my data to my Channel. Any ideas to fix it?
Below you can see my code:
#include <ThingSpeak.h>// Η βιβλιοθήκη του ThingSpeak.
#include <SPI.h>
#include <Ethernet.h>
#include <SoftwareSerial.h>
#include <Servo.h> // Η βιβλιοθήκη του σερβοκινητήρα.
#include "DHT.h"
#include "MQ7.h"
//Για τον αισθητήρα DHT11.
#define DHTPIN 7
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
//Για τον φωτισμό.
#define DELAY 5000 // Καθυστέρηση μεταξύ μετρήσεων σε ms για τον φωτισμό.
#define VIN 5 // Τάση 5V.
#define R 10000 //Αντίσταση 10.000 Ω.
//Για τον αισθητήρα MQ7.
int MQ7pin = A0;
int MQ7Reading;
int MQ7led = 3;
//Για τον αισθητήρα Soil.
int val;
int soil_dig= 8;
int soil_analog = A1;
//Μεταβλητές για τον φωτισμό και για τον υπολογισμό lux.
const int sensorPin = A3; // Το pin στο οποίο συνδέεται ο αισθητήρας.
const int led = 12;
const int greenled = 10;
const int redled = 4;
const long onduration = 10000;
const long offduration = 30000;
int ledstate = HIGH;
long remembertime = 0;
int sensorVal; // Αναλογική τιμή από τον αισθητήρα.
int lux; //Τιμές για τα lux
int valvePin = 13; // Το pin στο οποίο συνδέεται το ρελέ, ώστε να ενεργοποιεί την ηλεκτροβάνα.
//Για τον αισθητήρα Ultrasonic.
#define echoPin 5 // Το echoPin.
#define trigPin 6 // To trigPin.
long duration; // Η διάρκεια που κάνει το ηχητικό κύμα να ταξιδέψει.
int distance; // Ο υπολογισμός της ταχύτητας του ηχητικού κύματος.
//Για τον αισθητήρα Rain.
const int rain_D = 2;//Ψηφιακή τιμή αισθητήρα βροχής.
const int rain_A = A2;//Αναλογική τιμή αισθητήρα βροχής.
int rain_val;// Μεταβλητή για την αποθήκευση της τιμής του αισθητήρα βροχής.
int r;// Μεταβλητή για την αποθήκευση της τιμής του αισθητήρα βροχής.
Servo servo; // Το όνομα του σερβοκινητήρα.
int servoPin = 11; // Το pin στο οποίο συνδέεται ο σερβοκινητήρας.
#define pwm 9 // Το pin στο οποίο συνδέεται ο ανεμιστήρας.
//Σύνδεση
String readString;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //H mac address.
byte ip[] = {192,168,2,123};
EthernetClient client;
unsigned long myChannelNumber = 1*******; //Ο αριθμός του καναλιού.
const char * myWriteAPIKey = "****************"; //Το ΑΡΙ Κey του καναλιού.
const int updateInterval = 10000;
void setup() {
Serial.begin(9600);
ThingSpeak.begin(client);// Σύνδεση στο cloud του ThingSpeak.
dht.begin();
pinMode(MQ7led, OUTPUT);// Θέτει το led του MQ7 ως OUTPUT.
pinMode(trigPin, OUTPUT); // Θέτει το trigPin ως OUTPUT.
pinMode(echoPin, INPUT); // Θέτει το echoPin ως INPUT.
pinMode(rain_D, INPUT); // Θέτει το rain_D ως INPUT.
pinMode(rain_A, INPUT); // Θέτει το rain_A ως INPUT.
pinMode(servoPin, OUTPUT);// Θέτει το servoPin ως OUTPUT.
servo.attach(servoPin);// Σύνδεση σε pin της μεταβλητής του σερβοκινητήρα.
analogWrite(pwm, 255);// Γράφει μία αναλογική τιμή (PWM pin) σε ένα pin.
pinMode(soil_analog, INPUT); // Θέτει το soil_analog ως INPUT.
pinMode(soil_dig, INPUT);// Θέτει το soil_dig ως INPUT.
pinMode(valvePin, OUTPUT); //Θέτει το 13 ως OUTPUT pin, ώστε να στέλνει σήμα στο relay.
pinMode(led, OUTPUT); // Θέτει το ledPin ως output.
digitalWrite(led,ledstate);
pinMode(sensorPin, INPUT);// Θέτει το sensorPin ως INPUT.
pinMode(greenled, OUTPUT);// Θέτει το greenled ως OUTPUT.
pinMode(redled, OUTPUT);// Θέτει το redled ως OUTPUT.
}
void loop() {
Serial.println(F("======================================"));
Serial.println(F("Συνθήκες στο Θερμοκήπιο :"));
//DHT11
float t = dht.readTemperature();// Η θερμοκρασία σε °C.
float h = dht.readHumidity(); // Η υγρασία του χώρου.
float f = dht.readTemperature(true); //Η θερμοκρασία σε °F.
// Υπολογισμός του heat index, ο οποίος πρέπει να μετατραπεί σε Fahrenheit.
float hi = dht.computeHeatIndex(f, h);
Serial.print(F("Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(F(" and "));
Serial.print(f);
Serial.print(F("°F\t"));
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F(" %\t"));
Serial.print(F("Heat index: "));
Serial.print(hi);
Serial.println(F(" °F"));
// Έλεγχος της θερμοκρασίας , ώστε να ενεργοποιείται ο ανεμιστήρας όποτε χρειάζεται.
if (t == 26)
{
analogWrite(pwm, 51);
Serial.print(F("Ταχύτητα ανεμιστήρα: 20% "));
delay(1000);
}
else if (t == 30)
{
analogWrite(pwm, 204);
Serial.print(F("Ταχύτητα ανεμιστήρα: 80% "));
delay(1000);
}
else if (t > 40 )
{
analogWrite(pwm, 255);
Serial.print(F("Ταχύτητα ανεμιστήρα: 100% "));
delay(1000);
Serial.print(F("\t"));
}
//MQ7
float g = analogRead(MQ7pin);
Serial.print(F("MQ7: "));
Serial.println(g);
// Έλεγχος της τιμής του MQ7, ώστε να ανάβει το led που αναπαριστά ανεμιστήρα.
if ( g < 200)
{
Serial.println(F("O ανεμιστήρας κλειστός - Φυσιολογική ποσότητα CO στον χώρο εργασίας."));
digitalWrite(MQ7led, LOW);
}
else if (201 < g && g < 220)
{
Serial.println(F("Η μέγιστη φυσιολογική ποσότητα CO για χώρους εργασίας ξεπεράστηκε!!!- Λειτουργία εξαερισμού στο 50%."));
digitalWrite(MQ7led,HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(MQ7led, LOW); // turn the LED off by making the voltage LOW
delay(1000);
}
if (g > 220)
{
Serial.println(F("Eπικίνδυνα αέρια CO !!!- Λειτουργία εξαερισμού στο 100%."));
digitalWrite(MQ7led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(5000);
}
//Soil Humidity
int soil_analog_value = analogRead(soil_analog);
soil_analog_value = map(soil_analog_value, 0, 1023, 230, 0);//Συνάρτηση map για μετρατροπή τιμής σε %.
val = digitalRead(soil_dig);
Serial.print(F("Soil moisture (Αναλογική τιμή) : "));
Serial.print(soil_analog_value);
Serial.print(F("\t"));
//Έλεγχος υγρασίας εδάφους για αυτόματο πότισμα.
if (soil_analog_value < 50)
{
digitalWrite(valvePin, LOW); //Όταν η τιμή γίνει LOW,τότε και το relay πρέπει να λάβει τιμή LOW.
Serial.println(F("relay on"));
}
else
{
digitalWrite(valvePin, HIGH); //Όταν η τιμή γίνει HIGH, τότε και το relay πρέπει να λάβει τιμή HIGH.
Serial.println(F("relay off"));
}
//Ultrasonic Sensor
// Αρχικοποίηση του trigPin.
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Το trigPin σε HIGH (ACTIVE) για 10 μs.
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
//Διαβάζει το echoPin, επιστρέφει το χρόνο του ηχητικού κύματος σε μs.
duration = pulseIn(echoPin, HIGH);
// Υπολογισμός απόστασης.
distance = (duration - 10) * 0.034 / 2;
Serial.println("");
Serial.print(F("Distance: "));
Serial.print(distance);
Serial.print(F(" cm"));
//Rain Sensor
rain_val = digitalRead(rain_D);
r = analogRead (rain_A);
r = abs ((100*r-40000)/623);
Serial.println(F(""));
Serial.print(F("Rain (Αναλογική τιμή) :"));
Serial.print(r);
Serial.print(F("\t"));
Serial.print(F("Rain (Ψηφιακή τιμή) :"));
Serial.print(rain_val);
//Έλεγχος για λειτουργία του σερβοκινητήρα.
if ((rain_val != HIGH) && (distance < 9)) {
servo.write(0);
}
if ((rain_val != HIGH) && (distance > 9)) {
servo.write(90);
}
else {
servo.write(0);
}
//Μέτρηση τιμής της φωτοαντίστασης και μετατροπή σε lux.
sensorVal = analogRead(sensorPin);
lux=sensorRawToPhys(sensorVal);
Serial.print("Raw value from sensor= ");
Serial.println(sensorVal); //Τύπωση αναλογικής τιμής.
Serial.print("Physical value from sensor = ");
Serial.print(lux); //Τύπωση αναλογικής τιμής.
Serial.println(" lumen");
//Συνθήκες για το άνοιγμα των led και των κουρτινών σκίασης σε διάφορες γωνίες.
if (ledstate == HIGH){
if((millis() - remembertime) >= onduration){
ledstate = LOW;
remembertime = millis();
}
}
if (ledstate == LOW){
if((millis() - remembertime) >= offduration){
ledstate = HIGH;
remembertime = millis();
}
}
//Lux.
if (lux < 100){
digitalWrite(led, HIGH);
if (lux < 200){
digitalWrite(greenled, ledstate);
if (lux < 300){
digitalWrite(redled, ledstate);
}}}
if(lux > 350){
digitalWrite(redled, LOW);
if(lux > 450){
digitalWrite(greenled, LOW);
if (lux>600){
digitalWrite(led, LOW);
}}}
else
digitalWrite(led,ledstate);
//Τα δεδομένα για το ThingSpeak, τα οποία στέλνονται στα αριθμημένα fields.
ThingSpeak.writeField(myChannelNumber,1,t, myWriteAPIKey);
ThingSpeak.writeField(myChannelNumber,2,h ,myWriteAPIKey);
ThingSpeak.writeField(myChannelNumber,3,g ,myWriteAPIKey);
ThingSpeak.writeField(myChannelNumber,4,soil_analog_value, myWriteAPIKey);
ThingSpeak.writeField(myChannelNumber,5,distance, myWriteAPIKey);
ThingSpeak.writeField(myChannelNumber,6,r, myWriteAPIKey);
delay(15000);
}
int sensorRawToPhys(int raw){
// Conversion rule
float Vout = float(raw) * (VIN / float(1023));// Conversion analog to voltage
float RLDR = (R * (VIN - Vout))/Vout; // Conversion voltage to resistance
int phys=500/(RLDR/1000); // Conversion resitance to lumen
return phys;
}
Hi all,
I am trying to create a complete learning unit online using laravel. The unit of work is in Internet of Things. Hardware will be provided to students - Its a microbit plus the DFRobot Environment science expansion kit (https://www.dfrobot.com/product-2194.html) . All the video tutorials and project work is assigned via the laravel lms.They will log into the lms via their microsoft logins.
My next step is to simplify the process of users accessing the mathworks account. I would like to pre assign channels for students with API keys to write sensor values from each of their microbit shields to the cloud.
Has anyone tried to do this or has any suggestions how to?
The advantages of students having their own channels on my account is for me to monitor how they are progressing. These are year 9 students at high school who have never done any IoT or mathworks/thingspeak related content before.
J Slade
J Slade
Last activity on 31 May 2022

After leaving a Thingspeak trend tab dormant for a period of time, when I reopen it, numerous data points are missing:
After I refresh the tab, all the data points fill back in:
Any ideas what causes this and how to prevent it?
Thanks
I found I have to use VPN to access my thingspeak data channels from China. Is this a known problem? if yes, anything I can do to solve the problem?
I am trying to write a table to a TS channel using thingSpeakWrite. The code below results in error but the help documentation indicates that it can be done. Any suggestion on how to fix this?
Function to call:
thingSpeakWrite(channelID,ship1(1,:),'WriteKey',writeAPIKey);
The complete code:
%Initialize table to be written to ThingSpeak
varNames = ["MMSI", "LAT", "LONG", "SOG", "VesselName", "VesselType", "Length", "Width"];
varTypes = ["double", "double", "double", "double", "categorical", "double", "double", "double"];
ship1 = table('Size',[20 8],'VariableTypes',varTypes,'VariableNames',varNames);
ship1.MMSI(:) = 121212;
ship1.SOG(:) = 10;
ship1(:,5) = {'PRIMODY'};
ship1(:,6) = {37};
ship1.Length(:) = 20;
ship1.Width(:) = 12;
thingSpeakWrite(channelID,ship1(1,:),'WriteKey',writeAPIKey);
Hello everyone, I need some help, I am developing a POC for a TCC using the DEMO itself "Example of HTTPS GET with SoftwareSerial and Arduino-SIM800L-driver" and when sending the data I am receiving a 606 error, does anyone have any idea of what it might be and how to solve it, I've validated the string sends through POSTMAN and it's ok.
SIM800L : Receive "AT+HTTPPARA="URL","https://api.thingspeak.com/update?api_key=XXXXXXXXXXXXXXX&field1=-23.5204435&field2=-46.5078319&field3=10&field4=600"
OK
"
SIM800L : Send "AT+HTTPPARA="REDIR",1"
SIM800L : End of transmission
SIM800L : Receive "AT+HTTPPARA="REDIR",1
OK
"
SIM800L : Send "ATI"
SIM800L : End of transmission
SIM800L : Receive "ATI
SIM800 R14.18
"
SIM800L : initiateHTTP() - Support of SSL enabled
SIM800L : Send "AT+HTTPSSL=1"
SIM800L : End of transmission
SIM800L : Receive "AT+HTTPSSL=1
OK
"
SIM800L : Send "AT+HTTPACTION=0"
SIM800L : End of transmission
SIM800L : Receive "AT+HTTPACTION=0
OK
"
SIM800L : End of transmission
SIM800L : Receive "
+HTTPACTION: 0,606,0
"
SIM800L : readHTTP() - HTTP status 606
SIM800L : Send "AT+HTTPTERM"
SIM800L : End of transmission
SIM800L : Receive "AT+HTTPTERM
OK
All my mqtt python thingspeak publish programs are stop sending values to thingspeak cloud today. is that issue with thingspeak cloud or any other issue.
I am trying to simulate a 3 phase induction motor on Simulink by using its dynamic model. This is the information in its datasheet:
It also has 4 poles and J = 0.0117 kgm², star connection. After calculate parameters, I get these values:
Lm = 0.176 H; Rr=Rs = 0.613Ω; Llr=Lls = 0.00669 H.
Also in the datasheet, they said that the output torque = 705 Nm at speed 35 rpm, but the model cannot reach that value. The simulation motor run backward if applied torque greater than 70 Nm ( at about 1200 rpm). But in reality, this motor can withstand 705 Nm, I have seen it run.
Can someone help me with this problem? Thank you
/*
* Use software serial for the PZEM
* Pin gpio2 Rx (Connects to the Tx pin on the PZEM)
* Pin gpio0 Tx (Connects to the Rx pin on the PZEM)
*/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <PZEM004Tv30.h>
#include "credentials.h" //this file keeps some private settings
WiFiClient wificlient;
WiFiClient glient; //Google client
//PZEM004Tv30 pzem(11, 12); //original config
PZEM004Tv30 pzem(4, 5); //Rx,Tx,(D2,D1) on nodeMCU
//PZEM004Tv30 pzem(13, 15); //Rx,Tx,(D7,D8) another config
//PZEM004Tv30 pzem(2, 0); //Rx,Tx,(gpio2,gpio0) on ESP-01
String tsStatus; //ThingSpeak, 255 char, null terminated
String status;
int error = 0; //0 no error; 1,2,4,8,16,32 -> errors for each parameter
float voltage, current, power, energy, frequency, pf;
float Vmax = 120.0;
float Vmin = 120.0;
float Pmax = 0.0;
int readAvg = 29; //nr of measurements averaged and sent to TS
int hour, minute, second, day, month, year;
//==============================================================================
// Functions
//==============================================================================
void wifiConnect(int n)
// "n" is the max number of tries to connect to SSID. If "n" reaches zero then
// wait for 15min and tries again for k (=100) cycles (100 * 15min ~ 25 hours)
{
WiFi.disconnect();
WiFi.mode(WIFI_STA);
WiFi.begin(ssid,password);
int nn = n;
int k = 100; // [_] to put this at the beginning
while (k > 0)
{
while ((WiFi.status() != WL_CONNECTED) && (n > 0))
{
delay(1000);
Serial.print(n);Serial.print(" >");
//Serial.print(F(":"));
n--;
}//while
//either connected or n = 0
//if connected -> break
if (WiFi.status() == WL_CONNECTED)
{
Serial.print(F("\nWiFi connected to IP address: ")); //@
Serial.println(WiFi.localIP()); //@
break;//exit from outward while
//return;
}
if (n == 0) //not connected to wifi during n*1000ms
{
n = nn; //start over with initial n
k--;
if (k != 0)
{
delay(300000); // 5 min
}
else //-> k == 0
{
Serial.println(F("Restart"));
delay(50);//to allow the serial to finish
ESP.restart();
}
}
}//while
return;//this point is reached only from the above 'break'
}
String getTime()
{
//in the main prog is defined the client to access google as "glient"
Serial.println(F("connecting to google"));//@
//Serial.println(google); //@
if (!glient.connect(ghost, httpPort))
{
Serial.println(F("connection failed"));
return "google_fail";
}
// This will send the request to the server
glient.println("HEAD / HTTP/1.1");
glient.println("Host: www.google.com"); // "Host: www.google.com"
glient.println("Accept: */*");
glient.println("User-Agent: Mozilla/4.0 (compatible; esp8266 Arduino;)");
glient.println("Connection: close");
glient.println();
delay(500);
// Read all the characters of the reply from server and print them to Serial
String reply = String("");
while(glient.available())
{
char c = glient.read();
reply = reply + String(c);
}
//Serial.print(reply); //@
String d = reply.substring(reply.indexOf("Date: ")+11,reply.indexOf("Date: ")+23);
String t = reply.substring(reply.indexOf("Date: ")+23,reply.indexOf("Date: ")+35);
hour = t.substring(0, 2).toInt();
minute = t.substring(3, 5).toInt();
second = t.substring(6, 8).toInt();
day = d.substring(0, 2).toInt();
month = d.substring(3, 5).toInt();
year = d.substring(6, 8).toInt();
hour = hour + 16;//for summer is 16, for winter is 15
if (hour >= 24)
{
hour = hour % 24; //This is GMT - 4 -> Mtl hour
}
if(!glient.connected())
{
//Serial.println("disconnecting");
glient.stop();
}
Serial.println("connection closed"); //@
return t;
}
void setup()
{
Serial.begin(115200);
tsStatus = String("");
Serial.println(F("\nFilename: PZEM_SoftSerial.ino/30jun2020 "));
//WiFi.persistent(false);
//Wifi.persistent(false) is used for deep-sleep, to keep wifi param in ram
wifiConnect(60); //try for 60 seconds
glient.setTimeout(5000);
getTime();
}
void loop()
{
/*
the time is got first in setup(). The loop is parsed each two seconds so
I need a counter variable (or a time variable) to check the local time (millis)
to know when to ask again google for date&time.
I need also a procedure to sync on zero seconds.
The data is sent each minute.
*/
if ((WiFi.status() != WL_CONNECTED))
{
wifiConnect(60);
}
float Voltage = 0.0, Current = 0.0, Power = 0.0, Energy = 0.0, Frequency = 0.0, PF = 0.0;
for (int i = 0; i < readAvg; i++)
{
error = 0;
voltage = pzem.voltage();
if( !isnan(voltage) )
{
Serial.print("Voltage: "); Serial.print(voltage); Serial.println("V");
Voltage += voltage;
//check for Max and Min values
if (voltage > Vmax)
{
Vmax = voltage;
}
if (voltage < Vmin)
{
Vmin = voltage;
}
//send Vmax and Vmin as tsStatus
Serial.print("\tVmax = ");Serial.print(Vmax);Serial.println("V");
Serial.print("\tVmin = ");Serial.print(Vmin);Serial.println("V");
}
else
{
Serial.println(F("Error reading voltage"));
error = error + 1;
}
current = pzem.current();
if( !isnan(current) )
{
Serial.print("Current: "); Serial.print(current); Serial.println("A");
Current += current;
}
else
{
Serial.println(F("Error reading current"));
error = error + 2;
}
power = pzem.power();
if( !isnan(power) )
{
Serial.print("Power: "); Serial.print(power); Serial.println("W");
Power += power;
//check for Max value
if (power > Pmax)
{
Pmax = power;
}//--> to send Pmax as tsStatus
Serial.print("\tPmax = ");Serial.print(Pmax);Serial.println("W");
}
else
{
Serial.println(F("Error reading power"));
error = error + 4;
}
energy = pzem.energy();
if( !isnan(energy) )
{
Serial.print("Energy: "); Serial.print(energy,3); Serial.println("kWh");
Energy += energy;
}
else
{
Serial.println(F("Error reading energy"));
error = error + 8;
}
frequency = pzem.frequency();
if( !isnan(frequency) )
{
Serial.print("Frequency: "); Serial.print(frequency, 1); Serial.println("Hz");
Frequency += frequency;
}
else
{
Serial.println(F("Error reading frequency"));
error = error + 16;
}
pf = pzem.pf();
if( !isnan(pf) )
{
Serial.print("PF: "); Serial.println(pf);
PF += pf;
}
else
{
Serial.println(F("Error reading power factor"));
error = error + 32;
}
Serial.println();
if (error > 0)
{
Serial.print("--> Error: "); Serial.println(error);Serial.println();
}
delay(1950);
}//for
voltage = Voltage / readAvg;
current = Current / readAvg;
power = Power / readAvg;
energy = Energy / readAvg;
frequency = Frequency / readAvg;
pf = PF / readAvg;
status = "Error = " + String(error) + " / Pmax = " + String(Pmax) + " / Vmax = " + String(Vmax) + " / Vmin = " + String(Vmin);
tsStatus = String(status).c_str();
//Serial.println(status);
Serial.println(tsStatus);
//delay(2000);
transferData();
Vmax = 120.0;
Vmin = 120.0;
Pmax = 0.0;
}
//------------------------------------------------------------------------------
void transferData()
//transfer data to ThingSpeak
{
Serial.println(F("transferData()")); //@
String url = "/update?api_key=" + ThingSpeak_key +
"&field1=" + energy +
"&field2=" + power +
"&field3=" + voltage +
"&field4=" + current +
"&field5=" + frequency +
"&field6=" + pf +
"&status=" + tsStatus; //String(tsStatus).c_str();
//see the notes at the top of this file.
if (!wificlient.connect(ThingSpeak, 80))
{
Serial.println(F("connection to ThingSpeak failed"));
}
else
{
wificlient.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " +
ThingSpeak + "\r\n" + "Connection: keep-alive\r\n\r\n");
while (!wificlient.available())
{
//waiting...
}
delay(200);
}
if (gettsStatus()) Serial.println(F("Data transfer OK to ThingSpeak")); //@
}
boolean gettsStatus()
{
Serial.println(F("getStatus()")); //@
bool stat;
String _line;
_line = wificlient.readStringUntil('\n');
int separatorPosition = _line.indexOf("HTTP/1.1");
if (separatorPosition >= 0)
{
if (_line.substring(9, 12) == "200")
stat = true;
else
stat = false;
return stat;
}
}
const char* ssid = " ssid";
const char* password = " password";
const char* ThingSpeak = "api.thingspeak.com";
const String ThingSpeak_key = "xxxxxxxxxxxxxxxx"; //
unsigned long ThingSpeak_channel = ; //99999999999;
const char* ghost = "www.google.com";
const int httpPort = 80;
i want to send temperature read to field1 and ecg read to field2
how can i write the code
and i have another problem that i don't have ecg code to thingspeak

I wanted to ask of 3M messages of thingspeak free account will renew every year? Thank You

I want code for plotting windrose in matlab
I have 2 type of data for plot that is wind speed and wind direction
pls I want it quickly .
Kirito23
Kirito23
Last activity on 14 May 2022

Hi, How can I draw windrose on matlab,I have attached sample data for you, sorry for the inconvenience

WHAT DOES THIS MEAN PLS?
Need help :(
Warning: 'Unable to read 500 points as requested. Only 0 points available in the channel.
Hi,
I have two DGs and I want a secondary control between these two DGs using mpc toolbox. I don't know where I am going wrong with it. it is not working. when I am trying to change the load the frequency is still the same. Can anyone please help?
Thanks
Good morning,
I'm working with an Arduino MKR 1010 which takes information about weather from the website "api.openweathermap.org". On a ThingSpeak dashboard I displayed the main numbers (float) information like: temperature, wind speed, humidity, etc. Moreover, from the openweathermap I can also get string sentence (e.g. "light rain" or "broken clouds" etc.). I'm wondering if there is a way to display on the ThingSpeak dashboard string fields. Indeed, in the ThingSpeak Channel Settings I added an extra Field in order to store these string words but I don't know how to print them in the dashboard.
Someone can help me?
Thanks in advance,
Lorenzo