So I found a program(David Windspeed) which works for what I want ,I would like to understand how I could send this data to ThingSpeak. I have seen several examples even onThingSpeak site but for the life of me I am not able to figure out hoe to adapt this program to ThingSpeak.
I am using Lolin ESP 8266 12 E to collect data and I can view it on the serial monitor currently. I do have the settings on ThingSpeak showing the graphics , so that part is setup.
Is there a tutorial that would show me how to incorporate a existing program?
There a number of tutorials on how to send data to ThingSpeak then how to plot that data:
https://randomnerdtutorials.com/esp32-http-post-ifttt-thingspeak-arduino/
https://nothans.com/thingspeak-tutorials/arduino/send-data-to-thingspeak-with-arduino
https://iotdesignpro.com/projects/how-to-send-data-to-thingspeak-cloud-using-esp32
Perhaps if you could tell/show us the issue you are having?
Thanks ,I will try thoes links, but I don’t know how to explain any better my question here. I have gone on a few sites like ThingSpeak and looked over the information which for me just added confusion .
So here is an example , I write a program for the ESP8266 and compile it , send it to the serial monitor and all works as expected. So that part wasn’t too awfully hard,I know I get the data because I can see it on the monitor.Then I go to the web interface ThingSpeak and set up the visualizations, I see my graphs but no data sent to them as of yet.This is where I am stuck, if I read the examples , they are things like a DHT11 temperature reading and a bunch of code, doesn’t really help me in sending my code. It’s not like you can send a print command to the serial monitor ,so I don’t quite understand as to how the information is bein sent to the site.
In my program I send WindSpeed and view it on serial monitor , how does this get sent to the ThingSpeak. that is really my question. As I said I will check out your links and maybe I will get it but so far I haven’t really gotten any good instruction.Thasnk you for your help.
So here is what I was trying to display on ThingSpeak , the code below will give me the readout of the wind speed . I have made the charts on Thingspeak to display the readings,I just don’t know or understand from the examples how I would get this information on the web site(ThingSpeak).I have copied programs and displayed them on the site (DHT11porto.PT) as an example but that in its self doesn’t really help .Thanks again
#include
#define WindSensorPin (2) // The pin location of the anemometer sensor
volatile unsigned long Rotations; // cup rotation counter used in interrupt routine
volatile unsigned long ContactBounceTime; // Timer to avoid contact bounce in interrupt routine
float WindSpeed; // speed miles per hour
void setup() {
Serial.begin(9600);
pinMode(WindSensorPin, INPUT);
attachInterrupt(digitalPinToInterrupt(WindSensorPin), isr_rotation, FALLING);
Serial.println(“Davis Wind Speed Test”);
Serial.println(“Rotations\tMPH”);
}
void loop() {
Rotations = 0; // Set Rotations count to 0 ready for calculations
sei(); // Enables interrupts
delay (3000); // Wait 3 seconds to average
cli(); // Disable interrupts
// convert to mp/h using the formula V=P(2.25/T)
// V = P(2.25/3) = P * 0.75
WindSpeed = Rotations * 0.75;
Serial.print(Rotations); Serial.print(“\t\t”);
Serial.println(WindSpeed);
}
// This is the function that the interrupt calls to increment the rotation count
void isr_rotation () {
if ((millis() – ContactBounceTime) > 15 ) { // debounce the switch contact.
Rotations++;
ContactBounceTime = millis();
}
}
The easiest way is to just use the temperature example for the DHT11. Ignore any humidity readings. Wherever the example uses temperature you would plug in wind speed. The graph on ThingSpeak would then show wind speed over time.
Hi Larry.
Your current code doesn’t post anything to Thingspeak. It just reads wind speed and displays it in the serial monitor.
As Steve mentioned, you can look at examples that publish the temperature and replace the temperature readings with your wind speed readings.
Try to combine both sketches, and then tell me if you need further help.
Regards,
Sara
Sara , thank you, I realize that I don’t have anything written in that code that refers to thingspeak , I know I have to put in my WiFi credentials and the information for Thingspeak . Short of that is where I am stuck, seems like a lot of code to have to use and not sure what parts of it are required for what I want to display on the site. I have looked at several samples and they seem be different as day and night.
Hi.
You can first try to use our ThingSpeak example as it is: https://randomnerdtutorials.com/esp32-http-post-ifttt-thingspeak-arduino/
Then, copy the parts of your code responsible for getting wind measurements.
Then, replace the random(40) with a variable that holds your sensor data (in your case, it is the WindSpeed variable). in the next line.
String httpRequestData = "{\"api_key\":\"" + apiKey + "\",\"field1\":\"" + String(random(40)) + "\"}";
I hope this helps.
Regards,
Sara
Seeing as you only have one value to write to ThingSpeak you would use the example at https://github.com/mathworks/thingspeak-arduino/tree/master/examples/ESP8266/program%20board%20directly/WriteSingleField. Use the tutorial at https://nothans.com/thingspeak-tutorials/arduino/send-data-to-thingspeak-with-arduino to visualize. In the example it uses “number”. You would replace that with “windspeed”. Give that a try and if it doesn’t work post your code so we may help.
Sara;
Tried to do as you suggested , copied the raw code and had problems with that . Too many issues just doing that, first off it would not accept the
#include< HTTPClient.h >, changed that to #include<HttpClient.h>.
const char* on password and network changed to char*
line 58 http.begin (serverName);
line 61 http.addHeader
line 65 int httpResponseCode
and others all came up with errors like non class,I have to believe that if I can find the issue with the http then thoes will go away as the issues went away when i changed the const char*.
Sorry if this is getting old but I have been working on getting this to do what I want to no avail, still trying to learn but these code errors are killing me before I even get started.
Thank you Steve that helped some, still not displaying values on thingspeak channel. Also for some reason when I adapt the code to the one value program and compile it I get errors on
#58 pinMode(WindSensorPin, INPUT);
#59 attachInterrupt(digitalPinToInterrupt(WindSensorPin), isr_rotation, FALLING);
expected constructer, destructor or type conversion before ‘(‘ token
code:
#include <ESP8266WiFi.h>
#include “secrets.h”
#include <math.h>
#define WindSensorPin (2) // The pin location of the anemometer sensor
volatile unsigned long Rotations; // cup rotation counter used in interrupt routine
volatile unsigned long ContactBounceTime; // Timer to avoid contact bounce in interrupt routine
float WindSpeed; // speed miles per hour
#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;
void setup() {
Serial.begin(9600); // 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
}
pinMode(WindSensorPin, INPUT);
attachInterrupt(digitalPinToInterrupt(WindSensorPin), isr_rotation, FALLING);
// Serial.println(“Rotations\tMPH”); //use for debug
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(5000);
}
Serial.println(“\nConnected.”);
}
Rotations = 0; // Set Rotations count to 0 ready for calculations
sei(); // Enables interrupts
delay (3000); // Wait 3 seconds to average
cli(); // Disable interrupts
// convert to mp/h using the formula V=P(2.25/T)
// V = P(2.25/3) = P * 0.75
WindSpeed = Rotations * 0.75;
Serial.print(Rotations); Serial.print(“\t\t”);
Serial.println(WindSpeed);
}
// This is the function that the interrupt calls to increment the rotation count
void isr_rotation () {
if ((millis() – ContactBounceTime) > 15 ) { // debounce the switch contact.
Rotations++;
ContactBounceTime = millis();
// 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, WindSpeed, myWriteAPIKey);
if (x == 200) {
Serial.println(“Channel update successful.”);
}
else {
Serial.println(“Problem updating channel. HTTP error code ” + String(x));
}
// change the value
WindSpeed++;
if (WindSpeed > 99) {
WindSpeed = 0;
}
delay(20000); // Wait 20 seconds to update the channel again
}
}
Hi.
I don’t know why you get that issue. I’ve just tested the code again, and it compiled just fine. What’s the Arduino IDE version that you have? Do you have your ESP32 boards add-on updated? Go to Tools> Boards > Boards Manager, search for ESP32, and check if there is a more recent version.
As for your other code, can you share it using google drive, pastebin, dropbox, Github, or other? Copying and pasting the code here messes up with the formatting. If you can share the code, maybe I can take a closer look and try to see what’s going on.
Regards,
Sara
Sara
So I don’t have or know how to use the other apps but I can attach using notepad++ ,I just sent it again using code tags, like I should have the first time. One thing to note here is that by moving the the pinMode(WindSensorPin,INPUT);
attachInterrupt(digitalPinToInterrupt(WindSensorPin), isr_rotation, FALLING);
It compiles just fine but the serial window will show exception 9 and a page of what appears to be stack offsets.Still nothing updates the ThingSpeak ,Also using ESP8266 Lolin 12E developement board,IDE Arduino 1.8.13
Thanks.
#include <ESP8266WiFi.h> #include "secrets.h" #include <math.h> #define WindSensorPin (2) // The pin location of the anemometer sensor volatile unsigned long Rotations; // cup rotation counter used in interrupt routine volatile unsigned long ContactBounceTime; // Timer to avoid contact bounce in interrupt routine float WindSpeed; // speed miles per hour #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; void setup() { Serial.begin(9600); // Initialize serial pinMode(WindSensorPin,INPUT); attachInterrupt(digitalPinToInterrupt(WindSensorPin), isr_rotation, FALLING); while (!Serial) { ; // wait for serial port to connect. Needed for Leonardo native USB port only } WiFi.mode(WIFI_STA); ThingSpeak.begin(client); // Initialize ThingSpeak } //pinMode(WindSensorPin,INPUT); //attachInterrupt(digitalPinToInterrupt(WindSensorPin), isr_rotation, FALLING); // Serial.println("Rotations\tMPH"); //use for debug 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(5000); } Serial.println("\nConnected."); } Rotations = 0; // Set Rotations count to 0 ready for calculations sei(); // Enables interrupts delay (3000); // Wait 3 seconds to average cli(); // Disable interrupts // convert to mp/h using the formula V=P(2.25/T) // V = P(2.25/3) = P * 0.75 WindSpeed = Rotations * 0.75; Serial.print(Rotations); Serial.print("\t\t"); Serial.println(WindSpeed); } // This is the function that the interrupt calls to increment the rotation count void isr_rotation () { if ((millis() - ContactBounceTime) > 15 ) { // debounce the switch contact. Rotations++; ContactBounceTime = millis(); // 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, WindSpeed, myWriteAPIKey); if (x == 200) { Serial.println("Channel update successful."); } else { Serial.println("Problem updating channel. HTTP error code " + String(x)); } // change the value WindSpeed++; if (WindSpeed > 99) { WindSpeed = 0; } delay(20000); // Wait 20 seconds to update the channel again } }
Hi.
I think the problem may be related to the GPIO that the anemometer is connected to.
Try connecting to GPIO 4 instead. And then set that GPIO in the code like this:
#define WindSensorPin 4 // The pin location of the anemometer sensor
Regards,
Sara
Sara ;
Actually funny you would suggest that , change pin to 4 , I had also decided to do that and was againg able to see the serial monitor show some sort of reading. What I got looked normal ;
attempting to connect
connected
starts to show data for windspeed and rotations all at 0 ,spin vane and then get this ;
excp(9)
epc1=0x40104b4
ctx sys
sp:3fffeb00 end: 3fffffb0 offset:01a0
Also still no data to thingspeak
Thanks again
Hi.
I adapted your code, and it is now working. I’ve tested it myself (except that I don’t know if the anemometer values are right – I don’t have one to experiment with).
Here is the code:https://gist.github.com/sarasantos/77538510c47690b381f9042e5baa02b7
Don’t forget to include your ssid, password, API key and channel.
There were several issues with your code. I don’t think you should use delays or timers inside the callback function. Instead, you should create a boolean variable that changes to true when the interrupt happens. Then, in the loop() check the state of that variable and do whatever task you desire (in this case, calculate the windspeed and publish to thingspeak).
Additionally, with the ESP8266, the callback functions for the interrupts should be written in RAM. So, you must use this prefix ICACHE_RAM_ATTR before declaring the function.
Let me know if it is working for you.
Regards,
Sara
So actually I don’t have an anemometer either, this is more of a proof of concept. In the process of building the anemometer, what I actually have is a fidget and I put a magnet on it, I have a MicroSwitch S94A1 hall effect position sensor. I know $60.00 sensor and a $2.00 fidget LOL, kind of ridiculous but it works to reads revolutions and computes speed to the serial monitor.
So actually not sure what the issue is now, but now I am not connecting to my SSID sort of just waiting which is different from before. If I ran this program without your mods it connected but gave me the problems described before with the offset codes. Now it just says connecting but nothing happens been showing attempting to connect for over 10 minutes now. I sent a blank sketch to the ESP and then reloaded the sketch you modified , even changed the baud rate and still not working. At this point I have to thank you for your help but starting to believe that this endeavor is not worth your time .
I was hoping that modifying a single field program that it would work but that’s not the case , yes it compiles but still not working, nothing on the serial monitor. Actually had more information without thingspeak.LOL
Hi Larry.
The code worked just fine for me. I tested it with a “regular” interrupt.
I don’t know what else I can do to help you.
Did you insert your data in the secrets.h fine?
Regards,
Sara
Yes I did , I appriciate all your help, thank you.I took the code as you sent it to github and put in my credentials into a tab called secrets.h , loaded it and complied just fine, but for some unknown reason it will not connect to my ssid. I will have to look at this a bit harder , I believe you that it worked for you.
Just ran the ESP8266WiFi example WiFi scan and it worked fine , it sees the 4 networks I have. I guess the only other thing to try is a different board, thanks again.
Just ran the ThingSpeak example using the program board direct Single field and it went through perfectly, will have to check my code again,thanks