How to send HTTP request to external device from within APP. | ThingSpeak API | Forum

Avatar

Please consider registering
Guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed sp_TopicIcon
How to send HTTP request to external device from within APP.
No permission to create posts
January 8, 2019
7:19 pm
Avatar
CoupePete

Silver
Members
Forum Posts: 6
Member Since:
January 8, 2019
sp_UserOfflineSmall Offline

HI - I am creating my first APP on Thingspeak and its almost complete but I am now stuck. I am running a daily program to scrape some weather data off the web, calculating an irrigation amount and need to send a command to update the "watering percentage" to a web connected Irrigation Controller (Open Sprinkler). The command I need to send is :-

http://irrigation.duckdns.org:150/co?pw=a6d82bced798de3def1e9bbb4983225c&o23="wateringpercentage" - details have been changed for obvious reasons, but this command works from my browser and this command updates the required parameter in the controller.

Watering percentage is the calculated percentage of the irrigation controllers preprogrammed irrigation time.

Is there a command that allows me to do this from the APP directly and use the variable holding the wateringpercentage calculation result ? From what I have been able to understand it seems like the only way is to write the wateringpercentage to a new channel, use Thing HTTP to setup the command and then trigger ThingHttp using TimeControl or React which seems kind of clunky ?

Any advice appreciated.

January 10, 2019
1:57 pm
Avatar
cstapels
Moderator
Members


Moderators
Forum Posts: 610
Member Since:
March 7, 2017
sp_UserOfflineSmall Offline

Welcome to ThingSpeak! We are happy to hear you are developing new apps.
You can use a MATLAB analysis (or visualization) and the command webwrite() to perform the command. You can leave the parameters in the url or send them separately after commas. See https://www.mathworks.com/help/matlab/ref/webwrite.html

Let us know if you get it working. I have a plant monitor in my office, but I need to set up some automatic irrigation.
https://www.mathworks.com/help/thingspeak/MoistureMonitor.html

January 10, 2019
11:51 pm
Avatar
CoupePete

Silver
Members
Forum Posts: 6
Member Since:
January 8, 2019
sp_UserOfflineSmall Offline

Thanks for the response. ConfusedFrom what I can understand the API for Open Sprinkler will only work with a GET request and wont work with a POST. When playing around with POSTMAN using GET seems to be the only way I can get a successful response. I think that means I need to use webread(); because that appends the request parameters to the url which is the same format as a GET request. Does that sound correct ?

I use the following code in thingSPeak :-

opensprinklerurl = 'http://irrigation.duckdns.org:351/jo?';
ChgWL = 0
ChgWL = webread(opensprinklerurl, 'pw', 'a6d82bced638de3debb4983225c', 'o23', '102');

And i get following error messages :- Confused

Error using readContentFromWebService (line 46)
The server returned the status 403 with message "Forbidden" in response to the request to URL http://irrigation.duckdns.org:351/co?pw=a6d82bced638de3debbb4983225c&o23=102.

Error in webread (line 125)
[varargout{1:nargout}] = readContentFromWebService(connection, options);

Error in Scrape web ETo Sheoaks (line 53)
ChgWL = webread(opensprinklerurl, 'pw', 'a6d82bced638de3debbb4983225c', 'o23', '102');

If i cut and past the URL out of the error message into my browser and hit return it completes the request successfully. Yet the webread() does not work. ??? Obviously i have modified the details above for security reasons.

Any help appreciated.

January 11, 2019
5:06 am
Avatar
rw950431

Top
Members
Forum Posts: 279
Member Since:
January 30, 2014
sp_UserOfflineSmall Offline

Does irrigation.duckdns.org resolve to an address accessible from outside your local area network?

If you visit the URL in a browser with the web developers console open to the network tab what do you see? Is there a sneaky redirect happening?

Do you have access to a command-line tool like wget or curl on your PC? What happens if you use something like that to fetch your URL- does it differ?

Is it possible that the site rejects ThingSpeak based on its user-agent string?

Do you have access to logs from irrigation.duckdns.org?

January 12, 2019
7:45 am
Avatar
CoupePete

Silver
Members
Forum Posts: 6
Member Since:
January 8, 2019
sp_UserOfflineSmall Offline

Thanks for the response. Answers below as best I can.

Does irrigation.duckdns.org resolve to an address accessible from outside your local area network? Yes
It resolves to an address that is part of my local network, but can be accessed anywhere from the web. I can only access it using irrigation.duckdns.org from another network - but not from within the local network the system is on.

If you visit the URL in a browser with the web developers console open to the network tab what do you see? Is there a sneaky redirect happening? ------- I don't think so but am not really sure what to look for. Under the network tab it shows a bunch of HTTP GET commands like I am trying to use.

Do you have access to a command-line tool like wget or curl on your PC? What happens if you use something like that to fetch your URL- does it differ?------ I assume POSTMAN has the same functionality as CURL and using a GET command with the exact same text it gives the same response.

Is it possible that the site rejects ThingSpeak based on its user-agent string? ------- I don't know. Any suggestions on how I can check this ?

Do you have access to logs from irrigation.duckdns.org? -----Not from the device itself if that is what you mean.

January 12, 2019
11:19 pm
Avatar
rw950431

Top
Members
Forum Posts: 279
Member Since:
January 30, 2014
sp_UserOfflineSmall Offline

Perhaps try changing the user-agent string from your browser (see https://www.howtogeek.com/113439/how-to-change-your-browsers-user-agent-without-installing-any-extensions/ for help with this)- see if this causes the same 403 error that you get from ThingSpeak.

January 14, 2019
2:18 pm
Avatar
cstapels
Moderator
Members


Moderators
Forum Posts: 610
Member Since:
March 7, 2017
sp_UserOfflineSmall Offline

I found this note in the open sprinkler API doc: (https://openthings.freshdesk.com/support/solutions/articles/5000716363-os-api-documents)

The password is MD5 hashed by the app / web UI and therefore when using the API commands the password should be MD5 hashed too (all lower-case). You can find online MD5 hash tools to convert plaintext password to MD5.

Could this be related to your issue?

January 14, 2019
5:14 pm
Avatar
CoupePete

Silver
Members
Forum Posts: 6
Member Since:
January 8, 2019
sp_UserOfflineSmall Offline

Thanks again for the suggestions. The request I am sending has the password MD5 hashed and if I copy the url from the error message from thingspeak and paste it in my browser it works, so I am confident that is correct.

I have used both Edge and Chrome browsers and had success. I will look into the user agent string and see if that can help. I might also try a couple of different ways of writing url (as an ip address and a different dns service) and see if that makes a difference.

January 17, 2019
7:48 am
Avatar
CoupePete

Silver
Members
Forum Posts: 6
Member Since:
January 8, 2019
sp_UserOfflineSmall Offline

Have tried 3 different version of the url and none of them made any difference. 2 different DNS services and one using the public IP. All worked with POSTMAN before trying them so not sure where to go next. Changing the user agent is about the only idea I have left - does anyone know what user agent string/ details that thingSpeak uses so I can try that in a browser and see if that makes a difference ? Google did not seem to find that info.

January 17, 2019
3:17 pm
Avatar
cstapels
Moderator
Members


Moderators
Forum Posts: 610
Member Since:
March 7, 2017
sp_UserOfflineSmall Offline

ThingSpeak does not require a specific user-agent at present, but I think rw950431 was suggesting that the irrigation site might be checking the user agent to be sure it matches something they require or even perhaps they match the password with the agent.

If it was required though, I think it would also be required in POSTMAN.

Did you try appending the parameters to the URL?

opensprinklerurl = 'http://irrigation.duckdns.org:351/jo?pw=a6d82bced638de3debbb4983225c&o23=102';
webread(opensprnklerurl);

Perhaps the 102 is being characterized as a string and it needs to be a number. So when you write '102' the API gets confused?

January 24, 2019
6:19 am
Avatar
CoupePete

Silver
Members
Forum Posts: 6
Member Since:
January 8, 2019
sp_UserOfflineSmall Offline

Been thinking about the suggestion above, but when I type the opensprinkler url in my browser I assume the command is being sent as a string to the open sprinkler, and Open Sprinkler successfuly performs the command.

I assume when i execute the command shown below in thingSpeak it is also sending a string as per the browser.

opensprinklerurl = 'http://irrigation.duckdns.org:351/jo?pw=a6d82bced638de3debbb4983225c&o23=102';
webread(opensprnklerurl);

All the examples is the open Sprinkler API document seem to use strings if i understand it correctly.

Any thought on how I would test a string vs number ?

January 28, 2019
2:24 pm
Avatar
cstapels
Moderator
Members


Moderators
Forum Posts: 610
Member Since:
March 7, 2017
sp_UserOfflineSmall Offline

After some thought, I think the string thing is a dead end, as you suggested. You tested in a browser and the URL encoding is string like for the browser so I agree that's probably not it. I assume adding your parameters to the URL didn't help. Ill hope to give it a try myself before spring, and Ill post here if I get it working. Please let us know if you get it going.

Forum Timezone: America/New_York

Most Users Ever Online: 166

Currently Online:
28 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 272

Vinod: 240

piajola: 95

turgo: 70

vespapierre: 63

Adarsh_Murthy: 62

Member Stats:

Guest Posters: 1

Members: 8665

Moderators: 1

Admins: 2

Forum Stats:

Groups: 3

Forums: 14

Topics: 1600

Posts: 5760

Newest Members:

adeeeban, theweddingties, Vasant Gosai, pavithra, shivaniprakash, Akeyan

Moderators: cstapels: 460

Administrators: Hans: 405, lee: 457