python+ Raspberry Pi enables IoT (Internet of Things) data to be uploaded to the server

Source: Internet
Author: User

Environment: Raspbian-stretch (2018-06-27)

Raspberry Pi: 3 generation B Type

1, Raspberry Pi equipment, need to be in the field can also have independent networking capabilities, it is necessary to use the Internet of Things module.

The SIM868 communication module for micro-snow is used here, such as:

2, will SIM868 inserted in the empty display board, SIM868 used is unicom 4Gsim card, in fact SIM868 using Pin#8 (TX), pin#10 (RX) pin and Raspberry Pi serial communication, power supply is through PIN#4 (5V), pin#6 (Gound) Support. The SIM868 module uses the HTTP transport protocol to connect to the server.

3. Server Configuration

Install the Apache service, install the service using XAMPP software, XAMPP Link: https://pan.baidu.com/s/15dt5s3DbZVd8YPckv2GkzA Password: 78FG

Run Xampp Control Panel,

Start Apache service, if boot fails, should be port occupied, change Apache service port, ok!

4. Program code

1), Python code

import timeimport serial def send_data (param): W_http_6='at+httppara= "URL", "http://120.78.181.110:90/iot.php?status='+str (param) +'"\ r \ n'ser.write (w_http_6) time.sleep (2) w_http_7='at+httpaction=0\r\n'ser.write (w_http_7) time.sleep (1)if__name__ = ='__main__': Ser= Serial. Serial ("/DEV/TTYS0",115200) Print Ser w_http_1='at+httpterm\r\n'ser.write (w_http_1) Print"http_1"Time.sleep (2) w_http_2='at+sapbr=3,1, "Contype", "GPRS" \ r \ n'ser.write (w_http_2) Print"http_2"Time.sleep (2) W_http_3='at+sapbr=3,1, "APN", "CMNET" \ r \ n'ser.write (w_http_3) Print"Http_3"Time.sleep (2) W_http_4='at+sapbr=1,1\r\n'ser.write (w_http_4) Print"Http_4"Time.sleep (2) W_http_5='at+httpinit\r\n'ser.write (w_http_5) Print"Http_5"Time.sleep (2) Status=1     whiletrue:send_data (status) Print status status= status+1Time.sleep (3)

2), PHP program code:

<?Phpheader ("content-type:text/html; Charset=utf-8"); $file='Log.txt';//the file name to write to the file (which can be any file name), if the file does not exist, creates a$status= $_get['Status'];echo $status; if($status! ="") {$content="status=". $status.", Time=". Date ("y/m/d H:i:sa")."\ n"; if($f = file_put_contents ($file, $content, File_append)) {//This function supports version (PHP 5)Echo"The write succeeded. <br/>"; }}if($data = file_get_contents ($file)) {;//This function supports version (PHP 4 >= 4.3.0, PHP 5)Echo"the content written to the file is: $data";} ?>

5. Code parsing

The Python version number used here is 2.7.13

Import Timeimport serial
Reference Python's 2 class library time and the serial Port debug library serial, in order to implement the program debugging can be written, before using the serial serial debugging library, to achieve the Raspberry Pi serial port configuration and Linux system serial debugging tool minicom installation. The specific method can refer to the previous article "Raspberry Pi serial port configuration and minicom Installation"

def send_data (param): w_http_6= ' at+httppara= "URL", "http://120.78.181.110:90/iot.php?status=" +str (param) + ' \ r \ n 'Ser.write (W_http_6) Time.sleep (2) w_http_7= ' at+httpaction=0\r\n 'Ser.write (w_http_7) time.sleep (1)
Here you pass the value of the state status variable of the main function loop as a parameter to your Send_data () function, and in the URL that is added to the HTTP request, a PHP program is written on the server side iot.php (the program source code is parsed as follows) To receive the HTTP request sent by the SIM868 communication module, and get the status value into the HTTP request URL, and insert it into the log txt. if __name__ = = ' __main__ ' : ser = serial. Serial ("/dev/ttys0", 115200 ) print ser w_http_1= ' at+httpterm\r\n ' ser.write (w_http_1) print "Http_1" Time.sleep (2 ) w_http_2= ' at+sapbr=3,1, "Contype", "GPRS" \ r \ n ' ser.write (w_http_2) print "http_2" Time.sleep (2 ) w_http_3= ' at+sapbr=3,1, "APN", "CMNET" \ r \ n ' ser.write (w_http_3) print "Http_3" Time.sleep (2 ) w_http_4= ' at+sapbr=1,1\r\n ' ser.write (w_http_4) print "Http_4" time.sleep (2 ) W _http_5= ' at+httpinit\r\n ' ser.write (w_http_5) print "Http_5" time.sleep (2 ) Status=1 while True:send_data (status) Print status status = Status+1 time.sleep (3) /span>

The above code is in the call serial library, set the baud rate of the Raspberry Pi serial communication is 115200, and use the Ser.write () function to the serial port to write the operational SIM868 module to communicate the AT command, where the AT command is to SIM868 HTTP communication before the configuration, The meaning of the specific at directive is not mentioned here, but Baidu can find it by itself. This is the wait time that is performed using the Time.sleep () function control program to ensure that the serial port writes the data successfully.

<? Phpheader ("content-type:text/html; Charset=utf-8 "); $file  = ' log.txt ';//The file name to write to the file (which can be any file name), if the file does not exist, a $status = $_get[' status ' will be created];/ /Get the value of the status parameter from the URL echo $status; Output the value of the status parameter if ($status! = "")//If the status parameter has a value, insert the value into the log {    $content = "status=". $status. ", time=". Date ("y/m/ D h:i:sa ")."            \//define the data format to insert into the log if ($f = File_put_contents ($file, $content, file_append)) {//This function supports version (PHP 5) echo "Write succeeded. <br/> ";}} Read all the data in the log and output it to the HTML if ($data = file_get_contents ($file)) {;//This function supports version (PHP 4 >= 4.3.0, PHP 5) echo "writes the contents of the file: $data" c10>;} ?>       

Reference: http://www.eefocus.com/communication/397109

Python+ Raspberry Pi implements IoT (Internet of Things) data upload to server

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.