Example program to create IoT devices for a local network, which connect to a local server.

Dependencies:   WebSocketClient WiflyInterface mbed messages

This code is used in the second part of my Internet of Things (IoT) blog post available here. The code is fairly simple, but its real value is in its reliability. I have worked hard to try to make the wireless connection as reliable, and as fast, as possible. There are a few lines of code that must be modified before it will work correctly, and those are described in the following Wiki pages.

It is designed to work with a Python WebSocket Server running on a PC, the source code of which is available here.

Once operating with the server, each microcontroller, or IoT device, will broadcast a counter and its internal temperature to your WebSocket Server.

Committer:
defrost
Date:
Thu Oct 06 07:58:31 2016 +0000
Revision:
5:0c7d131e6089
Parent:
3:f20e114eb2ee
Child:
6:424e225d2a91
- Added comments, cleaned up code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
defrost 1:4403f2ed1c1f 1
defrost 1:4403f2ed1c1f 2 #include "ADC.h"
defrost 1:4403f2ed1c1f 3 #include "globals.h"
defrost 1:4403f2ed1c1f 4 #include "mbed.h"
defrost 1:4403f2ed1c1f 5
defrost 1:4403f2ed1c1f 6
defrost 1:4403f2ed1c1f 7 //#define DEBUG
defrost 1:4403f2ed1c1f 8 #define INFOMESSAGES
defrost 1:4403f2ed1c1f 9 #define WARNMESSAGES
defrost 1:4403f2ed1c1f 10 #define ERRMESSAGES
defrost 1:4403f2ed1c1f 11 #define FUNCNAME "ADC"
defrost 1:4403f2ed1c1f 12 #include "messages.h"
defrost 1:4403f2ed1c1f 13
defrost 1:4403f2ed1c1f 14
defrost 1:4403f2ed1c1f 15 void ConfigureADC(void){
defrost 1:4403f2ed1c1f 16
defrost 1:4403f2ed1c1f 17 unsigned int value;
defrost 1:4403f2ed1c1f 18
defrost 1:4403f2ed1c1f 19 // ensure power is turned on
defrost 1:4403f2ed1c1f 20 // Grabbed from lines 54-57 of analogin_api.c
defrost 1:4403f2ed1c1f 21 // This turns on the clock to Ports A, B, and C
defrost 1:4403f2ed1c1f 22 RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN | RCC_AHB1ENR_GPIOBEN | RCC_AHB1ENR_GPIOCEN;
defrost 1:4403f2ed1c1f 23 // This turns on the clock to the ADC:
defrost 1:4403f2ed1c1f 24 RCC->APB2ENR |= RCC_APB2ENR_ADC1EN;
defrost 1:4403f2ed1c1f 25
defrost 1:4403f2ed1c1f 26
defrost 1:4403f2ed1c1f 27 // Turn on the ADC:
defrost 1:4403f2ed1c1f 28 value = ADC_CR2_ADON;
defrost 1:4403f2ed1c1f 29 ADC1->CR2 = value;
defrost 1:4403f2ed1c1f 30 wait_us(100);
defrost 1:4403f2ed1c1f 31
defrost 2:7abdaa5a9209 32 // Set the EOC flag at the end of every regular conversion:
defrost 2:7abdaa5a9209 33 ADC1->CR2 |= ADC_CR2_EOCS;
defrost 2:7abdaa5a9209 34
defrost 1:4403f2ed1c1f 35 // Turn on the internal temperature sensor:
defrost 1:4403f2ed1c1f 36 ADC->CCR |= ADC_CCR_TSVREFE;
defrost 1:4403f2ed1c1f 37
defrost 2:7abdaa5a9209 38 // Set the first (and only channel) to convert to CH16, the internal temperature sensor:
defrost 2:7abdaa5a9209 39 ADC1->SQR3 |= ADC_SQR3_SQ1_4;
defrost 1:4403f2ed1c1f 40
defrost 1:4403f2ed1c1f 41 // Set the sample numbers (making this bigger samples more slowly):
defrost 2:7abdaa5a9209 42 ADC1->SMPR2 = ADC_SMPR1_SMP16_1 | ADC_SMPR1_SMP16_2; // Set for 144 ADC clock cycles
defrost 1:4403f2ed1c1f 43
defrost 1:4403f2ed1c1f 44
defrost 1:4403f2ed1c1f 45 INFO("ADC configuration complete!");
defrost 5:0c7d131e6089 46 DBG("ADC Registers:");
defrost 5:0c7d131e6089 47 DBG("The SR Register reads: %d", ADC1->SR);
defrost 5:0c7d131e6089 48 DBG("The CR1 Register reads: %d", ADC1->CR1);
defrost 5:0c7d131e6089 49 DBG("The CR2 Register reads: %d", ADC1->CR2);
defrost 5:0c7d131e6089 50 DBG("The JSQR Register reads: %d", ADC1->JSQR);
defrost 1:4403f2ed1c1f 51
defrost 1:4403f2ed1c1f 52 return;
defrost 1:4403f2ed1c1f 53 }