Bulk upload not working with static IP | 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
Bulk upload not working with static IP
No permission to create posts
January 30, 2018
1:29 pm
Avatar
Makeorbrake
New Member
Members
Forum Posts: 1
Member Since:
January 30, 2018
sp_UserOfflineSmall Offline

I can get the bulk upload to work fully when connecting to the wifi "the normal way" (with only SSID and password) but when I try and use static IP instead I do get connection to wifi, but I do not get a client.connect true anymore so cant upload to Thingspeak. Anyone that got it to work and might be able to help. Have used the same code for connecting to wifi with "normal" upload without problem. Thanks in advance.

The code (removed connection data and channel no and key):

/* This example shows how to bulk update a ThingSpeak channel using an Arduino MKR1000 or an ESP8266*/

//#include<Ethee3ernetClient.h> //Uncomment this library to work with ESP8266
#include<ESP8266WiFi.h> //Uncomment this library to work with ESP8266

char jsonBuffer[2500] = "["; // Initialize the jsonBuffer to hold data

char ssid[] = ""; // Your network SSID (name)
char pass[] = ""; // Your network password
WiFiClient client; // Initialize the WiFi client library

char server[] = "api.thingspeak.com"; // ThingSpeak Server

/* 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 1 minutes
//const unsigned long updateInterval = 15L * 1000L; // Update once every 15 seconds

 

IPAddress ip(192, 168, 1, 159); // Original with SHT31
IPAddress gateway(); // Removed to post to forum
IPAddress subnet();
IPAddress dns1();
IPAddress dns2();
byte mac[6]; // the MAC address of wemos

uint8_t router_mac[6] = { }; // Your router's wifi mac address
uint8_t router_channel = 10; // Your router's wifi channel

 

void setup() {
Serial.begin(115200);
//connectwifi(); // Works
connectstatic(); // does not work
}

 

void connectwifi(){
// Attempt to connect to WiFi network
while (WiFi.status() != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
delay(10000); // Wait 10 seconds to connect
}
Serial.println("Connected to wifi");
printWiFiStatus(); // Print WiFi connection information
}

void connectstatic(){
// Attempt to connect to WiFi network
while (WiFi.status() != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.config(ip, gateway, subnet, dns1, dns2);
delay(10);
WiFi.begin(ssid, pass, router_channel, router_mac);
delay(10000); // Wait 10 seconds to connect
}
Serial.println("Connected to wifi");
printWiFiStatus(); // Print WiFi connection information
}

void loop() {
updatesJson(jsonBuffer);
for (int i = 0; i <= 120 ; i++) { // la in för annars kommer det för tätt. löses automatiskt senare med deep sleep
delay(500);
}
}
// Updates the jsonBuffer with data
void updatesJson(char* jsonBuffer){
/* JSON format for updates paramter in the API
* This example uses the relative timestamp as it uses the "delta_t". If your device has a real-time clock, you can provide the absolute timestamp using the "created_at" parameter
* instead of "delta_t".
* "[{\"delta_t\":0,\"field1\":-70},{\"delta_t\":15,\"field1\":-66}]"
*/
// Format the jsonBuffer as noted above

Serial.print("Start build json");
Serial.println(millis());

for (int i = 0; i <= 31 ; i++) {

strcat(jsonBuffer,"{\"delta_t\":"); // Starta en rad i jsonBuffer
unsigned long deltaT = 20; // beräkna deltaT
size_t lengthT = String(deltaT).length(); // beräkna längden på deltaT
char temp[4]; // skapa parameter temp
String(deltaT).toCharArray(temp,lengthT+1); // gör om deltaT till String och till temp
strcat(jsonBuffer,temp); // lägg in strängen temp i jsonBuffer
strcat(jsonBuffer,","); // lägga in , i jsonBuffer
long rssi = i; // skapa parameter rssi
strcat(jsonBuffer, "\"field1\":"); // lägg in prefix för första parametern
lengthT = String(rssi).length(); // Ta reda på längden på parameter 1
String(rssi).toCharArray(temp,lengthT+1); // gör om till sträng och in i temp
strcat(jsonBuffer,temp); // in i json
strcat(jsonBuffer,"},"); // avsluta den delen

//Serial.print("json: ");
//Serial.println(jsonBuffer);
yield();
}
size_t len = strlen(jsonBuffer);
jsonBuffer[len-1] = ']'; // byter sista , mot ]
//Serial.print("Total json: ");
//Serial.println(jsonBuffer);
Serial.print("length json: ");
Serial.println(String(jsonBuffer).length());
httpRequest(jsonBuffer);

}

// Updates the ThingSpeakchannel with data
void httpRequest(char* jsonBuffer) {
/* JSON format for data buffer in the API
* This example uses the relative timestamp as it uses the "delta_t". If your device has a real-time clock, 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}]
*/
Serial.print("Start send: ");
Serial.println(millis());
// Format the data buffer as noted above
char data[2500] = "{\"write_api_key\":\"XXX\",\"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
// POST data to ThingSpeak
if (client.connect(server, 80)) {
client.println("POST /channels/YOUR-CHANNEL-ID/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 (Arduino ESP8266)");
client.println("Connection: close");
client.println("Content-Type: application/json");
client.println("Content-Length: "+data_length);
client.println();
client.println(data);
}
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 connection time
Serial.print("End send: ");
Serial.println(lastConnectionTime);
}

void printWiFiStatus() {
// Print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());

// Print your device IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);

// Print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online:
21 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 261

Vinod: 196

piajola: 85

turgo: 70

vespapierre: 63

Adarsh_Murthy: 62

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

Newest Members:

terranceqh2, DianeEmids, Alanawaype, uqovitay, ufijifode, manish01

Administrators: Hans: 387, lee: 457