Greet me!

Good evening, so, well, I'll just present my Arduino project. I connected the Arduino to a 4x20 SerLCD2.5, read data out of the Serial Stream and saved that into a variable. The var then got displayed on the LCD. After I had a lot of fun displaying stuff on my LCD, I thought "Why shouldn't everyone be able to do that?". So I wrote a PHP Script and a small Program in C#. Now people can access my website, write there some greeting down, then my C# program on my local computer reads the greeting over a php script out of the MySQL-Database and sends it via the Serial Port to the Arduino, which displays the greeting. After a Test-Run, I saw that some Idiots thought it would be funny writing down racist/nazi stuff, so I had to implement a wordfilter, a logging-System and a ban system. To prevent flooding there can be only 1 greeting all 5 minutes. If anyone wants the link to the website, PM me or something, I don't like when it gets spammed. :>

Schematic (german, but I think it isn't too complex):

cool, would you share your code?

(auch in Deutsch ;))

I’ll just paste the very messy coded C#-Program and the Arduino Code. But I think the PHP-Script is too shitty to share :slight_smile:

C#

using System;

using System.IO;

using System.IO.Ports;

using System.Net;

using System.Text;



public class greetme

{

      static SerialPort _serialPort;

      public static void Main()

      {

            StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;

            while (true)

            {

                  _serialPort = new SerialPort();

                  _serialPort.PortName = "COM5";

                  _serialPort.BaudRate = 9600;

                  _serialPort.Parity = Parity.None;

                  _serialPort.DataBits = 8;

                  _serialPort.StopBits = StopBits.One;

                  _serialPort.Handshake = Handshake.None;

                  //Webrequest

                  StringBuilder sb  = new StringBuilder();

                  byte[] buf = new byte[128];

                  HttpWebRequest  request  = (HttpWebRequest)

                        WebRequest.Create("URL_HERE");

                  HttpWebResponse response = (HttpWebResponse)

                        request.GetResponse();

                  Stream resStream = response.GetResponseStream();

                  string tempString = null;

                  int    count      = 0;

                  do

                  {

                        // fill the buffer with data

                        count = resStream.Read(buf, 0, buf.Length);

                        // make sure we read some data

                        if (count != 0)

                        {

                              // translate from bytes to ASCII text

                              tempString = Encoding.ASCII.GetString(buf, 0, count);



                              // continue building the string

                              sb.Append(tempString);

                        }

                  }

                  while (count > 0); // any more data to read?

                  resStream.Close();

                  response.Close();

                  

                  //Senden

                  if(stringComparer.Equals("", sb.ToString()) == false)

                  {

                        _serialPort.Open();

                        _serialPort.Write(sb.ToString());

                        _serialPort.Close();

                        //Console.WriteLine(sb.ToString());

                  }

                  System.Threading.Thread.Sleep(10000);

            }      

      }

}

Most of the code is copy-pasted from somewhere, I couldn’t even program C# when I started with the project.

Arduino-Code:

char message[60] = "Default String";
int count = 0;
int msglen = 13;
int i = 0;
void setup() {
  Serial.begin(9600);
}

void loop() {
  if(count != 0) {
    msglen = count - 1;
    count = 0;
    Serial.print(msglen);
  }
  while(Serial.available() > 0) {
    message[count] = char(Serial.read());
    count++;
    Serial.print(count);
  }
  Serial.print(0xFE, BYTE);
  Serial.print(0x01,BYTE);
  for(int i = 0; i <= msglen; i++) {
    Serial.print(message[i]);
  }
  delay(100);
}

I hope you didn’t puke because of my codes :>

thanks for sharing :)

the php code would be also interesting, as it makes in my case the part where i have no idea how to do such a thing :-[

Okay, I’ll just make a short explanation:
The PHP-Script index.php first removes all registered IPs which are older than 5 minutes (function time()) and not banned, then it gets the post data for the greeting-Parameter. If it’s not “” (nothing), it checks for bad words (MySQL-Query, stristr-PHP-Function) After that, it checks that the IP of the user hasen’t been registered lately. If everything is ok, it sends 2 MySQL-Querys: One to add the IP (and the time of adding) of the greeter, and one to add the greeting.
Last but not least in the greeting-routine, it adds a new entry in the log-table with IP, time and message.
At the end of the Script, it will just present a HTML-Form for the greeting.
In a second script, it first checks if the password in the get-parameter is the right one, then it gives out the first greeting (MySQL-Query just needs a “LIMIT 1” at the end) with echo and deletes it. That’s it. :slight_smile:

A little overview of the databasetables:

Log greeting greeters banned_words
ip id id word
msg msg ip warn
time(Timestamp) timestamp(PHP-Timestamp this time
banned

The warn-Field at “banned_words” is 1 or 2. The bannedword-check in the script adds the warn-values of each bad word in the greeting. If the result is 2 or higher, the ip will get banned. So if someone writes something about his ass, he won’t be banned instantly. :slight_smile:

I hope you can understand it, I know, I could have done it better =D
Since you are german: www.schattenbaum.net has good PHP and MySQL-Tutorials in german. Helped me alot!

Can't you control arduino directly with PHP? I think it's possible with fopen("COM*") etc.

That would mean that I’d have to run a webserver on my local computer. I just have some webspace. :slight_smile: