In short I’m trying to open a url with the spark core. I had this working the other night. I have an Arduino with this same code working as we speak. With the spark core, I want to open this url:
192.168.1.9:80/energy/LogArduinoData.aspx?Location=999&Voltage=23&Ampere=70
I’m posting code running on the Arduino first because it works and it might be easier to see why the Spark code doesn’t. Anything pop out at anyone why the spark code isn’t opening the url? Thanks!
Here’s the Arduino code that currently works:
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {
0x90, 0xA2, 0xDA, 0x0E, 0x03, 0xE2};
IPAddress ip(192,168,1,251);
byte server[] = {
192, 168, 1, 9};
// char server[] = "localhost";
IPAddress myDns(8,8,8,8);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);
EthernetClient client;
unsigned long lastUpdate = millis();
void setup(){
Serial.begin(9600);
Ethernet.begin(mac, ip, dns, gateway, subnet);
delay(2000);
}
void loop(){
double temp0 = analogRead(A0);
double temp1 = analogRead(A1);
double arduino;
if (millis() - lastUpdate > 5000){ //reset after 60 seconds
lastUpdate = millis();
Serial.print("OK");
UploadData(arduino, analogRead(A1), temp0);
}
}
void UploadData(double location, int voltage, int ampere){
client.connect(server, 80);
if (client.connected()) {
Serial.println("Client Connected to server -- writing");
client.print("GET /energy/LogArduinoData.aspx?Location=");
Serial.print("GET /energy/LogArduinoData.aspx?Location=");
client.print(location);
Serial.print(location);
client.print("&Voltage=");
client.print(voltage);
Serial.print("&voltage");
client.print("&Ampere=");
client.print(ampere);
Serial.print("&ere");
client.print(" HTTP/1.1\n");
Serial.print(" HTTP/1.1\n");
client.print("Host:192.168.1.5 \n");
Serial.print("Host:192.168.1.5 \n");
client.print("Connection: close\n");
Serial.print("Connection: close\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
Serial.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: 0 \n\n" );
Serial.print("Content-Length: 0 \n\n" );
Serial.println("Wrote everything");
delay(1000);
if (client.available()) {
char c = client.read();
Serial.print(c);
}
}
client.stop();
client.flush();
}
The spark code I currently have that doesn’t seem to work is:
TCPClient client;
char server[] = {192, 168, 1, 9};
unsigned long lastUpdate = millis();
void setup(){
Serial.begin(9600);
delay(2000);
}
void loop(){
double temp0 = analogRead(A0);
double temp1 = analogRead(A1);
double arduino;
if (millis() - lastUpdate > 5000){ //reset after 60 seconds
lastUpdate = millis();
Serial.println("OK");
UploadData(999, analogRead(A1), temp0);
Serial.print("Trying to upload");
}
}
void UploadData(double location, int voltage, int ampere){
client.connect(server, 80);
if (client.connected()) {
Serial.println("Client Connected to server -- writing");
client.print("GET /energy/LogArduinoData.aspx?Location=");
Serial.print("GET /energy/LogArduinoData.aspx?Location=");
client.print(location);
Serial.print(location);
client.print("&Voltage=");
client.print(voltage);
Serial.print("&voltage");
client.print("&Ampere=");
client.print(ampere);
Serial.print("&ere");
client.print(" HTTP/1.1\n");
Serial.print(" HTTP/1.1\n");
client.print("Host:192.168.1.9 \n");
Serial.print("Host:192.168.1.9 \n");
client.print("Connection: close\n");
Serial.print("Connection: close\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
Serial.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: 0 \n\n" );
Serial.print("Content-Length: 0 \n\n" );
Serial.println("Wrote everything");
delay(1000);
if (client.available()) {
char c = client.read();
Serial.print(c);
}
}
client.stop();
client.flush();
}