mathworks / thingspeak-arduino

ThingSpeak Communication Library for Arduino, ESP8266 and ESP32
https://thingspeak.com
431 stars 231 forks source link

ThingSpeak.h writeRaw() shows HTTP error code 302 #47

Closed HamAndGreenEggs closed 5 years ago

HamAndGreenEggs commented 5 years ago

I couldn't get the esp32's WriteMultipleFields example to work until I adjusted ThingSpeak.h. Until then it was giving:

Problem updating channel. HTTP error code 302

As a workaround, I added an additional line to my copy of ThingSpeak.h's writeRaw()

        // Post data to thingspeak
        if(!this->client->print("POST /update HTTP/1.1\r\n")) return abortWriteRaw();
        if(!writeHTTPHeader(writeAPIKey)) return abortWriteRaw();
        if(!this->client->print("Content-Type: application/x-www-form-urlencoded\r\n")) return abortWriteRaw();
        if(!this->client->print("Content-Length: ")) return abortWriteRaw();
        if(!this->client->print(postMessage.length())) return abortWriteRaw();
        if(!this->client->print("\r\n\r\n")) return abortWriteRaw();
        if(!this->client->print(postMessage)) return abortWriteRaw();
        if(!this->client->print("\r\n")) return abortWriteRaw();  //possible fix?

I'm too new at this to say whether it's a needed fix or if it's a usage error.

jasontwinters commented 5 years ago

I just double checked the example, it compiles and works as-is for me. Did you change something in the example when this started happening? If you needed to add a \n to get it to work, it's a sign that the actual length of the payload doesn't match the length specified in the POST header. Usually that is caused by a string or char[] missing it's null terminator. For example doing something like this: char value[5] = "12345";

HamAndGreenEggs commented 5 years ago

Thanks for checking this. I found that the behavior was a function of how the controller was connected to the internet. I did further testing and found that when I was connected to:

In these tests, I only changed the WiFi and Password strings, then toggled the patch in/out (I also initially set the channel and key). I don't know how to interpret the difference with T-Mobile--as a seeming weakness of my T-Mobile hotspot, or a robustness of the Verizon hotspot, or something else altogether.

jasontwinters commented 5 years ago

You are the second person in a week to mention problems with a hotspot. I was only testing on Verizon. I'll see if I can find some T-mobile hardware to test with.

jasontwinters commented 5 years ago

The one place it should absolutely work is your WiFi. There still might be something going wrong in your application code. If you want us to take a look at it, strip out anything personal and post it on our forum: https://community.thingspeak.com/forum/

HamAndGreenEggs commented 5 years ago

I submitted the code to the forum in case there's a defect. I also noted that my T-mobile hotspot has problems reading the channel fields.

Maybe this is best directed to the forum since it doesn't seem to be a problem with the library, but as followup to your interest in the hotspot topic, I peaked at the response from t-mobile in getHTTPResponse() in a different "read" sketch and saw this:

HTTP/1.1 302 Found Location: http://offers.t-mobile.com/tethering/upsell.do?source=pcweb Content-Type: text/html; charset=iso-8859-1 Proxy-Connection: close

The document has moved

Thanks

HamAndGreenEggs commented 5 years ago

I'll close this in favor of discussion on the forum as to why some providers and networks seem to not work well.