Muhammad in MATLAB Answers
上次活动时间: 2023-6-8

esp8266 keeps displaying 0 even though the field is 1, and it's not writing to it. it was working fine for months. and today out of the blue it's not working. checked the esp and it's working fine. tried writing and reading from the channel using an app and it's working fine. what could be the problem? void loop() { int A = ThingSpeak.readLongField(ChannelNumber, FieldNumber1, ReadAPIKey); Serial.println(A); if (A == 1 && !relayTriggered) { // only trigger the relay if it hasn't been triggered before digitalWrite(relay, LOW); delay(200); digitalWrite(relay, HIGH); ThingSpeak.writeField(ChannelNumber, FieldNumber1, 1, WriteAPIKey); // Write 0 back to FieldNumber1 using ThingSpeak API relayTriggered = true; // set the flag to true after the relay has been triggered } else if (A == 0) { relayTriggered = false; // reset the flag when the field value changes to 0 } ThingSpeak.writeField(ChannelNumber, FieldNumber1, 1, WriteAPIKey); }
Merlyn in Discussions
上次活动时间: 2023-3-2

Hi, I'm trying to communicate with my channel ohowever it looks like I'm missing something as my data doesn't appear in the feeds. Below is my code: #define SW_VERSION " ThinkSpeak.com" // SW version will appears at innitial LCD Display #define TINY_GSM_MODEM_SIM900 #include <TinyGsmClient.h> #define SerialAT Serial TinyGsm modem(SerialAT); TinyGsmClient client(modem); /* ESP12-E & Thinkspeak*/ const char apn[] = "xxxxx"; const char user[] = ""; const char pass[] = ""; const char server[] = "api.thingspeak.com"; const int port = 80; const char* TS_SERVER = "api.thingspeak.com"; String TS_API_KEY ="xxxxx"; int sent = 0; /* TIMER */ #include <SimpleTimer.h> SimpleTimer timer; /* OLED */ #include <ACROBOTIC_SSD1306.h> // library for OLED: SCL ==> D1; SDA ==> D2 #include <SPI.h> #include <Wire.h> /* DHT22*/ #include "DHT.h" #define DHTPIN D3 #define DHTTYPE DHT22 DHT dht(DHTPIN, DHTTYPE); float hum = 0; float temp = 0; /* Soil Moister */ #define soilMoisterPin A0 #define soilMoisterVcc D4 //not used. LM393 VCC connect to 3.3V int soilMoister = 0; /* DS18B20 Temperature Sensor */ #include <OneWire.h> #include <DallasTemperature.h> #define ONE_WIRE_BUS 14 // DS18B20 on NodeMCU pin D5 corresponds to GPIO 014 on Arduino float soilTemp; OneWire oneWire(ONE_WIRE_BUS); DallasTemperature DS18B20(&oneWire); void setup() { Serial.begin(115200); delay(10); Serial.println(".... Starting Setup"); SerialAT.begin(115200); delay(3000); // Restart takes quite some time // To skip it, call init() instead of restart() Serial.println("Initializing modem..."); modem.init(); pinMode(soilMoisterVcc, OUTPUT); Serial.begin(115200); delay(10); oledStart(); dht.begin(); DS18B20.begin(); timer.setInterval(2000L, getDhtData); timer.setInterval(7000L, getSoilMoisterData); timer.setInterval(9000L, getSoilTempData); timer.setInterval(19000L, sendDataTS); digitalWrite (soilMoisterVcc, LOW); } void loop() { displayData(); timer.run(); // Initiates SimpleTimer } /*************************************************** * Start OLED **************************************************/ void oledStart(void) { Wire.begin(); oled.init(); // Initialze SSD1306 OLED display clearOledDisplay(); oled.clearDisplay(); // Clear screen oled.setTextXY(0,0); oled.putString(" "); } /*************************************************** * Get DHT data **************************************************/ void getDhtData(void) { float tempIni = temp; float humIni = hum; temp = dht.readTemperature(); hum = dht.readHumidity(); if (isnan(hum) || isnan(temp)) // Check if any reads failed and exit early (to try again). { Serial.println("Failed to read from DHT sensor!"); temp = tempIni; hum = humIni; return; } } /*************************************************** * Get Soil Moister Sensor data **************************************************/ void getSoilMoisterData(void) { soilMoister = 0; digitalWrite (soilMoisterVcc, HIGH); delay (500); int N = 3; for(int i = 0; i < N; i++) // read sensor "N" times and get the average { soilMoister += analogRead(soilMoisterPin); delay(150); } digitalWrite (soilMoisterVcc, LOW); soilMoister = soilMoister/N; Serial.println(soilMoister); soilMoister = map(soilMoister, 689, 274, 0, 100); } /*************************************************** * Get SoilTemp sensor data **************************************************/ void getSoilTempData() { DS18B20.requestTemperatures(); soilTemp = DS18B20.getTempCByIndex(0); int newData = ((soilTemp + 0.05) * 10); //fix soilTemp value to 1 decimal place. soilTemp = (newData / 10.0); } /*************************************************** * Display data at Serial Monitora & OLED Display **************************************************/ void displayData(void) { Serial.print(" Temperature: "); Serial.print(temp); Serial.print("oC Humidity: "); Serial.print(hum); Serial.println("%"); Serial.print("SoilTemp: "); Serial.print(soilTemp); Serial.print("oC"); oled.setTextXY(1,0); // Set cursor position, start of line 2 oled.putString("TEMP: " + String(temp) + " oC"); oled.setTextXY(3,0); // Set cursor position, start of line 2 oled.putString("HUM : " + String(hum) + " %"); oled.setTextXY(5,0); // Set cursor position, start of line 2 oled.putString("Tsoil:" + String(soilTemp) + " oC"); oled.setTextXY(7,0); // Set cursor position, start of line 2 oled.putString("Hsoil: " + String(soilMoister) + " %"); } /*************************************************** * Clear OLED Display **************************************************/ void clearOledDisplay() { oled.setFont(font8x8); oled.setTextXY(0,0); oled.putString(" "); oled.setTextXY(1,0); oled.putString(" "); oled.setTextXY(2,0); oled.putString(" "); oled.setTextXY(3,0); oled.putString(" "); oled.setTextXY(4,0); oled.putString(" "); oled.setTextXY(5,0); oled.putString(" "); oled.setTextXY(6,0); oled.putString(" "); oled.setTextXY(7,0); oled.putString(" "); oled.setTextXY(0,0); oled.putString(" "); } /*************************************************** * Sending Data to Thinkspeak Channel **************************************************/ void sendDataTS(void) { if (client.connect(TS_SERVER, 80)) { String postStr = TS_API_KEY; postStr += "&field1="; postStr += String(temp); postStr += "&field2="; postStr += String(hum); postStr += "&field3="; postStr += String(soilMoister); postStr += "&field4="; postStr += String(soilTemp); postStr += "\r\n\r\n"; client.print("POST /update HTTP/1.1\n"); client.print("Host: api.thingspeak.com\n"); client.print("Connection: close\n"); client.print("X-THINGSPEAKAPIKEY: " + TS_API_KEY + "\n"); client.print("Content-Type: application/x-www-form-urlencoded\n"); client.print("Content-Length: "); client.print(postStr.length()); client.print("\n\n"); client.print(postStr); delay(1000); } sent++; client.stop(); } First I get this on my serial monitor: After some time: And after some more time: With my limited knowledge on the subject I can't figure it out. Any help would be appreciated. Thanks. Send sensors data with NodeMCU and SIM900 GSM module problem You have combined some methods here. I would suggest simplifying. First try communicating with ThingSpeak via wifi over the node MCU. Just send constant values and remove the soil moisture part for now. Then switch to the sim card, but build your code up one step at a time. Once you have that working for constants, then put the sensor data back in. If you get stuck again, you will be able to share much simpler code that we can parse. This code, with all the soil moisture and oled parts, is reletively hard to parse to help you. After several days of tweaking, trial and error I managed to get the code working. Main thing is I had to implement Software serial as the hardware serial refused to cooperate. Below is the working solution. /************************************************************** * * Sending DHT, Soil Temp and Soil Moisture sensor data with SIM900 module to Thinspeak * Based on TinyGSM examples and library *and mjrovai ArduFarmBot series at Instructables.com * * TinyGSM Getting Started guide: * http://tiny.cc/tiny-gsm-readme * **************************************************************/ #define TINY_GSM_MODEM_SIM900 // Increase RX buffer if needed //#define TINY_GSM_RX_BUFFER 512 #include <TinyGsmClient.h> // Uncomment this if you want to see all AT commands #define DUMP_AT_COMMANDS // Uncomment this if you want to use SSL //#define USE_SSL // Set serial for debug console (to the Serial Monitor, default speed 115200) #define SerialMon Serial // Use Hardware Serial on Mega, Leonardo, Micro //#define SerialAT Serial // or Software Serial on Uno, Nano #include <SoftwareSerial.h> SoftwareSerial SerialAT(12, 13); // RX, TX // Your GPRS credentials // Leave empty, if missing user or pass const char apn[] = "xxxxx"; const char user[] = ""; const char pass[] = ""; // Server details const char server[] = "api.thingspeak.com"; String TS_API_KEY ="xxxxx"; int sent = 0; #ifdef DUMP_AT_COMMANDS #include <StreamDebugger.h> StreamDebugger debugger(SerialAT, SerialMon); TinyGsm modem(debugger); #else TinyGsm modem(SerialAT); #endif #ifdef USE_SSL TinyGsmClientSecure client(modem); const int port = 443; #else TinyGsmClient client(modem); const int port = 80; #endif /* TIMER */ #include <SimpleTimer.h> SimpleTimer timer; /* OLED */ #include <ACROBOTIC_SSD1306.h> // library for OLED: SCL ==> D1; SDA ==> D2 #include <SPI.h> #include <Wire.h> /* DHT22*/ #include "DHT.h" #define DHTPIN D3 #define DHTTYPE DHT22 DHT dht(DHTPIN, DHTTYPE); float hum = 0; float temp = 0; /* Soil Moister */ #define soilMoisterPin A0 #define soilMoisterVcc D4 //not used. LM393 VCC connect to 3.3V int soilMoister = 0; /* DS18B20 Temperature Sensor */ #include <OneWire.h> #include <DallasTemperature.h> #define ONE_WIRE_BUS 14 // DS18B20 on NodeMCU pin D5 corresponds to GPIO 014 on Arduino float soilTemp; OneWire oneWire(ONE_WIRE_BUS); DallasTemperature DS18B20(&oneWire); void setup() { // Set console baud rate SerialMon.begin(9600); delay(10); // Set GSM module baud rate SerialAT.begin(9600); delay(3000); // Restart takes quite some time // To skip it, call init() instead of restart() SerialMon.println(F("Initializing modem...")); modem.init(); String modemInfo = modem.getModemInfo(); SerialMon.print(F("Modem: ")); SerialMon.println(modemInfo); // Unlock your SIM card with a PIN //modem.simUnlock("1234"); Serial.begin(9600); delay(10); oledStart(); dht.begin(); DS18B20.begin(); connectGPRS(); timer.setInterval(5000L, getDhtData); timer.setInterval(5000L, getSoilMoisterData); timer.setInterval(5000L, getSoilTempData); timer.setInterval(10000L, sendDataTS); } /*************************************************** * Start OLED **************************************************/ void oledStart(void) { Wire.begin(); oled.init(); // Initialze SSD1306 OLED display clearOledDisplay(); oled.clearDisplay(); // Clear screen oled.setTextXY(0,0); oled.putString(" "); } /*************************************************** * Get DHT data **************************************************/ void getDhtData(void) { float tempIni = temp; float humIni = hum; temp = dht.readTemperature(); hum = dht.readHumidity(); if (isnan(hum) || isnan(temp)) // Check if any reads failed and exit early (to try again). { Serial.println("Failed to read from DHT sensor!"); temp = tempIni; hum = humIni; return; } } /*************************************************** * Get Soil Moister Sensor data **************************************************/ void getSoilMoisterData(void) { soilMoister = 0; digitalWrite (soilMoisterVcc, HIGH); delay (500); int N = 3; for(int i = 0; i < N; i++) // read sensor "N" times and get the average { soilMoister += analogRead(soilMoisterPin); delay(150); } digitalWrite (soilMoisterVcc, LOW); soilMoister = soilMoister/N; Serial.println(soilMoister); soilMoister = map(soilMoister, 689, 274, 0, 100); } /*************************************************** * Get SoilTemp sensor data **************************************************/ void getSoilTempData() { DS18B20.requestTemperatures(); soilTemp = DS18B20.getTempCByIndex(0); int newData = ((soilTemp + 0.05) * 10); //fix soilTemp value to 1 decimal place. soilTemp = (newData / 10.0); } /*************************************************** * Display data at Serial Monitora & OLED Display **************************************************/ void displayData(void) { //Serial.print(" Temperature: "); //Serial.print(temp); //Serial.print("oC Humidity: "); //Serial.print(hum); //Serial.println("%"); //Serial.print("SoilTemp: "); //Serial.print(soilTemp); //Serial.print("oC"); oled.setTextXY(1,0); // Set cursor position, start of line 2 oled.putString("TEMP: " + String(temp) + " oC"); oled.setTextXY(3,0); // Set cursor position, start of line 2 oled.putString("HUM : " + String(hum) + " %"); oled.setTextXY(5,0); // Set cursor position, start of line 2 oled.putString("Tsoil:" + String(soilTemp) + " oC"); oled.setTextXY(7,0); // Set cursor position, start of line 2 oled.putString("Hsoil: " + String(soilMoister) + " %"); } /*************************************************** * Clear OLED Display **************************************************/ void clearOledDisplay() { oled.setFont(font8x8); oled.setTextXY(0,0); oled.putString(" "); oled.setTextXY(1,0); oled.putString(" "); oled.setTextXY(2,0); oled.putString(" "); oled.setTextXY(3,0); oled.putString(" "); oled.setTextXY(4,0); oled.putString(" "); oled.setTextXY(5,0); oled.putString(" "); oled.setTextXY(6,0); oled.putString(" "); oled.setTextXY(7,0); oled.putString(" "); oled.setTextXY(0,0); oled.putString(" "); } void loop() { displayData(); timer.run(); // Initiates SimpleTimer } void connectGPRS() { SerialMon.print(F("Waiting for network...")); if (!modem.waitForNetwork()) { SerialMon.println(" fail"); delay(10000); return; } SerialMon.println(" OK"); SerialMon.print(F("Connecting to ")); SerialMon.print(apn); if (!modem.gprsConnect(apn, user, pass)) { SerialMon.println(" fail"); delay(10000); return; } SerialMon.println(" OK"); SerialMon.print(F("Connecting to ")); SerialMon.print(server); if (!client.connect(server, port)) { SerialMon.println(" fail"); delay(10000); return; } SerialMon.println(" OK"); unsigned long timeout = millis(); while (client.connected() && millis() - timeout < 10000L) { // Print available data while (client.available()) { char c = client.read(); SerialMon.print(c); timeout = millis(); } } SerialMon.println(); } void sendDataTS(void) { if (client.connect(server, 80)) { String postStr = TS_API_KEY; postStr += "&field1="; postStr += String(temp); postStr += "&field2="; postStr += String(hum); postStr += "&field3="; postStr += String(soilMoister); postStr += "&field4="; postStr += String(soilTemp); postStr += "\r\n\r\n"; client.print("POST /update HTTP/1.1\n"); client.print("Host: api.thingspeak.com\n"); client.print("Connection: close\n"); client.print("X-THINGSPEAKAPIKEY: " + TS_API_KEY + "\n"); client.print("Content-Type: application/x-www-form-urlencoded\n"); client.print("Content-Length: "); client.print(postStr.length()); client.print("\n\n"); client.print(postStr); delay(1000); } sent++; client.stop(); } sim900 nodemcu
Akshada Shinde in MATLAB Answers
上次活动时间: 2021-8-16

#include <ESP8266WiFi.h> #include <Arduino.h> #include <U8g2lib.h> #ifdef U8X8_HAVE_HW_I2C #include <Wire.h> #endif #include "secrets.h" #include "ThingSpeak.h" // always include thingspeak header file after other header files and custom macros U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE); 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; const int buttonPin = D5; int buttonState = 0; // variable for reading the pushbutton status long count =0; void setup() { pinMode(buttonPin, INPUT); pinMode(ResetPin,INPUT); Serial.begin(115200); // Initialize serial u8g2.begin(); delay(1000); u8g2.setFont(u8g2_font_ncenB08_tr); u8g2.drawStr(60,60,"Connecting"); u8g2.sendBuffer(); Serial.begin(115200); WiFi.mode(WIFI_STA); ThingSpeak.begin(client); // Initialize ThingSpeak 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."); u8g2.clearBuffer(); u8g2.setFont(u8g2_font_ncenB12_tr); u8g2.drawStr(10,35,"CONNECTED !"); u8g2.sendBuffer(); delay(2000); u8g2.clearBuffer(); u8g2.setFont(u8g2_font_ncenB08_tr); u8g2.drawStr(03,10,"Total"); u8g2.setFont(u8g2_font_ncenB08_tr); u8g2.drawStr(60,60,"subscribers"); u8g2.setFont( u8g2_font_ncenB18_tn ); u8g2.setCursor(24, 40); u8g2.print(count); u8g2.sendBuffer(); ThingSpeak.writeField(myChannelNumber, 1, count, myWriteAPIKey); } } void loop() { buttonState = digitalRead(buttonPin); if (buttonState == 1) { count++; u8g2.clearBuffer(); u8g2.setFont(u8g2_font_ncenB08_tr); u8g2.drawStr(03,10,"Total"); u8g2.setFont(u8g2_font_ncenB08_tr); u8g2.drawStr(60,60,"subscribers"); u8g2.setFont( u8g2_font_ncenB18_tn ); u8g2.setCursor(24, 40); u8g2.print(count); u8g2.sendBuffer(); ThingSpeak.writeField(myChannelNumber, 1, count, myWriteAPIKey); } else { } }
salaheddine alshawwa in MATLAB Answers
上次活动时间: 2021-6-13

I am working on a home control system .i am using the thingSpeak server to control Nodemcu . The problem is that once i send the GET request to update a specific field it can take up to 12 seconds. And this is so slow. My question is , does this happen with other people before or am i doing something wrong.
pranjal gurav in MATLAB Answers
上次活动时间: 2020-10-13

I am using this code for sending the temperature to my channel but getting nun and did not received any value. plz help #include <ESP8266WiFi.h> #include "DHT.h" String apiWritekey = "KSB45J7R0MLLKF7U"; // replace with your THINGSPEAK WRITEAPI key here const char* ssid = "vivo"; // your wifi SSID name const char* password = "20051999" ;// wifi pasword const char* server = "api.thingspeak.com"; #define DHTPIN 14 // Data Pin of DHT 11 , for NodeMCU D5 GPIO no. is 14 #define DHTTYPE DHT11 // DHT 11 DHT dht(DHTPIN, DHTTYPE); //float resolution=3.3/1023;// 3.3 is the supply volt & 1023 is max analog read value WiFiClient client; void setup() { Serial.begin(115200); dht.begin(); WiFi.disconnect(); delay(10); WiFi.begin(ssid, password); Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.print("NodeMcu connected to wifi..."); Serial.println(ssid); Serial.println(); } void loop() { //float temp = (analogRead(A0) * resolution) * 100; float temp = dht.readTemperature(); Serial.println(temp); if (client.connect(server,80)) { String tsData = apiWritekey; tsData +="&field1="; tsData += String(temp); tsData += "\r\n\r\n"; client.print("POST /update HTTP/1.1\n"); client.print("Host: api.thingspeak.com\n"); client.print("Connection: close\n"); client.print("X-THINGSPEAKAPIKEY: "+apiWritekey+"\n"); client.print("Content-Type: application/x-www-form-urlencoded\n"); client.print("Content-Length: "); client.print(tsData.length()); client.print("\n\n"); // the 2 carriage returns indicate closing of Header fields & starting of data client.print(tsData); Serial.print("Temperature: "); Serial.print(temp); Serial.println("uploaded to Thingspeak server...."); } client.stop(); Serial.println("Waiting to upload next reading..."); Serial.println(); // thingspeak needs minimum 15 sec delay between updates delay(15000); }
Otávio Vanzelotti in MATLAB Answers
上次活动时间: 2019-10-8

I'm trying to create a pie chart with the energy comsumption of some of my electronic devices. I made a simple code for testing: Flow the logic: 1) Receives energy consumption of a cicle (interval) from TV (field 1) and Lights (field 2) [ESP calculates in X seconds how much energy was used and sends it] 2) In 'Field Chart' I select 'Sum' as 'Daily', so each time it receives an energy consumption this value is added to the previous one. Now about the MATLAB Visualization code: I got a pie chart using 'pie(X)' with X being a vector of only 2 numbers (energy from TV and energy from Lights). But the sum of the only two numbers not equals 100%. One thing that I did notice is that the proportion of the yellow and the blue slices is correct (when compared with the values that are displayed at the Field Charts), but I don't know what is the missing part of the pie. Look at the pie chart that I got: My objective is to show how one device affects the total daily consumption in comparison with another device. So, with the last value added to the daily sum of one field I want to use it as a slice of the pie in the chart. So, the objective is a Daily Pie Chart of Energy Consumtion. I'm also getting the message: "Error using Testing (Pie Chart) (line 20). Channel ID must be a positive integer." But my code doesen't even have 20 lines and my Channel ID is a positive integer. The simple code below: readChannelID = 123456; % exemple of how the channelId written in the original code fieldID1 = 1; fieldID2 = 2; readAPIKey = '6RW0V0UCJ7TPV24R'; etv = thingSpeakRead(readChannelID, 'Field', fieldID1, 'NumPoints', 1, 'Readkey', readAPIKey); % energy from TV eil = thingSpeakRead(readChannelID, 'Field', fieldID2, 'NumPoints', 1, 'Readkey', readAPIKey); % energy from illumination X = [etv eil]; pie(X); Thanks for the help and sorry if i made an english mistake. I'm from Brasil.
120004171 B.Tech ECE- Y J Prashaant in MATLAB Answers
上次活动时间: 2018-10-22

Arduino: 1.8.5 (Windows 10), Board: "NodeMCU 1.0 (ESP-12E Module), 80 MHz, Flash, 4M (1M SPIFFS), v2 Lower Memory, Disabled, None, Only Sketch, 115200" Build options changed, rebuilding all In file included from c:\programdata\matlab\supportpackages\r2018a\3p.instrset\arduinoide.instrset\idepkgs\packages\esp8266\tools\xtensa-lx106-elf-gcc\1.20.0-26-gb404fb9-2\xtensa-lx106-elf\include\c++\4.8.2\algorithm:60:0, from C:\ProgramData\MATLAB\SupportPackages\R2018a\3P.instrset\arduinoide.instrset\idepkgs\packages\esp8266\hardware\esp8266\2.4.2\cores\esp8266/Arduino.h:255, from sketch\thingspeak2.ino.cpp:1: c:\programdata\matlab\supportpackages\r2018a\3p.instrset\arduinoide.instrset\idepkgs\packages\esp8266\tools\xtensa-lx106-elf-gcc\1.20.0-26-gb404fb9-2\xtensa-lx106-elf\include\c++\4.8.2\utility:68:28: fatal error: bits/c++config.h: No such file or directory #include <bits/c++config.h> ^ compilation terminated. exit status 1 Error compiling for board NodeMCU 1.0 (ESP-12E Module). This report would have more information with "Show verbose output during compilation" option enabled in File -> Preferences.

关于 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.