"WriteMultipleFields" gives HTTP Error Code 302 on T-Mobile hotspot, but OK on Verizon. | 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
"WriteMultipleFields" gives HTTP Error Code 302 on T-Mobile hotspot, but OK on Verizon.
No permission to create posts
December 4, 2018
11:26 pm
Avatar
hamAndGreenEggs
New Member
Members
Forum Posts: 2
Member Since:
December 4, 2018
sp_UserOfflineSmall Offline

Hello, I tried to run the ThingSpeak example sketch called WriteMultipleFields on an esp32 labeled espduino-32 by connecting my espduino-32 to a T-Mobile hotspot and to a Verizon hotspot. It works fine on my Verizon hotspot, but doesn't upload my channel from the T-mobile hotspot unless I add a kludgey workaround to the library as shown below in the last line of this block:

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

")) return abortWriteRaw();
if(!this->client->print(postMessage)) return abortWriteRaw();
if(!this->client->print("
")) return abortWriteRaw(); //kludge workaround

It doesn't seem to matter what the kludge adds. I can add the letter "X" and T-Mobile will begin working.

I'm about to give up on my T-mobile hotspot, since the reading of fields also fails, though I'm curious where the error lies.

Does my sketch have a defect? I think I'm being true to the example code, but here it is, followed by the contents of secrets.h:

//#define PRINT_DEBUG_MESSAGES
/*
WriteMultipleFields

Description: Writes values to fields 1,2,3,4 and status in a single ThingSpeak update every 20 seconds.

Hardware: ESP32 based boards

!!! IMPORTANT - Modify the secrets.h file for this project with your network connection and ThingSpeak channel details. !!!

Note:
- Requires installation of EPS32 core. See https://github.com/espressif/arduino-esp32/blob/master/docs/arduino-ide/boards_manager.md for details.
- Select the target hardware from the Tools->Board menu
- This example is written for a network using WPA encryption. For WEP or WPA, change the WiFi.begin() call accordingly.

ThingSpeak ( https://www.thingspeak.com ) is an analytic IoT platform service that allows you to aggregate, visualize, and
analyze live data streams in the cloud. Visit https://www.thingspeak.com to sign up for a free account and create a channel.

Documentation for the ThingSpeak Communication Library for Arduino is in the README.md folder where the library was installed.
See https://www.mathworks.com/help/thingspeak/index.html for the full ThingSpeak documentation.

For licensing information, see the accompanying license file.

Copyright 2018, The MathWorks, Inc.
*/

#include "ThingSpeak.h"
#include "secrets.h"
#include

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;

// Initialize our values
int number1 = 0;
int number2 = random(0,100);
int number3 = random(0,100);
int number4 = random(0,100);
String myStatus = "";

void setup() {
Serial.begin(115200); //Initialize serial

WiFi.mode(WIFI_STA);
ThingSpeak.begin(client); // Initialize ThingSpeak
}

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("
Connected.");
}

// set the fields with the values
ThingSpeak.setField(1, number1);
ThingSpeak.setField(2, number2);
ThingSpeak.setField(3, number3);
ThingSpeak.setField(4, number4);

// figure out the status message
if(number1 > number2){
myStatus = String("field1 is greater than field2");
}
else if(number1 99){
number1 = 0;
}
number2 = random(0,100);
number3 = random(0,100);
number4 = random(0,100);

delay(20000); // Wait 20 seconds to update the channel again
}

// Use this file to store all of the private credentials
// and connection details

//public wifi w/ captive login seems to fail w/ or w/o patch, though manual query update works
//("Problem updating channel. HTTP error code -303")
//#define SECRET_SSID "WxxxxxxxWiFi" // replace MySSID with your WiFi network name
//#define SECRET_PASS "xxxxx" // replace MyPassword with your WiFi password
//-- t-mobile may require a patch to work else 302 -- a trailing
(or seemingly any character) in library
//"Problem updating channel. HTTP error code 302" unless "patch"
//#define SECRET_SSID "tmobile iot" // replace MySSID with your WiFi network name
//#define SECRET_PASS "xxxxxxxxx" // replace MyPassword with your WiFi password
#define SECRET_SSID "verizon iot" // replace MySSID with your WiFi network name
#define SECRET_PASS "xxxxxxxx" // replace MyPassword with your WiFi password

#define SECRET_CH_ID 6xxxx0 // replace 0000000 with your channel number
#define SECRET_WRITE_APIKEY "FxxxxxxxxxxxxxxS" // replace XYZ with your channel write API Key

Thanks

December 5, 2018
12:28 pm
Avatar
JasonW

MathWorks
Members
Forum Posts: 40
Member Since:
October 21, 2016
sp_UserOfflineSmall Offline

Your code looks correct. Based on your comment on github, it appears that the T-Mobile hotspot is attempting to redirect the request, possibly to act as a man-in-the-middle. The library doesn't account for that. It's curious you've figured out a work-around. I'm not exactly sure what is going on there.

Using TLS/SSL is fairly straightforward on an ESP32. Give this a try and see if works any better, you can use the same secrets.h file:

#include "ThingSpeak.h"
#include "secrets.h"
#include <WiFiClientSecure.h>

char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password

WiFiClientSecure client;

unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;

// Initialize our values
int number1 = 0;
int number2 = random(0,100);
int number3 = random(0,100);
int number4 = random(0,100);
String myStatus = "";

void setup() {
Serial.begin(115200); //Initialize serial

WiFi.mode(WIFI_STA);
ThingSpeak.begin(client, "api.thingspeak.com", 443); // Initialize ThingSpeak
}

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.
Serial.print(".");
delay(5000);
}
Serial.println("
Connected.");
}

// set the fields with the values
ThingSpeak.setField(1, number1);
ThingSpeak.setField(2, number2);
ThingSpeak.setField(3, number3);
ThingSpeak.setField(4, number4);

// figure out the status message
if(number1 > number2){
myStatus = String("field1 is greater than field2");
}
else if(number1 < number2){
myStatus = String("field1 is less than field2");
}
else{
myStatus = String("field1 equals field2");
}

// set the status
ThingSpeak.setStatus(myStatus);

// write to the ThingSpeak channel
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if(x == 200){
Serial.println("Channel update successful.");
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}

// change the values
number1++;
if(number1 > 99){
number1 = 0;
}
number2 = random(0,100);
number3 = random(0,100);
number4 = random(0,100);

delay(20000); // Wait 20 seconds to update the channel again
}

December 5, 2018
10:12 pm
Avatar
hamAndGreenEggs
New Member
Members
Forum Posts: 2
Member Since:
December 4, 2018
sp_UserOfflineSmall Offline

I tried the above code that used WiFiClientSecure.h. It worked in 1 of my 6 configurations--the verizon hotspot without the kludge. Here are the 6 outcomes:

Without "patch"/kludge
-------------------------------------
    Verizon: success
    TMobile: hung/stalled* for 1/2 hour+ till finally: Problem updating channel. HTTP error code -301
    Public wifi: HTTP Error Code 302

With "Patch"/kludge
-------------------------------------
    Verizon: HTTP Error Code -302
    TMobile: hung/stalled* for long time, I didn't finish waiting
    Public wifi: HTTP Error Code -302

*The sketch appeared stalled but was working on loop where it was trying to connect to thingspeak.

From this, I gather that my would-be
"patch" is unhealthy for an otherwise healthy Verizon connection.
 
 
 
 
 
----------------------------------------------------
Extra info based on debug definition:
Using the debug definition in thingspeak.h, it appears tmobile's stall occured within ConnectThingSpeak() on it's sub-callto client->connect() here:
if(NULL != this->customHostName)
{
// Connect to the server on port 80 (HTTP) at the URL address
#ifdef PRINT_DEBUG_MESSAGES
Serial.print(" Connect to ");Serial.print(this->customHostName);Serial.print(" ...");
#endif
connectSuccess = client->connect(customHostName,this->port);
---------------------------------------------------

Forum Timezone: America/New_York

Most Users Ever Online: 166

Currently Online:
33 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:

Epiphoneejf, Bbet1814On, Konstantintus, inalv69, bryancv1, priscillaql69

Moderators: cstapels: 460

Administrators: Hans: 405, lee: 457