Hey everybody,
im working on a projekt and im using the teleduino api with my aduino meha 2560 to connect it to the internet via the api. First i used the example programm of the api and this is working fine to connect it to the internert. I can conroll the arduino over the internet ( setting pins to on/off...) and i have always the connection to the www.
Additional to this example programm i wrote a programm by myself witch is controlling the arduino without the internet. Its a really simple programm. Its just reading some values from sensors (light, temp) and acting in different ways based on the read values. Its turning pins on and off.
Now i want to combine the to programms to make it once. But it is not working. I did not change anything in that programs, i just combined them. I have no idea where the errors are coming form. The compiler says that everything is fine. But the connection to the internert is not working anymore and the reading of the values and setting the pins is not working in the correct way they should.
Is anybody familiar with the api? I really need some help, im working on that problem since 2 weeks.
Here the programs:
thanks in advance!
David
example program from Teleduino:
#include <EEPROM.h>
#include <Servo.h>
#include <Wire.h>
#include <Teleduino2560.h>
#include <SPI.h>
#include <Ethernet.h>
// User configurable variables
byte useDhcp = true;
byte useDns = true;
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x47, 0xF7 };
IPAddress deviceIp(192, 168, 1, 100); // Only if useDhcp is false
IPAddress gatewayIp(192, 168, 1, 1); // Only if useDhcp is false
IPAddress dnsIp(192, 168, 1, 1); // Only if useDhcp is false
IPAddress subnet(255, 255, 255, 0); // Only if useDhcp is false
IPAddress serverIp(173, 230, 152, 173); // Only if useDns is false
char serverName[] = "us01.proxy.teleduino.org"; // Only if useDns is true
unsigned int serverPort = 5353; // Can be set to either 53 or 5353
byte statusLedPin = 8;
// User configurable key, this is used to authenticate with the proxy server
// This is checked against the EEPROM on boot and written if necessary
// The proxy server retreives the key from the EEPROM
byte key[] = { 0x00, 0x00, 0x99, 0x54,
0x8D, 0x33, 0x7B, 0xD9,
0x50, 0x00, 0x00, 0xD7,
0xCD, 0xFC, 0xB8, 0x27 };
// Other required variables
byte data[257];
byte dataLength;
byte hexStage;
unsigned long lastInstruction = 0;
unsigned long lastRefresh = 0;
byte stage = 0;
// Declare client object
EthernetClient Client;
void setup()
{
// This is here purely to add a bit of padding to the sketch to fix a compiling bug
// http://arduino.cc/forum/index.php/topic,60649.0.html
EEPROM.read(0);
// Load presets
Teleduino2560.loadPresets();
// Set status LED pin
Teleduino2560.setStatusLedPin(statusLedPin);
Teleduino2560.setStatusLed(1); // Initialisation
// Check the EEPROM header and check to see if the key is correct
// This is to ensure the key is not cleared from the EEPROM
if(EEPROM.read(0) != '#')
{
EEPROM.write(0, '#');
}
if(EEPROM.read(1) != 1)
{
EEPROM.write(1, 1);
}
if(EEPROM.read(2) != '#')
{
EEPROM.write(2, '#');
}
if(EEPROM.read(395) != '#')
{
EEPROM.write(395, '#');
}
for(byte i = 0; i < 16; i++)
{
if(EEPROM.read(396 + i) != key[i])
{
EEPROM.write(396 + i, key[i]);
}
}
if(EEPROM.read(412) != '#')
{
EEPROM.write(412, '#');
}
// Start network and attempt to connect to proxy server
Teleduino2560.setStatusLed(2); // Network configuration
if(useDhcp)
{
if(!Ethernet.begin(mac))
{
Teleduino2560.setStatusLed(2, false, 10000);
Teleduino2560.reset();
}
}
else
{
Ethernet.begin(mac, deviceIp, dnsIp, gatewayIp, subnet);
}
delay(1000);
Teleduino2560.setStatusLed(3); // Connect to server
if((useDns && !Client.connect(serverName, serverPort)) || (!useDns && !Client.connect(serverIp, serverPort)))
{
Teleduino2560.setStatusLed(3, false, 10000);
Teleduino2560.reset();
}
lastInstruction = millis();
}
void loop()
{
ethernetConnection();
}
void ethernetConnection() {
if(Client.connected())
{
// What we need to do depends on which 'stage' we are at
switch(stage)
{
case 0: // Wait for start byte
if(Client.available())
{
char c = Client.read();
if(c == '?')
{
stage++;
}
}
break;
case 1: // Reset variables
dataLength = 0;
hexStage = 0;
stage++;
break;
case 2: // Instruction byte
if(Client.available())
{
char c = Client.read();
if(c == '?')
{
stage = 1;
break;
}
else if(c == '\r' || c == '\n' || c == '.')
{
stage = 0;
break;
}
if(!hexStage)
{
data[0] = Teleduino2560.hexDecode(c) * 16;
}
else
{
data[0] += Teleduino2560.hexDecode(c);
}
hexStage = !hexStage;
if(!hexStage)
{
stage++;
}
}
break;
case 3: // Data length byte
if(Client.available())
{
char c = Client.read();
if(c == '?')
{
stage = 1;
break;
}
else if(c == '\r' || c == '\n' || c == '.')
{
stage = 0;
break;
}
if(!hexStage)
{
data[1] = Teleduino2560.hexDecode(c) * 16;
}
else
{
data[1] += Teleduino2560.hexDecode(c);
}
hexStage = !hexStage;
if(!hexStage)
{
stage++;
}
}
break;
case 4: // Data
if(Client.available())
{
char c = Client.read();
if(c == '?')
{
stage = 1;
break;
}
else if(c == '\r' || c == '\n' || c == '.')
{
if(dataLength == data[1])
{
stage++;
break;
}
else
{
stage = 0;
break;
}
}
if(!hexStage)
{
data[2 + dataLength] = Teleduino2560.hexDecode(c) * 16;
}
else
{
data[2 + dataLength] += Teleduino2560.hexDecode(c);
}
hexStage = !hexStage;
if(!hexStage)
{
dataLength++;
}
}
break;
case 5: // Execute instruction and return result
Teleduino2560.instruction(data);
Client.write('!');
for(int i = 0; i < data[1] + 2; i++)
{
Client.write(Teleduino2560.hexEncode(data[i] / 16));
Client.write(Teleduino2560.hexEncode(data[i] % 16));
}
Client.write('\n');
lastInstruction = millis();
stage = 0;
break;
}
}
else
{
Teleduino2560.setStatusLed(10);
Teleduino2560.reset();
}
// Has the instruction timeout been reached?
if(millis() - lastInstruction > 30000)
{
Client.flush();
Client.stop();
Teleduino2560.setStatusLed(9);
Teleduino2560.reset();
}
// Process refreshes every 50ms
if(millis() - lastRefresh >= 50)
{
Teleduino2560.pinTimers();
Teleduino2560.shiftRegisterTimers();
Teleduino2560.shiftRegisters();
lastRefresh = millis();
}
// Check to see if reset has been requested
Teleduino2560.checkReset();
}
self written programm:
#define LIGHTSENSOR A1
#define LED 4
void setup(){
Serial.begin(9600);
delay(300);//Let system settle
Serial.println("Humidity, temperature und Bodenfeuchtigkeit \n\n");
pinMode(LED, OUTPUT);
delay(1000);//Wait rest of 1000ms recommended delay before accessing sensor
}//end "setup()"
void loop(){
readLight();
delay(1000);
}
void readLight(){
int lightValue = analogRead(LIGHTSENSOR);
Serial.print("Lichtverhältnis ANALOG: ");
Serial.println(lightValue);
if(lightValue > 200) {
digitalWrite(LED, HIGH);
} else {
digitalWrite(LED, LOW);
}
combined programm (i deleted ethernetConnection, setup() and variables because its the same like in the example programm)
// define digital pins
#define LED 4
// define analog pins
#define LIGHTSENSOR A1
void setup()
{
lastInstruction = millis();
Serial.begin(9600);
pinMode(LED, OUTPUT);
}
void loop()
{
readLight();
delay(1000);
ethernetConnection();
}
void readLight(){
int lightValue = analogRead(LIGHTSENSOR);
Serial.print("Lichtverhältnis ANALOG: ");
Serial.println(lightValue);
if(lightValue > 200) {
digitalWrite(LED, HIGH);
} else {
digitalWrite(LED, LOW);
}
}
void ethernetConnection() {
// same as example
}