Results for
I followed the instructions here: Bulk-Update Using an Arduino or an ESP8266
And came up the following code for GSM module:
const char apn[] = "www";
const char gprsUser[] = "";
const char gprsPass[] = "";const char simPIN[] = "";
const char server[] = "api.thinkspeak.com";
const int port = 80;char jsonBuffer[500] = "["; // Initialize the jsonBuffer to hold data
// TTGO T-Call pins
#define MODEM_RST 5
#define MODEM_PWKEY 4
#define MODEM_POWER_ON 23
#define MODEM_TX 27
#define MODEM_RX 26 #define SerialMon Serial
#define SerialAT Serial1
#define TIMEOUT 5000
// Configure TinyGSM library
#define TINY_GSM_MODEM_SIM800 // Modem is SIM800
#define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb #include <TinyGsmClient.h>
#ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, SerialMon);
TinyGsm modem(debugger);
#else
TinyGsm modem(SerialAT);
#endifTinyGsmClient client(modem);
#define IP5306_ADDR 0x75
#define IP5306_REG_SYS_CTL0 0x00 /* Collect data once every 15 seconds and post data to ThingSpeak channel once every 2 minutes */
unsigned long lastConnectionTime = 0; // Track the last connection time
unsigned long lastUpdateTime = 0; // Track the last update time
const unsigned long postingInterval = 60L * 1000L; // Post data every 2 minutes
const unsigned long updateInterval = 15L * 1000L; // Update once every 15 seconds void setup() {
SerialMon.begin(115200); // Set modem reset, enable, power pins
pinMode(MODEM_PWKEY, OUTPUT);
pinMode(MODEM_RST, OUTPUT);
pinMode(MODEM_POWER_ON, OUTPUT);
digitalWrite(MODEM_PWKEY, LOW);
digitalWrite(MODEM_RST, HIGH);
digitalWrite(MODEM_POWER_ON, HIGH); // Set GSM module baud rate and UART pins
SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);
delay(3000); SerialMon.println("Initializing modem...");
modem.init();// Configure the wake up source as timer wake up
SerialMon.print("Connecting to APN: ");
SerialMon.print(apn);
if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
SerialMon.println(" fail");
}
else {
SerialMon.println(" OK"); SerialMon.print("Connecting to ");
SerialMon.print(server);
if (!client.connect(server, port)) { SerialMon.println(" Fail");
}
else {
SerialMon.println(" OK");
}
}}
void loop() {
// If update time has reached 1 second, then update the jsonBuffer
if (millis() - lastUpdateTime >= updateInterval) {
updatesJson(jsonBuffer);
}}
// Updates the josnBuffer with data
void updatesJson(char* jsonBuffer){
/* JSON format for updates paramter in the API
* This examples uses the relative timestamp as it uses the "delta_t". You can also provide the absolute timestamp using the "created_at" parameter
* instead of "delta_t".
* "[{\"delta_t\":0,\"field1\":-70},{\"delta_t\":3,\"field1\":-66}]"
*/
// Format the jsonBuffer as noted above
strcat(jsonBuffer,"{\"delta_t\":");
unsigned long deltaT = (millis() - lastUpdateTime)/1000;
size_t lengthT = String(deltaT).length();
char temp[4];
String(deltaT).toCharArray(temp,lengthT+1);
strcat(jsonBuffer,temp);
strcat(jsonBuffer,",");
int h = hallRead();
strcat(jsonBuffer, "\"field1\":");
lengthT = String(h).length();
String(h).toCharArray(temp,lengthT+1);
strcat(jsonBuffer,temp);
strcat(jsonBuffer,"},");
// If posting interval time has reached 2 minutes, update the ThingSpeak channel with your data
if (millis() - lastConnectionTime >= postingInterval) {
size_t len = strlen(jsonBuffer);
jsonBuffer[len-1] = ']';
httpRequest(jsonBuffer);
}
lastUpdateTime = millis(); // Update the last update time
} // Updates the ThingSpeakchannel with data
void httpRequest(char* jsonBuffer) {
/* JSON format for data buffer in the API
* This examples uses the relative timestamp as it uses the "delta_t". You can also provide the absolute timestamp using the "created_at" parameter
* instead of "delta_t".
* "{\"write_api_key\":\"YOUR-CHANNEL-WRITEAPIKEY\",\"updates\":[{\"delta_t\":0,\"field1\":-60},{\"delta_t\":15,\"field1\":200},{\"delta_t\":15,\"field1\":-66}]
*/
// Format the data buffer as noted above
char data[500] = "{\"write_api_key\":\"XXXXXXXXXXXXXXXX\",\"updates\":"; // Replace YOUR-CHANNEL-WRITEAPIKEY with your ThingSpeak channel write API key
strcat(data,jsonBuffer);
strcat(data,"}");
// Close any connection before sending a new request
client.stop();
String data_length = String(strlen(data)+1); //Compute the data buffer length
Serial.println(data);
// POST data to ThingSpeak
if (client.connect(server, 80)) {
client.println("POST /channels/1300373/bulk_update.json HTTP/1.1"); // Replace YOUR-CHANNEL-ID with your ThingSpeak channel ID
client.println("Host: api.thingspeak.com");
client.println("User-Agent: mw.doc.bulk-update (ESP8266)");
client.println("Connection: close");
client.println("Content-Type: application/json");
client.println("Content-Length: "+data_length);
client.println();
client.println(data);
String answer=getResponse();
Serial.println( answer );
}
else {
Serial.println("Failure: Failed to connect to ThingSpeak");
}
delay(250); //Wait to receive the response
client.parseFloat();
String resp = String(client.parseInt());
Serial.println("Response code:"+resp); // Print the response code. 202 indicates that the server has accepted the response
jsonBuffer[0] = '['; //Reinitialize the jsonBuffer for next batch of data
jsonBuffer[1] = '\0';
lastConnectionTime = millis(); //Update the last conenction time
} String getResponse(){
String response;
long startTime = millis(); delay( 200 );
while ( client.available() < 1 && (( millis() - startTime ) < TIMEOUT ) ){
delay( 5 );
} if( client.available() > 0 ){ // Get response from server.
char charIn;
do {
charIn = client.read(); // Read a char from the buffer.
response += charIn; // Append the char to the string response.
} while ( client.available() > 0 );
}
client.stop(); return response;
}The Response code I was getting initially was "0" Upon consulting the forum I added a new function to get response and here is the message it produced:
HTTP/1.1 405 Not Allowed Server: nginx Date: Wed, 17 Feb 2021 06:27:54 GMT Content-Type: text/html Content-Length: 8911 Connection: close ETag: "5e5304fb-22cf" X-DIS-Request-ID: 82c8c8535e18295aaeec025d18d14375 Set-Cookie: dis-remote-addr=1.39.31.10 Set-Cookie: dis-timestamp=2021-02-16T22:27:54-08:00 Set-Cookie: dis-request-id=82c8c8535e18295aaeec025d18d14375 X-Frame-Options: sameorigin
Please help me understand this error and rectify the code. P.S. I was able to run the code using WiFi module of the ESP32, so is this something to do with 256 limit? If so, how do I rectify it?