Posting data to a database using Photon

I’m working on a project to post data to a database. each device has an ID and a User has an ID which is associated with the device location. We have a code put together but it’s not posting. Do any of you have any advice. Here’s the code…

// This #include statement was automatically added by the Particle IDE.
#include "HttpClient.h"

// This #include statement was automatically added by the Particle IDE.
//#include "HttpClient/HttpClient.h"

int led2 = D7;
int Sig1 = D1;
int Sig2 = D2;


HttpClient http;

// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {
    //  { "Accept" , "application/json" },
    { "Accept" , "*/*"},
   // { "content-Type", "application/x-www-form-urlencoded" },
    { NULL, NULL } // NOTE: Always terminate headers will NULL
};

http_request_t request;
http_response_t response;


void setup()

{
    Serial.begin(9600);
    Serial.println("Ready!");
    pinMode(Sig1, INPUT);
    pinMode(Sig2, INPUT);
    digitalWrite(Sig1, LOW);
    digitalWrite(Sig2, LOW);
    pinMode(led2, OUTPUT);
    digitalWrite(led2, LOW);
    PostFormData();

    
}

void loop()
{
    if(Sig1 == HIGH)
	{
	PostFormData();
	}
	else if(Sig2 == HIGH)
	{
	   PostFormData2();
	}


}

void PostFormData(){
    Serial.println("Sending data to Qrimp!");
    request.hostname = "l-wasp.qrimp.net";
    request.port = 80;
    //request.path = "db.aspx?t=TransmisssionLog&Device=1234A&Transmitter=5678A&o=5";
    request.path = "db.aspx?t=TransmisssionLog&Device=1234A&Transmitter=5678A&o=5";
    request.body = "";

    
    http.get(request, response, headers);
    digitalWrite(Sig1, LOW);
    Serial.println(response.body);
   
 
}

void PostFormData2(){
    request.hostname = "l-wasp.qrimp.net";
    request.port = 80;
    request.path = "db.aspx";
    request.body = "t=TransmisssionLog&Device=1234B&Transmitter=5678B&o=5";
    
    http.post(request, response, headers);
    
}

We need the proper syntax for the request Body and Request Path… Pretty sure the rest is correct but I need to know if any of you have done this before and have any advice for us. Thanks in advance!
Anthony

Spelling mistake: you have three ‘s’ in a row in the word ‘Transmisssion’.

I’ve not used httpclient, but I wonder whether you need a ‘/’ at the start of the path.

I think you need to use digitalRead in your ‘if’ conditions. https://docs.particle.io/reference/firmware/photon/#digitalread-

2 Likes

You might also want to add a delay somewhere since you’re currently posting non-stop which may, or may not, be problematic.

1 Like

@Anthony, what is the purpose of writing a LOW to an INPUT port?

    pinMode(Sig1, INPUT);
    pinMode(Sig2, INPUT);
    digitalWrite(Sig1, LOW);  <--
    digitalWrite(Sig2, LOW);  <--

Thanks but it’s not a mistake. I named the table with 3 “S” to differentiate it from another one. (Stupid I know!) I’'m reading up on the digitalRead syntax . The button state is always high regardless of the pulldown resistor I use. I used a 10K pulldown resistor on Pin D0 and D1 and it still goes high and repeats the send. I accidentally set the argument to loop. It’s removed now !
Were you thinking something like this?

// This #include statement was automatically added by the Particle IDE.
#include "HttpClient.h"

// This #include statement was automatically added by the Particle IDE.
//#include "HttpClient/HttpClient.h"

int led2 = D7;
int Button1 = D1;
int Button2 = D2;


HttpClient http;

// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {
    //  { "Accept" , "application/json" },
    { "Accept" , "*/*"},
   // { "content-Type", "application/x-www-form-urlencoded" },
    { NULL, NULL } // NOTE: Always terminate headers will NULL
};

http_request_t request;
http_response_t response;


void setup()

{
    Serial.begin(9600);
    Serial.println("Ready!");
    pinMode(Button1, INPUT_PULLDOWN)
    pinMode(Butto2, INPUT_PULLDOWN)
    pinMode(led2, OUTPUT);

    
}

void loop()
{

    val1 = digitalRead(Button1);
    val2 = digitalRead(Button2);
        if(val1 == HIGH)
        {
            PostFormData()
        }
        if(val2 == HIGH)
        {
            PostFormData2()
        }
}

void PostFormData(){
    Serial.println("Sending data to Qrimp!");
    request.hostname = "l-wasp.qrimp.net";
    request.port = 80;
    //request.path = "db.aspx?t=TransmisssionLog&Device=1234A&Transmitter=5678A&o=5";
    request.path = "db.aspx?t=TransmisssionLog&Device=1234A&Transmitter=5678A&o=5";
    request.body = "";
    
    http.post(request, response, headers);
    digitalWrite(Sig1, LOW);
    Serial.println(response.body);
   
 
}

void PostFormData2(){
    request.hostname = "l-wasp.qrimp.net";
    request.port = 80;
    request.path = "db.aspx";
    request.body = "t=TransmisssionLog&Device=1234B&Transmitter=5678B&o=5";
    
    http.post(request, response, headers);
    
}

I’m used to PBasic having to pull the pin low… I thought Photon required this so I put a 10K pulldown resistor on my pins and pulled the Pins low with the command. It didn’t work though.

look to make an action only when the pin BECOMES high, not when it IS high (a.k.a. State Change Detection).

Like this basic example:

const int  buttonPin = D2; 
const int ledPin = D7;

int lastButtonState = HIGH;

void setup() 
{
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() 
{
  int buttonState = digitalRead(buttonPin);
  if (buttonState != lastButtonState) 
  {
    if (buttonState == LOW) 
    {
      Serial.println("Button Pressed");
    }
    lastButtonState = buttonState;
  }
  delay(50); // crude debounce
}

uses internal PullUp resistor, don’t use external!