Hello everyone. I have an arduino Uno, with an ethernet shield. I'm trying to have it connect to a database(done), find its id(done), then periodically update its value(with a placeholder for now) depending on a sensor.
Currently it will fairly reliably send the initial "register me" request to the server, read the response containing its id, and attempt to send updates. Problem is, the updates will not send. They are pretty much the same as the initial request, and while it claims to be connected, there are no signs of any sort of message getting through. Client.connect returns true, and there doesn't seem to be a problem stopping the client (ran into that already, complete with workaround), and yet nothing gets through, even as the initial request works perfectly.
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x8E, 0x8D, 0xBE, 0x8F, 0xFE, 0xED };
char server[] = "ec2-54-213-87-44.us-west-2.compute.amazonaws.com";
int myId = -1;
char myName[] = "PetersDesk";
int myVal = 23;
EthernetClient client;
void setup() {
Serial.begin(9600);
Serial.println("Setup Beginning");
if(Ethernet.begin(mac)==0)
Serial.println("Failed to configure Ethernet with DHCP");
else
Serial.println("Phew");
delay(1000);
Serial.println("connecting...");
while(!client.connect(server,4000)){
Serial.println("Connection failed, trying again in 1s");
delay(1000);
}
Serial.println("connected");
client.println("POST /sensors/add HTTP/1.0");
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println((sizeof(myName)/sizeof(myName[0]))+4);
client.println("Connection: close");
client.println();
client.print("city=");
client.println(myName);
client.println();
Serial.println("All good");
while (myId == -1) {
if(client.available()){
char c = client.read();
Serial.print(c);
if(c=='i'){
getId();
}
}
}
}
void loop()
{
Serial.println("Connecting...");
while (!client.connect(server, 4000)) {
Serial.println("Connection failed, trying again in 1s");
delay(1000);
}
if(client.connected())
Serial.println("Connected");
Serial.println(String("POST /sensors/")+ myId + String(" HTTP/1.0"));
client.println(String("POST /sensors/")+ myId + String(" HTTP/1.0"));
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: 6");
client.println("Connection: close");
client.println();
client.println(String("val=")+myVal);
client.println();
Serial.println("Post apparently sent");
stop();
delay(1000);
}
void getId(){
char c = client.read();
Serial.print(c);
if(c=='d'){
c = client.read();
Serial.print(c);
if(c==':'){
c = client.read();
Serial.print(c);
if(c >= '0' && c<= '9'){
myId=c-48;
c = client.read();
while(c>='0' && c<='9'){
myId*=10;
myId+=(c-48);
c = client.read();
}
Serial.print(String("\nMyId found: ") + myId);
stop();
}
}
}
}
void stop(){
client.stop();
while(client.connected()){
client.stop();
Serial.println("Stopping...");
}
Serial.println("Stopped");
}