Show Posts
Pages: [1] 2 3 ... 631
1  Using Arduino / Project Guidance / Re: Arduino WiFi shield HTTP request problem on: July 16, 2014, 12:18:13 am
Quote
The String class is IMO a mistake that should never have been included in the Arduino runtime API, and you would be far better off using plain old C strings (null-terminated char arrays).

Well, that is probably just whiney-ass crap. If the web page is too big to be captured in a String, it probably can't be captured in a c-string either. There is a utility called TextFinder that is made for capturing data from a character stream. Below is code that might be of interest as it captures weather data from a weather web page.

Code:
// Include description files for other libraries used (if any)
#include <SPI.h>
#include <Ethernet.h>

// Define Constants
// Max string length may have to be adjusted depending on data to be extracted
#define MAX_STRING_LEN  20

// Setup vars
char tagStr[MAX_STRING_LEN] = "";
char dataStr[MAX_STRING_LEN] = "";
char tmpStr[MAX_STRING_LEN] = "";
char endTag[3] = {'<', '/', '\0'};
int len;

// Flags to differentiate XML tags from document elements (ie. data)
boolean tagFlag = false;
boolean dataFlag = false;

// Ethernet vars
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 102 };
byte server[] = { 140, 90, 113, 200 }; // www.weather.gov

// Start ethernet client
EthernetClient client;

void setup()
{
  Serial.begin(9600);
  Serial.println("Starting WebWx");
  Serial.println("connecting...");
  Ethernet.begin(mac, ip);
  delay(1000);

  if (client.connect(server, 80)) {
    Serial.println("connected");
    client.println("GET /xml/current_obs/KRDU.xml HTTP/1.0");   
    client.println();
    delay(2000);
  } else {
    Serial.println("connection failed");
  } 
}

void loop() {

  // Read serial data in from web:
  while (client.available()) {
    serialEvent();
  }

  if (!client.connected()) {
    Serial.println();
    Serial.println("Disconnected");
    Serial.println("==================================");
    Serial.println("");
    client.stop();

    // Time until next update
    //Serial.println("Waiting");
    for (int t = 1; t <= 15; t++) {
      delay(60000); // 1 minute
    }

    if (client.connect(server, 80)) {
      //Serial.println("Reconnected");
      client.println("GET /xml/current_obs/KRDU.xml HTTP/1.0");   
      client.println();
      delay(2000);
    } else {
      Serial.println("Reconnect failed");
    }     
  }
}

// Process each char from web
void serialEvent() {

   // Read a char
char inChar = client.read();
   //Serial.print(".");
 
   if (inChar == '<') {
      addChar(inChar, tmpStr);
      tagFlag = true;
      dataFlag = false;

   } else if (inChar == '>') {
      addChar(inChar, tmpStr);

      if (tagFlag) {     
         strncpy(tagStr, tmpStr, strlen(tmpStr)+1);
      }

      // Clear tmp
      clearStr(tmpStr);

      tagFlag = false;
      dataFlag = true;     
     
   } else if (inChar != 10) {
      if (tagFlag) {
         // Add tag char to string
         addChar(inChar, tmpStr);

         // Check for </XML> end tag, ignore it
         if ( tagFlag && strcmp(tmpStr, endTag) == 0 ) {
            clearStr(tmpStr);
            tagFlag = false;
            dataFlag = false;
         }
      }
     
      if (dataFlag) {
         // Add data char to string
         addChar(inChar, dataStr);
      }
   } 
 
   // If a LF, process the line
   if (inChar == 10 ) {

/*
      Serial.print("tagStr: ");
      Serial.println(tagStr);
      Serial.print("dataStr: ");
      Serial.println(dataStr);
*/

      // Find specific tags and print data
      if (matchTag("<temp_f>")) {
      Serial.print("Temp: ");
         Serial.print(dataStr);
      }
      if (matchTag("<relative_humidity>")) {
      Serial.print(", Humidity: ");
         Serial.print(dataStr);
      }
      if (matchTag("<pressure_in>")) {
      Serial.print(", Pressure: ");
         Serial.print(dataStr);
         Serial.println("");
      }

      // Clear all strings
      clearStr(tmpStr);
      clearStr(tagStr);
      clearStr(dataStr);

      // Clear Flags
      tagFlag = false;
      dataFlag = false;
   }
}

/////////////////////
// Other Functions //
/////////////////////

// Function to clear a string
void clearStr (char* str) {
   int len = strlen(str);
   for (int c = 0; c < len; c++) {
      str[c] = 0;
   }
}

//Function to add a char to a string and check its length
void addChar (char ch, char* str) {
   char *tagMsg  = "<TRUNCATED_TAG>";
   char *dataMsg = "-TRUNCATED_DATA-";

   // Check the max size of the string to make sure it doesn't grow too
   // big.  If string is beyond MAX_STRING_LEN assume it is unimportant
   // and replace it with a warning message.
   if (strlen(str) > MAX_STRING_LEN - 2) {
      if (tagFlag) {
         clearStr(tagStr);
         strcpy(tagStr,tagMsg);
      }
      if (dataFlag) {
         clearStr(dataStr);
         strcpy(dataStr,dataMsg);
      }

      // Clear the temp buffer and flags to stop current processing
      clearStr(tmpStr);
      tagFlag = false;
      dataFlag = false;

   } else {
      // Add char to string
      str[strlen(str)] = ch;
   }
}

// Function to check the current tag for a specific string
boolean matchTag (char* searchTag) {
   if ( strcmp(tagStr, searchTag) == 0 ) {
      return true;
   } else {
      return false;
   }
}
2  Using Arduino / Programming Questions / Re: Ethernet Shield connection issue on: July 15, 2014, 11:53:29 pm
If your arduino is connected to your router, you can try thr below clirnt code without any modifications to see if you can connect to the server.

Code:
//zoomkat 9-22-12
//simple client test
//for use with IDE 1.0.1
//with DNS, DHCP, and Host
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields
//remove SD card if inserted

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address

char serverName[] = "web.comporium.net"; // zoomkat's test web page server
EthernetClient client;

//////////////////////

void setup(){

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }

  Serial.begin(9600);
  Serial.println("Better client test 9/22/12"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
}

void loop(){
  // check for serial input
  if (Serial.available() > 0) //if something in serial buffer
  {
    byte inChar; // sets inChar as a byte
    inChar = Serial.read(); //gets byte from buffer
    if(inChar == 'e') // checks to see byte is an e
    {
      sendGET(); // call sendGET function below when byte is an e
    }
  } 
}

//////////////////////////

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(serverName, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.1"); //download text
    client.println("Host: web.comporium.net");
    client.println("Connection: close");  //close 1.1 persistent connection 
    client.println(); //end of get request
  }
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop(); //stop client

}

3  Using Arduino / Motors, Mechanics, and Power / Re: Servo motor control code on: July 15, 2014, 11:39:58 pm
Very simple servo button code.

Code:
//zoomkat servo button test 7-30-2011

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(160);
    delay(2000);
    servo1.write(20);
  }
}

4  Using Arduino / Networking, Protocols, and Devices / Re: 1 page to control multiple arduino's on: July 15, 2014, 11:31:27 pm
I've not done it, but you might be able to embed 40 GET request in a web page. The setup would be like embedding 40 images in a web page such that when the page is called, the page sends request to 40 arduinos, which switch the lights and send back a no refresh reply to the web browser. Several possibilities.
5  Topics / Robotics / Re: RobotGeek Servo MTBF? on: July 15, 2014, 10:47:40 pm
Quote
I tried emailing the company but received no reply at all. Probably will not use RobotGeek again because of that, but for now it's what I have.

I've never seen any MTBF done for hobby grade servos. If you need that you are probably out of luck. You don't say what servo you have or the specific conditions the servo will be used in, so no realistic answers. I've had standard servos last several years on a simple web pan/tilt cam setup.
6  Topics / Home Automation and Networked Objects / Re: Send an email or an SNMP-Trap if a digital input change from 0 to 1 on: July 15, 2014, 06:07:21 pm
Sending event based email should be fairly easy like the bottom sample code. Below is some reading material.

https://www.google.com/search?as_q=email+send&as_epq=&as_oq=&as_eq=&as_nlo=&as_nhi=&lr=&cr=&as_qdr=all&as_sitesearch=http%3A%2F%2Fforum.arduino.cc%2Findex&as_occt=any&safe=images&tbs=&as_filetype=&as_rights=&gws_rd=ssl

Code:
//zoomkat 4/08/12
//email test code using DNS
//developed from code posted by various persons

#include <SPI.h>
#include <Ethernet.h>

#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
char serverName[] = "smtp.yourISP.net"; // your ISP's SMTP server
EthernetClient client;

//////////////////////

void setup(){
  Serial.begin(9600);
  Serial.println("DNS and DHCP-based email test 4/08/12"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }
  // print your local IP address:
  Serial.print("Arduino IP address: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print(".");
  }
  Serial.println();
  Serial.println();
}

void loop()
{
  byte inChar;
  inChar = Serial.read();
  if(inChar == 'e')
  {
    if(sendEmail()) Serial.println("Email sent"); //start sendEmail()
    else Serial.println("Email failed");
  }
}

//////////////////////////////// email function
byte sendEmail()  //sendEmail() function that returns 1 or 0
{
  //start and check for email server connection
  if (client.connect(serverName, 25)) {
    Serial.println("connected");
  }
  else {
    Serial.println("connection failed");
    return 0; //send 0 (failed) back to sendEmail() function
  }
 
  //wait for server "queing" response
  while(!client.available()) delay(1);

  client.println("HELO itismeletschat"); /*hello (statement after helo is needed but irrelevant)*/

  //wait for server "hello" response
  while(!client.available()) delay(1);

  client.println("MAIL From: me@athome.net"); // identify sender, this should be the same as the smtp server you are using*/

  //wait for server "sender ok" response
  while(!client.available()) delay(1);

  client.println("RCPT To: you@athome.net"); /* identify recipient */

  //wait for server "receipent ok" response
  while(!client.available()) delay(1);

  client.println("DATA");

  //wait for server to say "enter your message" response
  while(!client.available()) delay(1);
 
  //send email message to server
  client.println("To: you@athome.net"); /* identify recipient */
  client.println("Subject: You Have Arduino Mail!!"); /* insert subject */
  client.println("Please let me know it worked!!!"); /* insert body */
  client.println("."); /* end email */

  //wait for server "message accepted" response
  while(!client.available()) delay(1);

  client.println("QUIT"); /* terminate connection */
 
  //wait for server "goodby" response
  while(!client.available()) delay(1);

  //stop client connection
  client.stop();
  Serial.println("disconnected");
  return 1; //send 1 (success) back to sendEmail() function
}

7  Topics / Home Automation and Networked Objects / Re: HTML/Arduino coding for single ON/OFF button on: July 14, 2014, 09:31:59 pm
Quote
forgot that there are 128 ASCII characters, not only 9!

You can have many multi character identifiers like below using conditional "if" statements;

Code:
          ///////////////////// control arduino pin
          if(readString.indexOf("on1") >0)//checks for on1 in captured readString
          {
            digitalWrite(4, HIGH);    // set pin 4 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            digitalWrite(4, LOW);    // set pin 4 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";
8  Using Arduino / Project Guidance / Re: Fried servo halts project on: July 13, 2014, 11:07:12 pm
Quote
Therefore it overworked itself and burnt up? Is this possible?

Typical analog servos will rotate one direction until they are against the internal hard stop when sent corrupt/bad control signals. If your servos test ok with servo library code then burn up with your code, then I would suspect your code has issues.
9  Using Arduino / Project Guidance / Re: Latching Solenoid Control on: July 13, 2014, 10:39:20 pm
Is the solenoid getting enough current?
10  Using Arduino / Networking, Protocols, and Devices / Re: Possible to sent HTTP requests, but not directly from Arduino? on: July 13, 2014, 11:07:05 am
Quote
Is that possible?

Probably, but you will need to get/write a program for the pc that communicates with the arduino and performs internet functions.
11  Using Arduino / Networking, Protocols, and Devices / Re: Arduino To Arduino Serial Communication thru USB on: July 13, 2014, 11:02:14 am
Quote
I need to be able to run the slave from my master or my laptop using the USB.

Using USB protocol directly between two arduinos will be something I haven't seen before. Should be interesting.
12  Using Arduino / Programming Questions / Re: Ethernet Shield connection issue on: July 13, 2014, 09:58:18 am
Quote
I have connected the USB cable to the arduino and the ethernet cable to my computer.

Most typical arduino web server setups have the arduino ethernet shield connected to a router. If you want a PC to act as a gateway for the arduino, then there are things that need to be configured on the PC  to support that activity.
13  Using Arduino / Networking, Protocols, and Devices / Re: Arduino To Arduino Serial Communication thru USB on: July 13, 2014, 02:41:45 am
Quote
But I need the slave to receive the data through the USB port.

Why is that? Why don't you use the tx/rx on the slave arduino?
14  Topics / Robotics / Re: Why would it not accept DC jack as power supply on: July 13, 2014, 01:34:31 am
Quote
This is not a 'I'm better than you RANT' - it's a 'please respect the help offered & put a bit of thought into your post RANT'
We can't see your project, we don't know what or how you have done something.

Please stop using "we" in your postings.
15  Using Arduino / General Electronics / Re: H-Bridge component for designing 24V ,10A motor driver on: July 13, 2014, 01:12:29 am
DIY H-bridge project from about a week ago, and an older H-bridge project.

http://forum.arduino.cc/index.php?topic=248204.msg1773713#msg1773713

http://forum.arduino.cc/index.php?topic=53425.0
Pages: [1] 2 3 ... 631