This week, I really struggled with how / what connecting my Arduino to a Node server would look like, so I decided to go through and look at my classmates’ blogs to see if I could glean a bit more understanding.
After reading through the list of the homework blog entries, I found Viola’s (which uses POST and Express) and Yonaton’s (which uses GET and http module) quite helpful.
A couple of our classmates brought up having problems with -2 error messages was something that was an issue for them and found that issue addressed in Yonny’s blog. It appears that it’s due to using HTTPS to connect with certain servers—in our case, on Glitch. Here is a link to Yonni’s GLITCH SERVER CODE: https://glitch.com/edit/#!/yoserver?path=server.js%3A15%3A3 . Here is a link to his BLOG POST: https://wp.nyu.edu/yonatanrozin/connected-devices-week-9/
From his notes and my experience, the biggest problem going on regarding the -2 was that Glitch requires HTTPS connections (which means using a different Arduino sketch than non-secure http communication.) He started with the SimpleGet sketch, but it really calls for just a simple HTTP connection, so he had to modify the code for that.
For his Arduino sketch, because there isn’t an existing URL for the data, he hard-coded the temperature and I did so when I replicated it. When you upload the sketch, wait a minute and then refresh the Glitch server and it will show the update. If you want to do both humidity and temperature, the line would look like this: client.get(“/?temp=234&humidity=678”); [rather than this: client.get(“/?temp=123”);]
—————————————————————————————————
Here is a link to a copy of her code I made of Viola’s GLITCH SERVER CODE: https://glitch.com/edit/#!/violas-node-try-conndev?path=server.js%3A26%3A0
Here is a link to her BLOG POST: https://itp.leqihe.online/category/spring-2021/connected-device/
I made a copy/remix of Viola’s Glitch sketch to play with, but wasn’t able to get her sketch to work at first so I moved to Yonny’s… the code I had modified for her Arduino sketch was almost correct, but I kept getting a 400 error (bad request) in the Serial Monitor. I double-checked everything, even that I was using httpS in the server name, instead of http; no go… then I tried taking out the HTTPS:// -part, and it worked! (Jeez.)
So, if you’re having problems with the -2 error, try loading the TestHttpSSLClient Arduino sketch, modifying it using your Glitch server’s address without the ‘https://’ part, it may just solve the issue. From playing with this code, it appears that if you use POST, it sends the data to a file on the sever (or somewhere) that holds it until you request it. So, for Viola’s POST code, I hard coded a couple of values in there and then later hit the button on the Glitch Server’s page and it worked!
Going through others’ code and reading about their experiences really helped me get a better sense of the material. I’ll be doing it more!
Special thanks to Yonatan & Viola!