├── README.md ├── esp8266_sensor └── esp8266_sensor.ino └── interface ├── app.js ├── public ├── css │ └── interface.css ├── js │ └── interface.js └── pictures │ └── test.jpg └── views └── interface.jade /README.md: -------------------------------------------------------------------------------- 1 | # connect-esp8266-rpi 2 | Connect the ESP8266 WiFi Chip to your Raspberry Pi 3 | -------------------------------------------------------------------------------- /esp8266_sensor/esp8266_sensor.ino: -------------------------------------------------------------------------------- 1 | // Import required libraries 2 | #include "ESP8266WiFi.h" 3 | #include 4 | #include "DHT.h" 5 | 6 | // DHT11 sensor pins 7 | #define DHTPIN 5 8 | #define DHTTYPE DHT11 9 | 10 | // Create aREST instance 11 | aREST rest = aREST(); 12 | 13 | // Initialize DHT sensor 14 | DHT dht(DHTPIN, DHTTYPE, 15); 15 | 16 | // WiFi parameters 17 | const char* ssid = "your-wifi-name"; 18 | const char* password = "your-wifi-password"; 19 | 20 | // The port to listen for incoming TCP connections 21 | #define LISTEN_PORT 80 22 | 23 | // Create an instance of the server 24 | WiFiServer server(LISTEN_PORT); 25 | 26 | // Variables to be exposed to the API 27 | int temperature; 28 | int humidity; 29 | 30 | void setup(void) 31 | { 32 | // Start Serial 33 | Serial.begin(115200); 34 | 35 | // Init DHT 36 | dht.begin(); 37 | 38 | // Init variables and expose them to REST API 39 | rest.variable("temperature",&temperature); 40 | rest.variable("humidity",&humidity); 41 | 42 | // Give name and ID to device 43 | rest.set_id("1"); 44 | rest.set_name("sensor_module"); 45 | 46 | // Connect to WiFi 47 | WiFi.begin(ssid, password); 48 | while (WiFi.status() != WL_CONNECTED) { 49 | delay(500); 50 | Serial.print("."); 51 | } 52 | Serial.println(""); 53 | Serial.println("WiFi connected"); 54 | 55 | // Start the server 56 | server.begin(); 57 | Serial.println("Server started"); 58 | 59 | // Print the IP address 60 | Serial.println(WiFi.localIP()); 61 | 62 | } 63 | 64 | void loop() { 65 | 66 | // Reading temperature and humidity 67 | temperature = dht.readTemperature(); 68 | humidity = dht.readHumidity(); 69 | 70 | // Handle REST calls 71 | WiFiClient client = server.available(); 72 | if (!client) { 73 | return; 74 | } 75 | while(!client.available()){ 76 | delay(1); 77 | } 78 | rest.handle(client); 79 | 80 | } 81 | -------------------------------------------------------------------------------- /interface/app.js: -------------------------------------------------------------------------------- 1 | // Imports 2 | var express = require('express'); 3 | var app = express(); 4 | 5 | // View engine 6 | app.set('view engine', 'jade'); 7 | 8 | // Set public folder 9 | app.use(express.static(__dirname + '/public')); 10 | 11 | // node-aREST 12 | var rest = require("arest")(app); 13 | rest.addDevice('http','192.168.0.100'); 14 | 15 | // Interface routes 16 | app.get('/', function(req, res){ 17 | res.render('interface'); 18 | }); 19 | 20 | // Start server 21 | var server = app.listen(3000, function() { 22 | console.log('Listening on port %d', server.address().port); 23 | }); 24 | -------------------------------------------------------------------------------- /interface/public/css/interface.css: -------------------------------------------------------------------------------- 1 | .voffset { margin-top: 30px; } 2 | 3 | body { 4 | font-size: 30px; 5 | } 6 | -------------------------------------------------------------------------------- /interface/public/js/interface.js: -------------------------------------------------------------------------------- 1 | // Function to control lamp 2 | $(document).ready(function() { 3 | 4 | // Refresh sensor data 5 | refreshSensors(); 6 | setInterval(refreshSensors, 5000); 7 | 8 | }); 9 | 10 | function refreshSensors() { 11 | 12 | $.get('/sensor_module/temperature', function(json_data) { 13 | $("#temperature").text('Temperature: ' + json_data.temperature + ' C'); 14 | 15 | $.get('/sensor_module/humidity', function(json_data) { 16 | $("#humidity").text('Humidity: ' + json_data.humidity + ' %'); 17 | }); 18 | }); 19 | 20 | } 21 | -------------------------------------------------------------------------------- /interface/public/pictures/test.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/makecademy/connect-esp8266-rpi/882745dd704045a91d92830fce380894d6aa24d8/interface/public/pictures/test.jpg -------------------------------------------------------------------------------- /interface/views/interface.jade: -------------------------------------------------------------------------------- 1 | doctype 2 | html 3 | head 4 | title Raspberry Pi Interface 5 | link(rel='stylesheet', href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css") 6 | link(rel='stylesheet', href="/css/interface.css") 7 | script(src="https://code.jquery.com/jquery-2.1.1.min.js") 8 | script(src="/js/interface.js") 9 | body 10 | .container 11 | h1 Raspberry Pi Sensors 12 | .row.voffset 13 | .col-md-6 14 | div#temperature Temperature: 15 | .col-md-6 16 | div#humidity Humidity: 17 | --------------------------------------------------------------------------------