python - Send data from Arduino to a Web Server through Ethenet shield -
i have arduino ethernet shield, connected same hub computer. tried let arduino send http request web server written in python.
i fixed ip computer @ 192.168.1.2 , arduino ip automatically through a dhcp server or if fails new ip, static ip.
web server part: tested web server , worked whenever called request, save me record database(i using mongodb database)
arduino part: tested , saw connect dhcp server, not web server sent request , response dhcp server.
how config dhcp server can transfer arduino's request directly web server? or other solution let arduino send data directly web server?
small code of web server:
#!/usr/bin/env python2.7 # -*- coding: utf-8 -*- bottle import * pymongo import * import datetime @route('/hello') def hello(): return "hello world!" @get('/receiver', method = 'get') def receiver(): # data http request , save db print 'inserting data' receiver_id = request.query.getall('id') broadcaster_id = request.query.getall('bid') signal_strength = request.query.getall('s') client = mongoclient('localhost', 27017) db = client['arduino'] collection = db['arduino'] record = {'receiver_id':receiver_id, 'broadcaster_id':broadcaster_id, 'signal_strength':signal_strength, 'date': datetime.datetime.utcnow()} print record collection.insert_one(record) return template('record: {{record}}', record = record) @get('/db') def db(): # display data client = mongoclient('localhost', 27017) db = client['arduino'] collection = db['arduino'] record in collection.find(): return template('<div>{{receiver_id}}</div>',receiver_id = record) @error(404) def error404(error): return 'something wrong, try again!' run(host='localhost', port=80, debug=true)
small code of arduino program:
/* connect ethernet using dhcp author:- embedded laboratory */ #include <spi.h> #include <ethernet.h> #define eth_cs 10 #define sd_cs 4 // enter mac address controller below. // newer ethernet shields have mac address printed on sticker on shield byte mac[] = {0xca,0xfe,0x00,0x00,0x00,0x15}; ipaddress ip(192,168,1,15); ipaddress server(192,168,1,2); //char server[] = "localhost"; ethernetclient client; void setup() { serial.begin(9600); serial.println("starting ethernet connection"); pinmode(eth_cs,output); pinmode(sd_cs,output); digitalwrite(eth_cs,low); // select ethernet module. digitalwrite(sd_cs,high); // de-select internal sd card if (ethernet.begin(mac) == 0) // start in dhcp mode { serial.println("failed configure ethernet using dhcp, using static mode"); // if dhcp mode failed, start in static mode ethernet.begin(mac, ip); } serial.print("my ip address: "); (byte thisbyte = 0; thisbyte < 4; thisbyte++) { // print value of each byte of ip address: serial.print(ethernet.localip()[thisbyte], dec); serial.print("."); } serial.println(); // give ethernet shield second initialize: delay(1000); serial.println("connecting..."); for(int =1; < 6; i++){ serial.print("test number "); serial.println(i); // if connection, report via serial: if (client.connect(server, 80)) { serial.println("connected"); // make http request: //client.println("get /receiver?id=4&bid=3&s=70 http/1.1"); client.println("get / http/1.1"); client.println("host: localhost"); client.println("connection: close"); client.println(); delay(1000); } else { // if didn't connection server: serial.println("connection failed"); } } } void loop() { // if there incoming bytes available // server, read them , print them: if (client.available()) { char c = client.read(); client.write(c); serial.print("read client:"); serial.print(c); } // long there bytes in serial queue, // read them , send them out socket if it's open: while (serial.available() > 0) { char inchar = serial.read(); if (client.connected()) { client.print(inchar); } } // if server's disconnected, stop client: if (!client.connected()) { serial.println(); serial.println("disconnecting."); client.stop(); // nothing: while(true); } }
Comments
Post a Comment