-->![Portable raspberry pi 3 emulator Portable raspberry pi 3 emulator](/uploads/1/2/5/7/125723447/592163398.jpeg)
![Raspberry pi 3 b+ emulation Raspberry pi 3 b+ emulation](https://cdn.cultofmac.com/wp-content/uploads/2013/08/MiniMac-Steve-640x597.jpg)
The Raspberry Pi is a tiny and affordable computer that you can use to learn programming through fun, practical projects. Join the global Raspberry Pi community.
In this tutorial, you begin by learning the basics of working with Raspberry Pi online simulator. You then learn how to seamlessly connect the Pi simulator to the cloud by using Azure IoT Hub.
If you have physical devices, visit Connect Raspberry Pi to Azure IoT Hub to get started.
What you do
- Learn the basics of Raspberry Pi online simulator.
- Create an IoT hub.
- Register a device for Pi in your IoT hub.
- Run a sample application on Pi to send simulated sensor data to your IoT hub.
Connect simulated Raspberry Pi to an IoT hub that you create. Then you run a sample application with the simulator to generate sensor data. Finally, you send the sensor data to your IoT hub.
What you learn
- How to create an Azure IoT hub and get your new device connection string. If you don't have an Azure account, create a free Azure trial account in just a few minutes.
- How to work with Raspberry Pi online simulator.
- How to send sensor data to your IoT hub.
Overview of Raspberry Pi web simulator
Click the button to launch Raspberry Pi online simulator.
There are three areas in the web simulator.
- Assembly area - The default circuit is that a Pi connects with a BME280 sensor and an LED. The area is locked in preview version so currently you cannot do customization.
- Coding area - An online code editor for you to code with Raspberry Pi. The default sample application helps to collect sensor data from BME280 sensor and sends to your Azure IoT Hub. The application is fully compatible with real Pi devices.
- Integrated console window - It shows the output of your code. At the top of this window, there are three buttons.
- Run - Run the application in the coding area.
- Reset - Reset the coding area to the default sample application.
- Fold/Expand - On the right side there is a button for you to fold/expand the console window.
Note
The Raspberry Pi web simulator is now available in preview version. We'd like to hear your voice in the Gitter Chatroom. The source code is public on GitHub.
Create an IoT hub
This section describes how to create an IoT hub using the Azure portal.
- Sign in to the Azure portal.
- From the Azure homepage, select the + Create a resource button, and then enter IoT Hub in the Search the Marketplace field.
- Select IoT Hub from the search results, and then select Create.
- On the Basics tab, complete the fields as follows:
- Subscription: Select the subscription to use for your hub.
- Resource Group: Select a resource group or create a new one. To create a new one, select Create new and fill in the name you want to use. To use an existing resource group, select that resource group. For more information, see Manage Azure Resource Manager resource groups.
- Region: Select the region in which you want your hub to be located. Select the location closest to you.
- IoT Hub Name: Enter a name for your hub. This name must be globally unique. If the name you enter is available, a green check mark appears.
ImportantBecause the IoT hub will be publicly discoverable as a DNS endpoint, be sure to avoid entering any sensitive or personally identifiable information when you name it. - Select Next: Size and scale to continue creating your hub.This screen allows you to set the following values:
- Pricing and scale tier: Your selected tier. You can choose from several tiers, depending on how many features you want and how many messages you send through your solution per day. The free tier is intended for testing and evaluation. It allows 500 devices to be connected to the hub and up to 8,000 messages per day. Each Azure subscription can create one IoT hub in the free tier.
- IoT Hub units: The number of messages allowed per unit per day depends on your hub's pricing tier. For example, if you want the hub to support ingress of 700,000 messages, you choose two S1 tier units.For details about the other tier options, see Choosing the right IoT Hub tier.
- Advanced Settings > Device-to-cloud partitions: This property relates the device-to-cloud messages to the number of simultaneous readers of the messages. Most hubs need only four partitions.
- For this article, accept the default choices, and then select Review + create to review your choices. You see something similar to this screen.
- Select Create to create your new hub. Creating the hub takes a few minutes.
Register a new device in the IoT hub
In this section, you create a device identity in the identity registry in your IoT hub. A device cannot connect to a hub unless it has an entry in the identity registry. For more information, see the IoT Hub developer guide.
- In your IoT hub navigation menu, open IoT Devices, then select New to add a device in your IoT hub.
- In Create a device, provide a name for your new device, such as myDeviceId, and select Save. This action creates a device identity for your IoT hub.ImportantThe device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to avoid any sensitive information while naming it.
- After the device is created, open the device from the list in the IoT devices pane. Copy the Primary Connection String to use later.
Note
The IoT Hub identity registry only stores device identities to enable secure access to the IoT hub. It stores device IDs and keys to use as security credentials, and an enabled/disabled flag that you can use to disable access for an individual device. If your application needs to store other device-specific metadata, it should use an application-specific store. For more information, see IoT Hub developer guide.
Run a sample application on Pi web simulator
- In coding area, make sure you are working on the default sample application. Replace the placeholder in Line 15 with the Azure IoT hub device connection string.
- Select Run or type
npm start
to run the application.
You should see the following output that shows the sensor data and the messages that are sent to your IoT hub
Read the messages received by your hub
One way to monitor messages received by your IoT hub from the simulated device is to use the Azure IoT Tools for Visual Studio Code. To learn more, see Use Azure IoT Tools for Visual Studio Code to send and receive messages between your device and IoT Hub.
For more ways to process data sent by your device, continue on to the next section.
Next steps
You’ve run a sample application to collect sensor data and send it to your IoT hub.
To continue to get started with Azure IoT Hub and to explore all extended IoT scenarios, see the following:
I am very interested in trying simulators and emulators for popular IoT devices. One of those is the Raspberry Pi. As I was searching for simulators, I found that one of the best ones is QEMU. I found lots of information about how to use the QEMU simulator, in order to simulate a Raspberry Pi running Raspbian Wheezy (which is an older Raspbian version), but there was almost none for Raspbian Jessie (which is the latest Raspbian version). And the problem was that the steps that work for Wheezy, do not work for Jessie. So, in this article, I'll explain how to do this for Jessie. The same steps should also work if you are running Linux, however I have not tried them in Linux, so I cannot be sure.
1. Download the QEMU emulator
The easiest way to download and install QEMU is to download the 32-bit binaries from https://qemu.weilnetz.de/w32/. Even though my OS is 64-bit, the 64-bit version of QEMU did not work for me (the Raspbian kernel crashes during boot and the system restarts. The same problem happens with any QEMU version that is older than v2.6.0). After you download the latest file version (which is qemu-w32-setup-20161016.exe as I'm writing the article), you can either double click and install it or you can use 7-zip to extract the contents without installing (after you install 7-zip, you can right click on the QEMU exe file and select 'Extract').
2. Download Raspbian
You can download the latest Raspbian image from Raspberry Pi's website. You can either download the full image (https://downloads.raspberrypi.org/raspbian_latest) or the 'lite' version (https://downloads.raspberrypi.org/raspbian_lite_latest), which is smaller. The filename that I downloaded is 2016-09-23-raspbian-jessie.img .After you download the image, you should extract it to the same directory as QEMU.
3. Download the kernel
You can download the latest precompiled kernel from https://github.com/dhruvvyas90/qemu-rpi-kernel. I used kernel-qemu-4.4.13-jessie. Save this file in the same directory as QEMU.
4. Expand the Raspbian image
If you immediately boot into Raspbian without expanding the image, you will notice that there is very little free space. In order to be able to use more space, you need to expand the image. You can determine how much more space you want to allocate to Raspbian. In order to add 5GB to your Raspbian image you can open a command prompt, go to your QEMU directory and type the command:
qemu-img.exe resize <your_raspbian_image> +5G
e.g. qemu-img.exe resize 2016-09-23-raspbian-jessie.img +5G
5. First boot (to fix a setting)
Now you are ready to boot your emulator for the first time. In your command prompt, you can type:
qemu-system-arm -kernel <your_kernel_image> -cpu arm1176 -m 256 -M versatilepb -serial stdio -append 'root=/dev/sda2 panic=1 rootfstype=ext4 rw' -drive 'file=<your_rapsbian_image>,index=0,media=disk,format=raw' -redir tcp:2222::22
e.g. qemu-system-arm -kernel kernel-qemu-4.4.13-jessie -cpu arm1176 -m 256 -M versatilepb -serial stdio -append 'root=/dev/sda2 panic=1 rootfstype=ext4 rw' -drive 'file=2016-09-23-raspbian-jessie.img,index=0,media=disk,format=raw' -redir tcp:2222::22
Notes:
- If you see an error about missing dll files, then you can download them from https://qemu.weilnetz.de/w32/dll/.
- You will see a warning saying that the -redir option has been replaced by the -netdev option. I am still using -redir, since I could not understand how to use -netdev. If you know how, please leave a comment below and I will replace the command.
![Portable raspberry pi 3 emulator Portable raspberry pi 3 emulator](/uploads/1/2/5/7/125723447/592163398.jpeg)
After the emulator finishes booting, you will see a terminal window saying that you have started in emergency mode, instead of the default mode, due to an error. In order to fix this, you will need to create the file /etc/udev/rules.d/90-qemu.rules using your favorite editor, e.g.
sudo nano /etc/udev/rules.d/90-qemu.rules
In that file you need to add the following:
KERNEL'sda', SYMLINK+='mmcblk0'
KERNEL'sda?', SYMLINK+='mmcblk0p%n'
KERNEL'sda2', SYMLINK+='root'
KERNEL'sda?', SYMLINK+='mmcblk0p%n'
KERNEL'sda2', SYMLINK+='root'
Note: You are currently using the British locale (en-GB), that's why some characters do not correspond to the standard American keyboard, e.g. the character ' is typed when you press the key @ (and the opposite). We will fix this in a following step.
After saving the file, you can either close the emulator or issue the reboot command (sudo shutdown -r now).
Note: Previous versions of QEMU (before v2.6.0) required changes in /etc/ld.so.preload. However, this is not needed anymore.
During the beginning of the boot process, you will see lots of text. However, at some point the screen will go blank. You will have to wait for some time, but afterwards you will first see the mouse pointer and then you'll see the GUI. If you are asked for login credentials during the boot process, then you can use the following:
username: pi
password: raspberry
6. Expand the disk
At this point, we need to change the disk size so that it expands to the full size that we allocated in step 4 (i.e. to expand the disk by 5GB). In order to do that you need to do the following steps (I'm pasting the exact commands from http://www.xerxesb.com/2013/06/02/resizing-raspbian-image-for-qemu/):
1. > sudo fdisk /dev/sda
2. Print the partition table ('p'). Take note of the starting block of the main partition
3. Delete the main partition ('d'). Should be partition 2.
4. Create (n)ew partition. (P)rimary. Position (2)
5. Start block should be the same as start block from the original partition
6. Size should be the full size of the image file (just press 'enter')
7. Now write the partition table (w)
8. Reboot (shutdown -r now). After reboot,
9. > sudo resize2fs /dev/sda2
2. Print the partition table ('p'). Take note of the starting block of the main partition
3. Delete the main partition ('d'). Should be partition 2.
4. Create (n)ew partition. (P)rimary. Position (2)
5. Start block should be the same as start block from the original partition
6. Size should be the full size of the image file (just press 'enter')
7. Now write the partition table (w)
8. Reboot (shutdown -r now). After reboot,
9. > sudo resize2fs /dev/sda2
You can use the following command to verify the new size of the disk:
df -h
7. Increase the swap file for better performance
As you start using the emulator, you might observe that it is slow (especially if you have an old CPU). One reason for that is that the memory is only 256MB and the swap file is only 100MB. Unfortunately, there is no way to increase memory, but there is a way to increase the swap file. In order to do so, I'm using the instructions from https://www.bitpi.co/2015/02/11/how-to-change-raspberry-pis-swapfile-size-on-rasbian/
First you need to open the file /etc/dphys-swapfile using your favorite editor, e.g.
sudo nano /etc/dphys-swapfile
Then you need to find the line
CONF_SWAPSIZE=100
and change it to
CONF_SWAPSIZE=1024
After saving the file, you need to restart the service that manages the swapfile for Raspbian using the commands:
sudo /etc/init.d/dphys-swapfile stop
sudo /etc/init.d/dphys-swapfile start
sudo /etc/init.d/dphys-swapfile start
If everything finishes successfully, you can verify the change in the swap file by using the command
free -m
The output should look like the following (the important part is that the line next to 'swap' says '1023'):
total used free shared buffers cached
Mem: 248 213 33 2 12 131
-/+ buffers/cache: 70 177
Swap: 1023 0 1023
Mem: 248 213 33 2 12 131
-/+ buffers/cache: 70 177
Swap: 1023 0 1023
8. Use SSH for better performance
![Raspberry pi 3 b+ emulation Raspberry pi 3 b+ emulation](https://cdn.cultofmac.com/wp-content/uploads/2013/08/MiniMac-Steve-640x597.jpg)
Another way to improve performance is to access your Raspberry Pi using SSH and not using the GUI. In order to enable this, we have mapped port 2222 from your system to port 22 of the emulator. This was done using the option '-redir tcp:2222::22' in the command that starts the emulator (section #4). So, all you need to do is to ssh in port 2222 of your system.
In order to do that, you need an ssh client, such as putty (which you can download from https://the.earth.li/~sgtatham/putty/latest/x86/putty.exe). After you download and run putty, you need to go to the tab 'Session' and type [email protected] in the 'Host Name (or IP Address)' box, as well as 2222 in the 'Port' box. Then you can click on the 'Open' button to connect. You can use the following credentials:
username: pi
password: raspberry
All the following steps can be done either from ssh (faster) or from a terminal window inside the GUI (slower).
9. Overclock the CPU for better performance
In a terminal window or ssh type
sudo raspi-config
Select option '8 Overclock'
Select 'Ok' in the warning message
Select 'Turbo 1000MHz ARM'
Select 'Ok' in the confirmation message
Exit raspi-config
10. Change internalization options from British (en-GB) to American (en-US)
As we said earlier, we are currently using the British (en-GB) locale. In order to change it to American (en-US) or any other locale of your preference we need to execute the following commands from a terminal window or ssh:
export LANGUAGE='en_US'
export LC_ALL='en_US-UTF-8'
export
sudo dpkg-reconfigure locales
Uncheck 'en_GB-UTF-8'
Check 'en_US-UTF-8'
Press Ok
Select 'en_US.UTF-8' as the default locale for the system environment
11. Change the screen resolution
At this point, your emulator is up and running. However, you might be thinking that the window is too small and the resolution too low. This is because your current resolution is 640x480 at 16-bit color depth. If you want to change it to 800x600 (which will also increase the window size), you need to create the file /etc/X11/xorg.conf. I found the instructions for this at www.linux-mitterteich.de/fileadmin/datafile/papers/2013/qemu_raspiemu_lug_18_sep_2013.pdf
From a terminal window or ssh run your favorite editor, e.g.
sudo nano /etc/X11/xorg.conf
In that file you need to type the following contents:
Section 'Screen'
Identifier 'Default Screen'
SubSection 'Display'
Depth 16
Modes '800x600' '640x480'
EndSubSection
EndSection
After you save the file you can either close the emulator window or restart using the command
sudo shutdown -r now
12. Final observations
- Using the above instructions we can emulate an ARM1176 CPU (which is the same as the one on Raspberry Pi 1) that runs a generic kernel on a Raspbian OS. Technically, this is slightly different than emulating a Raspberry Pi 1, since there are differences in the kernel and the board configuration (e.g. GPIO pins). QEMU does provide support for better Raspberry Pi 1 and Raspberry Pi 2 emulation, but it does not support any network capabilities and I could not make it work (the screen remained black and the system did not boot). Andrew Baumann initially implemented the port for Raspberry Pi 2 at https://github.com/0xabu/ and he also helped me with this tutorial. If you want to try by yourselves (and please do so and let me know, if you make it work), then here are the changes that you need to start with, compared to the above instructions:
- In step #3 you need to use a precompiled kernel for Raspberry Pi 1 or Raspberry Pi 2.
- One option is to download them https://github.com/raspberrypi/firmware/tree/master/boot
- kernel.img is the kernel for Raspberry Pi 1
- kernel7.img is the kernel for Raspberry Pi 2
- A more difficult option is to install Raspbian on a real SD card, mount it (I'm not sure even if there are tools in Windows to do this) and then copy the kernel from the /boot directory.
- One option is to download them https://github.com/raspberrypi/firmware/tree/master/boot
- In order to boot the emulator you need to use the following commands:
- Raspberry Pi 1: qemu-system-arm -M raspi -kernel kernel.img -sd 2016-09-23-raspbian-jessie.img -append 'rw earlyprintk loglevel=8 console=ttyAMA0 root=/dev/mmcblk0p2' -serial stdio
- Raspberry Pi 2: qemu-system-arm -M raspi2 -kernel kernel7.img -sd 2016-09-23-raspbian-jessie.img -append 'rw earlyprintk loglevel=8 console=ttyAMA0 root=/dev/mmcblk0p2' -serial stdio
- In step #3 you need to use a precompiled kernel for Raspberry Pi 1 or Raspberry Pi 2.
- In order to enable SSH, we used the option -redir tcp:2222::22 in the qemu-system-arm command. This option is deprecated and replaced by -netdev, that's why it triggers a warning message in the output. However, I could not understand how the -netdev option works. If you know how, please let me know in your comments, so that I can replace the above command.
- It is not possible to copy data from the QEMU window, because it is an emulated framebuffer. If you know how to paste data to it, please write it in the comments and I will update the post accordingly.
- If you have any tips regarding how to improve performance, then please post them in the comments. I tried allocating more than 256MB memory (using the -m 512 option in qemu-system-arm) as well as > 1 CPU cores (using the -smp 4 option), but neither works.
- If you know of any other emulators/simulators for popular IoT devices (running on desktop, browser, etc), feel free to paste them in the comments section.