I have tried to make a sketch that posts a request to a REST-service every time a button is pushed and every time it's released.
I'm a using a Black Widow-board with wifi.
Sadly it doesn't work as i excpected. When i push the button (and hold it) the POSTrequest.submit() is called, but the createBody() function is not called by the POSTrequest instance. So nothing happens. But when i release the button, a new POSTrequest.submit() is called and now the createBody() function is called and the POSTrequest is executed. Sometimes a button-push and -release results in a single http post and sometimes it results in two posts..?
It seems that the POSTrequest instance won't process a request as long as the button is pressed...?
Can someone explain this behavior?
#include <WiServer.h>
#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2
// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {192,168,1,2}; // IP address of WiShield
unsigned char gateway_ip[] = {192,168,1,1}; // router or gateway IP address
//unsigned char local_ip[] = {192,168,100,2}; // IP address of WiShield
//unsigned char gateway_ip[] = {192,168,100,1}; // router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0}; // subnet mask for the local network
const prog_char ssid[] PROGMEM = {"XXXX"}; // max 32 bytes
unsigned char security_type = 3; // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2
// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"XXXX"}; // max 64 characters
prog_uchar wep_keys[] PROGMEM = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, // Key 0
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 1
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 2
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // Key 3
};
unsigned char wireless_mode = WIRELESS_MODE_INFRA;
unsigned char ssid_len;
unsigned char security_passphrase_len;
void printData(char* data, int len)
{
Serial.println("printData");
while (len-- > 0) {
Serial.print(*(data++));
}
}
int pin2State = LOW; // variable for reading the pushbutton status
const int pin2 = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
char deviceId[] = "1";
char hostName[] = "sensor.zonosoft.com";
//char hostName[] = "192.168.1.104";
char url[] = "/SensorService.svc/SaveMeasurement";
uint8 ip[] = {195,128,175,40};
//uint8 ip[] = {192,168,1,104};
POSTrequest sendInfo(ip, 80, hostName, url, createBody);
String _dataToSend;
void setup()
{
Serial.begin(9600);
Serial.println("Setup...");
WiServer.init(NULL);
WiServer.enableVerboseMode(true);
// sendInfo.setReturnFunc(printData);
pinMode(ledPin, OUTPUT);
pinMode(pin2, INPUT);
Serial.println("Setup finished");
}
void createBody()
{
Serial.println("createBody");
Serial.println(_dataToSend);
WiServer.print(_dataToSend);
}
void loop()
{
int newPin2State = digitalRead(pin2);
if (pin2State != newPin2State)
{
Serial.println("pin2 changed");
_dataToSend = "{\"Mt\":\"";
_dataToSend += + deviceId;
_dataToSend += ";2"; //pin2
if (newPin2State == HIGH)
_dataToSend += ";1";
else
_dataToSend += ";0";
_dataToSend += "\"}";
sendInfo.submit();
Serial.println("After submit");
digitalWrite(ledPin, pin2State);
}
WiServer.server_task();
delay(10);
if (pin2State != newPin2State)
{
pin2State = newPin2State;
Serial.println("After WiServer.server_task()");
}
}