Thursday, March 5, 2015

Giving Cypress a Try - for Jül - PSoC to Arduino to Huhushop OLED... Let's See

If this works, then the next step would be to try and dump stuff to the OLED from the phone by getting the PSoC BLE to receive it.. let's do a CBM first..

Start with the UART TX example code, delete the LCD thing in the schematic, and the code..

Build succeeds..

Then, using RealTerm... no luck.. Bombs..

Then, I try stuff from here : https://eewiki.net/display/microcontroller/PSoC+UART+Example

And that bombs - can't even find device.h ... man.. talk about Arduino compatibility - they suck..

Anyhow, I then search for PSoC4 BLE serial port hello world and get to :

http://www.cypress.com/?rID=101641

And I decide to try the ADC mit pre-amp example - I do a create workspace using that example (find example project) and then strip out all the other stuff, so I'm left with just :

void main()
{
    uint8 i = 0;

    /* Start the Components */
    UART_Start();
 
    /* Enable global interrupts */
    CyGlobalIntEnable;
 
    for(;;)
    {
        UART_UartPutChar( i++);
        delay( 1000u);    /* bad.. see below */
    }
}

Let's see... Note that, till now, I've no clue whether this UART will write the "Arduino compatible" pins.. And, as I learnt in the class, I try a right-click on the UART component hoping to get to API documentation - no luck..

Well, if they're shipping 2 billion of these a year, they're doing something right :)

Damn, there's no such function as delay. Why can't they make the thing smart enough to suggest that one might mean CyDelay - I mean, doesn't the perl compiler tell you where you might have made a mistake? Anyhow, fixes that, but still, not one peep on the RealTerm..

What's a mensch to do? I search for "pioneer cy8ckit-042-ble which pin of psoc is arduino tx rx?" .. in vain..

Changed (I think) to P1.5 for the TX and the Heap Size to 0x400 (instead of 0x100 default). And, WORKS!

Then, I see that that is the right port for Arduino already - you can see the silkscreen there.. Good. So, what happens if we connect to the Arduino pin 0 (RX)???

Works! Of course, I changed the code to
     uint8 i = 65;

To get letters on the OLED. Great! Now I need to get meaningful data from the phone to the OLED.. Be nice if I could just use the Mbientlab - but that piece of crap can't be programmed over USB.

Tuesday, February 24, 2015

You Thought Arduino Was Bad. Look at Digilent

Jeez, try finding a really good Hello World start page. Where did these guys ever get the gumption to dump so much stuff on their board.

They were smart to make everything look and feel like the Arduino, and then dumb enough to make it unusable. What the * are you supposed to do to get the programmer on the PC talking to the damn thing. Okay, Arduino needs some drivers (I think), but there's not a word about that here..

http://chipkit.net/arduino-blink-sketch-chipkit-dp32/

En Route to Jül : Serial to OLED with Arduino

Okay, been months since I touched this stuff... and to show you how a devious mind works ..

To get the heltec.cn (huhushop) monochrome OLED working with Arduino, (Duemilanove), I had

SDA*SCL*GND*VCC = pins 5*4*GND*3V3

As mentioned here, change the 0x3D to 0x3C for the address in their code and you should see the nifty graphics. Man, thank God ISIS doesn't have access to such technology.

Then, what if you want to relay a character from the serial port to the OLED?

To get up there, first example I try is DigitalReadSerial... Hmm, if you leave Pin 2 floating, you get random stufff.. Anyways. That works..
Anyways, had to look online for some relaying code, but that's not hard - they really don't have a good thing for reading a string at a time and relaying that. At least, I don't think they do.. Shame..

Really! Try googling for "arduino string" and see what you get.

Things you find out the hard way - each time you launch the serial port monitor, you reset the Arduino.

Anyways, this is the best I could do for now. If you think of a better way, please... I know this is shabby..

Only setup and loop here, coz this is a blog.
https://learn.adafruit.com/monochrome-oled-breakouts/arduino-library-and-examples has the rest that you put before and after.. (nothing needed after, coz we're not using those bells and w functions)

void setup()   {                
  Serial.begin(9600);

  // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3D (for the 128x64)
  // init done
  
  display.clearDisplay();  // prevents adafruit logo from being displayed, but compiler is not
                          // smart enough to know that that data being loaded into the buffer is not
                          // needed and some flash space can be saved. Shame on Arduino :)
  display.display();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.println("World,");

  display.display();

  delay(1000);
}

void loop() {

    char inChar;
  // read the input
  if( Serial.available()){
    delay(100);
    while( Serial.available()){
      inChar = Serial.read();
      Serial.print(inChar);
      display.print(inChar);
    }
    Serial.println();
    display.println();
    display.display();
    delay(500);
  }
}
I say shame on them for not providing a print function that also scrolls - how simple a need that is! Here's a guy ver sieht aus als sie kennt sein stuff : http://blog.oscarliang.net/arduino-oled-display-library/

Saturday, January 10, 2015

Web CGI Based Raspberry Pi GPIO LED Toggler

Finally, a setup that works, and it was a joy to go to mypi.freedns.org:NONCOXBLOCKEDPORT/cgi-bin/script.pl on my phone and see the LED toggle, and refresh the page and see it toggle again, and again and again...

So, on to how :-

What you need :
  • pigpio library (RPIO and RPi.GPIO don't cut it because they need root access)
  • lighttpd (other solutions possible, but this recipe is based on lite) an account with a free DNS service that will give you a static URL (you can't do 192.168.0.114 on your phone unless you're on the WiFi - and it's wimpy anyway)
  • Simple perl script that will call the python GPIO script (Yes, I tried calling the python script through CGI and gave up on that) 


1) Install lighttpd - consult online tutorials for that.
2) Once lighttpd is installed, enable CGI and perl by editing /etc/lighttpd/lighttpd.conf to have the relevant portions look like :
server.modules = (
        "mod_access",
        "mod_alias",
        "mod_compress",
        "mod_cgi",
        "mod_redirect",
#       "mod_rewrite",
)
and
$HTTP["url"] =~ "^/cgi-bin/" {
        cgi.assign = ( ".py" => "/usr/bin/python" ,
                        ".pl" => "/usr/bin/perl")
}
And, of course, if your ISP (Eg. Cox) is blocking port 80, you can change server.port to something else.
3) In the /var/www directory, create a cgi-bin directory (ownership can be root, no problem).
4) Install the pigpio library by following these steps.
5) Put the line "sudo /usr/local/bin/pigpiod &" in your /etc/rc.local so this daemon runs @ boot.
6) In a convenient place, put this script which will set port 17 (CPU view, not Board View) as an output port (name it meaningfully - I use caution_set_out_17.py :
#!/usr/bin/python
import RPIO as GPIO
# to use Raspberry Pi board pin numbers
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.OUT)

7) Run this script using "sudo /path/to/caution_set_out_17.py". It's named that way because it's not a good idea to blindly set a port to output - what if some app had set it as input and a voltage source was driving it on the board? Yes, until you know what you're doing with your Pi, it's not a bad idea to run this command manually :)
8) Put this script in a convenient place, which actually does the toggling of the logic-level on Port 17 :
#!/usr/bin/python
import pigpio
pi = pigpio.pi()
if 1 ==pi.get_mode(17) :
        pi.write(17, not pi.read(17))
9) Put this script in your /var/www/cgi.bin, name it conveniently
#!/usr/bin/perl
print "Content-type: text/html\n\n";
system("/home/pi/projects/pgpio_toggle_17.py");
print "<html>Hello, World!\n</html>";
10) Ensure you do a "chmod +x" on all the scripts you just finished installing
11) Using your breadboard, connect the LED longer wire to pin 11 (header view, google it), shorter wire to 330 ohm, and other terminal of the resistor to GND on the header (you'd want to do a sanity check by first connecting the LED, through 330 ohm of course) between 3V and 5V)
12) That's it, you should first try each of the scripts standalone - just run them from the command line and ensure the effect is as desired, then you can take your smartphone and go to yourpi.dnsserver.domain:80XX/cgi-bin/script.pl and you should see the LED toggle. 

Troubleshooting - running each script standalone should show get you there - obviously you edit the script I gave you to put in the actual name of the script you created, etc.


Joan Crawford pigpio Hello World

pi@raspberrypi ~/projects $ sudo pigpiod
pi@raspberrypi ~/projects $ python
Python 2.7.3 (default, Mar 18 2014, 05:13:23)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pigpio
>>> pi = pigpio.pi()
>>> mode = pi.get_mode(17)
>>> mode
1L
>>> pi.write(17,0)
0L

Friday, January 9, 2015

Toggle a GPIO LED on Raspberry Pi Through the Web

Going to try the CGI thing.

First order of biz is to ensure "mod_cgi" is part of server.modules in /etc/lighttpd/lighttpd.conf

This one seems pretty authoritative : http://raspberrypi.stackexchange.com/questions/1346/how-to-get-python-to-work-with-lighttpd

Add to the .conf :
$HTTP["url"] =~ "^/cgi-bin/" {
        cgi.assign = ( ".py" => "/usr/bin/python" )
}
And restart lighttpd with sudo service lighttpd force-reload.

Then, documentation is so crappy on RPIO and RPi.GPIO - horrible, I want to know what the port can be other than GPIO.OUT. I try all kinds of searches, finally ask where easy_install puts files and get some luck there (/usr/local/lib). So, there, the python3.2 is nothing - why all this confusion? Why can't the authors maintain some decent documentation?

GPIO_FUNCTIONS = {0: "OUTPUT", 1: "INPUT", 4: "ALT0", 7: "-"}

Is what I find in /usr/local/lib/python2.7/dist-packages/RPIO-0.10.0-py2.7-linux-armv6l.egg/RPIO/_RPIO.py
*ds!

Now, what is the default for a port? Ans : 1 - input - good to know - so, it's not a good idea to change that to an OUT arbitrarily if you don't know what you're doing..

Now, that being done, the CGI thing isn't working for running a python script in /var/www/cgi-bin. Seems like that thing doesn't run as root - which is needed to access GPIO. So, what can we do?

My workaround seems to be on the right lines - I was thinking of running a daemon that would look for requests to toggle the pin 17. Others online suggest the use of pigpio which doesn't require root, but, then you're again having to run the pigpio daemon, so it's the same concept.. Hmm... Why such crappiness?

Raspberry Pi Toggle An LED on GPIO Howto

With RPi.GPIO :

#!/usr/bin/python

import RPi.GPIO as GPIO

# to use Raspberry Pi board pin numbers
GPIO.setmode(GPIO.BOARD)

# set up GPIO output channel
GPIO.setup(11, GPIO.OUT)

GPIO.output(11, not GPIO.input(11) )

Don't run GPIO.cleanup at the end and then complain that it doesn't work :). Note that the above one sets the port as output. That's not what you want to do - you want to check that the port is already an output and only then toggle. If the port was configured as input from another app and is connected to a voltage source on the board, you don't want to pull it to the opposite voltage.

So, if you upgrade to RPIO using

sudo apt-get install python-setuptools
sudo easy_install -U RPIO

Then, you can do

#!/usr/bin/python

import RPIO as GPIO

# to use Raspberry Pi board pin numbers
GPIO.setmode(GPIO.BOARD)

if GPIO.gpio_function(11) is GPIO.OUT :
        # this is stupid, but just to trick RPIO
        GPIO.setup(11, GPIO.OUT)
        GPIO.output(11, not GPIO.forceinput(11))

Note how amateurish that is - finding out that something is already an output and then telling the same library to set as output - no surprise considering Pi was targeted at kids. If you didn't, you get :

Traceback (most recent call last):
  File "./nu_toggle_17.py", line 12, in
    GPIO.output(11, not GPIO.forceinput(11))
RPIO.Exceptions.WrongDirectionException: The GPIO channel has not been set up as an OUTPUT

..

Raspberry Pi LED Hello World

Most tutorials/blogs are raising more questions than they answer.. I've lost (?) a day reading Bunnie Huang's excellent reverse engineering adventure on the Xbox and am about to have lunch with my mentor and complain about how diffused all my efforts have been the last few weeks - just because the tools I want don't exist.

Anyhow, one facet of my idea stream is based on the Raspberry Pi. And I've done absolutely nothing with it - except hook up a cheezy surveillance system that takes a picture every minute and makes it available through a free DNS.

Now, before lunch, I want to do the LED blink example.

First, I just put in the LED (with 330 ohm) between 3V and GND and that works. Now, I've hooked it up between Pin 11 and GND in anticipation of following the steps on Rahul Kar's page.

Says import RPI.GPIO. What if that doesn't work? Man this is a crappy tutorial? Says do "sudo blah blah" - but that makes me nervous.

Now, I putty into the Pi and try :

#!/usr/bin/python

import RPi.GPIO as GPIO
from time import sleep

# to use Raspberry Pi board pin numbers
GPIO.setmode(GPIO.BOARD)

def blink(pin):
        GPIO.output(pin,GPIO.HIGH)
        time.sleep(1)
        GPIO.output(pin,GPIO.LOW)
        time.sleep(1)
        return
# set up GPIO output channel
GPIO.setup(11, GPIO.OUT)
# blink GPIO17 50 times
for i in range(0,50):
        blink(11)
GPIO.cleanup()

And get :

pi@raspberrypi ~/projects $ ./rahul_kar_led.py
Traceback (most recent call last):
  File "./rahul_kar_led.py", line 16, in
    GPIO.setup(11, GPIO.OUT)
RuntimeError: No access to /dev/mem.  Try running as root!
pi@raspberrypi ~/projects $ sudo !$
sudo ./rahul_kar_led.py
Traceback (most recent call last):
  File "./rahul_kar_led.py", line 19, in
    blink(11)
  File "./rahul_kar_led.py", line 11, in blink
    time.sleep(1)
NameError: global name 'time' is not defined

Scary? :)

I was trying to be smart and keep what I had from (I think) Jake Beningo's course a few monats ago : "from time import sleep" - which means I should then use just sleep(1). Instead, I decide to be a student again and change to "import time". And, works!

pi@raspberrypi ~/projects $ sudo ./rahul_kar_led.py
./rahul_kar_led.py:17: RuntimeWarning: This channel is already in use, continuing anyway.  Use GPIO.setwarnings(False) to disable warnings.
  GPIO.setup(11, GPIO.OUT)

And the Pi takes a picture while the LED is ON..

Okay, that's done. Now, I want to run a command each time the webserver serves this image so that I toggle the LED each time. Wouldn't that be cool?

And the realization : the R Pi world is being run by a bunch of !@#$s - I tried about 10 searches - "how to read state of GPIO output on raspberry pi" - nothing!