Send data both ways between a Linux Sweex router and an Arduino via 12c
It is convenient to use an Arduino to drive a graphic display and for it to display data received from my existing network of Linux router boxes.
The Arduino can also act as an analog and digital remote sensor for the Linux box.
The display below is a bright and clear TFT full colour display with 128x190 pixels (eBay under £4) and is driven by an Arduino Nano.
The Arduino is set up as an i2c slave.
The i2c master is my Sweex router (menu item 3 here) that pulls in data from my i2c and ethernet home network.
First working code for the Arduino - NB!!! - this is WORK IN PROGRESS!!!
// Wire Slave Receiver -------------------------------slave_receive_i2c_display2.ino // Demonstrates use of the Wire library // Created 29 March 2006 // This example code is in the public domain.
#define cs 10 // Arduino-Pin an Display CS #include <Wire.h> String StringFromSweex = String(""); // send an intro: void loop() // function that executes whenever data is received from master tft.fillScreen(ST7735_BLACK); } void requestEvent() |
Code for the Sweex Linux router
this reads data from the Arduino and also reads data from a text file in the Linux box
and sends the data to the Arduino for it to put in the display
/* LtoASendFiles8.c for Sweex Linux router - send the contents of a stored text file to the Arduino e.g. send the contents of /var/www/ramdisk/chicken_feed_percent.dat to the Arduino To the program name we add usage e.g. - type with spaces but without the < > #include <unistd.h> int main(int argc, char** argv) // see if he has added arguments after the program name as required=================== addr = atoi(argv[1]); /* address is the first number after the program name */ // get the i2c bus ready============================================================
// now read data from the Arduino to print on the Linux box terminal ============== // read the I2C data from the Arduino
// open up the Linux box stored data file and pull in the data - // We assume argv[2] is the name of the Linux stored data filename to open // fopen returns 0, the NULL pointer, on failure while ( ( x = fgetc( file ) ) != EOF ) // print out the buf contents in the terminal // now send the string of characters in buf out on the i2c bus i2c_file = close(i2c_file);// (some examples do not bother to do this) /*
the Linux box data file can have a mix of letters and numbers and return characters
Sunspot 270114 */ |
The text file contains
123456789
from graham
The Linux terminal shows
midge03# ./LtoASendFiles8 4 data.txt
Read 24 bytes from I2C:-
Hello world from Arduino
Sent 22 bytes to I2C:-
123456789
from graham
The Arduino rs232 terminal shows
read
123456789
from graham
===
With thanks to
http://arduino.cc/en/Tutorial/MasterWriter
http://www.larsen-b.com/Article/398.html
http://blog.simtronyx.de/en/a-1-8-inch-tft-color-display-hy-1-8-spi-and-an-arduino/
with thanks!
i2c connections :-
pin A5 connects to SCL (clock)
pin A4 connects to SDA
(data)