0% found this document useful (0 votes)
79 views

My Tinkering With Arduino - RasPi Local Web Server, Database and Posting Data From Arduino To Server

The document describes setting up a Raspberry Pi as a local web server to receive sensor data from multiple Arduino boards. An Arduino master communicates with Arduino slave boards using I2C to collect sensor readings. The master sends the data to the Raspberry Pi serially, where a Python script receives and processes the data. MySQL is installed on the Pi to store the data. A PHP script displays the sensor data from the MySQL database on a web page served from the Raspberry Pi web server.

Uploaded by

indorama
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views

My Tinkering With Arduino - RasPi Local Web Server, Database and Posting Data From Arduino To Server

The document describes setting up a Raspberry Pi as a local web server to receive sensor data from multiple Arduino boards. An Arduino master communicates with Arduino slave boards using I2C to collect sensor readings. The master sends the data to the Raspberry Pi serially, where a Python script receives and processes the data. MySQL is installed on the Pi to store the data. A PHP script displays the sensor data from the MySQL database on a web page served from the Raspberry Pi web server.

Uploaded by

indorama
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

11/3/2016

My Tinkering With Arduino: RasPi Local Web Server, Database and Posting Data From Arduino To Server
1

Lainnya BlogBerikut

[email protected] Dasbor Keluar

MyTinkeringWithArduino
Saturday,2July2016

Featuredpost

RasPiLocalWebServer,DatabaseandPosting
DataFromArduinoToServer

HireMe....

InthisPostIwillShowHowtoSetupLocalServer,DataBase,PhpmyadmintoReceive
theDatafromArduinoMasterWhichisReceivingDataFromOther3SlaveArduino's
BasicSetup:

ThanksforVisitingMyBlog...IAlso
DoCustomizedArduino/NodeMCU
/Raspberryprojects..orHelpyou
withyourCurrentProjectCon...

BlogArchive

2016(17)
September(1)
August(2)
July(2)
HelloWorldwithNodeMcu
usingLua
RasPiLocalWebServer,
DatabaseandPostingData
...
June(1)
May(1)
April(1)
March(1)
February(5)
January(3)
2015(60)
2014(10)

FirstWorkOntheArduinoPart:
IusedI2CProtocolToestablishMasterSlaveCommunicationBetweenArduinos.
MasterArduinoSendstherequesttotheslaveforDataForthreeArduinos
newMillis=millis()
if(newMillisprevMillis>=interval*1000){
prevMillis=newMillis
requestSlaveA()
delay(100)
requestSlaveB()
delay(100)
requestSlaveC()
}
OnSlaveSideUponReceivingRequest
voidrequestCallback(){
intinput=analogRead(AnalogInputPin)
//Tosendmultiplebytesfromtheslave,
//youhavetofillyourownbufferandsenditallatonce.
uint8_tbuffer[2]
buffer[0]=input>>8

http://anilarduino.blogspot.co.id/2016/07/raspilocalwebserverdatabaseand.html

1/5

11/3/2016

My Tinkering With Arduino: RasPi Local Web Server, Database and Posting Data From Arduino To Server
buffer[1]=input&0xff
Wire.write(buffer,2)
}
SlaveReadstheValuefromAnalog0PinandConverttheIntintobytewithShiftingand
"LogicalAND"OperationansSendthevaluetotheMaster.
OnMasterSideTheRequestFunctionisLikethis:
voidrequestSlaveA(){
if(Wire.requestFrom(slaveA,byteLength)){
Serial.print('A')
Serial.println(getData())
}else{
Serial.println("EA")
}
}
FromNickFammonSite:Wire.requestFromdoesnotreturnuntilthedata
isavailable(orifitfailstobecomeavailable).Thusitisnotnecessarytodo
aloopwaitingforWire.availableafterdoingWire.requestFrom.

UponreceivingtheDatawecallthegetData()FunctiontoParsetheData
intgetData(){
intreceivedValue=Wire.read()<<8
receivedValue|=Wire.read()
returnreceivedValue
}
weSenttheParseddataViaSeriallyTotheRaspberryPi.
OnRaspberryPiSideIUsedPythontoReceivetheDataSeriallyandRegular
ExpressionswithPythonfor
ParsingData
ser=serial.Serial('/dev/ttyACM0')#opentheserialConnection
betweenpiandArduino
whileTrue:
if(ser.inWaiting()>3):#IfSerialBufferhasMorethan3Bytes
data=ser.readline()#readaLineofDatafromSerialBuffer
processData(data)#CalltheFunctiontoReadtheData
ThisCodeReceivestheDataSeriallyandcallfunctionprocessData()toProcessthe
ReceivedDataandPasstheDataasArgument.
SoweReceivedtheData.WeneedtoSetupRaspberrypiasServerandSetup
Databaseintoit.
ForthatFollowtheseSteps:
1.ToInstallApache2inRaspberryPiusetheCommand
sudoaptgetinstallapache2php5libapache2modphp5
2.AfterFinishedInstallingUsetheFollowingCommandtoRestartthe
apache2Server
sudoserviceapache2restart
3.AfterRestartingCheckyourPiIpConfigurationwithCommand
ifconfig
4.EntertheipNumberintheWebbrowserinloclalan.youcanseeSample
Webpageas
ItWorks
YoucanEditthesourcefileLocationusing
sudonano/var/www/html/index.htmlnote:YouneedtoChangetheAbove
filebeforeUsingIt
5.InstallingPhp5InRaspberryPi:usethefollowingcommandtoinstallthe
Php
sudoaptgetinstallphp5libapache2modphp5y
6.InstallingMySqlonRaspberryPi:UsetheFollowingCommandtoInstall
ThemySql
sudoaptgetinstallmysqlserverpythonmysqldbThiswillinstallthemysql
serverandPythonMySQLdbModuleAlso
7.InstallingPhpMyAdmin:usethefollowingCommandtoInstallthe

http://anilarduino.blogspot.co.id/2016/07/raspilocalwebserverdatabaseand.html

2/5

11/3/2016

My Tinkering With Arduino: RasPi Local Web Server, Database and Posting Data From Arduino To Server
Phpmyadmin
sudoaptgetinstallphpmyadmin
8.ConfigureApache2toWorkwithPhpMyAdmin
nano/etc/apache2/apache2.conf
naviagatetotheBottomoftheFileandaddtheFollowingLine
Include/etc/phpmyadmin/apache.conf
9.restarttheapache2
/etc/init.d/apache2restartBeforeRunningtheCodecreateaDatabaseintheSQL
usingTerminal
Toenterintothemysqlshellenter
mysqlurootpwhererootistheusername
usetheCommand
a.CREATEDATABASEdatabase_nametoCreateaDatabase
b.USEdatabase_nametochangethecurrentdatabase
c.CREATETABLEtable_nameToCreateatableintheCurrent
Database
IusedPythonMySQLdbModuletoWritethevaluesIntoMySQLdatabase
ToStoretheValuesintotheDataBaseUse
db=MySQLdb.connect("localhost","root","raspberry","sensorData")
#ConnecttotheDataBase
curs=db.cursor()#openCursorwhichisusedtopassMySQLdb
Queries
curs.execute("""INSERTINTOsensorData.sensorDataTable
values(%s,%s,%s,%s)""",(deviceId,timeStamp,1,sensorValue))
#CommittheDatadb.commit()
WhichstorestheValuesintheDataBase

ToReadtheValuesfrommySQLDatabasetoWebPageIusedPhp
$conn=newmysqli($serverName,$userName,$password,$db)
if($conn>connect_error){
die("ConnectionFailed:".$conn>connect_error)
}
echo"Connectedsuccessfully"
$retval=mysqli_query($conn,"SELECT*FROMsensorDataTable")
if(!$retval){
die('CouldnotgetData:'.mysqli_error())
}
while($row=mysqli_fetch_assoc($retval))
{
echo"Deviceid:{$row['DeviceId']}<br>"
."TimeStamp:{$row['TimeStamp']}<br>"
."SensorType:{$row['SensorType']}<br>"
"SensorValue.{$row['SensorValue']}<br>"
."<br>"
}
WhichDisplaystheValuesFromWebpage

http://anilarduino.blogspot.co.id/2016/07/raspilocalwebserverdatabaseand.html

3/5

11/3/2016

My Tinkering With Arduino: RasPi Local Web Server, Database and Posting Data From Arduino To Server

YouCanGetAlltheSourceCodeFromMyGitHubRepositories
Output:

Local Server With Pi and

Postedbyanilkunchalaat23:05:00

+1 Recommend this on Google


Labels:apache2raspberrypi,connectingmultiplearduinos,I2C,I2CMasterSlave,iot,MySQLpython,
mysqldb,phppython,pilocalserver,piwebserver,pythonmySQL,RaspberrypiDatabase

http://anilarduino.blogspot.co.id/2016/07/raspilocalwebserverdatabaseand.html

4/5

11/3/2016

My Tinkering With Arduino: RasPi Local Web Server, Database and Posting Data From Arduino To Server

1 comment

Add a comment as DANI USMAN

Top comments

anil kunchala via Google+ 3 months ago - Shared publicly


as your Internet of Things Local Server, DataBase, Post your
Pi
Arduino Values into Database(Web-->MySQL) in Pi
1 Reply

NewerPost

Home

OlderPost

Subscribeto:PostComments(Atom)

Simpletemplate.PoweredbyBlogger.

http://anilarduino.blogspot.co.id/2016/07/raspilocalwebserverdatabaseand.html

5/5

You might also like